Hi everyone!
I have a problem with a program - game of craps. The game plays with 2 dice. On the first roll the player wins if the sum of the dice is 7 or 11.
The player loses if the sum is 2.3 or 12. Any other roll called the "point". On each subsequent roll the player wins if rolls the point again. The player loses by rolling 7. Any other roll is ignored and the game continues.
At the end of each game the program will ask the player whether or not to play again. When the player enter a response other than y or Y the program will display the number of wins and losses and then continue.
For example:
You rolled: 9
Your point is 9
You rolled: 4
You rolled: 2
You rolled: 9
You win!
Play again? y
You rolled: 6
Your point is 6
You rolled: 8
You rolled: 2
You rolled: 7
You lose!
Play again? y
You rolled: 11
You win!
Play again? n
Wins: 2 Losses: 1
My problem is that i can't figure out how to save the 'wins' and 'losses' and also the number of 'points'.
PS: This program is NOT a homework. It is an exercise from the book without any solution.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
int roll_dice(void);
bool play_game(void);
int main(void){
int win[100]={0}, lose[100]={0};
int n=0, i=0;
bool continuee = true, call_game;
char answer='j';
do{
call_game = play_game();
//printf(" %d\n", call_game);
printf("Play again? ");
//while (answer != '\n')
scanf("%c", &answer);
answer = getchar();
if(answer =='j' || answer == 'J'){
continuee = true;
//win[n]++;
}
else if(answer == 'n' || answer == 'N'){
continuee = false;
//lose[i]++;
}
}
while(continuee);
return 0;
}
int roll_dice(void){
int sum,n, random1, random2;
srand((unsigned)time(NULL));
//srand(2); // >>>>> number 3
random1 = rand()%6+1;
random2 = rand()%6+1;
//printf("Random is: %d %d\n", random1,random2);
sum = random1 + random2;
//printf("You rolled: %d\n", sum);
return sum;
}
bool play_game(void){
int game, points, win[100] = {0}, lose[100] = {0};
int summa, n=0,i=0;
bool shoutern, continuee;
summa = roll_dice();
//printf("SUM: %d\n", summa);
switch(summa){
case 7:
case 11:
shoutern = true;
printf("You win \n");
win[n]++;
//printf("Wins: %d\n", win[n]);
break;
case 2:
case 3:
case 12:
shoutern = false;
printf("You lose \n");
//for(i = 0; i < 1; i++)
lose[i]++;
//printf("Losses: %d\n", lose[i]);
break;
/*
case 4:
case 5:
case 6:
case 8:
case 9:
case 10:
*/
default:
while(1){
summa = roll_dice();
points = summa;
if(summa == points){
//printf("You rolled : %d\n", points);
printf("Your point is %d\n\n", points);
break;
}
}
}
}