交替的球员

时间:2015-02-14 22:03:00

标签: c++

我正在努力实现一场比赛,我想让我的两名球员转身。 这是代码

class Game {
private:
    Grid gameboard;
    Player *players[2];
public:
    Game(int len, string ps1, string ps2);
    void gamePlay();
};

Playerclass abstractAngelDevil继承了Player 在这里我的实施 我的构造函数:

Game::Game(int len, string ps1, string ps2){
    gameBoard.setLen(len);
    players[0] = new Angel(ps1, gameBoard.getLen()/2, gameBoard.getLen()/2, gameBoard);
    players[1] = new Devil(ps2);
}

并且在那里我想每轮旋转球员

void Game::gamePlay(){
    int count;
    do{
        gameBoard.display();
        cout << "It 's" << players[0]->getName() << " to play" << endl;
        players[0]->play(gameBoard);
        gameBoard.display();
        cout << "It's" << joueurs[1]->getName() << " to play !" << endl;
        players[1]->play(gameBoard);
        count++;
    }while(count != 10); /* It 's just for test */
}

我该怎么做?

谢谢你

2 个答案:

答案 0 :(得分:0)

你需要的是:

player = (player + 1) & 1

这将在0和1之间交替。

这也可以:

player = (player + 1) % 2

答案 1 :(得分:0)

这将连续给你0和1:

int counter = 0;

if(counter % 2 == 0)
{
// player O
}else
{
// player X
}

counter++;