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;
      }
    }
  }

}

Recommended Answers

All 9 Replies

I don't understand. You rememberd the number of wins and losses, therefore you saved them somewhere.

I think you need to explain in much more detail what you're confused about.

When i play the game again and again, when my choise is "no"-'n' then i need that wins and losses should appear on the screen. like this:
Play again? n

Wins: 2 Losses: 1

What variables do you have to keep track of total wins and total losses?
Where in the code do you add a win/loss to the win or loss variable?
Where in the code do you need to output these totals?

I can't post code! What's happend?

// >>>>> Craps game

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define TRUE 1
#define FALSE 0
#define NONE -1
#define bool int

int roll_dice(void);

bool play_game(void);



int main(void){

  int wins = 0, losses = 0;
  //bool continuee = true;
  bool result;
  char answer = 'j';

  do{
    result = play_game();

    /* 
       result = NONE - no win or no loss
       result = TRUE - win
       result = FALSE - loss */

    /* if(result == 2)
      wins++;
    else if(result == 3)
      losses++;
    */
    if(result == TRUE)
      wins++;
    else if(result == FALSE)
      losses++;
    else{}

    printf("Play again? ");
    //answer = getchar();
    scanf(" %c", &answer);
  }
  while(answer != 'n' && answer != 'N');

  printf("Wins-Losses are: %d %d\n",wins,losses);


    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, summa, n = 0, i = 0;
    //bool shoutern;

    bool result = NONE; 
    summa = roll_dice();
    //printf("SUM: %d\n", summa);

    switch(summa){

    case 7:
    case 11:

      result = TRUE; 
      printf("You win \n");
      break;

    case 2:
    case 3:
    case 12:

      result = FALSE; 
      printf("You lose \n");
      break;

    case 4:
    case 5:
    case 6:
    case 8:
    case 9:
    case 10:

      // default:
      //while(1);
      points = summa;
      //printf("You rolled : %d\n", points);
      printf("Your point is %d\n\n", points);
      break;
    }
    return result;

}

My problem still remains. For example if:
"You rolled"
"Your point is 8 "
then i dont want that the program asks me for a new game, the program must continue to roll until point is 8 again and the player wins, otherwise if point=7 player losses. Like this: (How can i do that?):
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

I repeat:

What variables do you have to keep track of total wins and total losses?
Where in the code do you add a win/loss to the win or loss variable?
Where in the code do you need to output these totals?

I solved the exercise, thanks for any help anyway.

can you pls show the codes? the finish one and it will run. cause we also have project like this and im having a hard time.

can you pls show the codes? the finish one and it will run. cause we also have project like this and im having a hard time.

Don't do it. There's nothing worse than someone stealing your code for their homework.

joyce15, maybe you should ask some questions yourself -- in your own thread, of course.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.