patternjavaMinor
Winning Loop Connect 4
Viewed 0 times
loopwinningconnect
Problem
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
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.
finally this is my column class which contains the 1st player and all the buttons.
```
package lab7;
import java.awt.even
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 col_button = new ArrayList();
for (Integer i = 1; i > col_list = new ArrayList>();
for (int i = 0; i ());
}
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> col_list)
{
}
}finally this is my column class which contains the 1st player and all the buttons.
```
package lab7;
import java.awt.even
Solution
Typically, a Swing application would be written using a Model-View-Controller pattern to split the responsibilities:
Here, I see View and Controller code, commingled. However, I don't see any Model code. In fact, in
Without a Model, you are no doubt going to have a difficult time examining the model state to determine the winner.
- The Model would be a data structure to represent the game state. It could, for example, consist of an
ConnectFourBoardclass 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.
Context
StackExchange Code Review Q#48896, answer score: 3
Revisions (0)
No revisions yet.