3

我正在制作井字游戏,并创建了一个将 X 或 O 插入我的数组的函数。我的设计遇到了一个问题。我调用该函数为 X 移动,但是当下一个玩家轮到时,我如何让它调用 O?

有没有办法在我放 makeMove() 之后我可以以某种方式调用它来代替 X。因为你可以看到如果我做 X 它总是会要求 X 而不是 O。我怎样才能做到选择拉入 X 或 O 转弯。

问题是我只需要一个可以移动的功能。

int main()
{
    while(SOME CONDITION HERE)
    {
        printBoard();
        cout << "Player X please choose a position: ";
        makeMove('X');
        cout << "Player O please choose a position: ";
        makeMove('O');
    }
}


int makeMove(char marker)
{
    int choosePosition = 0;

    cin >> choosePosition;

    ticTacBoard[choosePosition - 1] = marker;
}
4

5 回答 5

1

从这个开始:

int main()
{
    while(SOME CONDITION HERE)
    {
        printBoard();
        cout << "Player X please choose a position: ";
        makeMove('X');
        cout << "Player O please choose a position: ";
        makeMove('O');
    }
}


int makeMove(char marker)
{
    int choosePosition = 0;

    cin >> choosePosition;

    ticTacBoard[choosePosition - 1] = marker;
}

请注意,您将要更改该SOME CONDITION HERE部分,但您可以快速替换它1并获得与当前脚本相同的行为(实际上,更好一点)。

但是你最终会想要在那里放一些有意义的东西——告诉程序停止提示玩家位置的东西,比如宣布获胜者。


以下只是做同样事情的一种更简化的方式:

int main()
{
    while(SOME CONDITION HERE)
    {
        printBoard();

        makeMove('X');
        makeMove('O');
    }
}


int makeMove(char marker)
{
    cout << "Player " << marker << " please choose a position: ";

    int choosePosition = 0;

    cin >> choosePosition;

    ticTacBoard[choosePosition - 1] = marker;

    return 0;
}

请注意添加的return 0内容——如果您不想返回某些东西,则应该只makeMove返回void,以免造成混淆。

于 2011-11-07T23:03:38.190 回答
0

您可以尝试使用参数:

int makeMove(char player);

makeMove('O');
makeMove('X');
于 2011-11-07T23:02:51.910 回答
0

首先,不要main()递归调用。请改用循环。

其次,用一个变量(player如下图)来表示轮到谁了。

int main()
{
    char player = 'X';
    while (/* game not finished */) {
      printBoard();
      makeMove(player);
      player = (player == 'X') ? 'O' : 'X';
    }
}

void makeMove(char player)
{
    cout << "Player " << player << " please choose a position: ";
    int choosePosition = 0;
    cin >> choosePosition;
    ticTacBoard[choosePosition - 1] = player;
}
于 2011-11-07T23:03:44.050 回答
0

像这样的事情可能会奏效......只要确保使用循环进行移动。

char player = 'X';

while(...) {
  cout << "choose position...";
  makeMove(player);

  if(player == 'X')
    player = 'O';
  else
    player = 'X';
  ...
}

//in make move:
int makeMove(char player) {
  int choosePosition = 0;
  cin >> choosePosition;
  ticTacBoard[choosePosition - 1] = player;
}
于 2011-11-07T23:04:14.387 回答
0

http://scripts.franciscocharrua.com/javascript/tic-tac-toe/

几个月前,我将此添加到我的网站。我承认它可能有点复杂,并且在 JavaScript 中,但它可能会有所帮助。

JS:

