2015-03-02 65 views
-3

你好我目前正在使用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; 
} 
+0

你在哪里卡住了?请发布一个能够再现您问题的[MCVE](http://stackoverflow.com/help/mcve)。 – 2015-03-02 21:01:43

+0

因为规则适用于游戏,所以我建议将“游戏规则”功能分解出来,而“Die”可以用于许多游戏(例如:Monoply)。 – 2015-03-02 21:06:29

回答

0

你需要改变你的架构:

class Players 
{ 
    private: 
    int player1Num; 
    int player2Num; 

    public: 
    void playerTurn(); 
}; 

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

如上定义,你有4名选手。每个Players班有2个球员号码。

决定。你是否拥有一个包含所有球员并控制他们的课程,还是你有一个单人球员课程,并让球员控制球员?

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函数,只需使用本地字段而不是玩家选择和playersPoints数组。

+0

好的,谢谢你的帮助!我得到它从第一个玩家到第二个玩家,同时也跟踪每个玩家的积分数量,但由于某种奇怪的原因,它只运行了4次,我不知道为什么... – Madee 2015-03-03 02:05:44

+0

Post some代码然后:)另外,请记住,面向对象编程往往类似于现实生活中的关系。不应该有'球员'级,因为他们没有按照定义分组。另一方面,Player类有一个存在的理由,它可以包含单个玩家的字段和方法,然后成为数组或矢量:) – 2015-03-03 20:08:41