Skip to content

Commit

Permalink
Modified updateMoveMessage calls / added getLastMove method (to be us…
Browse files Browse the repository at this point in the history
…ed for double jumps)
  • Loading branch information
domenickd3 committed Apr 29, 2016
1 parent 362502e commit 6907b94
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 23 deletions.
3 changes: 1 addition & 2 deletions src/controller/Game.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
11 changes: 9 additions & 2 deletions src/model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Board {
private Piece[][] representation;

private Piece lastPieceMoved;
private Move lastMove;

// Move properties
private int movesSinceCapture;
Expand All @@ -26,7 +27,8 @@ public Board() {
representation = new Piece[BOARD_SIZE][BOARD_SIZE];
movesSinceCapture = 0;
init();
lastPieceMoved = null;
lastPieceMoved = null;
lastMove = null;
}

/**
Expand All @@ -43,6 +45,7 @@ public Board(Board other) {
}
movesSinceCapture = other.getMovesSinceCapture();
lastPieceMoved = other.getLastPieceMoved();
lastMove = other.getLastMove();
}

public boolean isValidSquare(Location location) {
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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();
}

Expand Down Expand Up @@ -352,6 +356,9 @@ private void resetMovesSinceCapture() {

public Piece getLastPieceMoved() {
return this.lastPieceMoved;
}
public Move getLastMove() {
return this.lastMove;
}
}

1 change: 1 addition & 0 deletions src/view/CheckersWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/view/GameEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
52 changes: 35 additions & 17 deletions src/view/GamePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.");
}
}

Expand Down Expand Up @@ -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();
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/view/Square.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ public void highlight() {
this.setBorder(BorderFactory.createLineBorder(Color.YELLOW));

}



public void dehighlight() {
this.setBorder(null);

Expand Down

0 comments on commit 6907b94

Please sign in to comment.