안녕하세요 여러분이이 프로그램을 살펴 봅니다.srand 함수가 같은 값을 반환합니다.
/* The craps game, KN king page 218 */
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#include <stdlib.h>
int roll_dice(void);
bool play_game(void);
int roll_dice(void)
{
int roll;
getchar();
srand((unsigned) time(NULL));
roll = rand() % 13;
if(roll == 0)
roll = roll + 1;
return roll;
}
bool play_game()
{
int sum = 0, wins = 0, loss = 0, point;
sum = roll_dice();
printf("You rolled: %d", sum);
if(sum == 7 || sum == 11)
{
printf("\nYou won!\n");
return true;
}
if(sum == 2 || sum == 3 || sum == 12)
{
printf("\nYou lost!!");
return false;
}
point = sum;
printf("\n\nYour point is: %d", point);
do
{
sum = roll_dice();
printf("\nYou rolled: %d", sum);
}while(sum != point);
if(sum == point)
{
printf("\nYou won!!!");
return true;
}
}
int main()
{
char c, wins = 0, losses = 0;
bool check;
do
{
check = play_game();
if(check == true)
wins++;
else if(check == false)
losses++;
printf("\nPlay Again? ");
scanf("%c", &c);
}while(c == 'Y' || c == 'y');
printf("\nWins: %d Losses: %d", wins, losses);
return 0;
}
srand 함수는 동일한 값 3 또는 4 번을 계속 반환합니다. y는 무엇입니까?
내가 다른 값을 내가 도박을 할 때마다 원하는 코드를 복사하고 난 (
이것이 숙제에 중요한지는 모르지만 궁금한 점을 수정 한 후에도 'roll = rand() % 13;'은 0과 0 사이의 값을 반환합니다. 및 12 포함. 6 면체 주사위 2 개를 시뮬레이션해야한다면 0 또는 1의 롤이 좋지 않습니다. 그리고 두 개의 주사위의 결과가 비 균일 분포를 가지므로 그 표현식을 2에서 12까지의 값을 갖도록 수정하는 것 역시 좋지 않을 수 있습니다. –
@MichaelBurr : 좋습니다. '(rand() % 6) + (rand() % 6) + 2;'이어야합니다. –
@MichaelBurr 그 좋은 해결책은 (rand() % 6 + 1)을 두 번 호출하고 두 개의 주사위 롤을 실제로 시뮬레이트하는 코드를 추가하는 것입니다. – RoneRackal