2016-05-12 77 views
1

当运行我的代码时,它不会正确地执行列停止操作,就像它似乎没有检测到其他玩家已将2放在列中并且只需要1次就可以获胜,所以它会随机移动。代码如下:// 停止播放器1从赢取TicTacToe游戏Java中的行和列搜索

​​

我的整个电脑关闭的方法是这样的:

所有的
public static void ComputerTurn(int playerTwo) 
{ 
    System.out.println("Computers Turn"); 
    int counterRow= 0; 
    int counterCol= 0; 
    int PotentialRow= -1; 
    int PotentialCol= -1; 
    int i,j; 
    int opponent=1; 

    //Check the Rows if comp already made a move there make another one 
    for (i=0; i<=2; i++) 
    { 
     for (j=0; j<=2; j++) 
     { 
      if (GameBoard[i][j]!= opponent) 
      { 
       PotentialRow=i; 
       PotentialCol=j; 
      } 
      else 
       counterRow++; 
     } 
     if (counterRow==2 && GameBoard[PotentialRow][PotentialCol] != playerTwo) 
     { 
      UpdateGameBoard(PotentialRow,PotentialCol, playerTwo); 
      return; 
     } 
    } 
    //Check the Columns if comp already made a move there make another one 
    PotentialRow=-1; 
    PotentialCol=-1; 

    for (i=0; i<=2; i++) 
    { 
     for (j=0; j<=2; j++) 
     { 
      if (GameBoard[j][i] != opponent) 
      { 
       PotentialRow=j; 
       PotentialCol=i; 
      } 
      else 
       counterCol++; 
     } 
     if (counterCol==2 && GameBoard[PotentialRow][PotentialCol] != playerTwo) 
     { 
      UpdateGameBoard(PotentialRow, PotentialCol, playerTwo); 
      return; 
     } 
    } 

    //Left Diagonal 
    int leftDiagonal=0; 
    if (GameBoard[0][0]==opponent) 
     leftDiagonal++; 
    else 
    { 
     PotentialRow=0; 
     PotentialCol=0; 
    } 
    if(GameBoard[1][1]==opponent) 
     leftDiagonal++; 
    else 
    { 
     PotentialRow=1; 
     PotentialCol=1; 
    } 
    if (GameBoard[2][2]== opponent) 
     leftDiagonal++; 
    else 
    { 
     PotentialRow=2; 
     PotentialCol=2; 
    } 
    if (leftDiagonal==2 && GameBoard[PotentialRow][PotentialCol] != playerTwo && GameBoard[PotentialRow][PotentialCol] != opponent) 
    { 
     UpdateGameBoard(PotentialRow, PotentialCol, playerTwo); 
     return; 
    } 

    //Right Diagonal 
    int rightDiagonal=0; 
    if (GameBoard[2][0]==opponent) 
     rightDiagonal++; 
    else 
    { 
     PotentialRow=2; 
     PotentialCol=0; 
    } 
    if(GameBoard[1][1]==opponent) 
     rightDiagonal++; 
    else 
    { 
     PotentialRow=1; 
     PotentialCol=1; 
    } 
    if (GameBoard[0][2]== opponent) 
     rightDiagonal++; 
    else 
    { 
     PotentialRow=0; 
     PotentialCol=2; 
    } 
    if (rightDiagonal==2 && GameBoard[PotentialRow][PotentialCol] != playerTwo && GameBoard[PotentialRow][PotentialCol] != opponent) 
    { 
     UpdateGameBoard(PotentialRow, PotentialCol, playerTwo); 
     return; 
    } 

    //STOP PLAYER 1 FROM WINNING 

    //Check the Rows 
    for (i=0; i<=2; i++) 
    { 
     counterRow=0; 
     for(j=0; j<=2; j++) 
     { 
      if (GameBoard[i][j]!= opponent) 
      { 
       PotentialRow=i; 
       PotentialCol=j; 
      } 
      else 
       counterRow++; 
     } 
     if (counterRow==2) 
     { 
      UpdateGameBoard(PotentialRow, PotentialCol, playerTwo); 
      return; 
     } 
    } 
    //Check the Columns 
    for(i=0; i<=2; i++) 
    { 
     counterCol=0; 
     for(j=0; j<=2; j++) 
     { 
      if (GameBoard[j][i] != opponent) 
      { 
       PotentialRow=j; 
       PotentialCol=i; 
      } 
      else 
       counterCol++; 
     } 
     if(counterCol==2) 
     { 
      UpdateGameBoard(PotentialRow, PotentialCol, playerTwo); 
      return; 
     } 
    } 
    //Check the Right Diagonal 
    rightDiagonal=0; 
    if (GameBoard[2][0]==opponent) 
     rightDiagonal++; 
    else 
    { 
     PotentialRow=2; 
     PotentialCol=0; 
    } 
    if(GameBoard[1][1]==opponent) 
     rightDiagonal++; 
    else 
    { 
     PotentialRow=1; 
     PotentialCol=1; 
    } 
    if (GameBoard[0][2]== opponent) 
     rightDiagonal++; 
    else 
    { 
     PotentialRow=0; 
     PotentialCol=2; 
    } 
    if (rightDiagonal==2) 
    { 
     UpdateGameBoard(PotentialRow, PotentialCol, playerTwo); 
     return; 
    } 

    //If none of the above applies, then make a random move 
    for(i=0; i<=2; i++) 
    { 
     for(j=0; j<=2; j++) 
     { 
      if (GameBoard[i][j] != playerTwo && GameBoard[i][j] != opponent) 
      { 
       UpdateGameBoard(i,j,playerTwo); 
       return; 
      } 
     } 
    } 
    System.out.println("Computer Finished Turn"); 
} 
+0

