From 95a2793a4539d9b3b66e23dd712bb929e15be59d Mon Sep 17 00:00:00 2001 From: aah13002 Date: Mon, 28 Mar 2016 14:04:21 -0400 Subject: [PATCH 01/30] Initial commit --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..0a4077d --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# CSE-4705-Checkers +A very good checkers player. From c9c4c2b260636e930862ba1876247347b3e10984 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Mon, 28 Mar 2016 14:40:12 -0400 Subject: [PATCH 02/30] Initial commit. Added class for Piece representation. --- .classpath | 6 ++++++ .project | 17 +++++++++++++++++ src/model/Color.java | 6 ++++++ src/model/Piece.java | 36 ++++++++++++++++++++++++++++++++++++ src/model/Type.java | 6 ++++++ 5 files changed, 71 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 src/model/Color.java create mode 100644 src/model/Piece.java create mode 100644 src/model/Type.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..fb50116 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..cb0f832 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + CSE-4705-Checkers + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/src/model/Color.java b/src/model/Color.java new file mode 100644 index 0000000..4b2a351 --- /dev/null +++ b/src/model/Color.java @@ -0,0 +1,6 @@ +package model; + +public enum Color { + RED, + BLACK +} diff --git a/src/model/Piece.java b/src/model/Piece.java new file mode 100644 index 0000000..b3765a1 --- /dev/null +++ b/src/model/Piece.java @@ -0,0 +1,36 @@ +package model; + +public class Piece { + public final Color color; + private int x; + private int y; + private Type type; + + public Piece(Color color, int x, int y) { + this.color = color; + this.x = x; + this.y = y; + this.type = Type.NORMAL; + } + + public void updateCoordinates(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { + return this.x; + } + + public int getY() { + return this.y; + } + + public void promote() { + this.type = Type.KING; + } + + public Type getType() { + return this.type; + } +} diff --git a/src/model/Type.java b/src/model/Type.java new file mode 100644 index 0000000..3156fdc --- /dev/null +++ b/src/model/Type.java @@ -0,0 +1,6 @@ +package model; + +public enum Type { + NORMAL, + KING +} From 565cb52c53110330a9797c772a7ce8ef4ffcaaa4 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Mon, 28 Mar 2016 14:51:50 -0400 Subject: [PATCH 03/30] eclipse is crazy --- .classpath | 0 .project | 0 README.md | 0 src/model/Board.java | 12 ++++++++++++ src/model/Color.java | 0 src/model/Piece.java | 0 src/model/Type.java | 0 7 files changed, 12 insertions(+) mode change 100644 => 100755 .classpath mode change 100644 => 100755 .project mode change 100644 => 100755 README.md create mode 100755 src/model/Board.java mode change 100644 => 100755 src/model/Color.java mode change 100644 => 100755 src/model/Piece.java mode change 100644 => 100755 src/model/Type.java diff --git a/.classpath b/.classpath old mode 100644 new mode 100755 diff --git a/.project b/.project old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/src/model/Board.java b/src/model/Board.java new file mode 100755 index 0000000..0ac97d6 --- /dev/null +++ b/src/model/Board.java @@ -0,0 +1,12 @@ +package model; + +public class Board { + private final int BOARD_SIZE = 8; + private Piece[][] representation; + private int movesSinceCapture; + + public Board() { + representation = new Piece[BOARD_SIZE][BOARD_SIZE]; + } + +} diff --git a/src/model/Color.java b/src/model/Color.java old mode 100644 new mode 100755 diff --git a/src/model/Piece.java b/src/model/Piece.java old mode 100644 new mode 100755 diff --git a/src/model/Type.java b/src/model/Type.java old mode 100644 new mode 100755 From 4a8bb69f37d83f5573e4be82aa9956a0bddc236c Mon Sep 17 00:00:00 2001 From: aah13002 Date: Mon, 28 Mar 2016 17:05:59 -0400 Subject: [PATCH 04/30] 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 05/30] 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 06/30] 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 07/30] 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 08/30] 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; - } } From 62a40cd61cb137074a5589af255b5339015c6492 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Mon, 28 Mar 2016 17:41:35 -0400 Subject: [PATCH 09/30] printBoard(), initBoard(), isOccupied() --- src/model/Board.java | 46 ++++++++++++++++++++++++++++++++++++++++++++ src/model/Piece.java | 4 ++++ src/model/Test.java | 12 ++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 src/model/Test.java diff --git a/src/model/Board.java b/src/model/Board.java index 0ac97d6..3dc164a 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -8,5 +8,51 @@ public class Board { public Board() { representation = new Piece[BOARD_SIZE][BOARD_SIZE]; } + + public void initBoard() + { + for(int row = 0; row < 3; row++){ + for (int col = 0; col < 4; col++) + { + representation[row][2*col+ (row % 2)] = new Piece(Color.RED, row, 2*col + (row % 2)); + representation[BOARD_SIZE - 1 - row][2*col + (BOARD_SIZE - 1 - row) %2] = new Piece(Color.BLACK, BOARD_SIZE - 1 - row, 2*col + (BOARD_SIZE - 1 - row) %2); + } + } + } + + public void printBoard() + { + 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) + { + if (representation[row][col].getType() == Type.NORMAL) + System.out.print("|r"); + else + System.out.print("|R"); + } + else + { + if(representation[row][col].getType() == Type.NORMAL) + System.out.print("|b"); + else + System.out.print("|B"); + } + + } + System.out.println("|"); + } + } + + public boolean isOccupied(int row, int col) + { + return representation[row][col] != null; + } + + } diff --git a/src/model/Piece.java b/src/model/Piece.java index b3765a1..332e29c 100755 --- a/src/model/Piece.java +++ b/src/model/Piece.java @@ -33,4 +33,8 @@ public void promote() { public Type getType() { return this.type; } + + public Color getColor() { + return this.color; + } } diff --git a/src/model/Test.java b/src/model/Test.java new file mode 100644 index 0000000..668ff38 --- /dev/null +++ b/src/model/Test.java @@ -0,0 +1,12 @@ +package model; + +public class Test { + public static void main(String[]args) + { + Board checkers = new Board(); + checkers.initBoard(); + checkers.printBoard(); + } + + +} From 28a072aff77f7b27badb6d38ff92b2c4aa66303e Mon Sep 17 00:00:00 2001 From: aah13002 Date: Mon, 28 Mar 2016 17:57:05 -0400 Subject: [PATCH 10/30] hasAttackVector supports both colors now --- src/model/Board.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/model/Board.java b/src/model/Board.java index b5480e6..abbe466 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -44,14 +44,18 @@ public boolean checkAttackDirection(Piece p, Direction X, Direction Y) { * 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. + * We go by the convention that black starts out at the "bottom", and + * red starts out at the "top". Smoke moves before fire. * @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)) { + if (p.color.equals(Color.BLACK) || p.getType().equals(Type.KING)) { + can_attack |= checkAttackDirection(p, Direction.UP, Direction.LEFT); + can_attack |= checkAttackDirection(p, Direction.UP, Direction.RIGHT); + } + if (p.color.equals(Color.RED) || p.getType().equals(Type.KING)) { can_attack |= checkAttackDirection(p, Direction.DOWN, Direction.LEFT); can_attack |= checkAttackDirection(p, Direction.DOWN, Direction.RIGHT); } From 80cc4bfd2b894aad71e4a5ef9363102b12ce63c0 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Mon, 28 Mar 2016 18:00:43 -0400 Subject: [PATCH 11/30] board test file refactored --- src/model/Test.java | 12 ------------ src/test/BoardTest.java | 13 +++++++++++++ 2 files changed, 13 insertions(+), 12 deletions(-) delete mode 100644 src/model/Test.java create mode 100755 src/test/BoardTest.java diff --git a/src/model/Test.java b/src/model/Test.java deleted file mode 100644 index 668ff38..0000000 --- a/src/model/Test.java +++ /dev/null @@ -1,12 +0,0 @@ -package model; - -public class Test { - public static void main(String[]args) - { - Board checkers = new Board(); - checkers.initBoard(); - checkers.printBoard(); - } - - -} diff --git a/src/test/BoardTest.java b/src/test/BoardTest.java new file mode 100755 index 0000000..6bbbda7 --- /dev/null +++ b/src/test/BoardTest.java @@ -0,0 +1,13 @@ +package test; + +import model.Board; + +public class BoardTest { + + public static void main(String[]args) { + Board checkers = new Board(); + checkers.initBoard(); + checkers.printBoard(); + } +} + From 97a12bd8357a90b5c1ca2b5529e02d559659cf64 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Mon, 28 Mar 2016 18:08:11 -0400 Subject: [PATCH 12/30] Board.init() now private and called in constructor --- src/model/Board.java | 3 ++- src/test/BoardTest.java | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/model/Board.java b/src/model/Board.java index d908c4d..a7fb891 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -9,6 +9,7 @@ public enum Direction {UP, DOWN, LEFT, RIGHT}; public Board() { representation = new Piece[BOARD_SIZE][BOARD_SIZE]; movesSinceCapture = 0; + initBoard(); } public boolean isValidSquare(int i, int j) { @@ -62,7 +63,7 @@ public boolean hasAttackVector(Piece p) { return can_attack; } - public void initBoard() + private void initBoard() { for(int row = 0; row < 3; row++){ for (int col = 0; col < 4; col++) diff --git a/src/test/BoardTest.java b/src/test/BoardTest.java index 6bbbda7..1e30c07 100755 --- a/src/test/BoardTest.java +++ b/src/test/BoardTest.java @@ -6,7 +6,6 @@ public class BoardTest { public static void main(String[]args) { Board checkers = new Board(); - checkers.initBoard(); checkers.printBoard(); } } From e180017f3c8ce5000c56639a1778e1c201147b51 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Mon, 28 Mar 2016 18:08:54 -0400 Subject: [PATCH 13/30] Game class added --- src/model/Game.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100755 src/model/Game.java diff --git a/src/model/Game.java b/src/model/Game.java new file mode 100755 index 0000000..f717339 --- /dev/null +++ b/src/model/Game.java @@ -0,0 +1,13 @@ +package model; + +public class Game { + private Board board; + private Player player_one, player_two; + + public Game(Player p1, Player p2) { + this.player_one = p1; + this.player_two = p2; + this.board = new Board(); + } + +} From d981827dd179e31dc11716d69f329813a23e5e6d Mon Sep 17 00:00:00 2001 From: aah13002 Date: Mon, 28 Mar 2016 18:26:12 -0400 Subject: [PATCH 14/30] Added assignPieces method to Game class. --- src/model/Board.java | 53 ++++++++++++++++++++++++++++++++----------- src/model/Game.java | 10 ++++++++ src/model/Player.java | 16 ++++++++++--- 3 files changed, 63 insertions(+), 16 deletions(-) diff --git a/src/model/Board.java b/src/model/Board.java index a7fb891..d48e607 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -1,8 +1,17 @@ package model; +import java.util.ArrayList; + public class Board { + // Board properties and representation private final int BOARD_SIZE = 8; private Piece[][] representation; + + // Pieces available to the Players + private ArrayList red_pieces; + private ArrayList black_pieces; + + // Move properties private int movesSinceCapture; public enum Direction {UP, DOWN, LEFT, RIGHT}; @@ -11,11 +20,11 @@ public Board() { movesSinceCapture = 0; initBoard(); } - + 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. @@ -33,14 +42,14 @@ public boolean checkAttackDirection(Piece p, Direction X, Direction Y) { 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; + 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, @@ -62,18 +71,20 @@ public boolean hasAttackVector(Piece p) { } return can_attack; } - + private void initBoard() { for(int row = 0; row < 3; row++){ for (int col = 0; col < 4; col++) { - representation[row][2*col+ (row % 2)] = new Piece(Color.RED, row, 2*col + (row % 2)); - representation[BOARD_SIZE - 1 - row][2*col + (BOARD_SIZE - 1 - row) %2] = new Piece(Color.BLACK, BOARD_SIZE - 1 - row, 2*col + (BOARD_SIZE - 1 - row) %2); + 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; } } } - + public void printBoard() { for(int row = 0; row < BOARD_SIZE; row++) @@ -96,16 +107,32 @@ else if (representation[row][col].getColor() == Color.RED) else System.out.print("|B"); } - + } System.out.println("|"); } } - + public boolean isOccupied(int row, int col) { return representation[row][col] != null; } + + public ArrayList getRedPieces() { + return red_pieces; + } + + public void setRedPieces(ArrayList red_pieces) { + this.red_pieces = red_pieces; + } + + public ArrayList getBlackPieces() { + return black_pieces; + } + + public void setBlackPieces(ArrayList black_pieces) { + this.black_pieces = black_pieces; + } } diff --git a/src/model/Game.java b/src/model/Game.java index f717339..f1d2ae8 100755 --- a/src/model/Game.java +++ b/src/model/Game.java @@ -9,5 +9,15 @@ public Game(Player p1, Player p2) { this.player_two = p2; this.board = new Board(); } + + 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()); + } + } } diff --git a/src/model/Player.java b/src/model/Player.java index 6036244..ec0ae71 100644 --- a/src/model/Player.java +++ b/src/model/Player.java @@ -4,15 +4,17 @@ public class Player { private ArrayList pieces; + private Color assigned_color; public final String name; - public Player(String name) { + public Player(String name, Color c) { this.name = name; this.pieces = new ArrayList(); + this.setAssignedColor(c); } - public void addPiece(Piece p) { - pieces.add(p); + public void setPieces(ArrayList pieces) { + this.pieces = pieces; } public ArrayList getPieces() { @@ -22,4 +24,12 @@ public ArrayList getPieces() { public String getName() { return name; } + + public Color getAssignedColor() { + return assigned_color; + } + + public void setAssignedColor(Color assigned_color) { + this.assigned_color = assigned_color; + } } From 5ed596ded03526c630fe4e94197f6e6b5af6b63d Mon Sep 17 00:00:00 2001 From: aah13002 Date: Tue, 29 Mar 2016 01:01:30 -0400 Subject: [PATCH 15/30] Added comments and improved readability --- src/model/Board.java | 24 ++++++++++++++++++------ src/test/BoardTest.java | 2 +- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/model/Board.java b/src/model/Board.java index d48e607..d0a0b0d 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -18,7 +18,7 @@ public enum Direction {UP, DOWN, LEFT, RIGHT}; public Board() { representation = new Piece[BOARD_SIZE][BOARD_SIZE]; movesSinceCapture = 0; - initBoard(); + init(); } public boolean isValidSquare(int i, int j) { @@ -27,6 +27,7 @@ public boolean isValidSquare(int i, int j) { /** * 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). @@ -56,6 +57,7 @@ public boolean checkAttackDirection(Piece p, Direction X, Direction Y) { * that move is invalid because the player can keep on attacking. * We go by the convention that black starts out at the "bottom", and * red starts out at the "top". Smoke moves before fire. + * * @param p * @return */ @@ -71,8 +73,11 @@ public boolean hasAttackVector(Piece p) { } return can_attack; } - - private void initBoard() + + /** + * Initialize the board putting checker pieces in their starting locations + */ + private void init() { for(int row = 0; row < 3; row++){ for (int col = 0; col < 4; col++) @@ -84,8 +89,11 @@ private void initBoard() } } } - - public void printBoard() + + /** + * Print the current board representation + */ + public void print() { for(int row = 0; row < BOARD_SIZE; row++) { @@ -112,7 +120,11 @@ else if (representation[row][col].getColor() == Color.RED) System.out.println("|"); } } - + + /** + * return true if square contains a piece + * return false otherwise + */ public boolean isOccupied(int row, int col) { return representation[row][col] != null; diff --git a/src/test/BoardTest.java b/src/test/BoardTest.java index 1e30c07..a30f653 100755 --- a/src/test/BoardTest.java +++ b/src/test/BoardTest.java @@ -6,7 +6,7 @@ public class BoardTest { public static void main(String[]args) { Board checkers = new Board(); - checkers.printBoard(); + checkers.print(); } } From 40bb19a950b52097b314b7f0df87732aced289fa Mon Sep 17 00:00:00 2001 From: aah13002 Date: Wed, 30 Mar 2016 17:37:06 -0400 Subject: [PATCH 16/30] partially finished checkers canvas --- src/model/Game.java | 1 + src/test/GUITest.java | 10 +++++++ src/test/Square.java | 37 ++++++++++++++++++++++++ src/view/CheckersCanvas.java | 54 ++++++++++++++++++++++++++++++++++++ src/view/CheckersWindow.java | 30 ++++++++++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 src/test/GUITest.java create mode 100644 src/test/Square.java create mode 100644 src/view/CheckersCanvas.java create mode 100644 src/view/CheckersWindow.java diff --git a/src/model/Game.java b/src/model/Game.java index f1d2ae8..6db6ad7 100755 --- a/src/model/Game.java +++ b/src/model/Game.java @@ -8,6 +8,7 @@ public Game(Player p1, Player p2) { this.player_one = p1; this.player_two = p2; this.board = new Board(); + assignPieces(); } private void assignPieces() { diff --git a/src/test/GUITest.java b/src/test/GUITest.java new file mode 100644 index 0000000..59f3532 --- /dev/null +++ b/src/test/GUITest.java @@ -0,0 +1,10 @@ +package test; + +import view.CheckersWindow; + +public class GUITest { + + public static void main(String[] args) { + CheckersWindow window = new CheckersWindow(); + } +} diff --git a/src/test/Square.java b/src/test/Square.java new file mode 100644 index 0000000..8ff6ee3 --- /dev/null +++ b/src/test/Square.java @@ -0,0 +1,37 @@ +package test; + +import java.awt.Color; + +import javax.swing.JButton; + +public class Square extends JButton { + private Color color; + private int row; + private int column; + + public Square(Color color, int row, int column) { + super(""); + this.color = color; + this.setRow(row); + this.setColumn(column); + this.setBackground(color); + } + + public int getRow() { + return row; + } + + public void setRow(int row) { + this.row = row; + } + + public int getColumn() { + return column; + } + + public void setColumn(int column) { + this.column = column; + } + + +} diff --git a/src/view/CheckersCanvas.java b/src/view/CheckersCanvas.java new file mode 100644 index 0000000..43b7ab1 --- /dev/null +++ b/src/view/CheckersCanvas.java @@ -0,0 +1,54 @@ +package view; + +import java.awt.Color; +import java.awt.GridBagConstraints; +import java.awt.GridLayout; +import java.util.ArrayList; + +import javax.swing.JPanel; + +import model.Board; +import test.Square; + +public class CheckersCanvas extends JPanel { + + public static final int BOARD_DIM = 8; + + ArrayList blackSquares; + ArrayList redSquares; + + /* Checker board representation */ + Board board; + GridBagConstraints layoutConstraints; + + public CheckersCanvas() { + super(new GridLayout(BOARD_DIM, BOARD_DIM)); + this.blackSquares = new ArrayList(); + this.redSquares = new ArrayList(); + initSquares(); + } + + public void initSquares() { + for (int i = 0; i < BOARD_DIM; ++i) { + if (i % 2 == 0) { + for (int j = 0; j < BOARD_DIM/4; ++j) { + Square black = new Square(Color.BLACK, i, j*2); + Square red = new Square(Color.RED, i, j*2 + 1); + this.add(black); + blackSquares.add(black); + this.add(red); + redSquares.add(red); + } + } else { + for (int j = 0; j < BOARD_DIM/4; ++j) { + Square black = new Square(Color.BLACK, i, j*2 + 1); + Square red = new Square(Color.RED, i, j*2); + this.add(red); + blackSquares.add(red); + this.add(black); + redSquares.add(black); + } + } + } + } +} diff --git a/src/view/CheckersWindow.java b/src/view/CheckersWindow.java new file mode 100644 index 0000000..c7ad350 --- /dev/null +++ b/src/view/CheckersWindow.java @@ -0,0 +1,30 @@ +package view; + +import javax.swing.JFrame; + +public class CheckersWindow extends JFrame { + + public static final int HEIGHT = 800; + public static final int WIDTH = 800; + private CheckersCanvas canvas; + + public CheckersWindow() { + super("Checkers"); + this.setSize(WIDTH, HEIGHT); + this.setDefaultCloseOperation(EXIT_ON_CLOSE); + this.setLocationRelativeTo(null); + this.getContentPane().add(new CheckersCanvas()); + this.setVisible(true); + + //pack(); + } + + public CheckersCanvas getCanvas() { + return canvas; + } + + public void setCanvas(CheckersCanvas canvas) { + this.canvas = canvas; + } + +} From 9b6b841d761401028f4e0f70094dc3abc83cbae8 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Wed, 30 Mar 2016 17:46:09 -0400 Subject: [PATCH 17/30] Look and feel is now set to be cross platform --- src/test/GUITest.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/test/GUITest.java b/src/test/GUITest.java index 59f3532..9a58bbc 100644 --- a/src/test/GUITest.java +++ b/src/test/GUITest.java @@ -1,10 +1,32 @@ package test; +import javax.swing.UIManager; +import javax.swing.UnsupportedLookAndFeelException; + import view.CheckersWindow; public class GUITest { public static void main(String[] args) { + + // Need to set the look and feel for this to be cross platform + try { + // Set cross-platform Java L&F (also called "Metal") + UIManager.setLookAndFeel( + UIManager.getCrossPlatformLookAndFeelClassName()); + } + catch (UnsupportedLookAndFeelException e) { + // handle exception + } + catch (ClassNotFoundException e) { + // handle exception + } + catch (InstantiationException e) { + // handle exception + } + catch (IllegalAccessException e) { + // handle exception + } CheckersWindow window = new CheckersWindow(); } } From 6d28637509170acdf9af54fed8fe002ebe333f8d Mon Sep 17 00:00:00 2001 From: aah13002 Date: Wed, 30 Mar 2016 17:47:14 -0400 Subject: [PATCH 18/30] Updated class placement --- src/view/CheckersCanvas.java | 5 ++--- src/{test => view}/Square.java | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) rename src/{test => view}/Square.java (97%) diff --git a/src/view/CheckersCanvas.java b/src/view/CheckersCanvas.java index 43b7ab1..085dd87 100644 --- a/src/view/CheckersCanvas.java +++ b/src/view/CheckersCanvas.java @@ -8,7 +8,6 @@ import javax.swing.JPanel; import model.Board; -import test.Square; public class CheckersCanvas extends JPanel { @@ -31,7 +30,7 @@ public CheckersCanvas() { public void initSquares() { for (int i = 0; i < BOARD_DIM; ++i) { if (i % 2 == 0) { - for (int j = 0; j < BOARD_DIM/4; ++j) { + for (int j = 0; j < BOARD_DIM/2; ++j) { Square black = new Square(Color.BLACK, i, j*2); Square red = new Square(Color.RED, i, j*2 + 1); this.add(black); @@ -40,7 +39,7 @@ public void initSquares() { redSquares.add(red); } } else { - for (int j = 0; j < BOARD_DIM/4; ++j) { + for (int j = 0; j < BOARD_DIM/2; ++j) { Square black = new Square(Color.BLACK, i, j*2 + 1); Square red = new Square(Color.RED, i, j*2); this.add(red); diff --git a/src/test/Square.java b/src/view/Square.java similarity index 97% rename from src/test/Square.java rename to src/view/Square.java index 8ff6ee3..fa9bb6f 100644 --- a/src/test/Square.java +++ b/src/view/Square.java @@ -1,4 +1,4 @@ -package test; +package view; import java.awt.Color; From e1ed8da1da4571a6bbdaa898369e63527b25084c Mon Sep 17 00:00:00 2001 From: aah13002 Date: Wed, 30 Mar 2016 18:16:46 -0400 Subject: [PATCH 19/30] Added canMove(Piece,int,int) and canMove(int,int,int,int) to Board class. --- src/model/Board.java | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/model/Board.java b/src/model/Board.java index d0a0b0d..693f24d 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -72,6 +72,52 @@ public boolean hasAttackVector(Piece p) { can_attack |= checkAttackDirection(p, Direction.DOWN, Direction.RIGHT); } return can_attack; + } + + /** + * Used for validation of moves. + * We go by the convention that black starts out at the "bottom", and + * red starts out at the "top". Smoke moves before fire. + * A piece can move if the spot is available AND the move matches the color. + * (Black can only go up and red can only go down, unless they are kinged.) + * @param p + * @return true if the piece can move to the specified coordinates. + */ + public boolean canMove(Piece p, int x, int y) { + boolean spot_available = this.isValidSquare(x, y) && (null != this.representation[x][y]); + boolean is_moving_up = (y == p.getY() + 1); + boolean is_moving_down = (y == p.getY() - 1); + boolean is_moving_left = (x == p.getX() - 1); + boolean is_moving_right = (x == p.getX() + 1); + + // Spot must be available and must be moving exactly one to the left or right + if (spot_available && (is_moving_left || is_moving_right)) { + if (p.color.equals(Color.BLACK) || p.getType().equals(Type.KING)) { + if (is_moving_up) return true; + } + if (p.color.equals(Color.RED) || p.getType().equals(Type.KING)) { + if (is_moving_down) return true; + } + } + return false; + } + + /** + * Tests whether we can move a piece at (src_x, src_y) + * to a spot on the board at (dest_x, dest_y). + * Delegates to canMove(Piece, int, int). + * If a piece does not exist at the source, returns false. + * @param src_x + * @param src_y + * @param dest_x + * @param dest_y + * @return + */ + public boolean canMove(int src_x, int src_y, int dest_x, int dest_y) { + if (!(this.isValidSquare(src_x, src_y) && this.isValidSquare(dest_x, dest_y))) return false; + Piece p = this.representation[src_x][src_y]; + if (null == p) return false; + return this.canMove(p, dest_x, dest_y); } /** From 08e8c79c8f296fc16e2c4c22e67ad0893ce65ec3 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Wed, 30 Mar 2016 18:22:25 -0400 Subject: [PATCH 20/30] canvas finished, partially finished checker graphic --- src/view/CheckersCanvas.java | 1 + src/view/GUIPiece.java | 36 ++++++++++++++++++++++++++++++++++++ src/view/RoundedBorder.java | 32 ++++++++++++++++++++++++++++++++ src/view/Square.java | 13 +++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 src/view/GUIPiece.java create mode 100644 src/view/RoundedBorder.java diff --git a/src/view/CheckersCanvas.java b/src/view/CheckersCanvas.java index 085dd87..8e7292c 100644 --- a/src/view/CheckersCanvas.java +++ b/src/view/CheckersCanvas.java @@ -32,6 +32,7 @@ public void initSquares() { if (i % 2 == 0) { for (int j = 0; j < BOARD_DIM/2; ++j) { Square black = new Square(Color.BLACK, i, j*2); + black.add(new GUIPiece(Color.WHITE)); Square red = new Square(Color.RED, i, j*2 + 1); this.add(black); blackSquares.add(black); diff --git a/src/view/GUIPiece.java b/src/view/GUIPiece.java new file mode 100644 index 0000000..72c121e --- /dev/null +++ b/src/view/GUIPiece.java @@ -0,0 +1,36 @@ +package view; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.RenderingHints; + +import javax.swing.JButton; + +public class GUIPiece extends JButton { + private Color color; + + public GUIPiece(Color color) { + super(""); + this.color = color; + this.setContentAreaFilled(false); + this.setBorderPainted(false); + } + + protected void paintComponent(Graphics g) { + if (getModel().isArmed()) { + g.setColor(color); + } else { + g.setColor(getBackground()); + } + Graphics2D g2 = (Graphics2D)g; + RenderingHints hints = new RenderingHints(null); + hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); + hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + g2.setRenderingHints(hints); + g2.fillOval(0, 0, getSize().width,getSize().height-1); + super.paintComponent(g2); + } + +} diff --git a/src/view/RoundedBorder.java b/src/view/RoundedBorder.java new file mode 100644 index 0000000..e93a279 --- /dev/null +++ b/src/view/RoundedBorder.java @@ -0,0 +1,32 @@ +package view; + +import java.awt.Component; +import java.awt.Graphics; +import java.awt.Insets; + +import javax.swing.border.Border; + +public class RoundedBorder implements Border { + + private int radius; + + + RoundedBorder(int radius) { + this.radius = radius; + } + + + public Insets getBorderInsets(Component c) { + return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius); + } + + + public boolean isBorderOpaque() { + return true; + } + + + public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { + g.drawRoundRect(x, y, width-2, height-2, radius, radius); + } +} diff --git a/src/view/Square.java b/src/view/Square.java index fa9bb6f..1b54205 100644 --- a/src/view/Square.java +++ b/src/view/Square.java @@ -1,6 +1,8 @@ package view; +import java.awt.BorderLayout; import java.awt.Color; +import java.awt.Dimension; import javax.swing.JButton; @@ -8,6 +10,7 @@ public class Square extends JButton { private Color color; private int row; private int column; + private GUIPiece piece; public Square(Color color, int row, int column) { super(""); @@ -15,6 +18,8 @@ public Square(Color color, int row, int column) { this.setRow(row); this.setColumn(column); this.setBackground(color); + this.setLayout(new BorderLayout()); + //this.setPreferredSize(new Dimension(100, 100)); } public int getRow() { @@ -33,5 +38,13 @@ public void setColumn(int column) { this.column = column; } + public GUIPiece getPiece() { + return piece; + } + + public void setPiece(GUIPiece piece) { + this.piece = piece; + } + } From 2059be3cde96de34735de97d3835e36d68f69e26 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Wed, 30 Mar 2016 18:49:38 -0400 Subject: [PATCH 21/30] BoardTest automated testing for canMove added --- src/test/BoardTest.java | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/test/BoardTest.java b/src/test/BoardTest.java index a30f653..57f8b2c 100755 --- a/src/test/BoardTest.java +++ b/src/test/BoardTest.java @@ -3,10 +3,40 @@ import model.Board; 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, 3); // move black up left + assertCanMove(b, 1, 5, 2, 4); // move blkac 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() { + Board b = new Board(); + b.print(); + } public static void main(String[]args) { - Board checkers = new Board(); - checkers.print(); + printTest(); + movementTest(); } } From 01904f82700ba6a222ee61090bc615a0bdbd0c47 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Wed, 30 Mar 2016 19:39:09 -0400 Subject: [PATCH 22/30] swapped x and y because rows are y and columns are x... --- src/model/Board.java | 48 +++++++++++++++++++++++++++-------------- src/test/BoardTest.java | 5 ++--- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/model/Board.java b/src/model/Board.java index 693f24d..c738f9b 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -2,6 +2,14 @@ import java.util.ArrayList; +/** + * 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". + * @author Ayamin + * + */ public class Board { // Board properties and representation private final int BOARD_SIZE = 8; @@ -22,12 +30,14 @@ public Board() { } public boolean isValidSquare(int i, int j) { - return i < BOARD_SIZE && j < BOARD_SIZE; + return 0 <= i && i < BOARD_SIZE && + 0 <= j && j < BOARD_SIZE; } /** * Checks if a piece can attack another in a given direction. - * + * Up is negative y direction, down is positive y direction. + * Left is negative x direction, right is positive x 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). @@ -35,17 +45,17 @@ public boolean isValidSquare(int i, int j) { */ public boolean checkAttackDirection(Piece p, Direction X, Direction Y) { Piece candidate = null; - int i = p.getX(), j = p.getY(); + int i = p.getX(), j = p.getY(); // y is row, x is column int enemy_i = X.equals(Direction.LEFT) ? i-1 : i+1; - int enemy_j = Y.equals(Direction.UP) ? j+1 : j-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]; + int end_position_j = Y.equals(Direction.UP) ? j-2: j+2; + if (isValidSquare(enemy_j, enemy_i)) { + candidate = representation[enemy_j][enemy_i]; 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 + isValidSquare(end_position_j, end_position_i) && // there is a free space + null == representation[end_position_j][end_position_i]) // that we can end up in return true; } return false; @@ -58,6 +68,9 @@ public boolean checkAttackDirection(Piece p, Direction X, Direction Y) { * We go by the convention that black starts out at the "bottom", and * red starts out at the "top". Smoke moves before fire. * + * Up is negative y direction, down is positive y direction. + * Left is negative x direction, right is positive x direction. + * * @param p * @return */ @@ -78,15 +91,18 @@ public boolean hasAttackVector(Piece p) { * Used for validation of moves. * We go by the convention that black starts out at the "bottom", and * red starts out at the "top". Smoke moves before fire. + * Up is negative y direction, down is positive y direction. + * Left is negative x direction, right is positive x direction. * A piece can move if the spot is available AND the move matches the color. * (Black can only go up and red can only go down, unless they are kinged.) * @param p * @return true if the piece can move to the specified coordinates. */ public boolean canMove(Piece p, int x, int y) { - boolean spot_available = this.isValidSquare(x, y) && (null != this.representation[x][y]); - boolean is_moving_up = (y == p.getY() + 1); - boolean is_moving_down = (y == p.getY() - 1); + if(!this.isValidSquare(y, x)) return false; + boolean spot_available = (null == this.representation[y][x]); + boolean is_moving_up = (y == p.getY() - 1); + boolean is_moving_down = (y == p.getY() + 1); boolean is_moving_left = (x == p.getX() - 1); boolean is_moving_right = (x == p.getX() + 1); @@ -114,8 +130,8 @@ public boolean canMove(Piece p, int x, int y) { * @return */ public boolean canMove(int src_x, int src_y, int dest_x, int dest_y) { - if (!(this.isValidSquare(src_x, src_y) && this.isValidSquare(dest_x, dest_y))) return false; - Piece p = this.representation[src_x][src_y]; + if (!(this.isValidSquare(src_y, src_x) && this.isValidSquare(dest_y, dest_x))) return false; + Piece p = this.representation[src_y][src_x]; if (null == p) return false; return this.canMove(p, dest_x, dest_y); } @@ -128,8 +144,8 @@ 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); + 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; representation[BOARD_SIZE - 1 - row][2*col + (BOARD_SIZE - 1 - row) %2] = black_piece; } diff --git a/src/test/BoardTest.java b/src/test/BoardTest.java index 57f8b2c..55b2e7a 100755 --- a/src/test/BoardTest.java +++ b/src/test/BoardTest.java @@ -14,7 +14,6 @@ public static void assertCannotMove(Board b, int i, int j, int dest_i, int dest_ "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 @@ -22,8 +21,8 @@ public static void movementTest() { 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, 3); // move black up left - assertCanMove(b, 1, 5, 2, 4); // move blkac 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 From c24a80d8c9459425bdd780d122b1222d2f9cbf20 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Wed, 30 Mar 2016 19:47:33 -0400 Subject: [PATCH 23/30] Added tryToMove, hasAttackVector now operates on an image (copy) of the board --- src/model/Board.java | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/model/Board.java b/src/model/Board.java index c738f9b..4327b18 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -43,7 +43,7 @@ public boolean isValidSquare(int i, int j) { * @param Y The direction along the y-coordinate (UP or DOWN). * @return */ - public boolean checkAttackDirection(Piece p, Direction X, Direction Y) { + public boolean checkAttackDirection(Piece p, Piece[][] image, Direction X, Direction Y) { Piece candidate = null; int i = p.getX(), j = p.getY(); // y is row, x is column int enemy_i = X.equals(Direction.LEFT) ? i-1 : i+1; @@ -51,11 +51,11 @@ public boolean checkAttackDirection(Piece p, Direction X, Direction Y) { 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_j, enemy_i)) { - candidate = representation[enemy_j][enemy_i]; + candidate = image[enemy_j][enemy_i]; if (null != candidate && // there exists a piece we can take) candidate.color.equals(p.opposite()) && // it is an enemy piece isValidSquare(end_position_j, end_position_i) && // there is a free space - null == representation[end_position_j][end_position_i]) // that we can end up in + null == image[end_position_j][end_position_i]) // that we can end up in return true; } return false; @@ -74,15 +74,15 @@ public boolean checkAttackDirection(Piece p, Direction X, Direction Y) { * @param p * @return */ - public boolean hasAttackVector(Piece p) { + public boolean hasAttackVector(Piece p, Piece[][] image) { boolean can_attack = false; if (p.color.equals(Color.BLACK) || p.getType().equals(Type.KING)) { - can_attack |= checkAttackDirection(p, Direction.UP, Direction.LEFT); - can_attack |= checkAttackDirection(p, Direction.UP, Direction.RIGHT); + can_attack |= checkAttackDirection(p, image, Direction.UP, Direction.LEFT); + can_attack |= checkAttackDirection(p, image, Direction.UP, Direction.RIGHT); } if (p.color.equals(Color.RED) || p.getType().equals(Type.KING)) { - can_attack |= checkAttackDirection(p, Direction.DOWN, Direction.LEFT); - can_attack |= checkAttackDirection(p, Direction.DOWN, Direction.RIGHT); + can_attack |= checkAttackDirection(p, image, Direction.DOWN, Direction.LEFT); + can_attack |= checkAttackDirection(p, image, Direction.DOWN, Direction.RIGHT); } return can_attack; } @@ -134,6 +134,17 @@ public boolean canMove(int src_x, int src_y, int dest_x, int dest_y) { Piece p = this.representation[src_y][src_x]; if (null == p) return false; return this.canMove(p, dest_x, dest_y); + } + + public boolean tryToMove(int src_x, int src_y, int dest_x, int dest_y) { + if (canMove(src_x, src_y, dest_x, dest_y)) { + Piece piece = this.representation[src_y][src_x]; + this.representation[src_y][src_x] = null; + this.representation[dest_y][dest_x] = piece; + return true; + } + else if (canJump) + } /** From 816c1aea489f402fecdc6f19bc6eeb44d86ee4f2 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Wed, 30 Mar 2016 20:33:15 -0400 Subject: [PATCH 24/30] GUI piece selection is now working --- src/view/CheckersCanvas.java | 85 ++++++++++++++++++++++++++++++++---- src/view/CheckersWindow.java | 5 ++- src/view/GUIPiece.java | 7 ++- src/view/Square.java | 66 +++++++++++++++++++++++++--- 4 files changed, 146 insertions(+), 17 deletions(-) diff --git a/src/view/CheckersCanvas.java b/src/view/CheckersCanvas.java index 8e7292c..1822137 100644 --- a/src/view/CheckersCanvas.java +++ b/src/view/CheckersCanvas.java @@ -3,37 +3,46 @@ import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridLayout; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; import java.util.ArrayList; +import javax.swing.BorderFactory; import javax.swing.JPanel; import model.Board; -public class CheckersCanvas extends JPanel { - +public class CheckersCanvas extends JPanel implements MouseListener { + public static final int BOARD_DIM = 8; - - ArrayList blackSquares; - ArrayList redSquares; - + + private ArrayList blackSquares; + private ArrayList redSquares; + + private Square moveDestination; + private Square moveSource; + /* Checker board representation */ Board board; GridBagConstraints layoutConstraints; - + public CheckersCanvas() { super(new GridLayout(BOARD_DIM, BOARD_DIM)); this.blackSquares = new ArrayList(); this.redSquares = new ArrayList(); initSquares(); } - + public void initSquares() { for (int i = 0; i < BOARD_DIM; ++i) { if (i % 2 == 0) { for (int j = 0; j < BOARD_DIM/2; ++j) { Square black = new Square(Color.BLACK, i, j*2); - black.add(new GUIPiece(Color.WHITE)); + black.addMouseListener(this); + GUIPiece p = new GUIPiece(Color.WHITE); + black.setPiece(new GUIPiece(Color.WHITE)); Square red = new Square(Color.RED, i, j*2 + 1); + red.addMouseListener(this); this.add(black); blackSquares.add(black); this.add(red); @@ -42,7 +51,9 @@ public void initSquares() { } else { for (int j = 0; j < BOARD_DIM/2; ++j) { Square black = new Square(Color.BLACK, i, j*2 + 1); + black.addMouseListener(this); Square red = new Square(Color.RED, i, j*2); + red.addMouseListener(this); this.add(red); blackSquares.add(red); this.add(black); @@ -51,4 +62,60 @@ public void initSquares() { } } } + + @Override + public void mouseClicked(MouseEvent arg0) { + + + } + + @Override + public void mouseEntered(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseExited(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void mousePressed(MouseEvent e) { + Square square = (Square) e.getComponent(); + if(square.hasPiece()) { + if (!square.isSelected()) { + square.setSelected(true); + if (moveSource != null) + moveSource.setSelected(false); + if (moveDestination != null) + moveDestination.setSelected(false); + moveDestination = null; + moveSource = square; + } else { + if (square == moveSource) + moveSource = null; + square.setSelected(false); + } + } else { + + if (!square.isSelected()) { + square.setSelected(true); + if (moveDestination != null) + moveDestination.setSelected(false); + moveDestination = square; + } else { + if (square == moveDestination) + moveDestination = null; + square.setSelected(false); + } + } + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } } diff --git a/src/view/CheckersWindow.java b/src/view/CheckersWindow.java index c7ad350..4000b13 100644 --- a/src/view/CheckersWindow.java +++ b/src/view/CheckersWindow.java @@ -1,5 +1,7 @@ package view; +import java.awt.BorderLayout; + import javax.swing.JFrame; public class CheckersWindow extends JFrame { @@ -13,8 +15,9 @@ public CheckersWindow() { this.setSize(WIDTH, HEIGHT); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocationRelativeTo(null); - this.getContentPane().add(new CheckersCanvas()); + this.getContentPane().add(new CheckersCanvas(), BorderLayout.CENTER); this.setVisible(true); + this.setResizable(false); //pack(); } diff --git a/src/view/GUIPiece.java b/src/view/GUIPiece.java index 72c121e..159b281 100644 --- a/src/view/GUIPiece.java +++ b/src/view/GUIPiece.java @@ -5,6 +5,7 @@ import java.awt.Graphics2D; import java.awt.RenderingHints; +import javax.swing.BorderFactory; import javax.swing.JButton; public class GUIPiece extends JButton { @@ -15,6 +16,8 @@ public GUIPiece(Color color) { this.color = color; this.setContentAreaFilled(false); this.setBorderPainted(false); + this.setFocusable(false); + this.setOpaque(false); } protected void paintComponent(Graphics g) { @@ -29,8 +32,8 @@ protected void paintComponent(Graphics g) { hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2.setRenderingHints(hints); - g2.fillOval(0, 0, getSize().width,getSize().height-1); + g2.fillOval(5, 5, getSize().width-10,getSize().height-10); super.paintComponent(g2); - } + } } diff --git a/src/view/Square.java b/src/view/Square.java index 1b54205..8f0a264 100644 --- a/src/view/Square.java +++ b/src/view/Square.java @@ -2,24 +2,27 @@ import java.awt.BorderLayout; import java.awt.Color; -import java.awt.Dimension; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; -import javax.swing.JButton; +import javax.swing.BorderFactory; +import javax.swing.JPanel; -public class Square extends JButton { +public class Square extends JPanel implements MouseListener { private Color color; private int row; private int column; private GUIPiece piece; + private boolean selected; public Square(Color color, int row, int column) { - super(""); + super(); this.color = color; this.setRow(row); + this.selected = false; this.setColumn(column); this.setBackground(color); this.setLayout(new BorderLayout()); - //this.setPreferredSize(new Dimension(100, 100)); } public int getRow() { @@ -44,6 +47,59 @@ public GUIPiece getPiece() { public void setPiece(GUIPiece piece) { this.piece = piece; + this.add(piece); + piece.addMouseListener(this); + this.validate(); + } + + public boolean isSelected() { + return selected; + } + + public void setSelected(boolean val) { + if (val) { + System.out.println("setting border"); + this.setBorder(BorderFactory.createLineBorder(Color.WHITE)); + } else { + this.setBorder(null); + } + this.selected = val; + } + + public boolean hasPiece() { + return piece != null; + } + + @Override + public void mouseClicked(MouseEvent arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseEntered(MouseEvent arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseExited(MouseEvent arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void mousePressed(MouseEvent e) { + MouseEvent newE = new MouseEvent(this, e.getID(), e.getWhen(), e.getModifiers(), + e.getClickCount(), e.getX(), e.getY(), e.isPopupTrigger()); + this.dispatchEvent(newE); + + } + + @Override + public void mouseReleased(MouseEvent arg0) { + // TODO Auto-generated method stub + } From 4af19723175efd5019315a4ace4b8be3a662a169 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Tue, 12 Apr 2016 04:46:24 -0400 Subject: [PATCH 25/30] finished up GUI for the checkers board. Pieces are now layed out properly with the correct colors. --- src/model/Location.java | 16 ++++++ src/view/{GUIPiece.java => Checker.java} | 12 ++--- src/view/CheckersCanvas.java | 64 +++++++++++++++--------- src/view/Square.java | 36 ++++--------- 4 files changed, 69 insertions(+), 59 deletions(-) create mode 100644 src/model/Location.java rename src/view/{GUIPiece.java => Checker.java} (79%) diff --git a/src/model/Location.java b/src/model/Location.java new file mode 100644 index 0000000..6ea1574 --- /dev/null +++ b/src/model/Location.java @@ -0,0 +1,16 @@ +package model; + +/** + * Represents a (row, column) location on a chess board. + * @author john + * + */ +public class Location { + public final int row; + public final int column; + + public Location(int row, int column) { + this.row = row; + this.column = column; + } +} diff --git a/src/view/GUIPiece.java b/src/view/Checker.java similarity index 79% rename from src/view/GUIPiece.java rename to src/view/Checker.java index 159b281..b4fed20 100644 --- a/src/view/GUIPiece.java +++ b/src/view/Checker.java @@ -8,10 +8,10 @@ import javax.swing.BorderFactory; import javax.swing.JButton; -public class GUIPiece extends JButton { +public class Checker extends JButton { private Color color; - public GUIPiece(Color color) { + public Checker(Color color) { super(""); this.color = color; this.setContentAreaFilled(false); @@ -21,17 +21,13 @@ public GUIPiece(Color color) { } protected void paintComponent(Graphics g) { - if (getModel().isArmed()) { - g.setColor(color); - } else { - g.setColor(getBackground()); - } - Graphics2D g2 = (Graphics2D)g; + Graphics2D g2 = (Graphics2D) g; RenderingHints hints = new RenderingHints(null); hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2.setRenderingHints(hints); + g2.setColor(color); g2.fillOval(5, 5, getSize().width-10,getSize().height-10); super.paintComponent(g2); } diff --git a/src/view/CheckersCanvas.java b/src/view/CheckersCanvas.java index 1822137..498d31a 100644 --- a/src/view/CheckersCanvas.java +++ b/src/view/CheckersCanvas.java @@ -7,61 +7,75 @@ import java.awt.event.MouseListener; import java.util.ArrayList; -import javax.swing.BorderFactory; import javax.swing.JPanel; import model.Board; +import model.Location; +import model.Piece; public class CheckersCanvas extends JPanel implements MouseListener { public static final int BOARD_DIM = 8; - private ArrayList blackSquares; - private ArrayList redSquares; + private Square[][] board; private Square moveDestination; private Square moveSource; - /* Checker board representation */ - Board board; + GridBagConstraints layoutConstraints; public CheckersCanvas() { super(new GridLayout(BOARD_DIM, BOARD_DIM)); - this.blackSquares = new ArrayList(); - this.redSquares = new ArrayList(); + this.board = new Square[BOARD_DIM][BOARD_DIM]; initSquares(); + initCheckers(); } public void initSquares() { for (int i = 0; i < BOARD_DIM; ++i) { if (i % 2 == 0) { for (int j = 0; j < BOARD_DIM/2; ++j) { - Square black = new Square(Color.BLACK, i, j*2); - black.addMouseListener(this); - GUIPiece p = new GUIPiece(Color.WHITE); - black.setPiece(new GUIPiece(Color.WHITE)); - Square red = new Square(Color.RED, i, j*2 + 1); - red.addMouseListener(this); - this.add(black); - blackSquares.add(black); - this.add(red); - redSquares.add(red); + /* Create a black square */ + Square blackSquare = new Square(Color.BLACK, new Location(i, j*2)); + board[i][j*2] = blackSquare; + blackSquare.addMouseListener(this); + 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(this); + this.add(redSquare); } } else { for (int j = 0; j < BOARD_DIM/2; ++j) { - Square black = new Square(Color.BLACK, i, j*2 + 1); - black.addMouseListener(this); - Square red = new Square(Color.RED, i, j*2); - red.addMouseListener(this); - this.add(red); - blackSquares.add(red); - this.add(black); - redSquares.add(black); + /* 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(this); + 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; + blackSquare.addMouseListener(this); + this.add(blackSquare); } } } } + + 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); + } + } + } @Override public void mouseClicked(MouseEvent arg0) { diff --git a/src/view/Square.java b/src/view/Square.java index 8f0a264..891c8bc 100644 --- a/src/view/Square.java +++ b/src/view/Square.java @@ -4,48 +4,33 @@ import java.awt.Color; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; - -import javax.swing.BorderFactory; -import javax.swing.JPanel; +import javax.swing.*; +import model.Location; public class Square extends JPanel implements MouseListener { private Color color; - private int row; - private int column; - private GUIPiece piece; + private final Location location; + private Checker piece; private boolean selected; - public Square(Color color, int row, int column) { + public Square(Color color, Location location) { super(); this.color = color; - this.setRow(row); + this.location = location; this.selected = false; - this.setColumn(column); this.setBackground(color); this.setLayout(new BorderLayout()); } - public int getRow() { - return row; - } - - public void setRow(int row) { - this.row = row; - } - - public int getColumn() { - return column; - } - - public void setColumn(int column) { - this.column = column; + public Location getCellLocation() { + return location; } - public GUIPiece getPiece() { + public Checker getPiece() { return piece; } - public void setPiece(GUIPiece piece) { + public void setPiece(Checker piece) { this.piece = piece; this.add(piece); piece.addMouseListener(this); @@ -58,7 +43,6 @@ public boolean isSelected() { public void setSelected(boolean val) { if (val) { - System.out.println("setting border"); this.setBorder(BorderFactory.createLineBorder(Color.WHITE)); } else { this.setBorder(null); From 00077203d72f6f6caf2f7891fb268ab0e18002e7 Mon Sep 17 00:00:00 2001 From: Domenick D'Onofrio Date: Tue, 12 Apr 2016 18:45:53 -0400 Subject: [PATCH 26/30] menubar --- src/view/CheckersCanvas.java | 1 - src/view/CheckersWindow.java | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/view/CheckersCanvas.java b/src/view/CheckersCanvas.java index 498d31a..dd618dd 100644 --- a/src/view/CheckersCanvas.java +++ b/src/view/CheckersCanvas.java @@ -85,7 +85,6 @@ public void mouseClicked(MouseEvent arg0) { @Override public void mouseEntered(MouseEvent e) { - // TODO Auto-generated method stub } diff --git a/src/view/CheckersWindow.java b/src/view/CheckersWindow.java index 4000b13..95a1785 100644 --- a/src/view/CheckersWindow.java +++ b/src/view/CheckersWindow.java @@ -1,8 +1,15 @@ package view; import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; import javax.swing.JFrame; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; public class CheckersWindow extends JFrame { @@ -10,17 +17,63 @@ public class CheckersWindow extends JFrame { public static final int WIDTH = 800; private CheckersCanvas canvas; + public CheckersWindow() { super("Checkers"); this.setSize(WIDTH, HEIGHT); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocationRelativeTo(null); + this.createMenuBar(); this.getContentPane().add(new CheckersCanvas(), BorderLayout.CENTER); this.setVisible(true); this.setResizable(false); //pack(); } + + private void createMenuBar() { + JMenuBar menubar = new JMenuBar(); + JMenu file = new JMenu("File"); + //New Game + JMenuItem newGame = new JMenuItem("New game"); + newGame.setMnemonic(KeyEvent.VK_N); + newGame.setToolTipText("Start a new game"); + newGame.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + //this.getContentPane().add(new CheckersCanvas(), BorderLayout.CENTER); + } + }); + //Quit + JMenuItem quit = new JMenuItem("Quit"); + quit.setMnemonic(KeyEvent.VK_Q); + quit.setToolTipText("Exit application"); + quit.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + System.exit(0); + } + }); + //Rules + JMenuItem instructions = new JMenuItem("Instructions"); + instructions.setMnemonic(KeyEvent.VK_I); + instructions.setToolTipText("How to play"); + instructions.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(null, "
  1. instr 1
