Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
cse4705-checkersgame/EightByEightArray.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
589 lines (578 sloc)
16.4 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class EightByEightArray implements CheckersGameState { | |
public String[][] pieces = new String[8][8]; | |
public String player; | |
public boolean captureAvailable; | |
public List<Move> captures; | |
public boolean doneMoving; | |
public EightByEightArray(){ | |
} | |
public EightByEightArray(String[][] _pieces){ | |
pieces = _pieces; | |
} | |
@Override | |
public String player() { | |
return player; | |
} | |
public void checkAllJumps(int x, int y, List<Integer> moves, String piece){ | |
moves.add(x); | |
moves.add(y); | |
boolean noLeftDownMoves = false; | |
boolean noRightDownMoves = false; | |
boolean noRightUpMoves = false; | |
boolean noLeftUpMoves = false; | |
if(piece == "b"){ | |
if(x > 1){ | |
if(y>1){ | |
if((pieces[x-1][y-1] == "w" || pieces[x-1][y-1] == "W") && pieces[x-2][y-2] == "-"){ | |
List<Integer> branch = new ArrayList<Integer>(); | |
branch.addAll(moves); | |
checkAllJumps(x-2,y-2,branch, "b"); | |
} | |
else{ | |
noLeftDownMoves = true; | |
} | |
} | |
else{ | |
noLeftDownMoves = true; | |
} | |
if(y<6){ | |
if((pieces[x-1][y+1] == "w" || pieces[x-1][y+1] == "W") && pieces[x-2][y+2] == "-"){ | |
List<Integer> branch = new ArrayList<Integer>(); | |
branch.addAll(moves); | |
checkAllJumps(x-2,y+2,branch, "b"); | |
} | |
else{ | |
noRightDownMoves = true; | |
} | |
} | |
else{ | |
noRightDownMoves = true; | |
} | |
} | |
else{ | |
noLeftDownMoves = true; | |
noRightDownMoves = true; | |
} | |
if(noLeftDownMoves && noRightDownMoves){ | |
Move newMove = new Move(); | |
newMove.move = moves; | |
newMove.piece = "b"; | |
captures.add(newMove); | |
} | |
} | |
else if(piece == "B"){ | |
if(x > 1){ | |
if(y>1){ | |
if((pieces[x-1][y-1] == "w" || pieces[x-1][y-1] == "W") && pieces[x-2][y-2] == "-"){ | |
//check if we've taken this move before | |
for(int i = 0; i < moves.size(); i+=2){ | |
if(moves.get(i) == x-2 && moves.get(i+1) == y-2){ | |
noLeftDownMoves = true; | |
} | |
} | |
if(!noLeftDownMoves){ | |
List<Integer> branch = new ArrayList<Integer>(); | |
branch.addAll(moves); | |
checkAllJumps(x-2,y-2,branch, "B"); | |
} | |
} | |
else{ | |
noLeftDownMoves = true; | |
} | |
} | |
else{ | |
noLeftDownMoves = true; | |
} | |
if(y<6){ | |
if((pieces[x-1][y+1] == "w" || pieces[x-1][y+1] == "W") && pieces[x-2][y+2] == "-"){ | |
for(int i = 0; i < moves.size(); i+=2){ | |
if(moves.get(i) == x-2 && moves.get(i+1) == y+2){ | |
noRightDownMoves = true; | |
} | |
} | |
if(!noRightDownMoves){ | |
List<Integer> branch = new ArrayList<Integer>(); | |
branch.addAll(moves); | |
checkAllJumps(x-2,y+2,branch, "B"); | |
} | |
} | |
else{ | |
noRightDownMoves = true; | |
} | |
} | |
else{ | |
noRightDownMoves = true; | |
} | |
} | |
else{ | |
noLeftDownMoves = true; | |
noRightDownMoves = true; | |
} | |
if(x<6){ | |
if(y>1){ | |
if((pieces[x+1][y-1] == "w" || pieces[x+1][y-1] == "W") && pieces[x+2][y-2] == "-"){ | |
for(int i = 0; i < moves.size(); i+=2){ | |
if(moves.get(i) == x+2 && moves.get(i+1) == y-2){ | |
noLeftUpMoves = true; | |
} | |
} | |
if(!noLeftUpMoves){ | |
List<Integer> branch = new ArrayList<Integer>(); | |
branch.addAll(moves); | |
checkAllJumps(x+2,y-2,branch, "B"); | |
} | |
} | |
else{ | |
noLeftUpMoves = true; | |
} | |
} | |
else{ | |
noLeftUpMoves = true; | |
} | |
if(y<6){ | |
if((pieces[x+1][y+1] == "w" || pieces[x+1][y+1] == "W") && pieces[x+2][y+2] == "-"){ | |
for(int i = 0; i < moves.size(); i+=2){ | |
if(moves.get(i) == x+2 && moves.get(i+1) == y+2){ | |
noRightUpMoves = true; | |
} | |
} | |
if(!noRightUpMoves){ | |
List<Integer> branch = new ArrayList<Integer>(); | |
branch.addAll(moves); | |
checkAllJumps(x+2,y+2,branch, "B"); | |
} | |
} | |
else{ | |
noRightUpMoves = true; | |
} | |
} | |
else{ | |
noRightUpMoves = true; | |
} | |
} | |
else{ | |
noLeftUpMoves = true; | |
noRightUpMoves = true; | |
} | |
if(noLeftDownMoves && noRightDownMoves && noLeftUpMoves && noRightUpMoves){ | |
Move newMove = new Move(); | |
newMove.move = moves; | |
newMove.piece = "B"; | |
captures.add(newMove); | |
} | |
} | |
if(piece == "w"){ | |
if(x < 6){ | |
if(y>1){ | |
if((pieces[x+1][y-1] == "b" || pieces[x+1][y-1] == "B") && pieces[x+2][y-2] == "-"){ | |
List<Integer> branch = new ArrayList<Integer>(); | |
branch.addAll(moves); | |
checkAllJumps(x+2,y-2,branch, "w"); | |
} | |
else{ | |
noLeftUpMoves = true; | |
} | |
} | |
else{ | |
noLeftUpMoves = true; | |
} | |
if(y<6){ | |
if((pieces[x+1][y+1] == "b" || pieces[x+1][y+1] == "B") && pieces[x+2][y+2] == "-"){ | |
List<Integer> branch = new ArrayList<Integer>(); | |
branch.addAll(moves); | |
checkAllJumps(x+2,y+2,branch, "w"); | |
} | |
else{ | |
noRightUpMoves = true; | |
} | |
} | |
else{ | |
noRightUpMoves = true; | |
} | |
} | |
else{ | |
noLeftUpMoves = true; | |
noRightUpMoves = true; | |
} | |
if(noLeftUpMoves && noRightUpMoves){ | |
Move newMove = new Move(); | |
newMove.move = moves; | |
newMove.piece = "w"; | |
captures.add(newMove); | |
} | |
} | |
else if(piece == "W"){ | |
if(x > 1){ | |
if(y>1){ | |
if((pieces[x-1][y-1] == "b" || pieces[x-1][y-1] == "B") && pieces[x-2][y-2] == "-"){ | |
//check if we've taken this move before | |
for(int i = 0; i < moves.size(); i+=2){ | |
if(moves.get(i) == x-2 && moves.get(i+1) == y-2){ | |
noLeftDownMoves = true; | |
} | |
} | |
if(!noLeftDownMoves){ | |
List<Integer> branch = new ArrayList<Integer>(); | |
branch.addAll(moves); | |
checkAllJumps(x-2,y-2,branch, "W"); | |
} | |
} | |
else{ | |
noLeftDownMoves = true; | |
} | |
} | |
else{ | |
noLeftDownMoves = true; | |
} | |
if(y<6){ | |
if((pieces[x-1][y+1] == "b" || pieces[x-1][y+1] == "B") && pieces[x-2][y+2] == "-"){ | |
for(int i = 0; i < moves.size(); i+=2){ | |
if(moves.get(i) == x-2 && moves.get(i+1) == y+2){ | |
noRightDownMoves = true; | |
} | |
} | |
if(!noRightDownMoves){ | |
List<Integer> branch = new ArrayList<Integer>(); | |
branch.addAll(moves); | |
checkAllJumps(x-2,y+2,branch, "W"); | |
} | |
} | |
else{ | |
noRightDownMoves = true; | |
} | |
} | |
else{ | |
noRightDownMoves = true; | |
} | |
} | |
else{ | |
noLeftDownMoves = true; | |
noRightDownMoves = true; | |
} | |
if(x<6){ | |
if(y>1){ | |
if((pieces[x+1][y-1] == "b" || pieces[x+1][y-1] == "B") && pieces[x+2][y-2] == "-"){ | |
for(int i = 0; i < moves.size(); i+=2){ | |
if(moves.get(i) == x+2 && moves.get(i+1) == y-2){ | |
noLeftUpMoves = true; | |
} | |
} | |
if(!noLeftUpMoves){ | |
List<Integer> branch = new ArrayList<Integer>(); | |
branch.addAll(moves); | |
checkAllJumps(x+2,y-2,branch, "W"); | |
} | |
} | |
else{ | |
noLeftUpMoves = true; | |
} | |
} | |
else{ | |
noLeftUpMoves = true; | |
} | |
if(y<6){ | |
if((pieces[x+1][y+1] == "b" || pieces[x+1][y+1] == "B") && pieces[x+2][y+2] == "-"){ | |
for(int i = 0; i < moves.size(); i+=2){ | |
if(moves.get(i) == x+2 && moves.get(i+1) == y+2){ | |
noRightUpMoves = true; | |
} | |
} | |
if(!noRightUpMoves){ | |
List<Integer> branch = new ArrayList<Integer>(); | |
branch.addAll(moves); | |
checkAllJumps(x+2,y+2,branch, "W"); | |
} | |
} | |
else{ | |
noRightUpMoves = true; | |
} | |
} | |
else{ | |
noRightUpMoves = true; | |
} | |
} | |
else{ | |
noLeftUpMoves = true; | |
noRightUpMoves = true; | |
} | |
if(noLeftDownMoves && noRightDownMoves && noLeftUpMoves && noRightUpMoves){ | |
Move newMove = new Move(); | |
newMove.move = moves; | |
newMove.piece = "W"; | |
captures.add(newMove); | |
} | |
} | |
} | |
@Override | |
public List<Move> actions() { | |
captures = new ArrayList<Move>(); | |
List<Move> moves = new ArrayList<Move>(); | |
captureAvailable = false; | |
if(player() == "Black"){ | |
for(int i = 0; i < 8; i++){ | |
for(int j = 0; j < 8; j++){ | |
if(pieces[i][j] == "b"){ | |
//Check if any captures are available | |
if((i > 1 && j > 1 && (pieces[i-1][j-1] == "w" || pieces[i-1][j-1] == "W") && pieces[i-2][j-2] == "-") || (i > 1 && j < 6 && (pieces[i-1][j+1] == "w" || pieces[i-1][j+1] == "W") && pieces[i-2][j+2] == "-")){ | |
checkAllJumps(i, j, new ArrayList<Integer>(), "b"); | |
captureAvailable = true; | |
} | |
//only proceed if no captures available | |
if(!captureAvailable){ | |
//check left move | |
if(i != 0 && j!= 0){ | |
if(pieces[i-1][j-1]== "-"){ | |
Move newMove = new Move(); | |
List<Integer> newLocation = new ArrayList<Integer>(); | |
newLocation.add(i); | |
newLocation.add(j); | |
newLocation.add(i-1); | |
newLocation.add(j-1); | |
newMove.move = newLocation; | |
newMove.piece = "b"; | |
moves.add(newMove); | |
} | |
} | |
//check right move | |
if(i !=0 && j !=7){ | |
if(pieces[i-1][j+1]== "-"){ | |
Move newMove = new Move(); | |
List<Integer> newLocation = new ArrayList<Integer>(); | |
newLocation.add(i); | |
newLocation.add(j); | |
newLocation.add(i-1); | |
newLocation.add(j+1); | |
newMove.move = newLocation; | |
newMove.piece = "b"; | |
moves.add(newMove); | |
} | |
} | |
} | |
} | |
//check if any captures are available | |
else if(pieces[i][j] == "B"){ | |
if((i > 1 && j > 1 && (pieces[i-1][j-1] == "w" || pieces[i-1][j-1] == "W") && pieces[i-2][j-2] == "-") || (i > 1 && j < 6 && (pieces[i-1][j+1] == "w" || pieces[i-1][j+1] == "W") && pieces[i-2][j+2] == "-") || (i < 6 && j > 1 && (pieces[i+1][j-1] == "w" || pieces[i+1][j-1] == "W") && pieces[i+2][j-2] == "-") || (i < 6 && j < 6 && (pieces[i+1][j+1] == "w" || pieces[i+1][j+1] == "W") && pieces[i+2][j+2] == "-")){ | |
checkAllJumps(i, j, new ArrayList<Integer>(), "B"); | |
captureAvailable = true; | |
} | |
if(!captureAvailable){ | |
//check left down | |
if(i != 0 && j != 0){ | |
if(pieces[i-1][j-1]== "-"){ | |
Move newMove = new Move(); | |
List<Integer> newLocation = new ArrayList<Integer>(); | |
newLocation.add(i); | |
newLocation.add(j); | |
newLocation.add(i-1); | |
newLocation.add(j-1); | |
newMove.move = newLocation; | |
newMove.piece = "B"; | |
moves.add(newMove); | |
} | |
} | |
//check right down | |
if(i != 0 && j!= 7){ | |
if(pieces[i-1][j+1]== "-"){ | |
Move newMove = new Move(); | |
List<Integer> newLocation = new ArrayList<Integer>(); | |
newLocation.add(i); | |
newLocation.add(j); | |
newLocation.add(i-1); | |
newLocation.add(j+1); | |
newMove.move = newLocation; | |
newMove.piece = "B"; | |
moves.add(newMove); | |
} | |
} | |
//check left up | |
if(i != 7 && j!= 0){ | |
if(pieces[i+1][j-1]== "-"){ | |
Move newMove = new Move(); | |
List<Integer> newLocation = new ArrayList<Integer>(); | |
newLocation.add(i); | |
newLocation.add(j); | |
newLocation.add(i+1); | |
newLocation.add(j-1); | |
newMove.move = newLocation; | |
newMove.piece = "B"; | |
moves.add(newMove); | |
} | |
} | |
//check right up | |
if(i != 7 && j!= 7){ | |
if(pieces[i+1][j+1]== "-"){ | |
Move newMove = new Move(); | |
List<Integer> newLocation = new ArrayList<Integer>(); | |
newLocation.add(i); | |
newLocation.add(j); | |
newLocation.add(i+1); | |
newLocation.add(j+1); | |
newMove.move = newLocation; | |
newMove.piece = "B"; | |
moves.add(newMove); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
else if(player() == "White"){ | |
for(int i = 0; i < 8; i++){ | |
for(int j = 0; j < 8; j++){ | |
if(pieces[i][j] == "w"){ | |
//Check if any captures are available | |
if((i < 6 && j > 1 && (pieces[i+1][j-1] == "b" || pieces[i+1][j-1] == "B") && pieces[i+2][j-2] == "-") || (i < 6 && j < 6 && (pieces[i+1][j+1] == "b" || pieces[i+1][j+1] == "B") && pieces[i+2][j+2] == "-")){ | |
checkAllJumps(i, j, new ArrayList<Integer>(), "w"); | |
captureAvailable = true; | |
} | |
//only proceed if no captures available | |
if(!captureAvailable){ | |
//check left move | |
if(i != 7 && j!= 0){ | |
if(pieces[i+1][j-1]== "-"){ | |
Move newMove = new Move(); | |
List<Integer> newLocation = new ArrayList<Integer>(); | |
newLocation.add(i); | |
newLocation.add(j); | |
newLocation.add(i+1); | |
newLocation.add(j-1); | |
newMove.move = newLocation; | |
newMove.piece = "w"; | |
moves.add(newMove); | |
} | |
} | |
//check right move | |
if(i != 7 && j !=7){ | |
if(pieces[i+1][j+1]== "-"){ | |
Move newMove = new Move(); | |
List<Integer> newLocation = new ArrayList<Integer>(); | |
newLocation.add(i); | |
newLocation.add(j); | |
newLocation.add(i+1); | |
newLocation.add(j+1); | |
newMove.move = newLocation; | |
newMove.piece = "w"; | |
moves.add(newMove); | |
} | |
} | |
} | |
} | |
//check if any captures are available | |
else if(pieces[i][j] == "W"){ | |
if((i > 1 && j > 1 && (pieces[i-1][j-1] == "b" || pieces[i-1][j-1] == "B") && pieces[i-2][j-2] == "-") || (i > 1 && j < 6 && (pieces[i-1][j+1] == "b" || pieces[i-1][j+1] == "B") && pieces[i-2][j+2] == "-") || (i < 6 && j > 1 && (pieces[i+1][j-1] == "b" || pieces[i+1][j-1] == "B") && pieces[i+2][j-2] == "-") || (i < 6 && j < 6 && (pieces[i+1][j+1] == "b" || pieces[i+1][j+1] == "B") && pieces[i+2][j+2] == "-")){ | |
checkAllJumps(i, j, new ArrayList<Integer>(), "W"); | |
captureAvailable = true; | |
} | |
if(!captureAvailable){ | |
//check left down | |
if(i != 0 && j != 0){ | |
if(pieces[i-1][j-1]== "-"){ | |
Move newMove = new Move(); | |
List<Integer> newLocation = new ArrayList<Integer>(); | |
newLocation.add(i); | |
newLocation.add(j); | |
newLocation.add(i-1); | |
newLocation.add(j-1); | |
newMove.move = newLocation; | |
newMove.piece = "W"; | |
moves.add(newMove); | |
} | |
} | |
//check right down | |
if(i != 0 && j!= 7){ | |
if(pieces[i-1][j+1]== "-"){ | |
Move newMove = new Move(); | |
List<Integer> newLocation = new ArrayList<Integer>(); | |
newLocation.add(i); | |
newLocation.add(j); | |
newLocation.add(i-1); | |
newLocation.add(j+1); | |
newMove.move = newLocation; | |
newMove.piece = "W"; | |
moves.add(newMove); | |
} | |
} | |
//check left up | |
if(i != 7 && j!= 0){ | |
if(pieces[i+1][j-1]== "-"){ | |
Move newMove = new Move(); | |
List<Integer> newLocation = new ArrayList<Integer>(); | |
newLocation.add(i); | |
newLocation.add(j); | |
newLocation.add(i+1); | |
newLocation.add(j-1); | |
newMove.move = newLocation; | |
newMove.piece = "W"; | |
moves.add(newMove); | |
} | |
} | |
//check right up | |
if(i != 7 && j!= 7){ | |
if(pieces[i+1][j+1]== "-"){ | |
Move newMove = new Move(); | |
List<Integer> newLocation = new ArrayList<Integer>(); | |
newLocation.add(i); | |
newLocation.add(j); | |
newLocation.add(i+1); | |
newLocation.add(j+1); | |
newMove.move = newLocation; | |
newMove.piece = "W"; | |
moves.add(newMove); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
if(captureAvailable){ | |
for(int i = 0; i < captures.size(); i++){ | |
//System.out.println(captures.get(i).move); | |
} | |
return captures; | |
} | |
for(int i = 0; i < moves.size(); i++){ | |
//System.out.println(moves.get(i).move); | |
} | |
return moves; | |
} | |
@Override | |
public CheckersGameState result(Move x) { | |
List<Integer> _move = x.move; | |
for(int i = 0; i < _move.size()-2; i+=2){ | |
if((_move.get(i)-_move.get(i+2)==-2)||(_move.get(i)-_move.get(i+2)==2)){ | |
int xVal = (_move.get(i)+_move.get(i+2))/2; | |
int yVal = (_move.get(i+1)+_move.get(i+3))/2; | |
pieces[xVal][yVal]="-"; | |
} | |
} | |
pieces[_move.get(0)][_move.get(1)] = "-"; | |
if(x.piece == "b" && _move.get(_move.size()-2) == 0){ | |
pieces[_move.get(_move.size()-2)][_move.get(_move.size()-1)] = "B"; | |
} | |
else if(x.piece == "w" && _move.get(_move.size()-2) == 7){ | |
pieces[_move.get(_move.size()-2)][_move.get(_move.size()-1)] = "W"; | |
} | |
else{ | |
pieces[_move.get(_move.size()-2)][_move.get(_move.size()-1)] = x.piece; | |
} | |
if(player == "Black"){ | |
player = "White"; | |
} | |
else if(player == "White"){ | |
player = "Black"; | |
} | |
EightByEightArray newState = new EightByEightArray(pieces); | |
newState.player = player; | |
return newState; | |
} | |
@Override | |
public void printState() { | |
for(int i = 7; i > -1; i--){ | |
System.out.println(pieces[i][0] + pieces[i][1] + pieces[i][2] + pieces[i][3] + pieces[i][4] + pieces[i][5] + pieces[i][6] + pieces[i][7]); | |
} | |
} | |
} |