#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int throwDice();
int getUserGuess();
void playGuessingGame();
int throwDice(){
return rand()%6+1;
}
int getUserGuess(){ //κ° μ
λ ₯ λ°μ
int guess;
printf("μ£Όμ¬μλ₯Ό λμ Έ λμ¬ μ«μλ₯Ό λ§ν보μΈμ(1μμ 6κΉμ§):");
scanf("%d",&guess);
return guess;
}
//do~whileλ¬Έ λμ whileλ¬Έμ 무ν루νλ‘ μμ±ν΄λ΄€μ΅λλ€.
void playGuessingGame() {
int userGuess, diceResult;
while (1) { // 무ν 루ν μμ
userGuess = getUserGuess(); // λ§€ λ°λ³΅λ§λ€ μ
λ ₯ λ°κΈ°
// μ
λ ₯κ°μ΄ 0μ΄λ©΄ λ°λ³΅ μ’
λ£
if (userGuess == 0) {
printf("κ²μμ μ’
λ£ν©λλ€.\n");
break; //μμ ν λΉ μ Έλκ°
}
if(!(userGuess >=1 && userGuess<=6)){ // (userGuess<1 || userGuess>6)μΌλ‘λ μΈ μ μμ
printf("μλͺ» μ
λ ₯λμμ΅λλ€. 1~6μ¬μ΄μ μ«μλ‘ λ€μ μ
λ ₯ν΄μ£ΌμΈμ.\n");
continue; //νλ‘κ·Έλ¨ μ’
λ£ νλ κ² μλλΌ λ€μ whileλ¬Έ μμͺ½μΌλ‘μ¬λΌκ°
}
diceResult = throwDice();
printf("μ£Όμ¬μ κ²°κ³Ό: %d\n", diceResult);
if (userGuess == diceResult) {
printf("μΆνν©λλ€! μ«μλ₯Ό λ§μ·μ΅λλ€.\n");
} else {
printf("μμ½μ΅λλ€. λ€μ μλνμΈμ.\n");
}
}
}
// do{
// userGuess=getUserGuess();
// diceResult=throwDice();
// printf("μ£Όμ¬μ κ²°κ³Ό:%d\n",diceResult);
// if(userGuess==diceResult){
// printf("μΆνν©λλ€!μ«μλ₯Ό λ§μ·μ΅λλ€.\n");
// }else{
// printf("μμ½μ΅λλ€.λ€μ μλνμΈμ.\n");
// }
// }while(userGuess != 0);
// }
int main(){
srand(time(NULL));
printf("μ£Όμ¬μ μ«μ λ§νκΈ° κ²μμ μμν©λλ€.\n");
playGuessingGame();
printf("κ²μμ’
λ£\n");
return 0;
}