如果在井字游戏中已经占据位置,则重复的玩家回合

时间:2018-12-11 02:53:04

标签: java

我必须为正在做的作业创建一个tick tac toe程序。

我遇到了一个问题,即如果已经选择了阵列中的位置,则程序将跳过玩家的回合。

我想知道是否有人会知道如何帮助我解决此问题...

例如:我输入数组[1] [1],第二位玩家也选择了该位置。该程序不会更新棋盘,而是继续寻找下一个玩家,因此跳过了该回合。

我将在下面粘贴我的代码,如果有人有任何疑问,请随时提出。

public class TicTacToe {

public static char[][] board = new char[3][3];
static Scanner scan = new Scanner(System.in);
public static String keepPlaying;
public static char yesOrNo;
public static boolean playing;
public static int row, col;
public static User human1 = new User();
public static User human2 = new User();

public static void main(String[] args) {

    human1.getUser1Name();
    human2.getUser2Name();

    System.out.println("\nPlayer 1: " + human1.human1Name() + ": X's");
    System.out.println("Player 2: " + human2.human2Name() + ": O's\n");

    initializeBoard();
    displayBoard();

    playing = true;


    while (playing == true)
    {

        System.out.println("\n" + human1.human1Name() + " it's your turn.");
        human1.getChoice();
        human1.convert();


        if (human1.returnRow() == 0 && human1.returnCol() == 0/* && board[0][0] == ' '*/)
        {
            board[0][0] = 'X';
            displayBoard();
        }
        else if (human1.returnRow() == 0 && human1.returnCol() == 1 && board[1][0] == ' ')
        {
            board[1][0] = 'X';
            displayBoard();
        }
        else if (human1.returnRow() == 0 && human1.returnCol() == 2 && board[2][0] == ' ')
        {
            board[2][0] = 'X';
            displayBoard();
        }
        else if (human1.returnRow() == 1 && human1.returnCol() == 0 && board[0][1] == ' ')
        {
            board[0][1] = 'X';
            displayBoard();
        }
        else if (human1.returnRow() == 1 && human1.returnCol() == 1 && board[1][1] == ' ')
        {
            board[1][1] = 'X';
            displayBoard();
        }
        else if (human1.returnRow() == 1 && human1.returnCol() == 2 && board[2][1] == ' ')
        {
            board[2][1] = 'X';
            displayBoard();
        }
        else if (human1.returnRow() == 2 && human1.returnCol() == 0 && board[0][2] == ' ')
        {
            board[0][2] = 'X';
            displayBoard();
        }
        else if (human1.returnRow() == 2 && human1.returnCol() == 1 && board[1][2] == ' ')
        {
            board[1][2] = 'X';
            displayBoard();
        }
        else if (human1.returnRow() == 2 && human1.returnCol() == 2 && board[2][2] == ' ')
        {
            board[2][2] = 'X';
            displayBoard();
        }

        if (checkForWinner() == true)
        {
            System.out.println("\n" + human1.human1Name() + " has won the game!!!");
            playAgain();
            if (playing == false)
            {
                break;
            }

        }

        if (board[0][0] != ' ' && board[0][1] != ' ' && board[0][2] != ' ' && board[1][0] != ' ' && board[1][1] != ' '
                && board[1][2] != ' ' && board[2][0] != ' ' && board[2][1] != ' ' && board[2][2] != ' ')
        {
            System.out.println("It's a draw!!!");
            playAgain();
            if (playing == false)
            {
                break;
            }
        }



        System.out.println("\n" + human2.human2Name() + " it's your turn.");
        human2.getChoice();
        human2.convert();

        if (human2.returnRow() == 0 && human2.returnCol() == 0 /*&& board[0][0] == ' '*/)
        {
            board[0][0] = 'O';
            displayBoard();
        }
        else if (human2.returnRow() == 0 && human2.returnCol() == 1 && board[1][0] == ' ')
        {
            board[1][0] = 'O';
            displayBoard();
        }
        else if (human2.returnRow() == 0 && human2.returnCol() == 2 && board[2][0] == ' ')
        {
            board[2][0] = 'O';
            displayBoard();
        }
        else if (human2.returnRow() == 1 && human2.returnCol() == 0 && board[0][1] == ' ')
        {
            board[0][1] = 'O';
            displayBoard();
        }
        else if (human2.returnRow() == 1 && human2.returnCol() == 1 && board[1][1] == ' ')
        {
            board[1][1] = 'O';
            displayBoard();
        }
        else if (human2.returnRow() == 1 && human2.returnCol() == 2 && board[2][1] == ' ')
        {
            board[2][1] = 'O';
            displayBoard();
        }
        else if (human2.returnRow() == 2 && human2.returnCol() == 0 && board[0][2] == ' ')
        {
            board[0][2] = 'O';
            displayBoard();
        }
        else if (human2.returnRow() == 2 && human2.returnCol() == 1 && board[1][2] == ' ')
        {
            board[1][2] = 'O';
            displayBoard();
        }
        else if (human2.returnRow() == 2 && human2.returnCol() == 2 && board[2][2] == ' ')
        {
            board[2][2] = 'O';
            displayBoard();
        }


        if (checkForWinner() == true)
        {
            System.out.println("\n" + human2.human2Name() + " has won the game!!!");
            playAgain();
        }

        if (board[0][0] != ' ' && board[0][1] != ' ' && board[0][2] != ' ' && board[1][0] != ' ' && board[1][1] != ' '
                && board[1][2] != ' ' && board[2][0] != ' ' && board[2][1] != ' ' && board[2][2] != ' ')
        {
            System.out.println("It's a draw!!!");
            playAgain();
        }


    }


}

public TicTacToe() {

    board = new char[3][3];

}

public static void initializeBoard() {

    for (int row = 0; row < 3; row++)
        {
            for (int col = 0; col < 3; col++)
            {
                board[row][col] = ' ';
            }
        }
    }

public static void displayBoard() {

    System.out.println("    A   B   C");
    System.out.println("  -------------");

    for (int row = 0; row < 3; row++)
    {
        System.out.print((row + 1) + " | ");
        for (int col = 0; col < 3; col++)
        {
            System.out.print(board[row][col] + " | ");
        }
        System.out.println();
        System.out.println("  -------------");
    }
}

public static boolean checkForWinner() {

    if (board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][1] != ' ')
    {
        return true;
    }
    if (board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][1] != ' ')
    {
        return true;
    }
    if (board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[2][1] != ' ')
    {
        return true;
    }
    if (board[0][0] == board[1][0] && board[1][0] == board[2][0] && board[1][0] != ' ')
    {
        return true;
    }
    if (board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[1][1] != ' ')
    {
        return true;
    }
    if (board[0][2] == board[1][2] && board[1][2] == board[2][2] && board[1][2] != ' ')
    {
        return true;
    }
    if (board[0][0] == board[1][1] && board [1][1] == board[2][2] && board[1][1] != ' ')
    {
        return true;
    }
    if (board[0][2] == board [1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
    {
        return true;
    }

    return false;
}

public static boolean playAgain() {

    System.out.println("\nWould you like to play again?");

        keepPlaying = scan.nextLine();

        yesOrNo = keepPlaying.toUpperCase().charAt(0);

    if (yesOrNo == 'N')
    {
        System.out.println("\nThanks for playing!");
        playing = false;
        initializeBoard();
        return playing;
    }   
        else if (yesOrNo == 'Y')
        {   
            initializeBoard();
            displayBoard();
            return true;
        }
    return playing;
}

public static boolean notValid() {

    if (human1.returnRow() > 2 || human1.returnRow() < 0 || human1.returnCol() > 2 || human1.returnCol() < 0 || human2.returnRow() > 2 
            || human2.returnRow() < 0 || human2.returnCol() > 2 || human2.returnCol() < 0 || !isEmpty())
    {
        System.out.println("Invalid");
        return true;
    }
    else
    {
        return false;
    }
}

public static boolean isEmpty() {

    if (board[0][0] == ' ' && board[0][1] == ' ' && board[0][2] == ' ' && board[1][0] == ' ' && board[1][1] == ' '
                && board[1][2] == ' ' && board[2][0] == ' ' && board[2][1] == ' ' && board[2][2] == ' ')
    {
        return true;
    }
    else
    {
        System.out.println("That position is taken.\n");
        return false;
    }
}

}

1 个答案:

答案 0 :(得分:2)

您的编程正在转弯,因为它完全按照您的要求进行。

else if (human2.returnRow() == 1 && human2.returnCol() == 1 && board[1][1]==' ')
    {
        board[1][1] = 'O';
        displayBoard();
    }

如果拾取的点是[1] [1],并且未被占用,则进行更新。如果该位置已被占用,则if语句中的代码将不会执行,并且程序将继续。

可以在不过度破坏现有代码的情况下检查和核实现场的另一种方法

boolean turnOver = false;
while(!turnOver){
    if(board[human2.returnRow()][human2.returnCol()] != ' '){
    System.out.println("That space is taken")
    }else{
    if(human2.returnRow() == 1 && human2.returnCol() == 1){
      board[1][1] = 'O';
      displayBoard();
     }

   //other cases
   turnOver = true;
   }

此外,您的代码非常多余。您可以通过一个方法调用而不是10个if语句来实现更新木板并检查该职位是否得到填补的目标。

public void setPosition(int row, int col, int player){
  if(board[row][col] != ' '){
      System.out.println("That space is taken")
      return;
  }

  if(player == 1){
    board[row][col] = 'X';
  }else{
    board[row][col] = 'O';
  }
}