简体   繁体   English

打印每一卷的得失

[英]printing the wins and losses for each roll

I am learning C++ right now and I am kinda confused about how to . 我现在正在学习C ++,有点困惑。 I am programming the gambling game CRAPS and I want the output to be like the number of games that are won and lost on the 1st roll, 2nd roll, etc 我正在对赌博游戏CRAPS进行编程,我希望输出像在第一轮,第二轮等中获胜的游戏数量一样

____ games won and ____ games lost on roll 
____ games won and ____ games lost on roll 

I am just lost on how to get each count for win and lost after each roll. 我只是迷失了如何获得胜利的基础,而每次掷骰后却迷失了方向。

I tried including arrays for each time the die are rolled, but it says there's an error of: 我尝试在每次掷骰子时都包含数组,但它表示存在以下错误:

invalid types 'int[int]' for array subscript
++wins[roll];

I do not understand what the error means. 我不明白该错误的含义。 Can someone help a newbie out? 有人可以帮助新手吗? Just need some hints :) 只需要一些提示:)

Here is my code so far and what I think I need to change: 到目前为止,这是我的代码,我认为我需要更改:

unsigned int rollDice();
int wins;
int loses;
int winSum = 0;
int loseSum = 0;
int roll;

int main(){
  enum class Status {CONTINUE, WON, LOST};
  srand(static_cast<unsigned int>(time(0)));

  unsigned int myPoint{0};
  Status gameStatus;
  unsigned int sumOfDice{rollDice()};

  for (int i = 1; i <=1000; i++){
    sumOfDice = rollDice();
    roll = 1;

    while(Status::CONTINUE == gameStatus){
      sumOfDice = rollDice();
      ++roll;

      if (sumOfDice == myPoint){
        gameStatus = Status::WON;
      }else if(sumOfDice == 7){
        gameStatus = Status::LOST;
      }
    }

    if(roll > 21){
      roll = 21;
    }

    if (Status::WON == gameStatus){
      ++wins[roll]; //maybe has smt to do w this one
      winSum++;
    }else{
      ++loses[roll]; //maybe has smt to do w this one
      loseSum++;
    }
  }
  int totalGame = winSum + loseSum;
  int length = 0;

  for (int i = 1; i <= 21; i++){
    if(i == 21){
      cout << wins[i] << " games won and " << loses[i]
         << " games lost on rolls after 20th roll" << endl;
         //maybe has smt to do w this one
    }else{
      cout << wins[i] << " games won and " << loses[i]
         << " games lost on roll " << i <<endl;
         //maybe has smt to do w this one
    }
  }
  cout << winSum << "\n"<<endl;
  cout << loseSum;

}

unsigned int rollDice() {
  int die1{1 + rand()%6};
  int die2{1 + rand()%6};
  int sum{die1+die2};

  return sum;
}

Again, tysm for the help!! 再次,tysm为您提供帮助!!

Make sure to pay close attention to your types. 确保密切注意您的类型。

You have declared the variables wins and loses as type int on lines 2 and 3, however you are trying to use them like an int array where you do ++wins[roll] and ++loses[roll] . 您已经在第2行和第3行中将变量winsloses声明为int类型,但是您试图像int数组一样使用它们,在其中执行++wins[roll]++loses[roll] So you did correctly identify what lines the problem was on, but not what the problem is. 因此,您确实可以正确确定问题所在的线路,而不是问题所在。

You will have to change the declaration of wins and loses to be an array, but also an array that is large enough to hold all the possible values of the dice rolls. 您必须将winsloses的声明更改为一个数组,但还要更改一个足够大以容纳骰子掷骰所有可能值的数组。 Since the dice roll maximum is 12, you should declare wins and loses as follows: 由于骰子掷骰最大数为12,因此您应按以下方式声明winsloses

int wins[13];
int loses[13];

The reason it is 13 and not 12 is because since arrays start at 0, if you want to be able to store an int at position 12, the array needs to be 13 elements long (0 - 12). 之所以是13而不是12的原因是因为数组从0开始,所以如果您想在位置12处存储一个int,则数组的长度必须为13个元素(0-12)。

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

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