你好我目前正在使用C++,而我是一名初学者。我们有选择为我们的程序选择一个骰子游戏。我选择了一个有以下规则的游戏:简单的骰子游戏C++无法切换球员
选择一个数字并掷骰子。每当你推出该号码时,你就得分。当你滚动这个数字时,你会得到另一个回合。当这个数字没有滚动时,转弯就结束了。在每次滚动号码时标记符号。首先以10分的成绩获胜。
过去两天我一直在为此工作,而我非常沮丧。任何帮助是极大的赞赏。我们正在使用类和构造函数。我的主要问题是能够在两名球员之间来回走动。我尝试使用do while循环,但它并没有真正的帮助。这里是我的代码:
//骰子游戏
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
class Die
{
private:
int num;
public:
Die(); //Default constructor because it doesn't require arguments
void roll();
int getNum();
void gameRules();
};
class Players
{
private:
int player1Num;
int player2Num;
public:
void playerTurn();
};
void Die::gameRules()
{
cout << " ****Welocome to Madawi's Dice Game ****\n\n ";
cout << "Here are the rules:\n\n ";
cout << "This is a two player game, so grab a buddy!\n\n ";
cout << "\t1.)Please choose a number from 1-6\n\n ";
cout << "\t2.)Then roll the dice, if it lands on the number";
cout << "\tyou chose, you get a point and go again\n\n ";
cout << "\t3.)If you choose a number and it doesn't land on";
cout << "\tthe number you chose,you don't recieve a point, ";
cout << "\tand the second player goes\n\n ";
cout << "\t4.)The first person to get to 5 points first wins!\n\n ";
cout << "ENJOY!!! Let's begin:\n\n ";
}
Die::Die()
{
num = 1; //Initialize so that the values start at one
srand((unsigned)time(NULL));
}
void Die::roll()
{
num = rand()%6 + 1;
}
int Die::getNum()
{
return num;
}
void Players::playerTurn()
{
Die die1;
cout << "Hello player 1! Please choose a number from 1-6:\n";
cin >> player1Num;
cout << "You've chosen the number " << player1Num << endl;
die1.roll(); //rolls the dice
cout << die1.getNum() << endl; //displays number rolled
if (player1Num == die1.getNum())
{
cout << "Good job player 1! You got the same number\n ";
player1Points++; //Keeps track of player 1's score
if(player1Points == 5)
{
cout << "Congratulations player 1 you've won the game!\n";
cout << "Thanks for playing!\n ";
}
}
else
{
cout << "Sorry the numbers didn't match up\n ";
cout << "Player 2 its your turn\n ";
cout << "Player 2 please choose a number ";
cin >> player2Num;
cout << "You've chosen the number " << player2Num << endl;
die1.roll();
cout << die1.getNum() << endl;
if(player2Num == die1.getNum())
{
cout << "Good job player 2! You got the same number\n ";
player2Points++; //Keeps track of player 2's points
cout << "Player 2 its your turn again, please choose a number:\n ";
cin >> player2Num;
die1.roll();
cout << die1.getNum() << endl;
}
if(player2Points == 5)
{
cout << "Congratulations player 1 you've won the game!\n";
cout << "Thanks for playing!\n ";
}
}
}
int main()
{
Die dice1;
Players player1;
Players player2;
dice1.gameRules(); //Says the game rules
player1.playerTurn(); //Player makes a selection
return 0;
}
你在哪里卡住了?请发布一个能够再现您问题的[MCVE](http://stackoverflow.com/help/mcve)。 – 2015-03-02 21:01:43
因为规则适用于游戏,所以我建议将“游戏规则”功能分解出来,而“Die”可以用于许多游戏(例如:Monoply)。 – 2015-03-02 21:06:29