From 4a8bb69f37d83f5573e4be82aa9956a0bddc236c Mon Sep 17 00:00:00 2001 From: aah13002 Date: Mon, 28 Mar 2016 17:05:59 -0400 Subject: [PATCH 1/5] Added Player class. --- src/model/Board.java | 3 +-- src/model/Player.java | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/model/Player.java diff --git a/src/model/Board.java b/src/model/Board.java index 0ac97d6..70c6f7c 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -8,5 +8,4 @@ public class Board { public Board() { representation = new Piece[BOARD_SIZE][BOARD_SIZE]; } - -} +} diff --git a/src/model/Player.java b/src/model/Player.java new file mode 100644 index 0000000..d666dd7 --- /dev/null +++ b/src/model/Player.java @@ -0,0 +1,29 @@ +package model; + +import java.util.ArrayList; + +public class Player { + private ArrayList pieces; + private String name; + + public Player(String name) { + this.name = name; + this.pieces = new ArrayList(); + } + + public void addPiece(Piece p) { + pieces.add(p); + } + + public ArrayList getPieces() { + return pieces; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} From 100adb667c47f1bff2c100d9058d486941b7938e Mon Sep 17 00:00:00 2001 From: aah13002 Date: Mon, 28 Mar 2016 17:25:05 -0400 Subject: [PATCH 2/5] Board.hasAttackVector(), Board.checkAttackDirection(), and Piece.opposite() added --- src/model/Board.java | 50 +++++++++++++++++++++++++++++++++++++++++++- src/model/Piece.java | 6 ++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/model/Board.java b/src/model/Board.java index 0ac97d6..1c716d4 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -4,9 +4,57 @@ public class Board { private final int BOARD_SIZE = 8; private Piece[][] representation; private int movesSinceCapture; + public enum Direction {UP, DOWN, LEFT, RIGHT}; public Board() { representation = new Piece[BOARD_SIZE][BOARD_SIZE]; + movesSinceCapture = 0; + } + + public boolean isValidSquare(int i, int j) { + return i < BOARD_SIZE && j < BOARD_SIZE; + } + + /** + * Checks if a piece can attack another in a given direction. + * @param p The piece. + * @param X The direction along the x-coordinate (LEFT or RIGHT). + * @param Y The direction along the y-coordinate (UP or DOWN). + * @return + */ + public boolean checkAttackDirection(Piece p, Direction X, Direction Y) { + Piece candidate = null; + int i = p.getX(), j = p.getY(); + int enemy_i = X.equals(Direction.LEFT) ? i-1 : i+1; + int enemy_j = Y.equals(Direction.UP) ? j+1 : j-1; + int end_position_i = X.equals(Direction.LEFT) ? i-2 : i+2; + int end_position_j = Y.equals(Direction.UP) ? j+2: j-2; + if (isValidSquare(enemy_i, enemy_j)) { + candidate = representation[enemy_i][enemy_j]; + if (null != candidate && // there exists a piece we can take) + candidate.color.equals(p.opposite()) && // it is an enemy piece + isValidSquare(end_position_i, end_position_j) && // there is a free space + null == representation[end_position_i][end_position_j]) // that we can end up in + return true; + } + return false; + } + + /** + * Used for validation of moves. + * If returns true after a player chooses a move, + * that move is invalid because the player can keep on attacking. + * @param p + * @return + */ + public boolean hasAttackVector(Piece p) { + boolean can_attack = false; + can_attack |= checkAttackDirection(p, Direction.UP, Direction.LEFT); + can_attack |= checkAttackDirection(p, Direction.UP, Direction.RIGHT); + if(p.getType().equals(Type.KING)) { + can_attack |= checkAttackDirection(p, Direction.DOWN, Direction.LEFT); + can_attack |= checkAttackDirection(p, Direction.DOWN, Direction.RIGHT); + } + return can_attack; } - } diff --git a/src/model/Piece.java b/src/model/Piece.java index b3765a1..cce93ae 100755 --- a/src/model/Piece.java +++ b/src/model/Piece.java @@ -18,6 +18,12 @@ public void updateCoordinates(int x, int y) { this.y = y; } + public Color opposite() { + if(this.color.equals(Color.RED)) return Color.BLACK; + if(this.color.equals(Color.BLACK)) return Color.RED; + return null; + } + public int getX() { return this.x; } From 298fa7d9b3f8adad70ddd9813c18e09bafe2faec Mon Sep 17 00:00:00 2001 From: aah13002 Date: Mon, 28 Mar 2016 17:29:08 -0400 Subject: [PATCH 3/5] conflict merge test --- src/model/Player.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model/Player.java b/src/model/Player.java index d666dd7..1be9e35 100644 --- a/src/model/Player.java +++ b/src/model/Player.java @@ -4,7 +4,7 @@ public class Player { private ArrayList pieces; - private String name; + private String _name; public Player(String name) { this.name = name; From d31541279ee0cc7c7b207ceb46f9f824986a7917 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Mon, 28 Mar 2016 17:29:50 -0400 Subject: [PATCH 4/5] testing merging --- src/model/Player.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/model/Player.java b/src/model/Player.java index d666dd7..8f20497 100644 --- a/src/model/Player.java +++ b/src/model/Player.java @@ -4,10 +4,10 @@ public class Player { private ArrayList pieces; - private String name; + private String name_; public Player(String name) { - this.name = name; + this.name_ = name; this.pieces = new ArrayList(); } @@ -20,10 +20,10 @@ public ArrayList getPieces() { } public String getName() { - return name; + return name_; } public void setName(String name) { - this.name = name; + this.name_ = name; } } From 97054df0a2f16940f9b6a46f0474884db0d22759 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Mon, 28 Mar 2016 17:36:41 -0400 Subject: [PATCH 5/5] players can't change names! --- src/model/Player.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/model/Player.java b/src/model/Player.java index d666dd7..6036244 100644 --- a/src/model/Player.java +++ b/src/model/Player.java @@ -4,7 +4,7 @@ public class Player { private ArrayList pieces; - private String name; + public final String name; public Player(String name) { this.name = name; @@ -22,8 +22,4 @@ public ArrayList getPieces() { public String getName() { return name; } - - public void setName(String name) { - this.name = name; - } }