ConnectFour Win条件

时间:2015-09-27 22:55:43

标签: algorithm jframe

我有一个ConnectFour程序,它使用一个非常大的if语句来检查是否有赢家,但它似乎只适用于中间的tile(JButtons)和我的代码。边缘,而不是......如果你能看到我做错了什么,这样如果状态会起作用,那就太好了!

源代码:

import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.Color;    
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ConnectFour extends JPanel
{
    private int turn;
    private final JButton BOARD[][];
    private final Color DEFAULT_COLOR;

    public ConnectFour()
    {
        super(new GridLayout(6, 7));
        turn = 0;

        BOARD = new JButton[6][7];
        HandlerClass handler = new HandlerClass();
        for(int i = 0; i < BOARD.length; i++)
        {
            for(int j = 0; j < BOARD[i].length; j++)
            {
                BOARD[i][j] = new JButton("");
                BOARD[i][j].addActionListener(handler);
                if(!(i == BOARD.length - 1))
                {
                    BOARD[i][j].setEnabled(false);
                }
                add(BOARD[i][j]);
            }
        }
        DEFAULT_COLOR = BOARD[0][0].getBackground();
    }

public static void main(String[] args)
{
    javax.swing.JFrame frame = new javax.swing.JFrame("test");
    frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 300);
    frame.setLocationRelativeTo(null);
    frame.add(new ConnectFour());
    frame.setVisible(true);
}

private class HandlerClass implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        JButton source = ((JButton) event.getSource());
        if(turn % 2 == 0)
        {
            source.setBackground(Color.RED);
            source.setEnabled(false);
        }
        else
        {
            source.setBackground(Color.BLACK);
            source.setEnabled(false);
        }

        boolean winner = checkWinner();

        if(winner)
        {
            System.out.println("Player " + (turn % 2 + 1) + " wins!");
        }
        //---------------------------------
        for(int i = 0; i < BOARD.length; i++)
        {
            for(int j = 0; j < BOARD[i].length; j++)
            {
                if(BOARD[i][j].equals(source))
                {
                    try
                    {
                        BOARD[i - 1][j].setEnabled(true);
                        break;
                    }
                    catch(ArrayIndexOutOfBoundsException e)
                    {
                        //System.out.println("Hello, World!");
                    }
                }
            }
        }
        turn++;
    }

    private boolean checkWinner()
    {
        for(int i = 0; i < BOARD.length; i++)
        {
            for(int j = 0; j < BOARD[i].length; j++)
            {
                try
                {
                    if(((BOARD[i][j].getBackground().equals(BOARD[i + 1][j].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i + 2][j].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i + 3][j].getBackground()))
                     && !(BOARD[i][j].getBackground().equals(DEFAULT_COLOR)))
                    || ((BOARD[i][j].getBackground().equals(BOARD[i - 1][j].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i - 2][j].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i - 3][j].getBackground()))
                     && !(BOARD[i][j].getBackground().equals(DEFAULT_COLOR)))
                    || ((BOARD[i][j].getBackground().equals(BOARD[i][j + 1].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i][j + 2].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i][j + 3].getBackground()))
                     && !(BOARD[i][j].getBackground().equals(DEFAULT_COLOR)))
                    || ((BOARD[i][j].getBackground().equals(BOARD[i][j - 1].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i][j - 2].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i][j - 3].getBackground()))
                     && !(BOARD[i][j].getBackground().equals(DEFAULT_COLOR)))
                    || ((BOARD[i][j].getBackground().equals(BOARD[i + 1][j + 1].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i + 2][j + 2].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i + 3][j + 3].getBackground()))
                     && !(BOARD[i][j].getBackground().equals(DEFAULT_COLOR)))
                    || ((BOARD[i][j].getBackground().equals(BOARD[i + 1][j - 1].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i + 2][j - 2].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i + 3][j - 3].getBackground()))
                     && !(BOARD[i][j].getBackground().equals(DEFAULT_COLOR)))
                    || ((BOARD[i][j].getBackground().equals(BOARD[i - 1][j + 1].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i - 2][j + 2].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i - 3][j + 3].getBackground()))
                     && !(BOARD[i][j].getBackground().equals(DEFAULT_COLOR)))
                    || ((BOARD[i][j].getBackground().equals(BOARD[i - 1][j - 1].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i - 2][j - 2].getBackground()))
                     && (BOARD[i][j].getBackground().equals(BOARD[i - 3][j - 3].getBackground()))
                     && !(BOARD[i][j].getBackground().equals(DEFAULT_COLOR))))
                        return true;
                }
                catch(ArrayIndexOutOfBoundsException e)
                {

                }
            }
        }
        return false;
    }
}

}

1 个答案:

答案 0 :(得分:0)

您遇到此错误的原因是因为您的代码首先难以阅读。你应该能够在没有我们帮助的情况下解决这个问题。凌乱的代码意味着错误将会蔓延到你身上。

一些意见:

1)将java添加到此帖子所属的语言

2)而不是“i”和“j”考虑使用“height”和“width”(或“h”和“w”),以便作为代码的读者我可以告诉这些变量指的是哪个轴。

3)将每个(if)案例分成它自己的方法。您似乎知道如何使用方法,以便利用它们!使您的代码可读,您将很快解决这个问题。

例如:

            if(((BOARD[i][j].getBackground().equals(BOARD[i + 1][j].getBackground()))
             && (BOARD[i][j].getBackground().equals(BOARD[i + 2][j].getBackground()))
             && (BOARD[i][j].getBackground().equals(BOARD[i + 3][j].getBackground()))

我相信正在检查相似颜色的“垂直”或“水平”线(我无法分辨,因为我不知道我和j是什么)。那么为什么不将它包装在一个名为“checkVertical”的方法中,该方法返回false。

4)了解如何使用调试器。它们非常易于使用,您可能会立即解决您的问题。