Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed merge conflict
  • Loading branch information
sos13004 committed Apr 5, 2017
1 parent 17bbac9 commit f063c74
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 43 deletions.
124 changes: 87 additions & 37 deletions src/CheckersGameState4.java
Expand Up @@ -22,7 +22,6 @@ public class CheckersGameState4 implements CheckersGameState{
_pieces = pieces;
_player = player;
}

public static void main(String[] args){ //for testing
CheckersGameState4 cg = new CheckersGameState4(0);
System.out.println(cg.player());
Expand All @@ -31,7 +30,8 @@ public class CheckersGameState4 implements CheckersGameState{
for(Move move : cg.actions()){
System.out.println(move);
cg.result(move).printState();
System.out.println();
// cg = cg.result(move);
System.out.println(cg);
}
}

Expand All @@ -52,18 +52,46 @@ public class CheckersGameState4 implements CheckersGameState{
}
}
}
//resulting row & col of piece moving in a direction
int[] dest(Piece p, int direction){
return dest(p._row, p._col, direction);
}

//resulting row & col if moving in a direction
int[] dest(int row, int col, int direction){
int[] dest = null;
switch(direction){
case 0: dest = new int[] {row +1, col -1};
break;
case 1: dest = new int[] {row+1, col+1};
break;
case 2: dest = new int[] {row-1, col-1};
break;
case 3: dest = new int[] {row-1, col+1};
break;
}
return dest;
}

@Override
public String player () {
if(_player == 1) return "black";
else return "white";
}

boolean samePlayer(int a, char b){
if((a==1 && b == 'b') || (a==1 && b == 'B')) return true;
if((a==0 && b == 'w') || (a==0 && b == 'W')) return true;
return false;
}
@Override
boolean oppPlayer(int a, char b){
if((a==1 && b == 'w') || (a==1 && b == 'W')) return true;
if((a==0 && b == 'b') || (a==0 && b == 'B')) return true;
return false;
}
boolean isEnemy(Piece p){
if(p== null) return false;
return oppPlayer(_player, p._token);
}
public List<Move> actions (){
ArrayList<Move> jumps = new ArrayList<Move>();
ArrayList<Move> avail = new ArrayList<Move>();
Expand All @@ -75,15 +103,27 @@ public class CheckersGameState4 implements CheckersGameState{
neigh = neighbors(p);
for(int i = 0; i<4; i++){
if(neigh[i] == 'x') avail.add(new Move4(p, i));
if(!samePlayer(_player, neigh[i])) //check for jumps
jumpPath = computeJumps(p, i); //do something with this
if(isJump(p._row, p._col, i, neigh)){
System.out.println("Jump Available");
} //do something with this
//check for jumps
}
}
}
if(jumps.isEmpty()) return avail;
return jumps;
}

boolean isJump(int row, int col, int index, char[] neigh){
int[] jumpto;
if(oppPlayer(_player, neigh[index])){
jumpto = dest(row, col, index);
jumpto = dest(jumpto[0], jumpto[1], index);
if(spotContains(jumpto[0], jumpto[1])._empty) {
return true;
}
}
return false;
}
ArrayList<Move> computeJumps(Piece p, int neighborIndex, Move4 lastMove){
ArrayList<Move> jumps = new ArrayList<Move>();
int[] nextSpot = dest(p, neighborIndex);
Expand Down Expand Up @@ -115,39 +155,52 @@ public class CheckersGameState4 implements CheckersGameState{
}
}
}

return jump;
return jumps;
}

//return piece's 4 neighbors
char[] neighbors(Piece p){
char[] neigh = new char[4];
Piece[] neighpieces = neighborPieces(p);
for(int i= 0; i<4; i++){
if(neighpieces[i] != null){
if(neighpieces[i]._empty) neigh[i] = 'x';
else neigh[i] = neighpieces[i]._token;
}
}
return neigh;
}

//find neighbors of the spot this piece is in.
Piece[] neighborPieces(Piece p){
return neighborPieces(p._row, p._col, p);
}

//find neighbors of a spot I'm not necessarily in
Piece[] neighborPieces(int row, int col, Piece p){
Piece[] neigh = new Piece[4];
if(p._token!='b'){ //all things b cannot do
if(p._row < 7 && p._col > 0)
neigh[0] = spotContains(p._row+1, p._col-1);
if(p._row < 7 && p._col < 7)
neigh[1] = spotContains(p._row+1, p._col+1);
if(row < 7 && col > 0)
neigh[0] = spotContains(row+1, col-1);
if(row < 7 && col < 7)
neigh[1] = spotContains(row+1, col+1);
}
if(p._token!='w'){ //all things w cannot do
if(p._row > 0 && p._col > 0)
neigh[2] = spotContains(p._row-1, p._col-1);
if(p._row > 0 && p._col < 7)
neigh[3] = spotContains(p._row-1, p._col+1);
if(row > 0 && col > 0)
neigh[2] = spotContains(row-1, col-1);
if(row > 0 && col < 7)
neigh[3] = spotContains(row-1, col+1);
}
return neigh;
}