唐不要删除你的问题内容,​​而是使用评论。 –

回答

2

首先,你需要重新设置每一行的柜台。 因此,而不是这样的:

counterRow=0; 
for(i=0; i<=2; i++) 
{ 
    for(j=0; j<=2; j++) 

这样做:

for(i=0; i<=2; i++) 
{ 
    int counterRow = 0; 
    for(j=0; j<=2; j++) 

你需要的列同样做。

接下来,检查列时,的 代替GameBoard[i][j] != opponent,你需要翻转ijGameBoard[j][i] != opponent,否则就不是真正的检查列,但再行。

最后,请注意,您并未检查对角线。

作为边注,这些线是没有意义的,并且可以被移除:

PotentialRow= -1; 
PotentialCol= -1; 

在施加上述建议,这里的修改后的代码:

  //Check the Rows 
      for(i=0; i<=2; i++) 
      { 
       int counterRow=0; 
       for(j=0; j<=2; j++) 
       { 
        if (GameBoard[i][j]!= opponent) 
        { 
         PotentialRow=i; 
         PotentialCol=j; 
        } 
        else 
         counterRow++; 
       } 
       if (counterRow==2) 
       { 
        UpdateGameBoard(PotentialRow, PotentialCol, playerTwo); 
        return; 
       } 
      } 
      //Check the Columns 
      for(i=0; i<=2; i++) 
      { 
       int counterCol=0; 
       for(j=0; j<=2; j++) 
       { 
        if (GameBoard[j][i] != opponent) 
        { 
         PotentialRow=j; 
         PotentialCol=i; 
        } 
        else 
         counterCol++; 
       } 
       if(counterCol==2) 
       { 
        UpdateGameBoard(PotentialRow, PotentialCol, playerTwo); 
        return; 
       } 
      } 
+0

@JaneDoe我用我的修改添加了代码。如果它仍然不起作用,那么也许你在其他地方有另一个问题 – janos

+0

你知道哪里可能吗?我没有看到其他的东西可能会影响它检查列? –

+0

我编辑了我的文章并添加了整个计算机方法。 –