From 799d9ae27e9db2118ec49019b2e64c196d9176a9 Mon Sep 17 00:00:00 2001 From: John Bojorquez Date: Mon, 28 Mar 2016 18:26:12 -0400 Subject: [PATCH] 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; + } }