Skip to content

Commit

Permalink
resolved merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron committed Mar 28, 2016
2 parents 62a40cd + 97054df commit 4391759
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
54 changes: 51 additions & 3 deletions src/model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,60 @@ 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;
}

public void initBoard()
{
for(int row = 0; row < 3; row++){
Expand Down Expand Up @@ -52,7 +101,6 @@ public boolean isOccupied(int row, int col)
{
return representation[row][col] != null;
}




}

6 changes: 6 additions & 0 deletions src/model/Piece.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
25 changes: 25 additions & 0 deletions src/model/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package model;

import java.util.ArrayList;

public class Player {
private ArrayList<Piece> pieces;
public final String name;

public Player(String name) {
this.name = name;
this.pieces = new ArrayList<Piece>();
}

public void addPiece(Piece p) {
pieces.add(p);
}

public ArrayList<Piece> getPieces() {
return pieces;
}

public String getName() {
return name;
}
}

0 comments on commit 4391759

Please sign in to comment.