import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class ConnectFour
{

    // the grid used for storing the game layout.
    private int[] [] grid;
    // the player whose turn it is.
    private int currentPlayer;

    public ConnectFour ()
    {
        // create the grid
        grid = new int [7] [6];
        // initialize the grid
        for (int row = 0 ; row < 6 ; row++)
        {
            for (int column = 0 ; column < 7 ; column++)
            {
                // set the position to a default value
                grid [column] [row] = 0;
            }
        }
        // set the first move to Player 1
        currentPlayer = 1;
    }


    int drop (int column)
    {
        if (hasWon ())
        {
            return -1;
        }

        int row = 0;
        for (; row < 6 && grid [column] [row] != 0 ; row++)
        {
        }
        ;
        if (row == 6)
        {

        }
        // fill the row of that column with a checker.
        grid [column] [row] = currentPlayer;
        // alternate the players
        currentPlayer = (currentPlayer % 2) + 1;
        return row;
    }


    public String toString ()
    {
        String returnString = "";
        for (int row = 5 ; row >= 0 ; row--)
        {
            for (int column = 0 ; column < 7 ; column++)
            {
                returnString = returnString + grid [column] [row];
            }
            returnString = returnString + "\n";
        }
        return returnString;
    }


    public boolean hasWon ()
    {
        boolean status = false;

        // check for a horizontal win
        for (int row = 0 ; row < 6 ; row++)
        {
            for (int column = 0 ; column < 4 ; column++)
            {
                if (grid [column] [row] != 0 &&
                        grid [column] [row] == grid [column + 1] [row] && grid [column] [row] == grid [column + 2] [row] && grid [column] [row] == grid [column + 3] [row])
                {
                    status = true;
                }
            }
        }

        // check for a vertical win
        for (int row = 0 ; row < 3 ; row++)
        {
            for (int column = 0 ; column < 7 ; column++)
            {
                if (grid [column] [row] != 0 &&
                        grid [column] [row] == grid [column] [row + 1] && grid [column] [row] == grid [column] [row + 2] && grid [column] [row] == grid [column] [row + 3])
                {
                    status = true;
                }
            }
        }

        // check for a diagonal win (positive slope)
        for (int row = 0 ; row < 3 ; row++)
        {
            for (int column = 0 ; column < 4 ; column++)
            {
                if (grid [column] [row] != 0 &&  grid [column] [row] == grid [column + 1] [row + 1] &&  grid [column] [row] == grid [column + 2] [row + 2] &&  grid [column] [row] == grid [column + 3] [row + 3])
                {
                    status = true;
                }
            }
        }

        // check for a diagonal win (negative slope)
        for (int row = 3 ; row < 6 ; row++)
        {
            for (int column = 0 ; column < 4 ; column++)
            {
                if (grid [column] [row] != 0 && grid [column] [row] == grid [column + 1] [row - 1] &&grid [column] [row] == grid [column + 2] [row - 2] &&grid [column] [row] == grid [column + 3] [row - 3])
                {
                    status = true;
                }
            }
        }

        return status;
    }
}
public class ConnectFourGUI
{

    private JFrame frame;
    private JLabel[] [] slots;
    private int currentPlayer;
    public ConnectFourGUI ()
    {

        frame = new JFrame ("Connect Four");
        JPanel panel = (JPanel) frame.getContentPane ();
        panel.setLayout (new GridLayout (6, 7));
        slots = new JLabel [7] [6];
        for (int row = 5 ; row >= 0 ; row--)
        {
            for (int column = 0 ; column < 7 ; column++)
            {
                slots [column] [row] = new JLabel ();
                // slots[column][row].setFont(new Font("SansSerif", Font.BOLD, 18));

                slots [column] [row].setHorizontalAlignment (SwingConstants.CENTER);
                slots [column] [row].setBorder (new LineBorder (Color.green));
                panel.add (slots [column] [row]);
            }
        }
        frame.setContentPane (panel);
        frame.setSize (700, 600);
        frame.setVisible (true);

        currentPlayer = 1;
    }


    public void addListener (Connect2listener)
    {
        for (int row = 0 ; row < 6 ; row++)
        {
            for (int column = 0 ; column < 7 ; column++)
            {
                slots [column] [row].addMouseListener (listener);
            }
        }
    }


    public int getColumn (JLabel label)
    {
        int returnColumn = -1;
        for (int row = 0 ; row < 6 ; row++)
        {
            for (int column = 0 ; column < 7 ; column++)
            {
                if (slots [column] [row] == label)
                {
                    returnColumn = column;
                }
            }
        }
        return returnColumn;
    }


    public void set (int column, int row)
    {
        // slots[column][row].setText("*" + currentPlayer + "*");
        if (currentPlayer == 1)
        {
            slots [column] [row].setIcon (new ImageIcon ("C:\\Documents and Settings \\ All Users \\ Documents \\ My Pictures \\ Sample  Pictures \\ Sunset.jpg "));
        }
        else
        {
            slots [column] [row].setIcon (new ImageIcon ("C:\\Documents and Settings \\ All Users \\ Documents \\ My Pictures \\ SamplePictures \\ Winter.jpg "));
        }
        currentPlayer = (currentPlayer % 2) + 1;
    }


    public static void main (String[] args)
    {
        ConnectFour game = new ConnectFour ();
        ConnectFourGUI gui = new ConnectFourGUI ();
        Connect2listener = new Connect2(game, gui);
    }
}

Recommended Answers

All 3 Replies

What is the error message(s) that you're getting?

public void addListener (Connect2listener)

It has no identifier

Connect2listener is a variable name.
You have to write the variable type.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.