【问题标题】:How can I change my checking for winner method in my tictactoe game如何在我的tictactoe游戏中更改我的检查获胜者方法
【发布时间】:2019-11-04 23:59:00
【问题描述】:

到目前为止,我有一个可以运行的井字游戏,但是我检查获胜者方法“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

【问题讨论】:

  • 每次,您只测试数组的两个值。我认为您应该测试其中的 4 个,因为您必须排成四行才能获胜(我想您的棋盘是 4x4)。然后对于i++ 指令,在递增之前评估 i 值,这就是为什么你在第一步就有赢家的原因。即使您的代码不起作用,如果您想在评估之前递增,请使用++i
  • 我把所有的“i++”和“j++”都改成了“++i”和“++j”。但是现在我在第二个玩家轮到他之后得到一个 ArrayIndexoutofBoundsException。
  • 因为您不必增加索引。对不起,我忘了告诉你这件事。当您评估您的条件时,如果您增加您的索引ij,它将为您要测试的每个条件增加。所以你不应该增加i,而只是评估this.board[i + 1][j]之类的东西。这样i的值不会改变
  • 但是我有一块柔性板。就像它的大小一样,如果是 4x4 或 7x7 或在 TicTacToe 类本身中实现的任何东西,并将在我的主类中给出。所以我有点需要实现它以使 i + x 达到最大值?

标签: java tic-tac-toe


【解决方案1】:
//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");

【讨论】:

  • 这是一个寻找解决方案的漫长过程,但是,这是获得目标的可靠方法,因为它考虑了获胜所需的所有组合。您可以添加一个在获胜时更新的布尔变量,在较低的循环中您可以检查是否已经获得了胜利,因此不运行代码来检查胜利
  • 很奇怪,这个对我根本不起作用......我的主要课程甚至没有到检查这个是否有赢家的地步。但是我的 numRows 和 numColumns 也是最终的,所以我使用 i 而不是你的 col 而不是你的 row 我使用 j,并迭代 i
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
  • 1970-01-01
  • 2016-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多