", "title", JOptionPane.INFORMATION_MESSAGE); + //JOptionPane.showMessageDialog(null, "instr 1
", "title", JOptionPane.INFORMATION_MESSAGE); + } + }); + + file.add(quit); + file.add(newGame); + file.add(instructions); + menubar.add(file); + menubar.setVisible(true); + setJMenuBar(menubar); + } + public CheckersCanvas getCanvas() { return canvas; From 0b675c229f6e33f39717a314fb8d00f4af0f42da Mon Sep 17 00:00:00 2001 From: aah13002 Date: Tue, 12 Apr 2016 18:45:53 -0400 Subject: [PATCH 27/30] menubar --- src/view/CheckersCanvas.java | 1 - src/view/CheckersWindow.java | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/view/CheckersCanvas.java b/src/view/CheckersCanvas.java index 498d31a..dd618dd 100644 --- a/src/view/CheckersCanvas.java +++ b/src/view/CheckersCanvas.java @@ -85,7 +85,6 @@ public void mouseClicked(MouseEvent arg0) { @Override public void mouseEntered(MouseEvent e) { - // TODO Auto-generated method stub } diff --git a/src/view/CheckersWindow.java b/src/view/CheckersWindow.java index 4000b13..95a1785 100644 --- a/src/view/CheckersWindow.java +++ b/src/view/CheckersWindow.java @@ -1,8 +1,15 @@ package view; import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; import javax.swing.JFrame; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; public class CheckersWindow extends JFrame { @@ -10,17 +17,63 @@ public class CheckersWindow extends JFrame { public static final int WIDTH = 800; private CheckersCanvas canvas; + public CheckersWindow() { super("Checkers"); this.setSize(WIDTH, HEIGHT); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocationRelativeTo(null); + this.createMenuBar(); this.getContentPane().add(new CheckersCanvas(), BorderLayout.CENTER); this.setVisible(true); this.setResizable(false); //pack(); } + + private void createMenuBar() { + JMenuBar menubar = new JMenuBar(); + JMenu file = new JMenu("File"); + //New Game + JMenuItem newGame = new JMenuItem("New game"); + newGame.setMnemonic(KeyEvent.VK_N); + newGame.setToolTipText("Start a new game"); + newGame.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + //this.getContentPane().add(new CheckersCanvas(), BorderLayout.CENTER); + } + }); + //Quit + JMenuItem quit = new JMenuItem("Quit"); + quit.setMnemonic(KeyEvent.VK_Q); + quit.setToolTipText("Exit application"); + quit.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + System.exit(0); + } + }); + //Rules + JMenuItem instructions = new JMenuItem("Instructions"); + instructions.setMnemonic(KeyEvent.VK_I); + instructions.setToolTipText("How to play"); + instructions.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(null, "
  1. instr 1
