骰子游戏不会返回正确的值

时间:2011-04-20 21:00:09

标签: c function

自从上次我在这里找到了极大的帮助后,我会再次提出问题

我的代码没有返回正确的值: 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;

}

OUTPUT

5 个答案:

答案 0 :(得分:3)

有几点:

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

答案 1 :(得分:2)

循环需要更像:

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
}

答案 2 :(得分:1)

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循环时,你所做的就是:
虽然(括号中的某些表达式)为真,但执行块{..}中的代码,然后再次检查括号中的表达式。

如果你写while(true),你实际上是在写while true is true, execute the code in the block。这将永远发生 如果你写while(false),你实际上写while false is true, execute the code in the block。并且这种假是永远不会的,因为它永远不会执行块中的代码 如果您想要真实条件,可以使用while(play_game())。这就像写while the returned value from the function play_game is true, execute the code in the block一样,只有当play_game函数返回true(表示游戏中的胜利)时才会执行代码。

有许多优秀的C教程,开始herehere

答案 3 :(得分:0)

很难从你的描述中说出来(请说出你预期会发生什么,以及发生了什么),但我注意到的第一件事是你正在为a和{{滚动5面骰子1}}。

答案 4 :(得分:0)

骰子的滚动发生在游戏序列中的不正确的位置。

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

}