From c88621fc0d512dbc89593c82d8351f032263db19 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Fri, 29 Apr 2016 01:22:43 -0400 Subject: [PATCH 1/4] fixed the null piece error --- src/view/Square.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/view/Square.java b/src/view/Square.java index 785ca8f..6ef7e93 100644 --- a/src/view/Square.java +++ b/src/view/Square.java @@ -84,8 +84,9 @@ public void setPiece(Checker piece) { } public void removePiece() { - - this.remove(this.piece); + if (null != this.piece) { + this.remove(this.piece); + } } From 362502eca4832041591f79d71e91bb1379d81ae2 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Fri, 29 Apr 2016 01:35:27 -0400 Subject: [PATCH 2/4] Human player is now forced to jump. TODO: fix the issue where the computer cannot go after the player jumps until the player clicks on a piece again... --- src/controller/Game.java | 27 +++++++++++++++++++-------- src/model/Board.java | 6 ++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/controller/Game.java b/src/controller/Game.java index 8e5a462..afb92ea 100755 --- a/src/controller/Game.java +++ b/src/controller/Game.java @@ -21,6 +21,10 @@ public Game(Board board) { current_turn = Color.BLACK; } + public ArrayList getMoveFrontier(Color color) { + return board.generateAllPossibleMoves(color); + } + public void movePiece(Move move) { if (move.isJump()) { board.jump(move); @@ -48,11 +52,18 @@ public ArrayList getAvailableMoves(Location source) { } return jumpset; } - ArrayList moves = board.generateMoves(board.getPiece(source)); - ArrayList jumps = board.generateJumpMoves(board.getPiece(source)); - ArrayList allMoves = new ArrayList(moves); - allMoves.addAll(jumps); - return allMoves; + + Piece movee = board.getPiece(source); + ArrayList moves = this.getMoveFrontier(movee.getColor()); + + ArrayList moves_of_movee = new ArrayList(); + + for (Move move : moves) { + if (move.source.equals(movee.getLocation())) { + moves_of_movee.add(move); + } + } + return moves_of_movee; } public void playVsThunk() { @@ -80,7 +91,7 @@ public void playVsThunk() { panel.moveArbitraryPiece(jump); } else { - ArrayList moveset = board.generateAllPossibleMoves(THUNK_COLOR); + ArrayList moveset = this.getMoveFrontier(THUNK_COLOR); if (moveset.isEmpty()) { System.out.println("Thunk is out of moves."); break; @@ -95,8 +106,8 @@ public void playVsThunk() { } } if (this.current_turn == USER_COLOR) { - //ArrayList moveset = board.generateAllPossibleMoves(USER_COLOR); - //if (moveset.isEmpty()) break; + ArrayList moveset = this.getMoveFrontier(USER_COLOR); + if (moveset.isEmpty()) break; } //board.print(); diff --git a/src/model/Board.java b/src/model/Board.java index fbb5ceb..e8abdc1 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -169,6 +169,8 @@ public void jump(Move jump) { Piece moved = representation[jump.destination.row][jump.destination.column]; this.lastPieceMoved = moved; + + this.resetMovesSinceCapture(); } /** @@ -344,6 +346,10 @@ public int getMovesSinceCapture() { return this.movesSinceCapture; } + private void resetMovesSinceCapture() { + this.movesSinceCapture = 0; + } + public Piece getLastPieceMoved() { return this.lastPieceMoved; } From 8f1f9d005cead44e89e07138e140a69706c8ed0b Mon Sep 17 00:00:00 2001 From: aah13002 Date: Fri, 29 Apr 2016 01:58:01 -0400 Subject: [PATCH 3/4] Revert "fixed the null piece error" This reverts commit c88621fc0d512dbc89593c82d8351f032263db19. --- src/controller/Game.java | 27 ++++++++------------------- src/model/Board.java | 6 ------ src/view/Square.java | 5 ++--- 3 files changed, 10 insertions(+), 28 deletions(-) diff --git a/src/controller/Game.java b/src/controller/Game.java index afb92ea..8e5a462 100755 --- a/src/controller/Game.java +++ b/src/controller/Game.java @@ -21,10 +21,6 @@ public Game(Board board) { current_turn = Color.BLACK; } - public ArrayList getMoveFrontier(Color color) { - return board.generateAllPossibleMoves(color); - } - public void movePiece(Move move) { if (move.isJump()) { board.jump(move); @@ -52,18 +48,11 @@ public ArrayList getAvailableMoves(Location source) { } return jumpset; } - - Piece movee = board.getPiece(source); - ArrayList moves = this.getMoveFrontier(movee.getColor()); - - ArrayList moves_of_movee = new ArrayList(); - - for (Move move : moves) { - if (move.source.equals(movee.getLocation())) { - moves_of_movee.add(move); - } - } - return moves_of_movee; + ArrayList moves = board.generateMoves(board.getPiece(source)); + ArrayList jumps = board.generateJumpMoves(board.getPiece(source)); + ArrayList allMoves = new ArrayList(moves); + allMoves.addAll(jumps); + return allMoves; } public void playVsThunk() { @@ -91,7 +80,7 @@ public void playVsThunk() { panel.moveArbitraryPiece(jump); } else { - ArrayList moveset = this.getMoveFrontier(THUNK_COLOR); + ArrayList moveset = board.generateAllPossibleMoves(THUNK_COLOR); if (moveset.isEmpty()) { System.out.println("Thunk is out of moves."); break; @@ -106,8 +95,8 @@ public void playVsThunk() { } } if (this.current_turn == USER_COLOR) { - ArrayList moveset = this.getMoveFrontier(USER_COLOR); - if (moveset.isEmpty()) break; + //ArrayList moveset = board.generateAllPossibleMoves(USER_COLOR); + //if (moveset.isEmpty()) break; } //board.print(); diff --git a/src/model/Board.java b/src/model/Board.java index e8abdc1..fbb5ceb 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -169,8 +169,6 @@ public void jump(Move jump) { Piece moved = representation[jump.destination.row][jump.destination.column]; this.lastPieceMoved = moved; - - this.resetMovesSinceCapture(); } /** @@ -346,10 +344,6 @@ public int getMovesSinceCapture() { return this.movesSinceCapture; } - private void resetMovesSinceCapture() { - this.movesSinceCapture = 0; - } - public Piece getLastPieceMoved() { return this.lastPieceMoved; } diff --git a/src/view/Square.java b/src/view/Square.java index 6ef7e93..785ca8f 100644 --- a/src/view/Square.java +++ b/src/view/Square.java @@ -84,9 +84,8 @@ public void setPiece(Checker piece) { } public void removePiece() { - if (null != this.piece) { - this.remove(this.piece); - } + + this.remove(this.piece); } From 6907b9402440dec4a4256fa7ff86540e9fd8bc08 Mon Sep 17 00:00:00 2001 From: domenickd3 Date: Fri, 29 Apr 2016 02:14:38 -0400 Subject: [PATCH 4/4] 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);