繁体   English   中英

随机的C ++骰子游戏

[英]c++ Dice Game With Random

您从掷骰子开始。 除去6,将剩下的骰子加和得到r_1。 现在滚动剩余的骰子,除去6,将其余的骰子相加得到r_2; 继续此操作,直到所有骰子都显示6(您没有剩余)。 您的得分是S = r_1 + r_2 + ... + r_t,其中t是掷骰数。 如果S> 5 m,您赢了,否则您输了。

码:

#include <iostream>
#include <stdio.h>      //  NULL
#include <stdlib.h>     // srand, rand
#include <time.h>       // time
using namespace std;

void cheddar();
void feta();

int main(){
  //cheddar();
  feta();
}

void feta(){
  int count = 1;
  int ri = 0;
  srand(time(0));
  int dice[7];
  int score = 0;
  int m = 7;
  int fiveofm = 5*m;
  int numofdice = 7;
  do{
    cout << count << " & ";
    for(int i=0;i<7;i++){
        dice[i]=rand()%6+1;
    }
    for(int i=0;i<7;i++){
        cout << dice[i] << " & ";
    }
    for(int i=0;i<7;i++){
        if (dice[i] != 6){
            ri += dice[i];
            //score += dice[i];
        }
        if (dice[i] == 6){
            numofdice--;
        }
    }
    cout << ri << endl;
    ri = 0;
    count++;
  }while(numofdice != 0);
  /*
  if (score <= fiveofm){
    cout << "You lose!" << endl;
  }
  else if (score > fiveofm){
    cout << "You win!" << endl;
  }
*/
}

任何帮助将不胜感激。

编辑:

抱歉,在我键入问题之前点击了发送。 它要么导致无限循环,要么不摆脱6,然后从那里生成。 它只会重新填充5个新数字,并在右侧保留与以前相同的数字。

例如:

`2 & 3 & 3 & 4 & 1 & 5 & 6 & 18`

`6 & 1 & 1 & 6 & 1 & 4 & 2 & 9`

`5 & 3 & 6 & 6 & 5 & 5 & 4 & 22`

`5 & 3 & 3 & 3 & 3 & 6 & 5 & 22`

`2 & 5 & 5 & 3 & 6 & 5 & 1 & 21`

你不应该保留所有的滚动骰子:

for(int i=0;i<7;i++)

相反,只重新滚动仍在播放的那些:

int m = 7;
int numofdice = m;
do{
  cout << count << " & ";
  for(int i=numofdice-1; i>=0; --i){
    dice[i]=rand()%6+1;
    cout << dice[i] << " & ";
    if (dice[i] == 6){
        numofdice--;
    }
    else{
        ri += dice[i];
        //score += dice[i];
    }
  }
  cout << ri << endl;
  ri = 0;
  count++;
}while(numofdice != 0);

(请注意,以这种方式执行操作时,实际上不需要数组。)

暂无
暂无

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

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