【问题标题】:Win condition method for tic tac toe not working only on the second row井字游戏的获胜条件方法仅适用于第二行
【发布时间】:2018-05-31 13:57:44
【问题描述】:

我有一个井字游戏,使用 gridlayout 在 3x3 网格中排列按钮,我的问题是使用名为 public void checkWinRowX() 的方法,在我的按钮的活动侦听器中,每当按下按钮时,它都会向两者添加 5他们所在的行/列的列 int 和行 int。检查 x win 的方法是检查一行是否等于 15,然后突出显示三个 x

要重现问题,请使用底部的完整代码链接,然后将三个 x 放在中间行,当连续三个时它们应该突出显示,但它们没有。

这些是影响第二行的活动侦听器:

private class twoOneListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
          if(four)
              getTurn(twoOnebutton);
              four = false;
              String twoOneString = twoOnebutton.getText().toString();      
              if (twoOneString.equals("X"))
              {
                     if(x4 == 0)
                     {
                      colOne += 5;
                      rowTwo += 5;
                     }
                     x4++;
                     checkWinColumX();
                     checkWinRowX();      
              }     
        }
   }

             //******2,2*********//
   private class twoTwoListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
             if(five)
              getTurn(twoTwobutton);
              five = false;
              String twoTwoString = oneTwobutton.getText().toString();      
              if (twoTwoString.equals("X"))
              {
                  if(x5 == 0)
                  {  
                     colTwo += 5; 
                     rowTwo += 5;   
                  } 
                     x5++;
                     checkWinColumX();
                     checkWinRowX();      
              }   
        }
   }

            //******2,3*********//
   private class twoThreeListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {

              if(six)
              getTurn(twoThreebutton);
              six = false;
              String twoThreeString = twoThreebutton.getText().toString();      
              if (twoThreeString.equals("X"))
              {
                  if(x6 == 0)
                     {  
                     colThree += 5; 
                     rowTwo += 5;    
                     }
                     x6++;
                     checkWinColumX();
                     checkWinRowX();      
              }
           }           
      }

这是检查行方法:

public void checkWinRowX()
             {
               if(gameWon = false); 
               {
                  //first row
                  if(rowOne == 15)
                  {
                     oneOnebutton.setBackground(Color.YELLOW);
                     oneTwobutton.setBackground(Color.YELLOW);
                     oneThreebutton.setBackground(Color.YELLOW);
                  }
                  //Second row
                  if(rowTwo == 15)
                  {
                     twoOnebutton.setBackground(Color.YELLOW);
                     twoTwobutton.setBackground(Color.YELLOW);
                     twoThreebutton.setBackground(Color.YELLOW);
                  }
                  //Third row
                  if(rowThree == 15)
                  {
                     threeOnebutton.setBackground(Color.YELLOW);
                     threeTwobutton.setBackground(Color.YELLOW);
                     threeThreebutton.setBackground(Color.YELLOW);
                  }
               }
           }

完整代码:https://pastebin.com/rYXrx3m4

【问题讨论】:

  • 帮大家一个忙,格式化你的代码
  • if(gameWon = false); 不应该是if(gameWon == false);吗?
  • 我通过打印 rowTwo 在代码中查明了问题,这与 twoTwo 的活动侦听器有关,twoOne 和 twoThree 都将 5 添加到 rowTwo,但 twoTwo 没有将 5 添加到 rwoTwo,因此它不会突出显示为 won .
  • 顺便说一句,if 之后的 ; 使 if 语句无效,因为 ; 是一个空语句。 { } 块将始终以这种方式执行。

标签: java


【解决方案1】:

我明白了,twoTwos 活动侦听器与其他侦听器不同,我知道有什么不同,但我将另一个侦听器复制并粘贴到其中并更改了对象

已修复:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多