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

问题描述 投票:0回答:1

我目前为止正在玩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
java tic-tac-toe
1个回答
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");
© www.soinside.com 2019 - 2024. All rights reserved.