Как я могу поменять свой метод проверки на победителя в моей игре tictactoe - PullRequest
0 голосов
/ 04 ноября 2019

У меня до сих пор работает игра 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 означает «X» игрока A, а 2 - «O» игрока B. Поэтому я хочу проверить, заполнены ли по горизонтали, вертикали или диагонали 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 голосов
/ 04 ноября 2019
//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");
...