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; package inf101.v20.lab4.mnkgames;
import java.util.function.Supplier;
public class GUIMain { public class GUIMain {
public static void main(String[] args) { public static void main(String[] args) {
MNKGameGUI.run(() -> new ConnectFour()); MNKGameGUI.run(new GameSupplier());
// MNKGameGUI.run(() -> new TicTacToe()); // 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 { ...@@ -82,15 +82,15 @@ public abstract class MNKGame {
if(current==Piece.NONE) if(current==Piece.NONE)
return Piece.NONE; return Piece.NONE;
//System.out.println(); System.out.println();
//System.out.println("Currently at " + x + "," + y + " value " + current); System.out.println("Currently at " + x + "," + y + " value " + current);
//System.out.println(); System.out.println();
//horizontal //horizontal
for (int i = x-k; i<x+k; i++) { for (int i = x-k; i<x+k; i++) {
if(i <0 || i>=getWidth()) if(i <0 || i>=getWidth())
continue; 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) { if(grid.get(i, y) != current) {
count = 0; count = 0;
} }
...@@ -106,7 +106,7 @@ public abstract class MNKGame { ...@@ -106,7 +106,7 @@ public abstract class MNKGame {
for (int j = y-k; j<y+k; j++) { for (int j = y-k; j<y+k; j++) {
if(j <0 || j>=getHeight()) if(j <0 || j>=getHeight())
continue; 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) { if(grid.get(x, j) != current) {
count = 0; count = 0;
} }
...@@ -122,7 +122,7 @@ public abstract class MNKGame { ...@@ -122,7 +122,7 @@ public abstract class MNKGame {
for (int i = x-k, j=y-k; i<x+k && j<y+k; i++, j++) { 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()) if(i <0 || j <0 || i>=getWidth() || j>=getHeight())
continue; 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) { if(grid.get(i, j) != current) {
count = 0; count = 0;
...@@ -139,7 +139,7 @@ public abstract class MNKGame { ...@@ -139,7 +139,7 @@ public abstract class MNKGame {
for (int i = x+k, j=y-k; i>x-k && j<y+k; i--, j++) { 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()) if(i <0 || j <0 || i>=getWidth() || j>=getHeight())
continue; 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) { if(grid.get(i, j) != current) {
count = 0; count = 0;
...@@ -166,7 +166,7 @@ public abstract class MNKGame { ...@@ -166,7 +166,7 @@ public abstract class MNKGame {
} }
private String debugToString(int x, int y) { private String debugToString(int x, int y) {
//System.out.println("x="+x+", y="+y); System.out.println("x="+x+", y="+y);
String r = ""; String r = "";
for (int j = 0; j < height; j++) { for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) { for (int i = 0; i < width; i++) {
......
...@@ -30,12 +30,10 @@ public class MNKGameGUI extends JPanel implements ActionListener{ ...@@ -30,12 +30,10 @@ public class MNKGameGUI extends JPanel implements ActionListener{
/** /**
* *
* Initializes a JFrame in which we place the a CellAutomataGUI containing * Initializes a JFrame in which we place the game
* the given ILabyrinth. * @param gameSupplier A lambda function that provides new games
*
* @param ca
*/ */
public static void run(Supplier<MNKGame> gameSupplier) { public static void run(GameSupplier gameSupplier) {
JFrame f = new JFrame("mnkGame"); JFrame f = new JFrame("mnkGame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MNKGameGUI ap = new MNKGameGUI(gameSupplier); MNKGameGUI ap = new MNKGameGUI(gameSupplier);
...@@ -46,15 +44,17 @@ public class MNKGameGUI extends JPanel implements ActionListener{ ...@@ -46,15 +44,17 @@ public class MNKGameGUI extends JPanel implements ActionListener{
} }
MNKGame game; MNKGame game;
private JButton setBoardButton; private JButton resetBoardButton;
private JLabel message; private JButton playConnectFourButton;
private Grid<GamePanel> grid; private JButton playTicTacToeButton;
private Supplier<MNKGame> gameSupplier; private JLabel statusMessage;
private Grid<GamePanel> clickablePanels;
public MNKGameGUI(Supplier<MNKGame> gameSupplier) { private GameSupplier gameSupplier;
public MNKGameGUI(GameSupplier gameSupplier) {
this.gameSupplier = gameSupplier; this.gameSupplier = gameSupplier;
this.game = gameSupplier.get(); this.game = gameSupplier.getTicTacToeGame();
this.grid = new Grid<GamePanel>(game.getWidth(), game.getHeight(), null);
} }
/* /*
...@@ -68,21 +68,27 @@ public class MNKGameGUI extends JPanel implements ActionListener{ ...@@ -68,21 +68,27 @@ public class MNKGameGUI extends JPanel implements ActionListener{
*/ */
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getSource() == setBoardButton) { // if (e.getSource() == resetBoardButton) {
reset(); // reset();
} // }
if(e.getSource() == playConnectFourButton) {
reset(gameSupplier.getConnectFourGame());
}
if(e.getSource() == playTicTacToeButton) {
reset(gameSupplier.getTicTacToeGame());
}
} }
private void reset() { private void reset(MNKGame game) {
game = gameSupplier.get(); this.game = game;
updateMessage(); updateMessage();
this.updateUI();
removeAll(); removeAll();
initialize(); initialize();
this.updateUI();
} }
private void updateBoardUI() { private void updateBoardUI() {
grid.forEach(item -> item.updateToGame()); clickablePanels.forEach(item -> item.updateToGame());
MNKGameGUI.super.updateUI(); MNKGameGUI.super.updateUI();
if(game.hasWinner()) { if(game.hasWinner()) {
System.out.println("Winner!"); System.out.println("Winner!");
...@@ -93,7 +99,7 @@ public class MNKGameGUI extends JPanel implements ActionListener{ ...@@ -93,7 +99,7 @@ public class MNKGameGUI extends JPanel implements ActionListener{
private void stop() { private void stop() {
updateMessage(); 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 //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{ ...@@ -174,7 +180,7 @@ public class MNKGameGUI extends JPanel implements ActionListener{
JPanel p = new JPanel(); JPanel p = new JPanel();
p.setLayout(new GridLayout(game.getHeight(), game.getWidth())); p.setLayout(new GridLayout(game.getHeight(), game.getWidth()));
p.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50)); 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 y = 0; y<game.getHeight(); y++) {
for (int x=0; x<game.getWidth(); x++) { for (int x=0; x<game.getWidth(); x++) {
GamePanel pan = new GamePanel(x, y); GamePanel pan = new GamePanel(x, y);
...@@ -183,35 +189,49 @@ public class MNKGameGUI extends JPanel implements ActionListener{ ...@@ -183,35 +189,49 @@ public class MNKGameGUI extends JPanel implements ActionListener{
pan.setBorder(BorderFactory.createLineBorder(Color.GRAY)); pan.setBorder(BorderFactory.createLineBorder(Color.GRAY));
pan.addMouseListener(new BoxListener()); // add a mouse listener to make the panels clickable pan.addMouseListener(new BoxListener()); // add a mouse listener to make the panels clickable
p.add(pan); p.add(pan);
grid.set(x, y, pan); clickablePanels.set(x, y, pan);
} }
} }
p.setRequestFocusEnabled(true); p.setRequestFocusEnabled(true);
p.requestFocus(); p.requestFocus();
setBoardButton = new JButton(); resetBoardButton = new JButton();
setBoardButton.addActionListener(this); resetBoardButton.addActionListener(this);
setBoardButton.setText("Reset Board"); resetBoardButton.setText("Reset Board");
message = new JLabel(); playTicTacToeButton = new JButton();
message.setText("Welcome to this game!"); 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(leftButton);
// p.add(rightButton); // p.add(rightButton);
// p.add(upButton); // p.add(upButton);
// p.add(downButton); // p.add(downButton);
add(p); add(p);
// add(gameComponent, BorderLayout.CENTER); // add(gameComponent, BorderLayout.CENTER);
add(setBoardButton, BorderLayout.SOUTH); add(buttons, BorderLayout.SOUTH);
add(message, BorderLayout.NORTH); add(statusMessage, BorderLayout.NORTH);
validate(); validate();
} }
private void updateMessage() { private void updateMessage() {
if (game.hasWinner()) 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 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