将计算机播放器添加到TicTacToe Java应用程序?

时间:2015-02-03 22:03:53

标签: java

如何将播放器2转换为计算机生成的答案而不是其他输入。计算机选择可以完全随机,只要它们遵循游戏规则并选择开放空间即可。

此外,我想保留现有代码的基本结构......只需要将一个播放器变成计算机生成的选择。选择不一定只是一个有效的选择。

不要让答案只是一个可以开始的地方,而不会破坏我所拥有的......请帮助。

package tictactoe;


import java.util.Scanner;

public class TicTacToe
{
private final int BOARDSIZE = 3; // size of the board
private enum Status { WIN, DRAW, CONTINUE }; // game states
private char[][] board; // board representation
private boolean firstPlayer; // whether it's player 1's move
private boolean gameOver; // whether game is over

// Constructor
public TicTacToe()
{
  board = new char[ BOARDSIZE ][ BOARDSIZE ];
  firstPlayer = true;
  gameOver = false;
} // end Constructor

// start game
public void play()
{
  Scanner input = new Scanner( System.in );
  int row; // row for next move
  int column; // column for next move

  System.out.println( "Player X's turn." );

  while ( !gameOver )
  {
     char player = ( firstPlayer ? 'X' : 'O' );

     // player's turn
     do
     {
        System.out.printf(
           "Player %c: Enter row ( 0, 1 or 2 ): ", player );
        row = input.nextInt();
        System.out.printf(
           "Player %c: Enter column ( 0, 1 or 2 ): ", player );
        column = input.nextInt();
     } while ( !validMove( row, column ) );

     board[ row ][ column ] = player;

     firstPlayer = !firstPlayer;

     printBoard();
     printStatus( player );
  } // end while
} // end method play

// show game status in status bar
private void printStatus( int player )
{
  Status status = gameStatus();

  // check game status
  switch ( status )
  {
     case WIN:
        System.out.printf( "Player %c wins.", player );
        gameOver = true;
        break;
     case DRAW:  
        System.out.println( "Game is a draw." );
        gameOver = true;
        break;
     case CONTINUE:  
        if ( player == 'X' )
           System.out.println( "Player O's turn." );
        else
           System.out.println( "Player X's turn." );
        break;
  } // end switch
} // end method printStatus

// get game status
private Status gameStatus()
{   
  int a;

  // check for a win on diagonals
  if ( board[ 0 ][ 0 ] != 0 && board[ 0 ][ 0 ] == board[ 1 ][ 1 ] &&
     board[ 0 ][ 0 ] == board[ 2 ][ 2 ] )
     return Status.WIN;
  else if ( board[ 2 ][ 0 ] != 0 && board[ 2 ][ 0 ] == 
     board[ 1 ][ 1 ] && board[ 2 ][ 0 ] == board[ 0 ][ 2 ] )
     return Status.WIN;

  // check for win in rows
  for ( a = 0; a < 3; a++ )
     if ( board[ a ][ 0 ] != 0 && board[ a ][ 0 ] == 
          board[ a ][ 1 ] && board[ a ][ 0 ] == board[ a ][ 2 ] )
        return Status.WIN;

  // check for win in columns
  for ( a = 0; a < 3; a++ )
     if ( board[ 0 ][ a ] != 0 && board[ 0 ][ a ] == 
          board[ 1 ][ a ] && board[ 0 ][ a ] == board[ 2 ][ a ] )
        return Status.WIN;

  // check for a completed game
  for ( int r = 0; r < 3; r++ )
     for ( int c = 0; c < 3; c++ )
        if ( board[ r ][ c ] == 0 )
           return Status.CONTINUE; // game is not finished

  return Status.DRAW; // game is a draw
} // end method gameStatus

// display board
public void printBoard() 
{
  System.out.println( " _______________________ " );

  for ( int row = 0; row < BOARDSIZE; row++ )
  {
     System.out.println( "|       |       |       |" );

     for ( int column = 0; column < BOARDSIZE; column++ )
        printSymbol( column, board[ row ][ column ] );

     System.out.println( "|_______|_______|_______|" );
  } // end for
} // end method printBoard

// print moves
private void printSymbol( int column, char value )
{
  System.out.printf( "|   %c   ", value );

  if ( column == 2 )
     System.out.println( "|" );
} // end method printSymbol

// validate move
private boolean validMove( int row, int column )
{
  return row >= 0 && row < 3 && column >= 0 && column < 3 &&
     board[ row ][ column ] == 0;
} // end method validMove
 } // end class TicTacToe

3 个答案:

答案 0 :(得分:0)

首先,您将修改do-while中的play(),以便在询问用户和为计算机选择移动之间切换。

如果您只想进行随机移动,您会发现Java类Random很有用。在循环外创建一个实例。

Random random = new Random();

现在,你可以要求[0,2]范围内的随机整数:

int row = random.nextInt(3);
int col = random.nextInt(3);

检查board以查看该地点是否已填满。如果是,你需要选择另一个。

如果您想找到最佳移动,可以使用minimax算法。它搜索了两个玩家可能移动的树,假设每个玩家都会玩得最好。

答案 1 :(得分:0)

当你向用户询问他们移动的行和列时,用一个代表一个玩家的另一个对象的方法调用替换它(在这种情况下它只会提示输入,然后将其返回给调用者)或计算机播放器(在这种情况下,它将通过您确定的任何方式计算移动,并将其返回)。

该方法需要将董事会传递给它。玩家类可以忽略它,但计算机玩家需要它来决定它的移动。

您还需要创建某种Move类,以便将移动(行和列)作为单个返回值传递回来。

答案 2 :(得分:0)

  public int[] autoPlay()
    {
        Random rnd = new Random();
        int randomValue[] = new int[2];
        do{
            randomValue[0] = rnd.nextInt(3);
            randomValue[1] = rnd.nextInt(3);
        } while ( !validMove( randomValue[0], randomValue[1] ) );
        return randomValue;
    }

// start game
    public void play()
    {
        Scanner input = new Scanner( System.in );
        int row; // row for next move
        int column; // column for next move

        System.out.println( "Player X's turn." );

        while ( !gameOver )
        {
            char player = ( firstPlayer ? 'X' : 'O' );

            // player's turn
            do
            {
                if(player == 'X') {
                    System.out.printf(
                            "Player %c: Enter row ( 0, 1 or 2 ): ", player);
                    row = input.nextInt();
                    System.out.printf(
                            "Player %c: Enter column ( 0, 1 or 2 ): ", player);
                    column = input.nextInt();
                }else
                {
                    int array[] = autoPlay();
                    row = array[0];
                    column = array[1];
                }
            } while ( !validMove( row, column ) );

            board[ row ][ column ] = player;

            firstPlayer = !firstPlayer;

            printBoard();
            printStatus( player );
        } // end while
    } // end method play