C ++:如何重新滚动以在骰子游戏中显示一组新的随机数? [英] C++: how do I get the re-roll to display a new set of random numbers in dice game?
问题描述
我差不多完成了这个程序,除了我无法弄清楚如何在用户选择重新滚动时获取一组新的随机数(在第一次提示时滚动R)。下面是我的代码 - 任何人都可以帮我找出我在语法,种子等方面出错的地方......?
我尝试过:
I am almost finished with this program, other than I cannot figure out how to get a new set of random numbers when the user chooses to re-roll (rolling R when prompted the first time). Below is my code -- can anyone help me find out where I went wrong with syntax, seed, etc...?
What I have tried:
/*
Program Name: P30DiceIfElseLoopRandom.cpp
Name: Amy Kaskowitz
Date Started: 5/24/2018 ; Date completed:
Program Description:
Write a Dice Game program that would result in output like this:
Beat the computer!
You rolled a 2 and a 1 (total = 3)
Do you want to keep those? (y or n)
n
Rolling again....
You rolled a 4 and a 1 (total = 5)
Do you want to keep those? (y or n)
n
Rolling again....
You rolled a 1 and a 1 (total = 2)
Do you want to keep those? (y or n)
y
The computer rolled 6 and 3 (total = 9)
So sorry. You lose.
===
Hint: use random numbers b/w 1-6, make 4 random numbers
*/
#include <stdlib.h> /// need this for srand() -- for random numbers
#include <time.h> /// need this for time() -- time
#include <iostream> /// need this for cout<< and cin>>
using namespace std; /// need this for cout<< and cin>>
int main()
{
int iseed = (int)time(0);
srand(iseed);
cout << "Beat the computer! \n";
int roll1 = 1+rand()%6; /// make a random number for die # 1 for user
int roll2 = 1+rand()%6; /// make a random number for die # 2 for user
int UserRoll = roll1 + roll2; /// totals the sum of die 1 and die 2 for the user
char keep;
do
{
cout << "You rolled a " << roll1 << " and a " << roll2 << " for a total of: " << UserRoll << "\n";
cout << "\n";
do
{
cout << "Would you like to keep this total, or roll again? \n";
cout << "\n";
cout << "Enter \"K\" for keep and \"R\" for roll again: \n";
cin >> keep;
if (keep != 'K' && keep != 'R')
{
cout << "That is not a valid choice. Please choose Y to keep your total or N to roll again. " << endl;
cout << "\n";
}
} while(keep != 'K' && keep != 'R');
if (keep == 'R')
{
cout << "You chose R--let's roll again. \n";
}
else
{
cout << "Great! Your total is " << UserRoll << "\n";
}
} while (keep == 'R');
int roll3 = 1+rand()%6; /// make a random number for die # 1 for computer
int roll4 = 1+rand()%6; /// make a random number for die # 2 for computer
int ComputerRoll = roll3 + roll4; /// totals the sum of die 1 and die 2 for the computer
cout << "The computer rolled a " << roll3 << " and a " << roll4 << " for a total of: " << ComputerRoll << "\n";
cout << "\n";
if (ComputerRoll < UserRoll)
{
cout << "Congratulations! You won! \n";
}
if (ComputerRoll > UserRoll)
{
cout << "Sorry. You lose. \n";
}
if (ComputerRoll == UserRoll)
{
cout << "It's a tie. \n";
}
return 0;
}
/*
SAMPLE RUNS:
------------
Beat the computer!
You rolled a 4 and a 6 for a total of: 10
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 4 and a 6 for a total of: 10
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
K
Great! Your total is 10
The computer rolled a 4 and a 6 for a total of: 10
It's a tie.
Process returned 0 (0x0) execution time : 8.763 s
Press any key to continue.
*/
推荐答案
我差不多完成了这个程序,除了我无法弄清楚如何在用户选择重新滚动时获取一组新的随机数(在第一次提示时滚动R)
I am almost finished with this program, other than I cannot figure out how to get a new set of random numbers when the user chooses to re-roll (rolling R when prompted the first time)
你需要锻炼逻辑,一个简单的方法是输出并记录引擎盖后面发生的事情。
You need to workout the logic, a simple method is take output and note what is going on behind the hood.
say game greetings
// Beat the computer!
roll user dices
say user score
// You rolled a 2 and a 1 (total = 3)
say want to keep ?
// Do you want to keep those? (y or n)
get answer
// n
say roll again
// Rolling again....
roll user dices
say user score
// You rolled a 4 and a 1 (total = 5)
say want to keep ?
// Do you want to keep those? (y or n)
get answer
// n
say roll again
// Rolling again....
roll user dices
say user score
// You rolled a 1 and a 1 (total = 2)
say want to keep ?
// Do you want to keep those? (y or n)
get answer
// y
roll computer dices
say computer score
// The computer rolled 6 and 3 (total = 9)
say winner
// So sorry. You lose.
现在,分析一下,然后设计掉掉掉了什么东西,什么不是。
这是你的程序的结构。代码中的任何不同都是结构错误。
来自着名作家的好读物:结构化编程.pdf [ ^ ]
创建一个执行滚动的函数,并允许它接受一个参数,告诉它是否为随机函数设定种子:
Create a function that does the rolling, and allow it to accept a parameter which tells it whether to seed the randomiser or not:
int DiceRoll(bool seed = false)
{
if (seed)
{
// seed the randomiser
srand((int)time(0));
}
int roll = 1+rand()%6; // get a random number between 1 and 6
return roll;
}
// ... in your main loop
int roll1 = DiceRoll(true);
while (doing some actions)
{
int roll2 = DiceRoll();
// more code
}
// ...
试试
Try
#include <cstdlib> /// need this for srand() -- for random numbers
#include <ctime> /// need this for time() -- time
#include <iostream> /// need this for cout<< and cin>>
using namespace std; /// need this for cout<< and cin>>
int main()
{
srand(time(0));
cout << "Beat the computer! \n";
char playAgain;
do
{
int roll1;
int roll2;
int UserRoll;
char keep;
do
{
roll1 = 1+rand()%6; /// make a random number for die # 1 for user
roll2 = 1+rand()%6; /// make a random number for die # 2 for user
UserRoll = roll1 + roll2; /// totals the sum of die 1 and die 2 for the user
cout << "You rolled a " << roll1 << " and a " << roll2 << " for a total of: " << UserRoll << "\n\n";
cout << "Would you like to keep this total, or roll again? \n";
cout << "\n";
while (true)
{
cout << "Enter \"K\" for keep and \"R\" for roll again: \n";
cin >> keep;
if (keep == 'K' || keep == 'R') break; // exit the loop
cout << "That is not a valid choice. Please choose Y to keep your total or N to roll again.\n\n";
}
if (keep == 'R')
cout << "You chose R--let's roll again. \n";
else
cout << "Great! Your total is " << UserRoll << "\n";
} while (keep == 'R');
int roll3 = 1+rand()%6; /// make a random number for die # 1 for computer
int roll4 = 1+rand()%6; /// make a random number for die # 2 for computer
int ComputerRoll = roll3 + roll4; /// totals the sum of die 1 and die 2 for the computer
cout << "The computer rolled a " << roll3 << " and a " << roll4 << " for a total of: " << ComputerRoll << "\n\n";
if (ComputerRoll < UserRoll)
cout << "Congratulations! You won! \n";
else if (ComputerRoll > UserRoll)
cout << "Sorry. You lose. \n";
else //if (ComputerRoll == UserRoll)
cout << "It's a tie. \n";
cout << "Enter \"Y\" to play again, any other value to quit.\n";
cin >> playAgain;
} while (playAgain =='Y');
}
这篇关于C ++:如何重新滚动以在骰子游戏中显示一组新的随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!