[英]c++ Dice Game With Random
You start by rolling m dice. 您从掷骰子开始。 Remove the 6's and sum the rest of the dice to get r_1 . 除去6,将剩下的骰子加和得到r_1。 Now roll the remaining dice, remove the 6's and sum the rest of the dice to get r_2 ; 现在滚动剩余的骰子,除去6,将其余的骰子相加得到r_2; continue this until all dice have shown 6 (you have none remaining). 继续此操作,直到所有骰子都显示6(您没有剩余)。 Your score is S = r_1 + r_2 + ... + r_t , where t was the number of rolls. 您的得分是S = r_1 + r_2 + ... + r_t,其中t是掷骰数。 If S > 5 m you win, otherwise you lose. 如果S> 5 m,您赢了,否则您输了。
Code: 码:
#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;
}
*/
}
Any help will be appreciated. 任何帮助将不胜感激。
EDIT: 编辑:
Sorry I hit send before I typed the problem. 抱歉,在我键入问题之前点击了发送。 It either causes an endless loop or it doesn't get rid of a 6 and then generate from there. 它要么导致无限循环,要么不摆脱6,然后从那里生成。 It just renerates 5 new numbers and keeps the same number on the right as before. 它只会重新填充5个新数字,并在右侧保留与以前相同的数字。
Ex: 例如:
`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`
You should not keep rolling all the dice: 你不应该保留所有的滚动骰子:
for(int i=0;i<7;i++)
Instead, reroll only the ones that are still in play: 相反,只重新滚动仍在播放的那些:
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);
(Notice that when you do it this way, there's really no need for an array.) (请注意,以这种方式执行操作时,实际上不需要数组。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.