taeyounkim LOG

Game of Craps code, 그리고 stackoverflow.com 본문

Career&Study/Coding Practice

Game of Craps code, 그리고 stackoverflow.com

taeyounkim 2021. 7. 2. 08:33
728x90

K.N.King C Programming Chapter9, Programming Projects #8
아래의 코드는 Game of Craps라는 게임에 대한 코드이다. 플레이어는 2개의 주사위를 던지면서 게임을 시작하는데, 그 두 주사위의 눈의 합에 따라 게임의 승패가 결정된다. First Roll에서 플레이어는 7 혹은 11을 굴리면 이긴다. 반대로 2, 3, 혹은 12를 굴리면 진다. 이를 제외한 다른 결과들은 point로 적립이 되며, 주사위를 한 번 더 굴릴 기회가 주어진다.
두 번째 Roll에서 플레이어는 7을 굴리면 게임을 진다. 플레이어가 이기는 방법은 First Roll에서 굴렸던 숫자를 다시 한 번 굴리는 것이다. 두 개의 숫자를 제외한 그 어느 결과들은 모두 무시되며 주사위를 계속 굴릴 기회가 주어진다.

책에서 제시한 조건은 int type function 한 개와 bool type function 한 개를 쓰라는 것이었다. 또한, 한 게임이 끝날 때마다 다시 play할 것인지 여부를 질문을 통해 확인해야 하며, yes의 y를 입력하면 게임이 다시 실행되고, no의 n를 입력하면 여태까지 몇 승 몇 패를 기록했는지 출력함과 동시에 코드를 terminate해야한다.

다음은 내가 처음에 작성한 코드이다.



#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>

int roll_dice(void)
{
srand((unsigned)time(0));
int a, b;

a = rand() % 6 + 1;
b = rand() % 6 + 1;

return a + b;
}

bool play_game(void)
{
int a = roll_dice();
printf("You rolled: %d\n", a);

if (a == 2 || a == 3 || a == 12)
{
printf("You lose!\n");
return false;
}

else if(a == 7 || a == 11)
{
printf("You win!\n");
return true;
}

else
{
printf("Your point is: %d\n", a);

while(1){
int b = rand() % 11 + 2;
printf("You rolled: %d\n", b);

if(b == 7)
{
printf("You lose!\n");
return false;
}

else if (b == a)
{
printf("You win!\n");
return true;
}

else
{
continue;
}
}
}
}

int main()
{
int wins=0, loses=0;
char resp;

do{
bool n = play_game();

if(n==false)
loses++;

else if(n==true)
wins++;

printf("Play again?: ");
scanf("%c", &resp);

} while (resp !='n');

printf("Wins: %d, Loses: %d\n", wins, loses);
return 0;
}




문제없이 잘 작성했다 생각했는데
계속 문제가 하나 생겼다.
게임을 다시 하겠다고 대답을 할 때마다 게임이 두 번 실행되는 것이다.
문제의 결과는 아래와 같았다.


그래서 평소 코딩을 하다가 막히는 부분이 있으면 찾아가는 stackoverflow.com에 구글 계정으로 처음으로 가입을 하고, 질문을 올렸다. 올리니까 2분만에 어떤 착하고 박학다식한 분께서 답변을 달아주셨다. (아니, 이 코드를 어떻게 2분도 안되서 다 읽은거지 싶었다.)

답변은 아래와 같았다. scanf를 쓸 때 문제가 있었다는 것이다. 그래서 하라는 대로 space 하나를 추가해보니까 코드가 실행이 되더라.



#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>

int roll_dice(void)
{
srand((unsigned)time(0));
int a, b;

a = rand() % 6 + 1;
b = rand() % 6 + 1;

return a + b;
}

bool play_game(void)
{
int a = roll_dice();
printf("You rolled: %d\n", a);

if (a == 2 || a == 3 || a == 12)
{
printf("You lose!\n");
return false;
}

else if(a == 7 || a == 11)
{
printf("You win!\n");
return true;
}

else
{
printf("Your point is: %d\n", a);

while(1){
int b = rand() % 11 + 2;
printf("You rolled: %d\n", b);

if(b == 7)
{
printf("You lose!\n");
return false;
}

else if (b == a)
{
printf("You win!\n");
return true;
}

else
{
continue;
}
}
}
}

int main()
{
int wins=0, loses=0;
char resp;

do{
bool n = play_game();

if(n==false)
loses++;

else if(n==true)
wins++;

printf("Play again?: ");
scanf(" %c", &resp);

} while (resp !='n');

printf("Wins: %d, Loses: %d\n", wins, loses);
return 0;
}




결론은 이러하고, stackoverflow.com에는 엄청난 고수들이 서식하는구나 싶었다. 예전부터 조금씩 참고하던 사이트이긴 한데, 이제 코딩을 본격적으로 공부하는만큼 더 많이 활용해보는 방향으로 가려고 한다.

728x90