Reputation: 63
I am in trouble with my dice game. I have a task:
The rules of the game are the following: 1. The player rolls the dice and adds up the face values. 2. If the first roll is a 7 or 11, the player wins. 3. If the first roll is a 2, 3 or 12, the player looses. 4. If the first roll is any other number, that sum becomes the player's point. 5. To win, the player must continue rolling the dice until he/she “makes point.” 6. The player loses by rolling a 7 before the point.
1) Define WON and LOST as macros in your program. Use the values of 0 for WON and 1 for LOSE 2) Implement a function, with function prototype int rollDice( void );
rollDice( ) should use rand( ) to randomly generate a number between 1 - 6
return the number generated by rand( )
3) Implement a function, with function prototype int playGame( void );
When the player is ready to play, (s)he would use the key ENTER to roll the dice
If the user wins in his/her first roll, congratulate the player and return with WON
If the user looses in his/her first roll, congratulate the player and return with LOSE
Let the user keep playing until (s)he wins / loses, give an appropriate massage and finish the game with the last roll value.
4) Your main( ) should Call your function playGame( )
Ask the user if (s)he wants to continue playing another game, keeping track of the numbers of losses and wins
When the user decides to finish playing, display the number of wins and losses (s)he had.
Give the user an appropriate message depending on the number of wins or losses (s)he had
Return with a value of EXIT_SUCCESS
Here is what I have now, but it tells me that there are mistakes. Can anyone please help me with this 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 THE DICE WITH [ENTER]\n");
dice_1 = rollDice();
dice_2 = rollDice();
sum = dice_1 + dice_2;
if (sum == 7 || sum == 11){
printf("Congratulations you roll %d and WON at your first try!", sum);
}
else {
printf("Your roll was %d ,you lose try agian.\n", sum);
}
return 0;
}
int main (void){
playGame();
}
The Error is (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
Upvotes: 0
Views: 27520
Reputation: 19333
There are a few things wrong here.
playGame()
. You should be storing the result and acting on it.I have included a completed code listing for you 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 THE DICE WITH [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 for 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 the dice again with [ENTER].\n");
fgetc(stdin);
}
}
return result;
}
/*----------------------------------------------------------------------------*/
int main (void){
int result = playGame();
switch ( result )
{
case WON:
printf("You won the game.\n");
break;
case LOSE:
printf("You lost the game.\n");
break;
default:
printf("Something went wrong in the program.\n");
break;
}
return 0;
}
Sample Output
ROLL THE DICE WITH [ENTER]
D1: 3 - D2: 5 - Sum: 8
Playing for point:8. Please hit enter.
D1: 3 - D2: 1 - Sum: 4
Please roll the dice again with [ENTER].
D1: 3 - D2: 2 - Sum: 5
Please roll the dice again with [ENTER].
D1: 1 - D2: 5 - Sum: 6
Please roll the dice again with [ENTER].
D1: 3 - D2: 2 - Sum: 5
Please roll the dice again with [ENTER].
D1: 2 - D2: 6 - Sum: 8
You won the game.
Upvotes: 1
Reputation: 223917
You haven't yet satisfied the rules of the game. Your code takes 7 and 11 as winners and any other number as a loser.
After the 7/11 check, you need to check for 2, 3, or 12 and print a "lose" message if true. If not, you have the point number, and you need to prompt the user to keep rolling until he either gets the point number (win) or a 7 (lose).
You also need to keep track of wins/losses in main
. You'll need to call playGame
in a loop, prompting the user to continue on each iteration.
Upvotes: 1