简单的骰子游戏C ++无法切换玩家

时间:2015-03-02 20:59:15

标签: 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;
}

2 个答案:

答案 0 :(得分:0)

您需要更改架构:

class Players
{
    private:
     int player1Num;
     int player2Num;

    public:
     void playerTurn();
};

int main()
{
    Die dice1;
    Players player1;
    Players player2;
//...
}

如上所述,您有4名玩家。每个Players班级都有2个玩家编号。

决定。您是否有一个包含所有玩家并控制它们的类,或者您是否拥有单个玩家类并让main函数控制玩家?

答案 1 :(得分:0)

您应该创建辅助函数void makeTurn(int player);并使用它来为任何玩家进行移动。它应该包含带有滚动和评分的do-while循环。

伪代码:

function makeTurn(playerNumber):
    display "Player " + playerNumber + "turn"
    do
        roll = rollDice();
        if (roll == playersChoice[playerNumber])
            display "You scored one point!"
            playersPoints[playerNumber]++;
        else
            display "Sorry, you have failed :("
            break //End the loop
    while true //Do infinitely (till break)
end function

另外,我没有看到球员课程的原因。使用数组来包含玩家得分和选择,然后您可以轻松地将其扩展到更多玩家。如果您认为它是播放器的 实例 ,那么您应该只有得分和选择字段,并且播放器移动应该是我提供的代码中的makeMove函数你之前,只使用本地字段而不是playerChoice和playersPoints数组。

相关问题