Skip to content

Commit

Permalink
Added assignPieces method to Game class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron committed Mar 28, 2016
1 parent e180017 commit d981827
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
53 changes: 40 additions & 13 deletions src/model/Board.java
Original file line number Diff line number Diff line change
@@ -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<Piece> red_pieces;
private ArrayList<Piece> black_pieces;

// Move properties
private int movesSinceCapture;
public enum Direction {UP, DOWN, LEFT, RIGHT};

Expand All @@ -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.
Expand All @@ -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,
Expand All @@ -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++)
Expand All @@ -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<Piece> getRedPieces() {
return red_pieces;
}

public void setRedPieces(ArrayList<Piece> red_pieces) {
this.red_pieces = red_pieces;
}

public ArrayList<Piece> getBlackPieces() {
return black_pieces;
}

public void setBlackPieces(ArrayList<Piece> black_pieces) {
this.black_pieces = black_pieces;
}

}

10 changes: 10 additions & 0 deletions src/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

}
16 changes: 13 additions & 3 deletions src/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

public class Player {
private ArrayList<Piece> 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<Piece>();
this.setAssignedColor(c);
}

public void addPiece(Piece p) {
pieces.add(p);
public void setPieces(ArrayList<Piece> pieces) {
this.pieces = pieces;
}

public ArrayList<Piece> getPieces() {
Expand All @@ -22,4 +24,12 @@ public ArrayList<Piece> getPieces() {
public String getName() {
return name;
}

public Color getAssignedColor() {
return assigned_color;
}

public void setAssignedColor(Color assigned_color) {
this.assigned_color = assigned_color;
}
}

0 comments on commit d981827

Please sign in to comment.