From 6907b9402440dec4a4256fa7ff86540e9fd8bc08 Mon Sep 17 00:00:00 2001 From: domenickd3 Date: Fri, 29 Apr 2016 02:14:38 -0400 Subject: [PATCH] Modified updateMoveMessage calls / added getLastMove method (to be used for double jumps) --- src/controller/Game.java | 3 +- src/model/Board.java | 11 +++++-- src/view/CheckersWindow.java | 1 + src/view/GameEventListener.java | 1 + src/view/GamePanel.java | 52 ++++++++++++++++++++++----------- src/view/Square.java | 3 +- 6 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/controller/Game.java b/src/controller/Game.java index afb92ea..61d983a 100755 --- a/src/controller/Game.java +++ b/src/controller/Game.java @@ -1,14 +1,13 @@ package controller; -import java.io.Console; import java.util.ArrayList; -import view.GamePanel; import model.Board; import model.Color; import model.Location; import model.Move; import model.Piece; +import view.GamePanel; public class Game { private Board board; diff --git a/src/model/Board.java b/src/model/Board.java index e8abdc1..da97c3b 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -16,6 +16,7 @@ public class Board { private Piece[][] representation; private Piece lastPieceMoved; + private Move lastMove; // Move properties private int movesSinceCapture; @@ -26,7 +27,8 @@ public Board() { representation = new Piece[BOARD_SIZE][BOARD_SIZE]; movesSinceCapture = 0; init(); - lastPieceMoved = null; + lastPieceMoved = null; + lastMove = null; } /** @@ -43,6 +45,7 @@ public Board(Board other) { } movesSinceCapture = other.getMovesSinceCapture(); lastPieceMoved = other.getLastPieceMoved(); + lastMove = other.getLastMove(); } public boolean isValidSquare(Location location) { @@ -151,6 +154,7 @@ public void move(Move move) { .setLocation(new Location(move.destination.row, move.destination.column)); Piece moved = representation[move.destination.row][move.destination.column]; this.lastPieceMoved = moved; + this.lastMove = move; } /** @@ -169,7 +173,7 @@ public void jump(Move jump) { Piece moved = representation[jump.destination.row][jump.destination.column]; this.lastPieceMoved = moved; - + this.lastMove = jump; this.resetMovesSinceCapture(); } @@ -352,6 +356,9 @@ private void resetMovesSinceCapture() { public Piece getLastPieceMoved() { return this.lastPieceMoved; + } + public Move getLastMove() { + return this.lastMove; } } diff --git a/src/view/CheckersWindow.java b/src/view/CheckersWindow.java index ec64f69..d1b065c 100644 --- a/src/view/CheckersWindow.java +++ b/src/view/CheckersWindow.java @@ -90,6 +90,7 @@ private void createMenuBar() { file.add(quit); file.add(newGame); file.add(instructions); + menubar.add(file); menubar.setVisible(true); this.setJMenuBar(menubar); diff --git a/src/view/GameEventListener.java b/src/view/GameEventListener.java index 056de2c..c56c3c1 100644 --- a/src/view/GameEventListener.java +++ b/src/view/GameEventListener.java @@ -56,6 +56,7 @@ public void keyPressed(KeyEvent arg0) {} public void keyReleased(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_ENTER && gamePanel.moveReady()) { gamePanel.moveSelectedPiece(); + gamePanel.updateMoveMessage(); } } diff --git a/src/view/GamePanel.java b/src/view/GamePanel.java index 6c4c100..fe4c080 100644 --- a/src/view/GamePanel.java +++ b/src/view/GamePanel.java @@ -7,10 +7,10 @@ import javax.swing.JLabel; import javax.swing.JPanel; -import controller.Game; import model.Color; import model.Location; import model.Move; +import controller.Game; /** * Represents the panel which will hold all of the graphical @@ -67,12 +67,15 @@ public void displayMessage(String message) { } public void updateMoveMessage() { - if (moveSource == null) { +// if (moveSource == null && moveDestination == null) { +// displayMessage("---"); +// } else + if (moveSource == null) { displayMessage("Select a piece to move."); } else if (moveDestination == null) { displayMessage("Select a destination."); } else { - displayMessage("Select \"Move\" to move the piece."); + displayMessage("Press enter to confirm move."); } } @@ -118,25 +121,40 @@ public void dehighlightAllSquares() { canvas.invalidateAllSquares(); } + public Location getMoveSource(){ + return moveSource.getCellLocation(); + } + + public Location getMoveDestination(){ + return moveDestination.getCellLocation(); + } + public void moveSelectedPiece() { - Move theMove = new Move(moveSource.getCellLocation(), moveDestination.getCellLocation()); - game.movePiece(theMove); - canvas.moveChecker(moveSource.getCellLocation(), moveDestination.getCellLocation()); + Move move = new Move(getMoveSource(), getMoveDestination()); + game.movePiece(move); + + canvas.moveChecker(getMoveSource(), getMoveDestination()); dehighlightAllSquares(); - moveSource.setSelected(false); - moveDestination.setSelected(false); - moveSource = null; - moveDestination = null; - if(!theMove.isJump()) { - game.switchTurn(); - } else { - Location monkeyLoc = new Location((theMove.destination.row - + theMove.source.row)/2, - (theMove.source.column - + theMove.destination.column) / 2); + + if(move.isJump()) { + Location monkeyLoc = new Location((move.destination.row + + move.source.row)/2, + (move.source.column + + move.destination.column) / 2); System.out.println(monkeyLoc); removePiece(monkeyLoc); + moveSource.setSelected(false); + //moveDestination.setSelected(false); + moveSource = null; + //moveDestination = null; + + } else { + moveSource.setSelected(false); + moveDestination.setSelected(false); + moveSource = null; + moveDestination = null; + game.switchTurn(); } } diff --git a/src/view/Square.java b/src/view/Square.java index 6ef7e93..990e5b4 100644 --- a/src/view/Square.java +++ b/src/view/Square.java @@ -155,8 +155,7 @@ public void highlight() { this.setBorder(BorderFactory.createLineBorder(Color.YELLOW)); } - - + public void dehighlight() { this.setBorder(null);