From 41331804c329b5d27aa2b4c6f4e31800b098e1ac Mon Sep 17 00:00:00 2001 From: jwb11006 Date: Sun, 1 May 2016 13:47:47 -0400 Subject: [PATCH] Added Main class. User is now prompted to choose a mode. Interactions are disabled in server mode and when the AI is making a move --- src/controller/CheckersMain.java | 63 ++++++++++++++++++++++++++++++++ src/test/GameTest.java | 3 +- src/view/CheckersWindow.java | 12 ++++-- src/view/GameEventListener.java | 29 ++++++++------- src/view/GamePanel.java | 24 ++++++++++-- 5 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 src/controller/CheckersMain.java diff --git a/src/controller/CheckersMain.java b/src/controller/CheckersMain.java new file mode 100644 index 0000000..461139d --- /dev/null +++ b/src/controller/CheckersMain.java @@ -0,0 +1,63 @@ +package controller; + +import javax.swing.JOptionPane; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; +import javax.swing.UnsupportedLookAndFeelException; + +import model.Board; +import view.CheckersWindow; + +public class CheckersMain { + + public static void main(String[] args) { + + /* Prompt the user to choose a mode */ + Object[] options = {"Human vs Computer", "Computer vs Server"}; + final int mode = JOptionPane.showOptionDialog(null, + "Please choose a game mode", + "Checkers Game Mode", + JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, + null, + options, + options[0]); + + // Need to set the look and feel for this to be cross platform + try { + // Set cross-platform Java L&F (also called "Metal") + UIManager.setLookAndFeel( + UIManager.getCrossPlatformLookAndFeelClassName()); + } + catch (UnsupportedLookAndFeelException e) { + // handle exception + } + catch (ClassNotFoundException e) { + // handle exception + } + catch (InstantiationException e) { + // handle exception + } + catch (IllegalAccessException e) { + // handle exception + } + + Board board = new Board(); + + final Game game = new Game(board); + + if (mode == GameConstants.SERVER_MODE) { + /* Create a ServerListener to listen for messages from the server */ + } + + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + CheckersWindow window = new CheckersWindow(game, mode); + window.open(); + } + }); + + } + +} diff --git a/src/test/GameTest.java b/src/test/GameTest.java index 1094f59..263c0f8 100644 --- a/src/test/GameTest.java +++ b/src/test/GameTest.java @@ -5,6 +5,7 @@ import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import controller.Game; +import controller.GameConstants; import model.Board; import view.CheckersWindow; @@ -37,7 +38,7 @@ public class GameTest { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { - CheckersWindow window = new CheckersWindow(game); + CheckersWindow window = new CheckersWindow(game, GameConstants.USER_MODE); window.open(); } }); diff --git a/src/view/CheckersWindow.java b/src/view/CheckersWindow.java index d1b065c..d808bb2 100644 --- a/src/view/CheckersWindow.java +++ b/src/view/CheckersWindow.java @@ -8,6 +8,7 @@ import javax.swing.JMenuBar; import javax.swing.JMenuItem; import controller.Game; +import controller.GameConstants; /** * Represents the JFrame window that will hold the graphical components @@ -34,7 +35,7 @@ public class CheckersWindow extends JFrame { private GameEventListener gameListener; - public CheckersWindow(Game game) { + public CheckersWindow(Game game, int mode) { super("Checkers"); this.setSize(WIDTH, HEIGHT); this.setDefaultCloseOperation(EXIT_ON_CLOSE); @@ -44,7 +45,7 @@ public class CheckersWindow extends JFrame { /* MUST be done in this order */ this.initGameListener(); this.createMenuBar(); - this.initGamePanel(game); + this.initGamePanel(game, mode); this.pack(); } @@ -99,8 +100,13 @@ public class CheckersWindow extends JFrame { /** * Initializes the {@link GamePanel} instance */ - private void initGamePanel(Game game) { + private void initGamePanel(Game game, int mode) { this.gamePanel = new GamePanel(game, gameListener); + if (mode == GameConstants.SERVER_MODE) { + gamePanel.disableInteraction(); + } else { + gamePanel.enableInteraction(); + } game.setGamePanel(this.gamePanel); this.getContentPane().add(this.gamePanel); this.gameListener.setGamePanel(gamePanel); diff --git a/src/view/GameEventListener.java b/src/view/GameEventListener.java index 3db9013..c88753d 100644 --- a/src/view/GameEventListener.java +++ b/src/view/GameEventListener.java @@ -35,21 +35,22 @@ public class GameEventListener implements MouseListener, KeyListener, ActionList @Override public void mousePressed(MouseEvent e) { Square square = (Square) e.getComponent(); - if (square.hasPiece() && !gamePanel.isInJumpSequence() - && square.getPieceColor() == GameConstants.USER_COLOR - && (!gamePanel.isForceJump() || (square.hasPiece() - && square.isValid()))) { - gamePanel.dehighlightValidDestinations(); - gamePanel.setMoveSource(square); - if (square.isSelected()) - gamePanel.highlightValidDestinations(square.getCellLocation()); - gamePanel.updateMoveMessage(); - } else if (!square.hasPiece() && square.isValid()) { - gamePanel.setMoveDestination(square); - gamePanel.moveSelectedPiece(); - gamePanel.updateMoveMessage(); + if (gamePanel.isInteractionEnabled()) { + if (square.hasPiece() && !gamePanel.isInJumpSequence() + && square.getPieceColor() == GameConstants.USER_COLOR + && (!gamePanel.isForceJump() || (square.hasPiece() + && square.isValid()))) { + gamePanel.dehighlightValidDestinations(); + gamePanel.setMoveSource(square); + if (square.isSelected()) + gamePanel.highlightValidDestinations(square.getCellLocation()); + gamePanel.updateMoveMessage(); + } else if (!square.hasPiece() && square.isValid()) { + gamePanel.setMoveDestination(square); + gamePanel.moveSelectedPiece(); + gamePanel.updateMoveMessage(); + } } - } @Override diff --git a/src/view/GamePanel.java b/src/view/GamePanel.java index ca32ba8..37f8d18 100644 --- a/src/view/GamePanel.java +++ b/src/view/GamePanel.java @@ -31,6 +31,8 @@ public class GamePanel extends JPanel { private Square moveDestination; private Square moveSource; + + private boolean interactionEnabled; public GamePanel(Game game, GameEventListener gameListener) { @@ -69,9 +71,6 @@ public class GamePanel extends JPanel { } public void updateMoveMessage() { -// if (moveSource == null && moveDestination == null) { -// displayMessage("---"); -// } else if (moveSource == null) { displayMessage("Select a piece to move."); } else if (moveDestination == null) { @@ -151,8 +150,14 @@ public class GamePanel extends JPanel { new Thread(new Runnable() { @Override public void run() { + + /* Disable all user interaction before moving */ + disableInteraction(); + /* Request the move */ game.requestMove(move); + + enableInteraction(); /* If the user just jumped and the game is still in a jump sequence */ /* Select the same piece again */ @@ -211,4 +216,17 @@ public class GamePanel extends JPanel { public boolean outOfMoves() { return game.getAllAvailableMoves(GameConstants.USER_COLOR).isEmpty(); } + + public void disableInteraction() { + this.interactionEnabled = false; + + } + + public void enableInteraction() { + this.interactionEnabled = true; + } + + public boolean isInteractionEnabled() { + return interactionEnabled; + } }