i in trouble dice game. have task:
the rules of game following: 1. player rolls dice , adds face values. 2. if first roll 7 or 11, player wins. 3. if first roll 2, 3 or 12, player looses. 4. if first roll other number, sum becomes player's point. 5. win, player must continue rolling dice until he/she “makes point.” 6. player loses rolling 7 before point.
1) define won , lost macros in program. use values of 0 won , 1 lose 2) implement function, function prototype int rolldice( void );
rolldice( ) should use rand( ) randomly generate number between 1 - 6
return number generated rand( )
3) implement function, function prototype int playgame( void );
when player ready play, (s)he use key enter roll dice
if user wins in his/her first roll, congratulate player , return won
if user looses in his/her first roll, congratulate player , return lose
let user keep playing until (s)he wins / loses, give appropriate massage , finish game last roll value.
4) main( ) should call function playgame( )
ask user if (s)he wants continue playing game, keeping track of numbers of losses , wins
when user decides finish playing, display number of wins , losses (s)he had.
give user appropriate message depending on number of wins or losses (s)he had
return value of exit_success
here have now, tells me there mistakes. can please me task?
#include <stdio.h> #include <ctype.h> #include <time.h> #include <stdlib.h> #define won 0 #define lose 1 int rolldice(void); int playgame(void); int rolldice(void) { return ((rand() % 6) + 1); } int playgame(void){ int dice_1 = 0; int dice_2 = 0; int sum = 0; time_t t; srand(time(&t)); printf("roll dice [enter]\n"); dice_1 = rolldice(); dice_2 = rolldice(); sum = dice_1 + dice_2; if (sum == 7 || sum == 11){ printf("congratulations roll %d , won @ first try!", sum); } else { printf("your roll %d ,you lose try agian.\n", sum); } return 0; } int main (void){ playgame(); }
the error (in gcc linux):
x.c:9:1: error: stray ‘\302’ in program
int rolldice(void);
^
x.c:9:1: error: stray ‘\240’ in program
x.c:10:1: error: stray ‘\302’ in program
int playgame(void);
^
x.c:10:1: error: stray ‘\240’ in program
x.c:12:1: error: stray ‘\302’ in program
int rolldice(void) {
^
x.c:12:1: error: stray ‘\240’ in program
x.c:16:1: error: stray ‘\302’ in program
int playgame(void){
^
x.c:16:1: error: stray ‘\240’ in program
there few things wrong here.
- you're not reading/using return value
playgame()
. should storing result , acting on it. - your logic isn't complete, criteria "playing point" , loss both same.
- you don't have in place forces program wait user press enter.
i have included completed code listing below.
code listing
/******************************************************************************* * preprocessor directives ******************************************************************************/ #include <stdio.h> #include <ctype.h> #include <time.h> #include <stdlib.h> #include <stdbool.h> #define won 0 #define lose 1 /******************************************************************************* * function prototypes ******************************************************************************/ int rolldice(void); int playgame(void); /******************************************************************************* * function definitions ******************************************************************************/ /*----------------------------------------------------------------------------*/ int rolldice(void) { return ((rand() % 6) + 1); } /*----------------------------------------------------------------------------*/ int playgame(void){ int dice_1 = 0; int dice_2 = 0; int sum = 0; int result; int point = 0; time_t t; bool playforpoint = false; srand(time(&t)); // initialize random seed printf("roll dice [enter]\n"); fgetc(stdin); dice_1 = rolldice(); dice_2 = rolldice(); sum = dice_1 + dice_2; printf("d1:%2d - d2:%2d - sum:%2d\n", dice_1, dice_2, sum); switch ( sum ) { case 7: case 11: result = won; break; case 2: case 3: case 12: result = lose; break; default: playforpoint = true; point = sum; printf("playing point:%d. please hit enter.\n", point); fgetc(stdin); break; } while ( playforpoint ) { dice_1 = rolldice(); dice_2 = rolldice(); sum = dice_1 + dice_2; printf("d1:%2d - d2:%2d - sum:%2d\n", dice_1, dice_2, sum); if ( sum == 7 ) { playforpoint = false; result = lose; } else if ( sum == point ) { playforpoint = false; result = won; } else { printf("please roll dice again [enter].\n"); fgetc(stdin); } } return result; } /*----------------------------------------------------------------------------*/ int main (void){ int result = playgame(); switch ( result ) { case won: printf("you won game.\n"); break; case lose: printf("you lost game.\n"); break; default: printf("something went wrong in program.\n"); break; } return 0; }
sample output
roll dice [enter] d1: 3 - d2: 5 - sum: 8 playing point:8. please hit enter. d1: 3 - d2: 1 - sum: 4 please roll dice again [enter]. d1: 3 - d2: 2 - sum: 5 please roll dice again [enter]. d1: 1 - d2: 5 - sum: 6 please roll dice again [enter]. d1: 3 - d2: 2 - sum: 5 please roll dice again [enter]. d1: 2 - d2: 6 - sum: 8 won game.
Comments
Post a Comment