From f063c741332e0c8835e7d966627ba12b4ce4d090 Mon Sep 17 00:00:00 2001 From: savannaos Date: Tue, 4 Apr 2017 23:01:09 -0400 Subject: [PATCH] fixed merge conflict --- src/CheckersGameState4.java | 124 +++++++++++++++++++++++++----------- src/Move4.java | 38 +++++++++-- src/Piece.java | 7 ++ 3 files changed, 126 insertions(+), 43 deletions(-) diff --git a/src/CheckersGameState4.java b/src/CheckersGameState4.java index 08c74f6..9b2e444 100644 --- a/src/CheckersGameState4.java +++ b/src/CheckersGameState4.java @@ -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()); @@ -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); } } @@ -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 actions (){ ArrayList jumps = new ArrayList(); ArrayList avail = new ArrayList(); @@ -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 computeJumps(Piece p, int neighborIndex, Move4 lastMove){ ArrayList jumps = new ArrayList(); int[] nextSpot = dest(p, neighborIndex); @@ -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; @@ -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 } @@ -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){ @@ -202,5 +253,4 @@ public class CheckersGameState4 implements CheckersGameState{ System.out.println(""); } } - } diff --git a/src/Move4.java b/src/Move4.java index fd59c4a..45c14f4 100644 --- a/src/Move4.java +++ b/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;} @@ -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"); @@ -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 diff --git a/src/Piece.java b/src/Piece.java index 1e98c51..2d3105d 100644 --- a/src/Piece.java +++ b/src/Piece.java @@ -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() {