From 6f956c9704056f05a66a2748907839f79827c0dd Mon Sep 17 00:00:00 2001 From: aah13002 Date: Wed, 20 Apr 2016 15:41:50 -0400 Subject: [PATCH 1/5] fixed isValidSquare to comply with row/col standard. refactored curly braces. --- src/model/Board.java | 40 +++++++++++++++++----------------------- src/model/Player.java | 35 ----------------------------------- 2 files changed, 17 insertions(+), 58 deletions(-) delete mode 100644 src/model/Player.java diff --git a/src/model/Board.java b/src/model/Board.java index 2bd1142..184dc54 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -4,9 +4,8 @@ /** * The representation is a 8x8 grid where - * A[y][x] denotes the (x, y) indexed piece. - * It is swapped because we dereference by "row" first, - * which is "y". + * A[row][col] marks the position of the checker piece. + * Smoke starts at lower row and fire starts at higher row. * @author Ayamin * */ @@ -28,15 +27,18 @@ public Board() { movesSinceCapture = 0; init(); } - + + public boolean isValidSquare(int row, int col) { + return 0 <= row && row < BOARD_SIZE && + 0 <= col && col < BOARD_SIZE; + } + /** * Initialize the board putting checker pieces in their starting locations */ - private void init() - { + private void init() { for(int row = 0; row < 3; row++){ - for (int col = 0; col < 4; col++) - { + 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); representation[row][2*col+ (row % 2)] = red_piece; @@ -48,29 +50,23 @@ private void init() /** * Print the current board representation */ - public void print() - { - for(int row = 0; row < BOARD_SIZE; row++) - { - for (int col = 0; col < BOARD_SIZE; col++) - { + public void print() { + for(int row = 0; row < BOARD_SIZE; row++) { + for (int col = 0; col < BOARD_SIZE; col++) { if (!isOccupied(row, col)) System.out.print("| "); - else if (representation[row][col].getColor() == Color.RED) - { + else if (representation[row][col].getColor() == Color.RED) { if (representation[row][col].getType() == Type.NORMAL) System.out.print("|r"); else System.out.print("|R"); } - else - { + else { if(representation[row][col].getType() == Type.NORMAL) System.out.print("|b"); else System.out.print("|B"); } - } System.out.println("|"); } @@ -80,8 +76,7 @@ else if (representation[row][col].getColor() == Color.RED) * return true if square contains a piece * return false otherwise */ - public boolean isOccupied(int row, int col) - { + public boolean isOccupied(int row, int col) { return representation[row][col] != null; } @@ -99,7 +94,6 @@ public ArrayList getBlackPieces() { public void setBlackPieces(ArrayList black_pieces) { this.black_pieces = black_pieces; - } - + } } diff --git a/src/model/Player.java b/src/model/Player.java deleted file mode 100644 index ec0ae71..0000000 --- a/src/model/Player.java +++ /dev/null @@ -1,35 +0,0 @@ -package model; - -import java.util.ArrayList; - -public class Player { - private ArrayList pieces; - private Color assigned_color; - public final String name; - - public Player(String name, Color c) { - this.name = name; - this.pieces = new ArrayList(); - this.setAssignedColor(c); - } - - public void setPieces(ArrayList pieces) { - this.pieces = pieces; - } - - public ArrayList getPieces() { - return pieces; - } - - public String getName() { - return name; - } - - public Color getAssignedColor() { - return assigned_color; - } - - public void setAssignedColor(Color assigned_color) { - this.assigned_color = assigned_color; - } -} From 33938c89dbe8cbe00cab2b266e7d0e1981a74e01 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Wed, 20 Apr 2016 15:46:32 -0400 Subject: [PATCH 2/5] removed a bunch of the tests for board --- src/test/BoardTest.java | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/src/test/BoardTest.java b/src/test/BoardTest.java index 55b2e7a..7a178b8 100755 --- a/src/test/BoardTest.java +++ b/src/test/BoardTest.java @@ -4,28 +4,10 @@ public class BoardTest { - public static void assertCanMove(Board b, int i, int j, int dest_i, int dest_j) { - assert(b.canMove(i, j, dest_i,dest_j)) : - "Can move from (" + i + "," + j +") to (" + dest_i + "," + dest_j + ")."; - } - - public static void assertCannotMove(Board b, int i, int j, int dest_i, int dest_j) { - assert(!b.canMove(i, j, dest_i,dest_j)) : - "Cannot move from (" + i + "," + j +") to (" + dest_i + "," + dest_j + ")."; - } public static void movementTest() { Board b = new Board(); - assertCannotMove(b, 0, 6, 1, 5); // move black onto another - assertCannotMove(b, 6, 2, 7, 1); // move red up - assertCannotMove(b, 0, 2, -1, 3); // move red out of bounds - assertCannotMove(b, 7, 5, 8, 4); // move black out of bounds - assertCanMove(b, 3, 5, 4, 4); // move black up right - assertCanMove(b, 3, 5, 2, 4); // move black up left - assertCanMove(b, 1, 5, 2, 4); // move black up right - assertCanMove(b, 0, 2, 1, 3); // move red down right - assertCanMove(b, 4, 2, 3, 3); // move red down left - assertCanMove(b, 4, 2, 5, 3); // move red down right + } public static void printTest() { From d86040d3e5395f91ed81a387260fb22f88ac63ed Mon Sep 17 00:00:00 2001 From: aah13002 Date: Wed, 20 Apr 2016 15:48:40 -0400 Subject: [PATCH 3/5] game class no longer uses Player --- src/controller/Game.java | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/controller/Game.java b/src/controller/Game.java index 9f70302..2ca5991 100755 --- a/src/controller/Game.java +++ b/src/controller/Game.java @@ -2,27 +2,14 @@ import model.Board; import model.Color; -import model.Player; public class Game { private Board board; - private Player player_one, player_two; + private Color current_turn; - public Game(Player p1, Player p2) { - this.player_one = p1; - this.player_two = p2; + public Game(Color start) { this.board = new Board(); - assignPieces(); - } - - private void assignPieces() { - if (player_one.getAssignedColor() == Color.RED) { - player_one.setPieces(board.getRedPieces()); - player_two.setPieces(board.getBlackPieces()); - } else { - player_two.setPieces(board.getRedPieces()); - player_one.setPieces(board.getBlackPieces()); - } + current_turn = start; } } From 7c62a12d580b9ee78f07658111ba5e3779c3ad32 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Wed, 20 Apr 2016 15:50:54 -0400 Subject: [PATCH 4/5] refactored Move class to public final fields --- src/model/Move.java | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/src/model/Move.java b/src/model/Move.java index 033a321..ccfe1d8 100644 --- a/src/model/Move.java +++ b/src/model/Move.java @@ -1,29 +1,13 @@ package model; public class Move { - private Location source; - private Location destination; + public final Location source; + public final Location destination; public Move(Location source, Location destination) { - this.setSource(source); - this.setDestination(destination); - } - - public Location getSource() { - return source; - } - - public void setSource(Location source) { this.source = source; + this.destination = destination; } - public Location getDestination() { - return destination; - } - public void setDestination(Location destination) { - this.destination = destination; - } - - } From 753b2b0e88c862a3efe8cafcd27e02770db4ccdc Mon Sep 17 00:00:00 2001 From: aah13002 Date: Wed, 20 Apr 2016 15:58:44 -0400 Subject: [PATCH 5/5] added deep-equality functions --- src/model/Board.java | 25 ++++++++++++++++++++++--- src/model/Location.java | 4 ++++ src/model/Piece.java | 6 ++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/model/Board.java b/src/model/Board.java index 184dc54..b87416a 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -37,7 +37,7 @@ public boolean isValidSquare(int row, int col) { * Initialize the board putting checker pieces in their starting locations */ private void init() { - for(int row = 0; row < 3; row++){ + 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); @@ -47,11 +47,26 @@ private void init() { } } + /** + * Tests board equality. + * @param other The other board. + * @return true if the board's pieces are all equal to the other board's pieces. + */ + public boolean equals(Board other) { + for (int i = 0; i < BOARD_SIZE; ++i) { + for (int j = 0; j < BOARD_SIZE; ++j) { + if(!(this.representation[i][j]).equals(other.getRepresentation()[i][j])) + return false; + } + } + return true; + } + /** * Print the current board representation */ public void print() { - for(int row = 0; row < BOARD_SIZE; row++) { + for (int row = 0; row < BOARD_SIZE; row++) { for (int col = 0; col < BOARD_SIZE; col++) { if (!isOccupied(row, col)) System.out.print("| "); @@ -62,7 +77,7 @@ else if (representation[row][col].getColor() == Color.RED) { System.out.print("|R"); } else { - if(representation[row][col].getType() == Type.NORMAL) + if (representation[row][col].getType() == Type.NORMAL) System.out.print("|b"); else System.out.print("|B"); @@ -94,6 +109,10 @@ public ArrayList getBlackPieces() { public void setBlackPieces(ArrayList black_pieces) { this.black_pieces = black_pieces; + } + + public Piece[][] getRepresentation() { + return this.representation; } } diff --git a/src/model/Location.java b/src/model/Location.java index 6ea1574..4bb4498 100644 --- a/src/model/Location.java +++ b/src/model/Location.java @@ -13,4 +13,8 @@ public Location(int row, int column) { this.row = row; this.column = column; } + + public boolean equals(Location other) { + return this.row == other.row && this.column == other.column; + } } diff --git a/src/model/Piece.java b/src/model/Piece.java index 0a4f8e6..909c2e8 100755 --- a/src/model/Piece.java +++ b/src/model/Piece.java @@ -36,4 +36,10 @@ public Type getType() { public Color getColor() { return this.color; } + + public boolean equals(Piece other) { + return this.color == other.color && + this.location.equals(other.getLocation()) && + this.type == other.getType(); + } }