C언어 주사위 게임을 짜봤는데 실행이 안되네요ㅠㅠ 수정할 부분을 부탁드립니다.
조회수 895회
List item
게임 규칙
면이 12개인 주사위를 두 개를 굴린다.
플레이어는 잔액 100개의 코인으로 시작한다.
세 번째 주사위가 두 개의 주사위 수의 사이에 들어가면 배팅한 만큼 코인을 얻고 배팅한 만큼 잔액이 올라간다.
세 번째 주사위가 두 개의 주사위의 수를 벗어나면 배팅한 만큼 코인을 잃고 배팅한 만큼 잔액을 잃는다.
플레이어의 잔액이 0이 되면 게임은 종료된다.
두 개의 주사위가 같은 숫자(예 첫 번째 주사위: 1, 두 번째 주사위: 1) 다시 할건지를 선택할 수 있고, 만약 선택하지 않으면 게임은 종료된다.
제가 한번 짜봤는데 안되더라구요 잘못된 부분있으면 알려주세요ㅠㅠ
define _CRT_SECURE_NO_WARNINGS //removing deprecation warning
define MAX_CHIPS 100 // Chips capped at 100 default
include
include
include
void display_details(void);
void display_dice(int, int, int);
int roll_die(void);
int main(void) {
//variables that are used
int die1, die2, die3;
int temp;
srand((int)time(NULL));//현재 시간을 이용해서 씨드 결정
die1 = rand() % 12; //12로 나눠 0~11까지 나머지 발생
die2 = rand() % 12; //12로 나눠 0~11까지 나머지 발생
die1 = die1 + 1; //1~12
die2 = die2 + 1; //1~12
printf("die1 : %d\n\n", die1);
printf("die2 : %d\n\n", die2);
int Games_Played = 0; //how many games played
int Games_Won = 0; //how many games won
int Games_Lost = 0; //how many games lost
int CurrentChips = MAX_CHIPS; //linking it to the acutal system input
int BetChips;
char PlayerName[100]; //player will enter their name.
char PlayAgain; //to play again
display_details(); //displays details
//this function uses real time as a random generator, every second will be a different outcome
//c4244 error anyother solution to fix this is 'srand( static_cast<unsigned int>(time(NULL)));'
srand(time(NULL));
//asked if want to play game
printf("\nWould you like to play Test your luck [y|n]? ");
//player chooses yes or no to play or not
scanf(" %c", &PlayAgain);
//This loop allows the game to be carry on as long as there are chips left and able to play more games
while ((PlayAgain == 'y') && (CurrentChips > 0))
{
//shows how many games played which is obviously 0
if (Games_Played == 0)
{
printf("\n A new chellenger! What is your name? ");
scanf(" %s", &PlayerName); //link to playername
printf("\nHi %s!\n", PlayerName); //Greting the player
}
//each new game it will increase by 1
Games_Played = Games_Played + 1;
//displaying current chips
printf("\nNumber of chips: %d\n", CurrentChips);
printf("\nPlace your bet: "); //betting
scanf("%d", &BetChips); //how much the player want to bet
//for validation as people might be like bet<0 or bet>100
while ((BetChips < 0) || (BetChips > CurrentChips)) //using loop until valid
{
printf("\n uuh ....You can only bet the chips you have.. (0-%d)!\n", CurrentChips);
printf("\nPlace your bet: ");
scanf("%d", &BetChips);
}
if (die1 == die2)
printf("Even-steven - let's play again!\n\n");
else
printf("Not the same, let's play!\n\n");
if ((die1 > die3) && (die2 > die3))
{
//+1 to games won
Games_Won = Games_Won + 1;
//adding to bet balance
CurrentChips = CurrentChips + BetChips;
printf("\nXXXXX Winner! d(^_^) XXXXX\n");
}
else if ((die1 < die3) && (die2 < die3))
{
//+1 to games won
Games_Won = Games_Won + 1;
//adding to bet balance
CurrentChips = CurrentChips + BetChips;
printf("\nXXXXX Winner! d(^_^) XXXXX\n");
}
else if ((die1 <= die3) && (die2 <= die3))
{
//+1 to lose games
Games_Lost = Games_Lost + 1;
//-bet from balance
CurrentChips = CurrentChips - BetChips;
printf("\nXXXXX Loser! :( XXXXX\n");
}
else
{
//+1 to lose games
Games_Lost = Games_Lost + 1;
//-bet from balance
CurrentChips = CurrentChips - BetChips;
printf("\nXXXXX Loser! :( XXXXX\n");
while (CurrentChips == 0)
if (throw_sum == PointToMake)
{
printf("\nXXXXX Winner! (x2 the bet) XXXXX\n");
//x2 added to balance
CurrentChips = CurrentChips + 2 * BetChips;
Games_Won = Games_Won + 1;
}
else
{
//+1 in lost
Games_Lost = Games_Lost + 1;
//-chips that are were betted
CurrentChips = CurrentChips - BetChips;
printf("\nXXXXX Loser!:( XXXXX\n");
}
}
printf("Thanks for playing! ");
//chips count is checked after the end of game
//if there are chips remainding
if (CurrentChips > 0)
{
printf("You now have %d chips.\n", CurrentChips);
printf("\n============================\n\n");
printf("Play Again %s [y|n]? ", PlayerName); //starting a new game again?
scanf(" %c", &PlayAgain); //for the user input y/n response
printf("\n============================\n");
}
else //when no chips are left
{
printf("\n you're out of chips.\n\n");
printf("\nXXXXX GG TRY AGAIN NEXT TIME XXXXX\n");
}
}
//display now many games are played
if (Games_Played == 0)
{
printf("\nOh well.... better luck next time!\n");
}
//only 1 game
else if (Games_Played == 1)
{ printf("\n%s played %d game and is cashing out with %d chips!\n", PlayerName, Games_Played, CurrentChips);
printf(" |---> Games won: %d\n", Games_Won);
printf(" |---> Games lost: %d\n", Games_Lost);
printf("\nThanks for playing, come again to test that luck of yours %s.\n", PlayerName);
}
//played games are more than 1
else
{
//display game results
printf("\n%s played %d games and is cashing out with %d chips!\n", PlayerName,
Games_Played, CurrentChips);
printf(" |---> Games won: %d\n", Games_Won);
printf(" |---> Games lost: %d\n", Games_Lost);
printf("\nThanks for playing, come again to test that luck of yours %s!\n", PlayerName);
}
return 0;
}
void display_details(void)
{
printf("File :.... assignment.c\n");
printf("Author :.... \n");
printf("Stud ID :.... \n");
printf("Email ID :.... \n");
printf("This is my own work as defined by the \n");
printf("University's Academic Misconduct Policy.\n");
}
int throws_die(void)
{
return 1 + rand() % 12;
}
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
아직 구현이 다 안된 부분도 있고, 실행을 해보지 못해서 모든 이슈를 다 지적해드리지는 못하고 눈에 띄는 부분만 적어봤습니다.
1. scanf(" %s", &PlayerName);
%s에 대응되는 PlayerName은 char PlayerName[100]; 으로 선언되었기 때문에 &를 생략해야 합니다. 배열의 이름, 즉 PlayerName 은 &PlayerName[0] 과 같기 때문입니다.
2. 정의되지 않은 변수
아래 코드에서 throw_sum 과 PointToMake이 정의되지 않았습니다. 그리고 아래와 같이 while을 쓰는 경우에는 { } 블럭을 지정해주세요.
while (CurrentChips == 0) if (throw_sum == PointToMake) { printf("\nXXXXX Winner! (x2 the bet) XXXXX\n"); //x2 added to balance CurrentChips = CurrentChips + 2 * BetChips; Games_Won = Games_Won + 1; } else { //+1 in lost Games_Lost = Games_Lost + 1; //-chips that are were betted CurrentChips = CurrentChips - BetChips; printf("\nXXXXX Loser!:( XXXXX\n"); }
3. 초기화되지 않은 변수 사용
die3 변수는 초기화되지 않은 채로 사용됐습니다. 모든 변수는 선언시 초기화 해주는 습관을 들이시기 바랍니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력