4
\$\begingroup\$

I am writing a fully functional single player Connect 4 game. I am working on the second player and the while loop that tells the player who won.

GUI

package lab7; 



import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GUIClass implements Runnable {

    @Override
    public void run() {

        JFrame f = new JFrame("CSE 115 Connect 4");
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridLayout myGrid = new GridLayout(7, 6);
        JPanel panel;
        panel = new JPanel();
        panel.setLayout(myGrid);
        f.setLayout(myGrid);

        f.pack();

        ArrayList<JButton> col_button = new ArrayList<JButton>();
        for (Integer i = 1; i <= 7; i++)
        {
            JButton b = new JButton("Col " + i.toString());
            col_button.add(b);
            f.add(b);
        }

        ArrayList<ArrayList<JLabel>> col_list = new ArrayList<ArrayList<JLabel>>();
        for (int i = 0; i < 7; i++)
        {
            col_list.add(new ArrayList<JLabel>());
        }

        for (int i = 0; i < 42; i++)
        {


            JLabel label = new JLabel(new ImageIcon("images/blank.png"));
            //System.out.println(label.getIcon().toString())
            col_list.get(i % 7).add(label);
            f.add(label);
        }
        GameClass gc = new GameClass(col_list);

        for (int i = 0; i < 7; i++)
        {
            col_button.get(i).addActionListener(new ColButton(col_list.get(i)));
        }

        f.pack();

    }

    ImageIcon whiteCircle = new ImageIcon("blank.png");
}

that contains all of the graphical interface behind my code, as you might have guessed I used Java and Eclipse.

This is my game class where my second player is going to be, and I also assume my winning loop will be here.

import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JLabel;

public class GameClass{

    public GameClass(ArrayList<ArrayList<JLabel>> col_list)
    {

    }
}

finally this is my column class which contains the 1st player and all the buttons.

package lab7;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class ColButton implements ActionListener {

    private ArrayList<JLabel> _column;
    private GameClass _game;

    public ColButton(ArrayList<JLabel> c)
    {
        _column = c;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        for (int i = _column.size() - 1; i >= 0; i--)
        {
            JLabel l = _column.get(i);
            if(l.getIcon().toString().equals("images/blank.png"))
            {
                l.setIcon(new ImageIcon("images/red.png"));
                break;
            }
        }
    }

}

What do you think? Any suggestions on how to get get going for the second part? I am working on the 2nd player but I have run into some setbacks.

\$\endgroup\$

migrated from programmers.stackexchange.com May 4 '14 at 1:28

This question came from our site for professionals, academics, and students working within the systems development life cycle.

  • \$\begingroup\$ Great article on game loop implementation here: gafferongames.com/game-physics/fix-your-timestep Also, your question is too generic. Try doing an actual game loop implementation, and then ask a more punctual question, if you have issues. \$\endgroup\$ – Shivan Dragon May 3 '14 at 21:40
  • \$\begingroup\$ thanks for the article shivan dragon, I will get with it then. \$\endgroup\$ – Lucian09474 May 3 '14 at 22:03
3
\$\begingroup\$

Typically, a Swing application would be written using a Model-View-Controller pattern to split the responsibilities:

  • The Model would be a data structure to represent the game state. It could, for example, consist of an ConnectFourBoard class that holds a two-dimensional array of chips in the board.
  • The View would be the Swing UI code
  • The Controller acts as the glue between the Model and View. It handles actions triggered by the View, and notifies the View when the Model has changed.

Here, I see View and Controller code, commingled. However, I don't see any Model code. In fact, in ColButton.actionPerformed(), it appears that the lack of a Model is causing you some difficulty: you're resorting to determining the model state by parsing the icon name out of the view.

Without a Model, you are no doubt going to have a difficult time examining the model state to determine the winner.

\$\endgroup\$
  • \$\begingroup\$ I thought of what you said, and I decided to create a Volatile Boolean Loop and so far so good, I have not run into any trouble for now, at least. I'll show you the final product. \$\endgroup\$ – Lucian09474 May 4 '14 at 23:00
  • \$\begingroup\$ Feel free to post a new question with the revised code. \$\endgroup\$ – 200_success May 5 '14 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.