Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
john committed May 1, 2016
1 parent b3a2c68 commit 4133180
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 21 deletions.
63 changes: 63 additions & 0 deletions 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();
}
});

}

}
3 changes: 2 additions & 1 deletion src/test/GameTest.java
Expand Up @@ -5,6 +5,7 @@ import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import controller.Game;
import controller.GameConstants;
import model.Board;
import view.CheckersWindow;

Expand Down Expand Up @@ -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();
}
});
Expand Down
12 changes: 9 additions & 3 deletions src/view/CheckersWindow.java
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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();
}

Expand Down Expand Up @@ -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);
Expand Down
29 changes: 15 additions & 14 deletions src/view/GameEventListener.java
Expand Up @@ -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
Expand Down
24 changes: 21 additions & 3 deletions src/view/GamePanel.java
Expand Up @@ -31,6 +31,8 @@ public class GamePanel extends JPanel {

private Square moveDestination;
private Square moveSource;

private boolean interactionEnabled;


public GamePanel(Game game, GameEventListener gameListener) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 4133180

Please sign in to comment.