Skip to content

Commit

Permalink
Swapped colors, and changed row convention kind of.
Browse files Browse the repository at this point in the history
  • Loading branch information
john committed Apr 28, 2016
1 parent 03078c0 commit b330443
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 32 deletions.
4 changes: 3 additions & 1 deletion src/controller/Game.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package controller;

import java.io.Console;
import java.util.ArrayList;

import model.Board;
Expand All @@ -18,8 +19,9 @@ public void movePiece(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);
if (allMoves.isEmpty())
System.out.println("No available moves");
allMoves.addAll(jumps);
return allMoves;
}
Expand Down
26 changes: 13 additions & 13 deletions src/model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ 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, 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;
Piece white_piece = new Piece(Color.WHITE, BOARD_SIZE - row - 1, 2*col + (row % 2));
Piece black_piece = new Piece(Color.BLACK, row, 2*col + (BOARD_SIZE - 1 - row) %2);
representation[BOARD_SIZE - row - 1][2*col+ (row % 2)] = white_piece;
representation[row][2*col + (BOARD_SIZE - 1 - row) %2] = black_piece;
}
}
}
Expand Down Expand Up @@ -86,8 +86,8 @@ public ArrayList<Move> generateMoves(Piece p) {
int row = p.getLocation().row;
int col = p.getLocation().column;
boolean up, down;
up = p.getColor() == Color.BLACK || p.getType() == Type.KING;
down = p.getColor() == Color.RED || p.getType() == Type.KING;
up = p.getColor() == Color.WHITE || p.getType() == Type.KING;
down = p.getColor() == Color.BLACK || p.getType() == Type.KING;
if (up) {
// up left
Move upLeft = new Move(p.getLocation(), new Location(row - 1, col - 1));
Expand Down Expand Up @@ -170,8 +170,8 @@ public ArrayList<Move> generateJumpMoves(Piece p) {
ArrayList<Move> jumps = new ArrayList<Move>();
int row = p.getLocation().row,
col = p.getLocation().column;
boolean up = p.getColor() == Color.BLACK || p.getType() == Type.KING;
boolean down = p.getColor() == Color.RED || p.getType() == Type.KING;
boolean up = p.getColor() == Color.WHITE || p.getType() == Type.KING;
boolean down = p.getColor() == Color.BLACK || p.getType() == Type.KING;
if (up) {
// Up left
Move upleft = new Move(new Location(row, col), new Location(row - 2, col - 2));
Expand Down Expand Up @@ -238,17 +238,17 @@ public void print() {
for (int col = 0; col < BOARD_SIZE; col++) {
if (!isOccupied(new Location(row, col)))
System.out.print("| ");
else if (representation[row][col].getColor() == Color.RED) {
else if (representation[row][col].getColor() == Color.BLACK) {
if (representation[row][col].getType() == Type.NORMAL)
System.out.print("|r");
System.out.print("|b");
else
System.out.print("|R");
System.out.print("|B");
}
else {
if (representation[row][col].getType() == Type.NORMAL)
System.out.print("|b");
System.out.print("|w");
else
System.out.print("|B");
System.out.print("|W");
}
}
System.out.println("|");
Expand Down
2 changes: 1 addition & 1 deletion src/model/Color.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package model;

public enum Color {
RED,
WHITE,
BLACK
}
4 changes: 2 additions & 2 deletions src/model/Piece.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public void setLocation(Location location) {
}

public Color opposite() {
if(this.color.equals(Color.RED)) return Color.BLACK;
if(this.color.equals(Color.BLACK)) return Color.RED;
if(this.color.equals(Color.WHITE)) return Color.BLACK;
if(this.color.equals(Color.BLACK)) return Color.WHITE;
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/GUITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void main(String[] args) {
catch (IllegalAccessException e) {
// handle exception
}
CheckersWindow window = new CheckersWindow(new Game(Color.RED));
CheckersWindow window = new CheckersWindow(new Game(Color.WHITE));
window.open();
}
}
28 changes: 14 additions & 14 deletions src/view/CheckersCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,27 @@ private void initCanvas(GameEventListener boardListener) {

private void initSquares(GameEventListener boardListener) {
for (int i = 0; i < BOARD_DIM; ++i) {
if (i % 2 == 0) {
if (i % 2 != 0) {
for (int j = 0; j < BOARD_DIM/2; ++j) {
/* Create a black square */
Square blackSquare = new Square(Color.BLACK, new Location(i, j*2));
board[i][j*2] = blackSquare;
blackSquare.addMouseListener(boardListener);
this.add(blackSquare);

/* Create a red square */
Square redSquare = new Square(new Color(150, 0, 0), new Location(i, j*2 + 1));
board[i][j*2 + 1] = redSquare;
redSquare.addMouseListener(boardListener);
this.add(redSquare);
/* Create a white square */
Square whiteSquare = new Square(new Color(150, 0, 0), new Location(i, j*2 + 1));
board[i][j*2 + 1] = whiteSquare;
whiteSquare.addMouseListener(boardListener);
this.add(whiteSquare);
}
} else {
for (int j = 0; j < BOARD_DIM/2; ++j) {
/* Create a red square */
Square redSquare = new Square(new Color(150, 0, 0), new Location(i, j*2));
board[i][j*2] = redSquare;
redSquare.addMouseListener(boardListener);
this.add(redSquare);
/* Create a white square */
Square whiteSquare = new Square(new Color(150, 0, 0), new Location(i, j*2));
board[i][j*2] = whiteSquare;
whiteSquare.addMouseListener(boardListener);
this.add(whiteSquare);

/* Create a black square */
Square blackSquare = new Square(Color.BLACK, new Location(i, j*2 + 1));
Expand All @@ -78,10 +78,10 @@ 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 whiteChecker = new Checker(new Color(0xB1B2B3));
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]
board[BOARD_DIM - 1 - row][2*col+ (row % 2)].setPiece(whiteChecker);
board[row][2*col + (BOARD_DIM - 1 - row) %2]
.setPiece(blackChecker);
}
}
Expand Down

0 comments on commit b330443

Please sign in to comment.