简体   繁体   English

掷骰子游戏未返回正确的值

[英]Craps game 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. 我的代码没有返回正确的值:play_game函数中出了点问题,我无法弄清楚它是什么。我相信所有情况都被涵盖了,但最终还是搞砸了。 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 您可能需要1 + rand() % 6
  • The return value of printf() is probably not what you want to return from roll_dice() printf()的返回值可能不是您要从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. Kyle的回答很好(如我所见),但是我可以发现一些问题,希望它能在更多情况下为您提供帮助。

  • 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() . 我认为您打算检查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? 为什么在那里使用while循环? 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语句仅被评估一次:

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循环时,您的意思是:
While (some expression in the parenthesis) is true, execute the code in the block {..} and then check again the expression in parenthesis. 当(括号中的某些表达式)为true时,执行块{..}中的代码,然后再次检查括号中的表达式。

If you write while(true) , you actually writing while true is true, execute the code in the block . 如果编写while(true)while true is true, execute the code in the block实际上编写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 . 如果您编写while(false)时实际编写了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. 这个false永远不会是真的,也永远不会执行代码块中的代码。
If you want a real condition here, you can use while(play_game()) . 如果您想在此处获得实际条件,可以使用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). 这就像写while the returned value from the function play_game is true, execute the code in the block一样, while the returned value from the function play_game is true, execute the code in the block的代码,然后仅当play_game函数返回true(表示游戏中获胜)时才while the returned value from the function play_game is true, execute the code in the block

There are many good C tutorials out there, start here or here 有很多不错的C教程,从此处此处开始

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;

}

从您的描述中很难分辨(请说出您期望发生什么,以及发生了什么),但是我注意到的第一件事是您正在为ab滚动5面骰子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM