开发者

Craps game doesnt return right values

开发者 https://www.devze.com 2023-02-27 04:56 出处:网络
wiHi everyone since last time i found extreme help on here, im gonna ask a question again My code doesnt return right values :

wiHi everyone since last time i found extreme help on here, im gonna ask a question again

My code doesnt return right values : something is wrong in the play_game function and i cant figure out what it is.I believe that all cases are covered but somehow they end up messed up. also the code doesnt loop for everytime i want to play a game after the second game it stops. this is not an assignment

any suggestion?

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

static int sum, point, win = 0, roll = 0;    
bool play_game(void);
int roll_dice(void);

int main(void){

srand(time(NULL)); 


play_game();

char input[10];

do{ point = 0;
    play_game();
    if(win == 1){ // I'm assuming that play returns whether you won or not
        printf("You won!\n");
    }else{
        printf("You lost!\n");
    }
    printf("Would you like to continue? y/n\n");
    gets(input);
}while(*input == 'y'); // gets() flushes the buffer for next time you need input
return 0;
}


bool play_game(void){

point=0;
roll_dice();
printf("Your point is %d\n", sum);

while(roll == 1) /* first round */
{
  if(sum == 7 || sum == 11)
     return win = 1;
  else if(sum == 2 || sum == 3 || sum == 12)
开发者_运维知识库     return win = 0;
  else if(sum == 1 || sum == 4 || sum == 5 || sum == 6 || sum == 8 || sum == 9 || sum    == 10){
     point=sum;
     roll_dice();
     }

}

while(roll > 1) /* all others rounds*/
{  
      if(sum == 7)
        return win = 0;
      else if(sum == point)
        return win = 1;
      else if(sum != point || sum != 7)
      roll_dice();

} 

}

int roll_dice(void){

int a,b;

a=1+rand() % (6);
b=1+rand() % (6);
sum=a+b;
roll++;
printf("You rolled %d\n", sum);
return sum;

}

OUTPUT


A couple of points:

  • You probably want 1 + rand() % 6
  • The return value of printf() is probably not what you want to return from roll_dice()


The loop needs to be more like:

main(){
    char input[10];

    do{
        score = 0; //Always initialize the score
        if(play_game()){ // I'm assuming that play returns whether you won or not
            printf("You won!\n");
        }else{
            printf("You lost!\n");
        }
        printf("Would you like to continue? y/n\n");
        gets_s(input, 9);
    }while(*input == 'y'); // gets() flushes the buffer for next time you need input
}


Kyle's answer is just fine (as I see it), But I can spot a few problems, hope it'll help you in further cases.

  • You always win, and I know it's nice, but I bet it's not the expected behavior:

while(true) // This will always happen, because true is always evaluated as true
 {
  printf("Won\n\n");
  printf("Play again? y/n: ");
  break;
  }  

while(false) //This will never happen, since false is always evaluated as false
 {
  printf("Lost\n\n");
  printf("Play again? y/n: ");
  break;
  }

I think you meant to check the result of play_game(). So add another variable and check against it:

bool win;
win = play_game();
while (win == true)
...
while (win == false)
...

  • Why using while loop there? you break it in the first iteration anyway

if(win == true)
{
  printf("Won\n\n");
}  
else
{
  printf("Lost\n\n");
}
printf("Play again? y/n: ");

  • The game will run not more than twice, because you don't have a loop that depends on the answer, but only an if statement that is evaluated just one time:

if(v=getchar() == 'y') //This is the second time the code runs, after that? nada.
 {
  point =0; /* reset point var */
  play_game();
  }
 else if(v=getchar() == 'n') // Why adding this check? you're going out anyway after the if-else
  exit(1);

EDIT

When you use a while loop, what you do is saying:
While (some expression in the parenthesis) is true, execute the code in the block {..} and then check again the expression in parenthesis.

If you write while(true), you actually writing while true is true, execute the code in the block. And this will always happen.
If you write while(false) you actually write while false is true, execute the code in the block. and this false is never true, than it will never execute the code in the block.
If you want a real condition here, you can use while(play_game()). this is like writing, while the returned value from the function play_game is true, execute the code in the block and then the code will be executed only when the play_game function return true (which indicates a win in the game).

There are many good C tutorials out there, start here or here


It is hard to tell from your description (please say what you expected to happen, and what happened instead), but the first thing I notice is that you are rolling 5-sided dice for a and b.


Rolling of the dice is happening at at incorrect points during your game sequence.

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

// add defines to make states easier to read
#define WIN 1
#define LOSE 0

static int sum, point, win = 0, roll = 0;    
//bool play_game(void); 
int play_game(void); // changed return type to be int
int roll_dice(void);

int main(void){

    srand(time(NULL)); 


    // play_game(); // unncessary

    char input[10];

    do
    {
        point = 0;
        //play_game();
        // if(win == 1){
        if(play_game()){ // use return value from play_game()
            printf("You won!\n");
        }else{
            printf("You lost!\n");
        }
        printf("Would you like to continue? y/n\n");
        // gets(input);
        fgets(input, sizeof(input), stdin); // a safer input read
    } while(*input == 'y'); // gets() flushes the buffer for next time you need input
    return 0;
}


// bool play_game(void)
int play_game(void) // changed return type to be int
{

    point=0;
    // remove as this messes up the roll sequence.
    // roll_dice();
    // incorrect place to display this message
    //printf("Your point is %d\n", sum);

    // the while loop here is unnecessary
    //while(roll == 1) /* first round */
    //{
        roll_dice(); // add for initial come out roll.
        if(sum == 7 || sum == 11) { // I use braces to remove ambiguity
            // return win = 1;
            return WIN;
        } else if(sum == 2 || sum == 3 || sum == 12) {
            //return win = 0;
            return LOSE;
        }
        // sum will never be 1
        // on that note if it control reaches here it will be one of the other numbers.
        //} else if(sum == 1 || sum == 4 || sum == 5 || sum == 6 || sum == 8 || sum == 9 || sum == 10){
        // point=sum;
        // roll_dice(); // remove as this messes up the roll sequence.
        // }
        point=sum;
        printf("Your point is %d\n", sum);

    //}

    // while(roll > 1) /* all others rounds*/
    while (1) // might as well loop forever 
    {  
        roll_dice(); // add for subsequent dice rolls
        if(sum == 7) {
            //return win = 0;
            return LOSE;
        } else if(sum == point) {
            // return win = 1;
            return WIN;
        }
        // remove as this is unnecessary
        //  else if(sum != point || sum != 7)
        // remove as this messes up the roll sequence.
          //roll_dice();

    } 

}

int roll_dice(void){

    int a,b;

    a=1+rand() % (6);
    b=1+rand() % (6);
    sum=a+b;
    // roll++; // unncessary
    printf("You rolled %d\n", sum);
    return sum;

}
0

精彩评论

暂无评论...
验证码 换一张
取 消