我如何在我的tictactoe游戏中更改对获胜者的检查方式

时间:2019-11-04 14:53:46

标签: java tic-tac-toe

我目前为止正在玩TicTacToe游戏,但是我检查赢家方法“ hasPlayerWon”不起作用。每一次有玩家采取行动,它就已经给了我一个赢家。

我尝试使用numRows和numColumns来代替编写“ i ++”等。但是它也没有用。

private boolean hasPlayerWon() {
        boolean winner = false;
        for (int i = 0; i < numRows; i++) {

            for (int j = 0; j < numColumns; j++) {
                {
                    if (this.board[i][j] == PLAYER_A && this.board[i++][j++] == PLAYER_A) {
                        System.out.println("Player A has won");
                    }
                    if (this.board[i][j] == PLAYER_A && this.board[i++][j] == PLAYER_A) {
                        System.out.println("Player A has won");
                    }
                    if (this.board[i][j] == PLAYER_A && this.board[i][j++] == PLAYER_A) {
                        System.out.println("Player A has won");
                    }
                        if (this.board[i][j] == PLAYER_B && this.board[i++][j++] == PLAYER_B) {
                            System.out.println("Player B has won");
                        }
                    if (this.board[i][j] == PLAYER_B && this.board[i++][j] == PLAYER_B) {
                        System.out.println("Player B has won");
                    }
                    if (this.board[i][j] == PLAYER_B && this.board[i][j++] == PLAYER_B) {
                        System.out.println("Player B has won");
                    }

                    }
                }
            }

         return winner;

    }

当然,我希望它能在用对角线水平或垂直方向正确填满他的标志后将我送出。但是,仅需输入一次,它就可以将结果打印出来。 1代表玩家A的“ X”,而2代表玩家B的“ O”。所以我想检查在水平,垂直还是对角线中是否填充了1或2。

我得到的输出:

Move: 0
0 0 0 0 
0 0 0 0 
0 0 0 0 
0 0 0 0 

Turn for Player1
Select a rowposition between 0 and 4
1
Select a columnposition between 0 and 4
1
Move: 1
0 0 0 0 
0 1 0 0 
0 0 0 0 
0 0 0 0 

Player A has won
Turn for Player2
Select a rowposition between 0 and 4

1 个答案:

答案 0 :(得分:0)

//check if all the data across the row is the same player
for(int row=0; row<4, row++){
 int col=0;

//Check if player A has won 
 if(this.board[row][col++] == PLAYER_A && this.board[row][col++] == PLAYER_A && 
    this.board[row][col++] == PLAYER_A && this.board[row][col++] == PLAYER_A)
      System.out.println("Player A has won");

 //reset col to 0
    col=0;

 //Check if player B has won 
  else if(this.board[i][col++] == PLAYER_B && this.board[i][col++] == PLAYER_B && 
    this.board[i][col++] == PLAYER_B && this.board[i][col++] == PLAYER_B)
      System.out.println("Player B has won");


}

//check if all the data across the column is the same player
for(int col=0; col<4, col++){
 int row=0;

//Check if player A has won 
 if(this.board[row++][col] == PLAYER_A && this.board[row++][col]  == PLAYER_A && 
    this.board[row++][col] == PLAYER_A && this.board[row++][col]  == PLAYER_A)
      System.out.println("Player A has won");

   reset the row to 0
   col=0;

 //Check if player B has won 
else if(this.board[row++][col] == PLAYER_B && this.board[row++][col] == PLAYER_B && 
       this.board[row++][col] == PLAYER_B && this.board[row++][col] == PLAYER_B)
      System.out.println("Player B has won");
 }

 //check if the diagonals all have the same board

    int row =0;
    int col =0;

 //check from the top left to bottom right
 //Player A
  if(this.board[row++][col++] == PLAYER_A && this.board[row++][col++]  == PLAYER_A && 
  this.board[row++][col++] == PLAYER_A && this.board[row++][col++]  == PLAYER_A)
      System.out.println("Player A has won");
  //Player B
  if(this.board[row++][col++] == PLAYER_B && this.board[row++][col++]  == PLAYER_B && 
    this.board[row++][col++] == PLAYER_B && this.board[row++][col++]  == PLAYER_B)
      System.out.println("Player B has won");

  row=0;
  col=3;
  //check from the top right to bottom left
  //Player A
  if(this.board[row++][col--] == PLAYER_A && this.board[row++][col--]  == PLAYER_A && 
 this.board[row++][col--] == PLAYER_A && this.board[row++][col--]  == PLAYER_A)
      System.out.println("Player A has won");
 //Player B
  if(this.board[row++][col--] == PLAYER_B && this.board[row++][col--]  == PLAYER_B && 
 this.board[row++][col--] == PLAYER_B && this.board[row++][col--]  == PLAYER_B)
      System.out.println("Player B has won");