function tic_tac_toe(blank_token, player_tokens, artificial_intelligence)
{         
         this.board = [[blank_token, blank_token, blank_token],
                   [blank_token, blank_token, blank_token],
                   [blank_token, blank_token, blank_token]];

     this.blank_token = blank_token;
     this.player_tokens = player_tokens;         
     this.display_choice = function() {};
     this.declare_human_win = function() {};
     this.declare_computer_win = function() {};
     this.declare_tie = function() {};         
     this.artificial_intelligence = artificial_intelligence; 

     this.start = 
     function() 
     {                  
              //Randomly choose a token for the human player.
              var human_token_index = Math.floor(Math.random() * this.player_tokens.length);
              this.human_player = this.player_tokens[human_token_index];

              //Place the chosen token at the end of the array.
              this.player_tokens[human_token_index] = this.player_tokens[this.player_tokens.length - 1];
              this.player_tokens[this.player_tokens.length - 1] = this.human_player;

              //Randomly choose a different token for the computer player.
              var computer_token_index = Math.floor(Math.random() * (this.player_tokens.length - 1));
              this.computer_player = this.player_tokens[computer_token_index];                                    

              //Clear the board.
              for(var row = 0; row < 3; row++)
              for(var collumn = 0; collumn < 3; collumn++)
              {
                  this.place(this.blank_token, row, collumn);
              }   

              if(Math.random() < 0.5)
              {                    
                 this.turn = this.computer_player;
                 this.computer_turn();
              }
              else
              {
                 this.turn = this.human_player;
              }                           
     };

     //Returns the token of the winning player. 
     //If no one has won yet or the game is tied, returns the blank token.
     //Used in combination with blank_token_count() to determine if the game is tied.
     this.winner =
     function()
     {
              var winner = this.blank_token;

              //Check for 3 consecutive horisontal tokens.
              for(var row = 0; row < 3; row++)
              {
                  winner = this.board[row][0];
                  for(var collumn = 1; collumn < 3; collumn++)
                  {
                      if(this.board[row][collumn] != winner)
                      {
                         winner = this.blank_token;
                      }                        
                  }

                  if(winner != this.blank_token)
                  {
                     return(winner);
                  }                      
              }

              //Check for 3 consecutive vertical tokens.
              for(var collumn = 0; collumn < 3; collumn++)
              {
                  winner = this.board[0][collumn];
                  for(var row = 1; row < 3; row++)
                  {
                      if(this.board[row][collumn] != winner)
                      {
                         winner = this.blank_token;
                      }                               
                  }

                  if(winner != this.blank_token)
                  {
                     return(winner);
                  }                                            
              }

             //Check for 3 consecutive diagonal tokens.
             winner = this.board[0][0];
             for(var row = 1; row < 3; row++)
             {
                 if(this.board[row][row] != winner)
                 {
                    winner = this.blank_token;
                 }
             }

             if(winner != this.blank_token)
     {
        return(winner);
             }

             winner = this.board[0][2];
             for(var row = 1; row < 3; row++)
             {
                 if(this.board[row][2 - row] != winner)
                 {
                    winner = this.blank_token;
                 }
             }

             if(winner != this.blank_token)
     {
        return(winner);
             }

             return(winner);
     };                

     this.blank_token_count =
     function()
     {
              var blank_token_count = 0;

              for(var row = 0; row < 3; row++)
              for(var collumn = 0; collumn < 3; collumn++)
              {
                  if(this.board[row][collumn] == this.blank_token)
                  {
                     blank_token_count++;
                  }
              }

              return(blank_token_count);
     };

     this.computer_turn =
     function()
     {  
             //Lets the computer take its turn if the game is not over.
             if(this.turn != this.blank_token)
             {
                this.turn = this.computer_player;
                var computer_move = this.artificial_intelligence();
                this.place(this.computer_player, computer_move.row, computer_move.collumn);                                  
             }
     };

     this.human_turn = 
     function(row, collumn)
     {
             this.place(this.human_player, row, collumn);
             this.computer_turn();
     }

     this.place =
     function(token, row, collumn)
     {
              if(row < 3 && collumn < 3 && 
                ((this.turn == token && this.board[row][collumn] == this.blank_token) || token == this.blank_token))
              {                     
                 this.board[row][collumn] = token;
                 this.display_choice(token, row, collumn)

                 //Finishes the game in case a of a win or a tie.
                 //When the board is not being reset.
                 if(token != this.blank_token)
                 {
                    var winner_token = this.winner(); 

                    if(winner_token == this.human_player)
                    {
                       this.declare_human_win();
                       this.turn = this.blank_token;
                    }

                    if(winner_token == this.computer_player)
                    {
                       this.declare_computer_win();
                       this.turn = this.blank_token;
                    }

                    if(winner_token == this.blank_token && this.blank_token_count() == 0)
                    {
                       this.declare_tie();
                       this.turn = this.blank_token;
                    }

                    //Gives the human player a turn, if the game is not over.
                    if(this.turn == this.computer_player)
                    {
                       this.turn = this.human_player
                    }
                 }
              }
     };

}

于 2011-11-07T23:06:05.930 回答