", "title", JOptionPane.INFORMATION_MESSAGE); + //JOptionPane.showMessageDialog(null, "instr 1
", "title", JOptionPane.INFORMATION_MESSAGE); + } + }); + + file.add(quit); + file.add(newGame); + file.add(instructions); + menubar.add(file); + menubar.setVisible(true); + setJMenuBar(menubar); + } + public CheckersCanvas getCanvas() { return canvas; From 48fed73a4e99feb32251c97b0a7fb49a0f8505fb Mon Sep 17 00:00:00 2001 From: aah13002 Date: Tue, 12 Apr 2016 18:50:24 -0400 Subject: [PATCH 28/30] updated readme. testing git identity. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0a4077d..c17881f 100755 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # CSE-4705-Checkers A very good checkers player. + +So far the view works. \ No newline at end of file From 9673ac6b7c6d149930c20442f1a4f3b742b35c62 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Tue, 12 Apr 2016 18:50:24 -0400 Subject: [PATCH 29/30] updated readme. testing git identity. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0a4077d..c17881f 100755 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # CSE-4705-Checkers A very good checkers player. + +So far the view works. \ No newline at end of file From 50ac9dd20b16a3ba27f47223448b0645694749d6 Mon Sep 17 00:00:00 2001 From: aah13002 Date: Tue, 12 Apr 2016 18:50:24 -0400 Subject: [PATCH 30/30] updated readme. testing git identity. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0a4077d..c17881f 100755 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # CSE-4705-Checkers A very good checkers player. + +So far the view works. \ No newline at end of file