//go through all pieces to see what this spot contains
char spotContains(int row, int col){
Piece spotContains(int row, int col){
for(Piece p : _pieces){
if(p._row == row && p._col == col){
return p._token;
return p;
}
}
return 'x';
return new Piece(true);
}

@Override
//return resulting state from taking move x
public CheckersGameState result (Move x){
Move4 m = (Move4) x;
Expand All @@ -159,8 +212,8 @@ public class CheckersGameState4 implements CheckersGameState{
boolean addPiece = true;
if(p == m._piece) {
Piece clone = p.clone();
clone._row = m._path[2]; //is this actually changing the piece that belongs to _pieces?
clone._col = m._path[3];
clone._row = m._path[m._path.length-2]; //is this actually changing the piece that belongs to _pieces?
clone._col = m._path[m._path.length-1];
if(m._path[2] == 7 && m._piece._token == 'w'){
clone._token = 'W'; //king me
}
Expand All @@ -170,25 +223,23 @@ public class CheckersGameState4 implements CheckersGameState{
newPieces.add(clone);
addPiece = false;
}
for(Piece c : captures) {
if(c==p){
addPiece = false;
break;
if(captures!=null){
for(Piece c : captures) {
if(c==p){
addPiece = false;
break;
}
}
}

if(addPiece){
newPieces.add(p.clone());
}
}
return new CheckersGameState4((_player+1)%2, newPieces);
}
//else consider jumps.
//remove from array list if captured.
//return (new CheckersGameState4((player+1)%2, pieces));
return this;
return null;
}

@Override
public void printState (){
char[][] board = new char[8][8];
for(Piece p: _pieces){
Expand All @@ -202,5 +253,4 @@ public class CheckersGameState4 implements CheckersGameState{
System.out.println("");
}
}

}
38 changes: 32 additions & 6 deletions src/Move4.java
@@ -1,7 +1,9 @@
import java.util.Arrays;

public class Move4 implements Move{
int[] _path;
Piece _piece;
Piece[] _captured;

public int source(){ return 0;}
public int destination(){ return 0;}
Expand All @@ -11,19 +13,34 @@ public class Move4 implements Move{
_path = path;
}

public Move4(Piece p, int dest){
public Move4(Piece p, int[] dest, Piece captured){
_piece = p;
_captured = new Piece[] {captured};
_path = new int[] {p._row, p._col, dest[0], dest[1]};
}

public Move4(Move4 lastMove, Piece captured, int[] dest){
_piece = lastMove._piece;
_captured = Arrays.copyOf(lastMove._captured, lastMove._captured.length +1);
_captured[_captured.length-1] = captured;
_path = Arrays.copyOf(lastMove._path, lastMove._path.length +2);
_path[_path.length-2] = dest[0];
_path[_path.length-1] = dest[1];
}

public Move4(Piece p, int destIndex){
_piece = p;
//_path = new int[4];
if(dest == 0) {
if(destIndex == 0) {
_path = new int[] {p._row, p._col, p._row + 1, p._col-1};
}
else if(dest == 1) {
else if(destIndex == 1) {
_path = new int[] {p._row, p._col, p._row + 1, p._col+1};
}
else if(dest == 2) {
else if(destIndex == 2) {
_path = new int[] {p._row, p._col, p._row - 1, p._col-1};
}
else if(dest == 3) {
else if(destIndex == 3) {
_path = new int[] {p._row, p._col, p._row - 1, p._col+1};
}
else System.out.println("Problem in Move constructor");
Expand All @@ -34,8 +51,17 @@ public class Move4 implements Move{
return new int[2];
}

public boolean notCaptured(Piece p){
for(Piece c : _captured){
if(c==p){
return false;
}
}
return true;
}

public Piece[] capturedPieces(){
return null;
return _captured;
}

// Returns the string representation of a move
Expand Down
7 changes: 7 additions & 0 deletions src/Piece.java
Expand Up @@ -3,10 +3,17 @@ class Piece{
char _token;
int _row;
int _col;
boolean _empty;

public Piece(char t, int r, int c) {
_token = t;
_row = r;
_col = c;
_empty = false;
}

public Piece(boolean empty){
_empty = empty;
}

public Piece clone() {
Expand Down

0 comments on commit f063c74

Please sign in to comment.