繁体   English   中英

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

[英]Craps game doesnt return right values

自上次以来大家大家都在这里找到了极大的帮助,我会再问一个问题

我的代码没有返回正确的值:play_game函数中出了点问题,我无法弄清楚它是什么。我相信所有情况都被涵盖了,但最终还是搞砸了。 第二场比赛结束后,我每次玩游戏时代码也不会循环播放。 这不是一项任务

有什么建议吗?

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

}

输出值

要点:

  • 您可能需要1 + rand() % 6
  • printf()的返回值可能不是您要从roll_dice()返回的值

循环需要更像是:

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的回答很好(如我所见),但是我可以发现一些问题,希望它能在更多情况下为您提供帮助。

  • 您总是赢,我知道这很好,但是我敢打赌这不是预期的行为:

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

我认为您打算检查play_game()的结果。 因此,添加另一个变量并对其进行检查:

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

  • 为什么在那里使用while循环? 您还是会在第一次迭代中将其破坏

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

  • 该游戏将运行不超过两次,因为您没有一个循环取决于答案,而只有一个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);

编辑

当您使用while循环时,您的意思是:
当(括号中的某些表达式)为true时,执行块{..}中的代码,然后再次检查括号中的表达式。

如果编写while(true)while true is true, execute the code in the block实际上编写while true is true, execute the code in the block 这将永远发生。
如果您编写while(false)时实际编写了while false is true, execute the code in the block 这个false永远不会是真的,也永远不会执行代码块中的代码。
如果您想在此处获得实际条件,可以使用while(play_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

有很多不错的C教程,从此处此处开始

在您的游戏序列中,掷骰子的时间不正确。

#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