diff --git a/CheckersGameState3.java b/CheckersGameState3.java new file mode 100644 index 0000000..5cc4446 --- /dev/null +++ b/CheckersGameState3.java @@ -0,0 +1,188 @@ +import java.util.List; +import java.util.LinkedList; + +public class CheckersGameState3{ + + int player; + int[] board; + + public CheckersGameState3(int player, int[] board){ + this.player = player; + this.board = board; + } + + String player(){ + if(this.player == 1){ + return "black"; + } + else{ + return "white"; + } + } + + private String character(int p){ + if(p == 0){ + return "-"; + } + else if (p==1){ + return "b"; + } + else if (p==2){ + return "w"; + } + else if (p==3){ + return "B"; + } + else{ + return "W"; + } + } + + private boolean equal(int pos){ + return (pos == this.player || pos - 2 == this.player); + } + + private boolean within_bounds(int x){ + return (x >= 0 && x <= 34 && x % 9 != 8); + } + + private boolean can_move(int orig, int delta){ + return (within_bounds(orig + delta) && this.board[orig + delta] == 0); + } + + private boolean can_jump(int orig, int delta, int[] board){ + return (within_bounds(orig + delta) && !equal(board[orig + delta]) && board[orig + delta] != 0 && within_bounds(orig + 2 * delta) && board[orig + 2 * delta] == 0); + } + + public List actions(){ + LinkedList actions = new LinkedList(); + for(int i = 0; i < this.board.length; i++){ + if(this.board[i] == this.player || this.board[i] == (this.player + 2)){ + if(this.player == 1){ + if(this.board[i] == 3){ + generate_moves(i, -4, -5, actions, true); + generate_moves(i, 4, 5, actions, true); + } + else{ + generate_moves(i, 4, 5, actions, false); + } + } + else{ + if(this.board[i] == 4){ + generate_moves(i, 4, 5, actions, true); + generate_moves(i, -4, -5, actions, true); + } + else{ + generate_moves(i, -4, -5, actions, false); + } + } + } + } + return actions; + } + + + private void generate_moves(int origin, int delta1, int delta2, List actions, boolean king){ + if(can_move(origin, delta1)){ + String act = origin + "," + (origin + delta1); + actions.add(new Move(act)); + } + else if(can_jump(origin, delta1, this.board)){ + int jump = origin + 2 * delta1; + int[] b2 = this.board.clone(); + b2[origin + delta1] = 0; + String act = origin + "," + jump; + calculate_jumps(act, b2, jump, delta1, delta2, actions, king); + } + if(can_move(origin, delta2)){ + String act = origin + "," + (origin + delta2); + actions.add(new Move(act)); + } + else if(can_jump(origin, delta2, this.board)){ + int jump = origin + 2 * delta2; + String act = origin + "," + jump; + int[] b2 = this.board.clone(); + b2[origin + delta2] = 0; + calculate_jumps(origin + "," + jump, b2, jump, delta1, delta2, actions, king); + } + } + + + private void calculate_jumps(String path, int[] b, int orig, int delta1, int delta2, List actions, boolean king){ + if(!can_jump(orig, delta1, b) && !can_jump(orig, delta2, b) && !king){ + actions.add(new Move(path)); + return; + } + if(king && !can_jump(orig, delta1, b) && !can_jump(orig, delta2, b) && !can_jump(orig, -1 * delta1, b) && !can_jump(orig, -1 * delta2, b)){ + actions.add(new Move(path)); + return; + } + if(can_jump(orig, delta1, b)){ + int jump = orig + 2 * delta1; + int[] b2 = b.clone(); + b2[orig + delta1] = 0; + calculate_jumps(path + "," + jump, b2, jump, delta1, delta2, actions, king); + } + if(can_jump(orig, delta2, b)){ + int jump = orig + 2 * delta2; + int[] b3 = b.clone(); + b3[orig + delta2] = 0; + calculate_jumps(path + "," + jump, b3, jump, delta1, delta2, actions, king); + } + if(king && can_jump(orig, -1 * delta1, b)){ + int jump = orig + -2 * delta1; + int[] b4 = b.clone(); + b4[orig + (-1 * delta1)] = 0; + calculate_jumps(path + "," + jump, b4, jump, delta1, delta2, actions, king); + } + if(king && can_jump(orig, -1 * delta2, b)){ + int jump = orig + -2 * delta2; + int[] b5 = b.clone(); + b5[orig + -1 * delta2] = 0; + calculate_jumps(path + "," + jump, b5, jump, delta1, delta2, actions, king); + } + } + + + CheckersGameState3 result(Move x){ + int[] newState = this.board.clone(); + newState[x.destination()] = this.board[x.origin()]; + newState[x.origin()] = 0; + if(x.destination < 4 && this.player == 2){ + newState[x.destination()] = 4; + } + else if(x.destination() > 30 && this.player == 1){ + newState[x.destination()] = 3; + } + if(x.kills() != null){ + for(int k: x.kills()){ + newState[k] = 0; + } + } + return new CheckersGameState3(1 - this.player, newState); + } + + void printState(){ + boolean leading = false; + int printed = 0; + for(int i = 0; i < this.board.length; i++){ + if(i % 9 != 8){ + if(leading){ + System.out.print(character(this.board[i]) + "-"); + } + else{ + System.out.print("-" + character(this.board[i])); + } + printed++; + } + if(printed == 4){ + leading = !leading; + printed = 0; + System.out.print("\n"); + } + } + System.out.print("\n"); + } +} + + diff --git a/Move.java b/Move.java new file mode 100644 index 0000000..a46c70b --- /dev/null +++ b/Move.java @@ -0,0 +1,60 @@ +public class Move{ + + int origin, destination; + String[] steps; + int[] kills; + String check; + + public Move(String steps){ + String[] s = steps.split(","); + this.steps = s; + this.origin = Integer.parseInt(s[0]); + this.destination = Integer.parseInt(s[s.length - 1]); + kills = calculate_kills(s); + } + + public int[] calculate_kills(String[] steps){ + int diff = this.origin - this.destination; + if(Math.abs(diff) == 4 || Math.abs(diff) == 5){ + return null; + } + int[] k = new int[steps.length - 1]; + for(int i = 0; i < steps.length - 1; i++){ + k[i] = (Integer.parseInt(steps[i]) + Integer.parseInt(steps[i+1]))/2; + } + return k; + } + + public int origin(){ + return origin; + } + + public int destination(){ + return destination; + } + + public int[] kills(){ + return kills; + } + +// private String convert(int pos){ +// int x; +// int y; +// if(pos < 8){ +// int x = 2 * p + 1; +// int y = pos / 4; +// } +// else if(pos < 17){ +// int x = +// + public String toString(){ + String move = "(" + this.steps[0] + ":" + this.steps[1] + ")"; + if(this.steps.length > 2){ + for(int i = 1; i < this.steps.length - 1; i++){ + move += ":(" + this.steps[i] + ":" + this.steps[i+1] + ")"; + } + } + return move; + } + +}