简体   繁体   English

“大猪”骰子游戏代码无限循环:C

[英]"Big Pig" dice game code goes in infinite loop :C

Hello I'm new to c and i'm currently learning with my university curriculum so i need to abide by these rules: We can't use arrays or global variables.您好,我是 c 新手,目前正在学习我的大学课程,所以我需要遵守以下规则:我们不能使用数组或全局变量。

So i've been trying to make a dice game named "big pig".所以我一直在尝试制作一个名为“大猪”的骰子游戏。 I'm right now creating the function the computer is going to use to play the game called "play_computer()".我现在正在创建计算机将用来玩名为“play_computer()”的游戏的函数。 There is also a function called "computer_strategy_decider()".Computer_strategy_decider() is supposed to pick from yes or no.还有一个名为“computer_strategy_decider()”的函数。Computer_strategy_decider() 应该从是或否中进行选择。 I just made a rand function that calls either 1 or 2 to make that work.我刚刚做了一个 rand 函数,它调用 1 或 2 来完成这项工作。 Play_computer() let's you pick two dices and from there it needs to calculate the score. Play_computer() 让你选择两个骰子,然后它需要计算分数。 If you pick only one one, then your score doesn't increase and your game is terminated.如果您只选择一项,那么您的分数不会增加并且您的游戏将终止。 If you et two ones you get 25 added.如果您遇到两个,则会增加 25 个。 If you get any other double value for example a , is added such as (a+a)*2 or 4*a.如果您获得任何其他双精度值,例如 a ,则会添加 (a+a)*2 或 4*a 等。 And lastly if you get two random numbers the computer gets to decide if it wants to continue.最后,如果您得到两个随机数,计算机将决定是否继续。 That's where the computer_strategy_decider() comes in..这就是 computer_strategy_decider() 的用武之地。

The problem comes with the play_computer() function.问题来自 play_computer() 函数。 Everything seems to be working well when the computer rolls two different values and doesn't continue.当计算机滚动两个不同的值并且不继续时,一切似乎都运行良好。 It terminates ok.它终止正常。 But if it wants to continue it goes in an infinite loop.但是如果它想继续,它就会进入无限循环。 The infinite loop also has the same dice values.无限循环也具有相同的骰子值。 The same loop happens when doubles are rolled.滚动双打时会发生相同的循环。 Something in my code doesn't loop properly.我的代码中的某些内容无法正确循环。 I don't know whether it is something to do with rand() or not.我不知道这是否与 rand() 有关。 I don't think it's rand() since i use rand() on computer_strategy_decider().我不认为它是 rand(),因为我在 computer_strategy_decider() 上使用了 rand()。 My theory is it's hopefully something small i have missed.我的理论是希望这是我错过的一些小事。

My code was working an hour ago before i added some changes so that's why im frustrated haha.在我添加一些更改之前,我的代码在一个小时前工作,这就是为什么我很沮丧哈哈。

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

int computer_strategy_decider(){

int deci;
srand(time(NULL));
deci=1+ (int) rand() % 2;
    return deci;}






int play_computer(round_number,strategy){

  int roll_1,roll_2,cntrl_var=0,score_comp=0;
  char answ;

  printf("\nRound %d-- My Turn:",round_number);printf("\n---------------------------------");

  while(cntrl_var==0){
   srand(time(NULL));
   roll_1 = 1 + (int) rand() % 6;
   roll_2 = 1 + (int) rand() % 6;

  printf("\nI got --> [Dice 1]: %d [Dice 2]: %d",roll_1,roll_2);
  if(roll_1==roll_2 && roll_1!=1){
    score_comp=score_comp+roll_1*4;
    printf("\nScore: %d",score_comp);printf("\nDoubles! Roll again!");}
  else if(roll_1==1 && roll_2==1){
    score_comp=score_comp+25;
    printf("\nScore: %d",score_comp);printf("\nDoubles! Roll again!");}
  else if(roll_1==1 || roll_2==1){
    cntrl_var=1;
    printf("\nYou got a single one! End of your turn!");}
  else{
    score_comp=score_comp+roll_1+roll_2;
    printf("\nScore: %d",score_comp);
    while(cntrl_var==0){
      printf("\nDo you want to continue (Y/N)?");
      if (strategy==1){
        printf("Y");
        break;}
      else if (strategy==2){
        printf("N");
        break;}}
    if (strategy==1)
        cntrl_var=0;
    else if (strategy==2)
        cntrl_var=1;
    else
        cntrl_var=0;}}

  printf("\nMy Score: %d",score_comp);

  return score_comp;}

int main(){

int round_no=1,deci;
deci=computer_strategy_decider();
play_computer(round_no,deci);
}

I put the srand in the while loop and that caused the srand to be called multiple times.我将 srand 放在 while 循环中,这导致 srand 被多次调用。 So i put the srand above the loop.所以我把 srand 放在循环上面。 That fixed it!那就修好了!

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

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