我怎样才能写出获胜者';Java上的Tic-Tac-Toe游戏中的逻辑是什么? public void WinnerCheck() { 如果(m_-allButtons[0][0]。getText()==m_-allButtons[0][1]。getText()&&m_-allButtons[0][1]。getText()==m_-allButtons[0][2]。getText()) { 禁用按钮(); } else if(m_allButtons[1][0]。getText()==m_allButtons[1][1]。getText()&&m_allButtons[1][1]。getText()==m_allButtons[1][2]。getText()) { 禁用按钮(); } } 私有void AddAllEventHandlers() { if(m_allButtons!=null) { 对于(int i=0;i

我怎样才能写出获胜者';Java上的Tic-Tac-Toe游戏中的逻辑是什么? public void WinnerCheck() { 如果(m_-allButtons[0][0]。getText()==m_-allButtons[0][1]。getText()&&m_-allButtons[0][1]。getText()==m_-allButtons[0][2]。getText()) { 禁用按钮(); } else if(m_allButtons[1][0]。getText()==m_allButtons[1][1]。getText()&&m_allButtons[1][1]。getText()==m_allButtons[1][2]。getText()) { 禁用按钮(); } } 私有void AddAllEventHandlers() { if(m_allButtons!=null) { 对于(int i=0;i,java,tic-tac-toe,Java,Tic Tac Toe,当我在ActionEvent中调用WinnerCheck()方法时,它实际上同时禁用了所有按钮。我试图写一个逻辑,一旦有人赢了tic tac toe游戏,所有的按钮都会被禁用;我所说的赢是指任何一行或一列具有相同的值(X或O),但没有任何玩家值或任何赋值。它只是以X开始,并以相反的值替换每次单击。谢谢大家! 我猜这是学校的作业 你需要一个算法来检查赢的条件。你不需要检查整个棋盘,只需要检查上一局可能获胜路径上的方块 通用算法: 将总数设置为1 如果位置(x-1)%3,(y-1)%3不是相同的标记

当我在ActionEvent中调用WinnerCheck()方法时,它实际上同时禁用了所有按钮。我试图写一个逻辑,一旦有人赢了tic tac toe游戏,所有的按钮都会被禁用;我所说的赢是指任何一行或一列具有相同的值(X或O),但没有任何玩家值或任何赋值。它只是以X开始,并以相反的值替换每次单击。谢谢大家!

我猜这是学校的作业

你需要一个算法来检查赢的条件。你不需要检查整个棋盘,只需要检查上一局可能获胜路径上的方块

通用算法:

  • 将总数设置为1
  • 如果位置(x-1)%3,(y-1)%3不是相同的标记(x或y),则从步骤6继续
  • 如果位置(x-1)%3,(y-1)%3是相同的标记,则添加到总数中
  • 如果位置(x+1)%3,(y+1)%3不是相同的标记,请从步骤6继续
  • 如果位置(x+1)%3,(y+1)%3是相同的标记,则添加到总数中。若总数为3,游戏获胜
  • 对这些坐标((x-1,y)、(x+1,y))、((x,y-1)、(x,y+1))和((x-1,y+1)、(x+1,y-1))重复1-5,以代替((x-1,y-1)、(x+1,y+1))

  • 我猜这是学校的作业

    你需要一个算法来检查赢的条件。你不需要检查整个棋盘,只需要检查上一局可能获胜路径上的方块

    通用算法:

  • 将总数设置为1
  • 如果位置(x-1)%3,(y-1)%3不是相同的标记(x或y),则从步骤6继续
  • 如果位置(x-1)%3,(y-1)%3是相同的标记,则添加到总数中
  • 如果位置(x+1)%3,(y+1)%3不是相同的标记,请从步骤6继续
  • 如果位置(x+1)%3,(y+1)%3是相同的标记,则添加到总数中。若总数为3,游戏获胜
  • 对这些坐标((x-1,y)、(x+1,y))、((x,y-1)、(x,y+1))和((x-1,y+1)、(x+1,y-1))重复1-5,以代替((x-1,y-1)、(x+1,y+1))

  • 如果要比较字符串,请使用
    .equals
    而不是
    =
    。一个空字符串相当于另一个空字符串,因此当您进行检查时,您必须确保它们是“X”或“O”,而不仅仅是一个空字符串。“我如何编写这个?”可能是一个过于宽泛的问题,这不是一个代码编写服务。如果您想比较字符串,使用
    .equals
    而不是
    =
    。一个空字符串相当于另一个空字符串,所以当您进行检查时,您必须确保它们是“X”或“O”,而不仅仅是一个空字符串。“我如何编写这个?”对于so来说可能是一个过于宽泛的问题,因为so不是一个代码编写服务。
    public void WinnerCheck() 
    {
        if (m_allButtons[0][0].getText() == m_allButtons[0][1].getText() && m_allButtons[0][1].getText() == m_allButtons[0][2].getText()) 
        {
            DisableAllButtons();
        }
        else if (m_allButtons[1][0].getText() == m_allButtons[1][1].getText() && m_allButtons[1][1].getText() == m_allButtons[1][2].getText())
        {
            DisableAllButtons();
        }
    
    }   
    
    private void AddAllEventHandlers()
    {
        if (m_allButtons != null)
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    JButton currentButton = m_allButtons[i][j];
                    currentButton.addActionListener(new ActionListener() 
                    {
                        public void actionPerformed(ActionEvent arg0) 
                        {
                            ChangeButtonText(currentButton);
                            WinnerCheck();
                        }
                    });
                }
            }
        }
    }