Skip to content

Commit

Permalink
Got user movement working
Browse files Browse the repository at this point in the history
  • Loading branch information
john committed Apr 26, 2016
1 parent 185b9da commit e896423
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 35 deletions.
17 changes: 17 additions & 0 deletions src/controller/Game.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
package controller;

import java.util.ArrayList;

import model.Board;
import model.Color;
import model.Location;
import model.Move;

public class Game {
private Board board;
private Color current_turn;

public void movePiece(Move move) {
board.move(move);
}

public ArrayList<Move> getAvailableMoves(Location source) {
ArrayList<Move> moves = board.generateMoves(board.getPiece(source));
ArrayList<Move> jumps = board.generateJumpMoves(board.getPiece(source));

ArrayList<Move> allMoves = new ArrayList<Move>(moves);
allMoves.addAll(jumps);
return allMoves;
}

public Game(Color start) {
this.board = new Board();
Expand Down
25 changes: 17 additions & 8 deletions src/model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public boolean isValidSquare(Location location) {
private void init() {
for (int row = 0; row < 3; row++){
for (int col = 0; col < 4; col++) {
Piece red_piece = new Piece(Color.RED, 2*col + (row % 2), row);
Piece black_piece = new Piece(Color.BLACK, 2*col + (BOARD_SIZE - 1 - row) %2, BOARD_SIZE - 1 - row);
Piece red_piece = new Piece(Color.RED, row, 2*col + (row % 2));
Piece black_piece = new Piece(Color.BLACK, BOARD_SIZE - 1 - row, 2*col + (BOARD_SIZE - 1 - row) %2);
representation[row][2*col+ (row % 2)] = red_piece;
representation[BOARD_SIZE - 1 - row][2*col + (BOARD_SIZE - 1 - row) %2] = black_piece;
}
Expand Down Expand Up @@ -261,13 +261,18 @@ else if (representation[row][col].getColor() == Color.RED) {
* @return
*/
public boolean isValidJump(Move move) {
Piece monkey = representation[(move.destination.row + move.source.row)/2][(move.destination.column + move.source.column)/2];
Piece toMove = representation[move.source.row][move.source.column];
return isValidSquare(move.destination) && !isOccupied(move.destination)
&& monkey != null
&& monkey.getColor() == toMove.opposite();
if (isValidSquare(move.destination)) {
Piece monkey = representation[(move.destination.row + move.source.row)/2][(move.destination.column + move.source.column)/2];
Piece toMove = representation[move.source.row][move.source.column];
return !isOccupied(move.destination)
&& monkey != null
&& monkey.getColor() == toMove.opposite();

} else {
return false;
}
}

public boolean isValidMove(Move move) {
return isValidSquare(move.destination) && !isOccupied(move.destination);
}
Expand All @@ -279,6 +284,10 @@ public boolean isValidMove(Move move) {
public boolean isOccupied(Location location) {
return representation[location.row][location.column] != null;
}

public Piece getPiece(Location location) {
return representation[location.row][location.column];
}


public Piece[][] getRepresentation() {
Expand Down
4 changes: 3 additions & 1 deletion src/test/GUITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import controller.Game;
import model.Color;
import view.CheckersWindow;

public class GUITest {
Expand All @@ -27,7 +29,7 @@ public static void main(String[] args) {
catch (IllegalAccessException e) {
// handle exception
}
CheckersWindow window = new CheckersWindow();
CheckersWindow window = new CheckersWindow(new Game(Color.RED));
window.open();
}
}
37 changes: 31 additions & 6 deletions src/view/CheckersCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public class CheckersCanvas extends JPanel {
public CheckersCanvas(GameEventListener boardListener) {
super(new GridLayout(BOARD_DIM, BOARD_DIM));
this.board = new Square[BOARD_DIM][BOARD_DIM];

this.setPreferredSize(new Dimension(CANVAS_SIZE, CANVAS_SIZE));
this.setMaximumSize(new Dimension(CANVAS_SIZE, CANVAS_SIZE));
this.setMinimumSize(new Dimension(CANVAS_SIZE, CANVAS_SIZE));

this.initCanvas(boardListener);
}

private void initCanvas(GameEventListener boardListener) {
initSquares(boardListener);
initCheckers();
Expand Down Expand Up @@ -64,7 +64,7 @@ private void initSquares(GameEventListener boardListener) {
board[i][j*2] = redSquare;
redSquare.addMouseListener(boardListener);
this.add(redSquare);

/* Create a black square */
Square blackSquare = new Square(Color.BLACK, new Location(i, j*2 + 1));
board[i][j*2 + 1] = blackSquare;
Expand All @@ -74,16 +74,41 @@ private void initSquares(GameEventListener boardListener) {
}
}
}

private void initCheckers() {
for (int row = 0; row < BOARD_DIM / 2 - 1; ++row) {
for (int col = 0; col < BOARD_DIM / 2; ++col) {
Checker redChecker = new Checker(new Color(255, 51, 51));
Checker blackChecker = new Checker(new Color(89, 89, 89));
board[row][2*col+ (row % 2)].setPiece(redChecker);
board[BOARD_DIM - 1 - row][2*col + (BOARD_DIM - 1 - row) %2].setPiece(blackChecker);
board[BOARD_DIM - 1 - row][2*col + (BOARD_DIM - 1 - row) %2]
.setPiece(blackChecker);
}
}
}

public void highlightAndValidateSquare(Location location) {
board[location.row][location.column].highlight();
board[location.row][location.column].setValid(true);
}

public void dehighlightAndInvalidateSquare(Location location) {
board[location.row][location.column].dehighlight();
board[location.row][location.column].setValid(false);
}

public void invalidateAllSquares() {
for (int row = 0; row < BOARD_DIM; ++row) {
for (int col = 0; col < BOARD_DIM; ++col) {
dehighlightAndInvalidateSquare(new Location(row, col));
}
}
}

public void moveChecker(Location source, Location destination) {
board[destination.row][destination.column].setPiece(board[source.row]
[source.column].getPiece());
board[source.row][source.column].setPiece(null);

}
}
10 changes: 6 additions & 4 deletions src/view/CheckersWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import controller.Game;

/**
* Represents the JFrame window that will hold the graphical components
* of the game.
Expand All @@ -32,7 +34,7 @@ public class CheckersWindow extends JFrame {
private GameEventListener gameListener;


public CheckersWindow() {
public CheckersWindow(Game game) {
super("Checkers");
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Expand All @@ -42,7 +44,7 @@ public CheckersWindow() {
/* MUST be done in this order */
this.initGameListener();
this.createMenuBar();
this.initGamePanel();
this.initGamePanel(game);
this.pack();
}

Expand Down Expand Up @@ -96,8 +98,8 @@ private void createMenuBar() {
/**
* Initializes the {@link GamePanel} instance
*/
private void initGamePanel() {
this.gamePanel = new GamePanel(gameListener);
private void initGamePanel(Game game) {
this.gamePanel = new GamePanel(game, gameListener);
this.getContentPane().add(this.gamePanel);
this.gameListener.setGamePanel(gamePanel);
}
Expand Down
8 changes: 5 additions & 3 deletions src/view/GameEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
Square square = (Square) e.getComponent();
if(square.hasPiece()) {
gamePanel.dehighlightAllSquares();
gamePanel.setMoveSource(square);
if (square.isSelected())
gamePanel.highlightValidDestinations(square.getCellLocation());
gamePanel.updateMoveMessage();
} else {
} else if (square.isValid()) {
gamePanel.setMoveDestination(square);
gamePanel.updateMoveMessage();
}
Expand All @@ -52,8 +55,7 @@ public void keyPressed(KeyEvent arg0) {}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER && gamePanel.moveReady()) {
System.out.println("Moving piece.");
// TODO: Request a move from the controller.
gamePanel.moveSelectedPiece();
}

}
Expand Down
34 changes: 33 additions & 1 deletion src/view/GamePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JPanel;

import controller.Game;
import model.Location;
import model.Move;

/**
* Represents the panel which will hold all of the graphical
* components of the game.
Expand All @@ -14,16 +19,21 @@
*/
@SuppressWarnings("serial")
public class GamePanel extends JPanel {

private Game game;

private JLabel messageBar;
private CheckersCanvas canvas;
private GridBagConstraints layoutConstraints;

private Square moveDestination;
private Square moveSource;

public GamePanel(GameEventListener gameListener) {
public GamePanel(Game game, GameEventListener gameListener) {
super(new GridBagLayout());

this.game = game;

/* Initialize the layout manager */
this.layoutConstraints = new GridBagConstraints();
this.layoutConstraints.gridy = 0;
Expand Down Expand Up @@ -94,4 +104,26 @@ public boolean moveReady() {
return moveSource != null && moveDestination != null;
}

public void highlightValidDestinations(Location source) {
ArrayList<Move> availMoves = game.getAvailableMoves(source);

for (Move move : availMoves) {
canvas.highlightAndValidateSquare(move.destination);
}
}

public void dehighlightAllSquares() {
canvas.invalidateAllSquares();
}

public void moveSelectedPiece() {
game.movePiece(new Move(moveSource.getCellLocation(), moveDestination.getCellLocation()));
canvas.moveChecker(moveSource.getCellLocation(), moveDestination.getCellLocation());
dehighlightAllSquares();
moveSource.setSelected(false);
moveDestination.setSelected(false);
moveSource = null;
moveDestination = null;
}

}
50 changes: 38 additions & 12 deletions src/view/Square.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ public class Square extends JPanel implements MouseListener {
* The {@link Location} (row, col) of the square on the board.
*/
private final Location location;

/**
* The {@link Checker} object contained in this square. Null if there is none.
*/
private Checker piece;

/**
* A boolean value representing whether the square is selected for movement.
*/
private boolean selected;



private boolean valid;


/**
* Constructs a graphical square with the given color and location.
* @param color A {@link Color} object representing the square's color.
Expand All @@ -41,7 +43,7 @@ public Square(Color color, Location location) {
this.selected = false;
initSquare(color);
}


/**
* Initializes the properties of the square such as color and layout.
Expand Down Expand Up @@ -73,11 +75,13 @@ public Checker getPiece() {
*/
public void setPiece(Checker piece) {
this.piece = piece;
this.add(piece);
piece.addMouseListener(this);
this.validate();
if (piece != null) {
this.add(piece);
piece.addMouseListener(this);
this.validate();
}
}

/**
* {@link Square#selected}
*/
Expand All @@ -92,13 +96,23 @@ public boolean isSelected() {
*/
public void setSelected(boolean val) {
if (val) {
this.setBorder(BorderFactory.createLineBorder(Color.WHITE));
this.setBorder(BorderFactory.createLineBorder(Color.GREEN));
} else if (valid){
this.setBorder(BorderFactory.createLineBorder(Color.YELLOW));
} else {
this.setBorder(null);
}
this.selected = val;
}


public void setValid(boolean state) {
this.valid = state;
}

public boolean isValid() {
return valid;
}

/**
* Check's if the square contains a {@link Checker} object or not.
* @return <code>true</code> if the square contains a checker. <br />
Expand All @@ -107,7 +121,7 @@ public void setSelected(boolean val) {
public boolean hasPiece() {
return this.piece != null;
}

@Override
public void mousePressed(MouseEvent e) {
/* Send the event to the lister of the square (GameEventListener) */
Expand All @@ -129,4 +143,16 @@ public void mouseExited(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0) {}


public void highlight() {
this.setBorder(BorderFactory.createLineBorder(Color.YELLOW));

}


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

}


}

0 comments on commit e896423

Please sign in to comment.