Commit bb062a60 authored by Anna Maria Eilertsen's avatar Anna Maria Eilertsen
Browse files

added buttons for switching games. Includes debug statements

parent 7411f39e
package inf101.v20.lab4.mnkgames;
import java.util.function.Supplier;
public class GUIMain {
public static void main(String[] args) {
MNKGameGUI.run(() -> new ConnectFour());
MNKGameGUI.run(new GameSupplier());
// MNKGameGUI.run(() -> new TicTacToe());
}
}
class GameSupplier implements Supplier<MNKGame>{
@Override
public MNKGame get() {
return null;
}
public MNKGame getConnectFourGame() {
return new ConnectFour();
}
public MNKGame getTicTacToeGame() {
return new TicTacToe();
}
}
......@@ -82,15 +82,15 @@ public abstract class MNKGame {
if(current==Piece.NONE)
return Piece.NONE;
//System.out.println();
//System.out.println("Currently at " + x + "," + y + " value " + current);
//System.out.println();
System.out.println();
System.out.println("Currently at " + x + "," + y + " value " + current);
System.out.println();
//horizontal
for (int i = x-k; i<x+k; i++) {
if(i <0 || i>=getWidth())
continue;
//System.out.println("Horizontal Looking at \n" + debugToString(i, y) + ": count is " + count);
System.out.println("Horizontal Looking at \n" + debugToString(i, y) + ": count is " + count);
if(grid.get(i, y) != current) {
count = 0;
}
......@@ -106,7 +106,7 @@ public abstract class MNKGame {
for (int j = y-k; j<y+k; j++) {
if(j <0 || j>=getHeight())
continue;
//System.out.println("Vertical Looking at \n" + debugToString(x, j) + ": count is " + count);
System.out.println("Vertical Looking at \n" + debugToString(x, j) + ": count is " + count);
if(grid.get(x, j) != current) {
count = 0;
}
......@@ -122,7 +122,7 @@ public abstract class MNKGame {
for (int i = x-k, j=y-k; i<x+k && j<y+k; i++, j++) {
if(i <0 || j <0 || i>=getWidth() || j>=getHeight())
continue;
//System.out.println("Diagonal TL-BR Looking at \n" + debugToString(i, j) + ": count is " + count);
System.out.println("Diagonal TL-BR Looking at \n" + debugToString(i, j) + ": count is " + count);
if(grid.get(i, j) != current) {
count = 0;
......@@ -139,7 +139,7 @@ public abstract class MNKGame {
for (int i = x+k, j=y-k; i>x-k && j<y+k; i--, j++) {
if(i <0 || j <0 || i>=getWidth() || j>=getHeight())
continue;
//System.out.println("Diagonal TR-BL Looking at \n" + debugToString(i, j) + ": count is " + count);
System.out.println("Diagonal TR-BL Looking at \n" + debugToString(i, j) + ": count is " + count);
if(grid.get(i, j) != current) {
count = 0;
......@@ -166,7 +166,7 @@ public abstract class MNKGame {
}
private String debugToString(int x, int y) {
//System.out.println("x="+x+", y="+y);
System.out.println("x="+x+", y="+y);
String r = "";
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
......
......@@ -30,12 +30,10 @@ public class MNKGameGUI extends JPanel implements ActionListener{
/**
*
* Initializes a JFrame in which we place the a CellAutomataGUI containing
* the given ILabyrinth.
*
* @param ca
* Initializes a JFrame in which we place the game
* @param gameSupplier A lambda function that provides new games
*/
public static void run(Supplier<MNKGame> gameSupplier) {
public static void run(GameSupplier gameSupplier) {
JFrame f = new JFrame("mnkGame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MNKGameGUI ap = new MNKGameGUI(gameSupplier);
......@@ -46,15 +44,17 @@ public class MNKGameGUI extends JPanel implements ActionListener{
}
MNKGame game;
private JButton setBoardButton;
private JLabel message;
private Grid<GamePanel> grid;
private Supplier<MNKGame> gameSupplier;
public MNKGameGUI(Supplier<MNKGame> gameSupplier) {
private JButton resetBoardButton;
private JButton playConnectFourButton;
private JButton playTicTacToeButton;
private JLabel statusMessage;
private Grid<GamePanel> clickablePanels;
private GameSupplier gameSupplier;
public MNKGameGUI(GameSupplier gameSupplier) {
this.gameSupplier = gameSupplier;
this.game = gameSupplier.get();
this.grid = new Grid<GamePanel>(game.getWidth(), game.getHeight(), null);
this.game = gameSupplier.getTicTacToeGame();
}
/*
......@@ -68,21 +68,27 @@ public class MNKGameGUI extends JPanel implements ActionListener{
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == setBoardButton) {
reset();
}
// if (e.getSource() == resetBoardButton) {
// reset();
// }
if(e.getSource() == playConnectFourButton) {
reset(gameSupplier.getConnectFourGame());
}
if(e.getSource() == playTicTacToeButton) {
reset(gameSupplier.getTicTacToeGame());
}
}
private void reset() {
game = gameSupplier.get();
private void reset(MNKGame game) {
this.game = game;
updateMessage();
this.updateUI();
removeAll();
initialize();
this.updateUI();
}
private void updateBoardUI() {
grid.forEach(item -> item.updateToGame());
clickablePanels.forEach(item -> item.updateToGame());
MNKGameGUI.super.updateUI();
if(game.hasWinner()) {
System.out.println("Winner!");
......@@ -93,7 +99,7 @@ public class MNKGameGUI extends JPanel implements ActionListener{
private void stop() {
updateMessage();
grid.forEach(item -> item.removeMouseListeners());
clickablePanels.forEach(item -> item.removeMouseListeners());
}
//Class that defines what happens (i.e: the color changes) when a panel is clicked
......@@ -174,7 +180,7 @@ public class MNKGameGUI extends JPanel implements ActionListener{
JPanel p = new JPanel();
p.setLayout(new GridLayout(game.getHeight(), game.getWidth()));
p.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
this.clickablePanels = new Grid<GamePanel>(game.getWidth(), game.getHeight(), null);
for(int y = 0; y<game.getHeight(); y++) {
for (int x=0; x<game.getWidth(); x++) {
GamePanel pan = new GamePanel(x, y);
......@@ -183,35 +189,49 @@ public class MNKGameGUI extends JPanel implements ActionListener{
pan.setBorder(BorderFactory.createLineBorder(Color.GRAY));
pan.addMouseListener(new BoxListener()); // add a mouse listener to make the panels clickable
p.add(pan);
grid.set(x, y, pan);
clickablePanels.set(x, y, pan);
}
}
p.setRequestFocusEnabled(true);
p.requestFocus();
setBoardButton = new JButton();
setBoardButton.addActionListener(this);
setBoardButton.setText("Reset Board");
message = new JLabel();
message.setText("Welcome to this game!");
resetBoardButton = new JButton();
resetBoardButton.addActionListener(this);
resetBoardButton.setText("Reset Board");
playTicTacToeButton = new JButton();
playTicTacToeButton.addActionListener(this);
playTicTacToeButton.setText("Tic-Tac-Toe");
playConnectFourButton = new JButton();
playConnectFourButton.addActionListener(this);
playConnectFourButton.setText("Connect Four");
statusMessage = new JLabel();
statusMessage.setText("Welcome to this game!");
JPanel buttons = new JPanel();
buttons.setLayout(new BorderLayout());
buttons.add(resetBoardButton, BorderLayout.CENTER);
buttons.add(playConnectFourButton, BorderLayout.WEST);
buttons.add(playTicTacToeButton, BorderLayout.EAST);
// p.add(leftButton);
// p.add(rightButton);
// p.add(upButton);
// p.add(downButton);
add(p);
// add(gameComponent, BorderLayout.CENTER);
add(setBoardButton, BorderLayout.SOUTH);
add(message, BorderLayout.NORTH);
add(buttons, BorderLayout.SOUTH);
add(statusMessage, BorderLayout.NORTH);
validate();
}
private void updateMessage() {
if (game.hasWinner())
message.setText("The game is over!");// The winner is " + game.getWinner().name().toLowerCase());
statusMessage.setText("The game is over!");// The winner is " + game.getWinner().name().toLowerCase());
else
message.setText("Playing");
statusMessage.setText("Playing");
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment