diff --git a/src/CheckersGameState3.java b/src/CheckersGameState3.java index 3b6ba66..c46313b 100644 --- a/src/CheckersGameState3.java +++ b/src/CheckersGameState3.java @@ -1,7 +1,7 @@ import java.util.List; import java.util.LinkedList; -public class CheckersGameState3{ +public class CheckersGameState3 implements CheckersGameState{ int player; int[] board; @@ -11,7 +11,66 @@ public class CheckersGameState3{ this.board = board; } - String player(){ + public CheckersGameState3(int player, String[] board){ + this.player = player; + this.board = to_array(board); + } + + public int convert(String s){ + if(s.equals("-")){ + return 0; + } + else if(s.equals("b")){ + return 1; + } + else if(s.equals("w")){ + return 2; + } + else if(s.equals("B")){ + return 3; + } + else{ + return 4; + } + } + + public int[] to_array(String[] board){ + int[] b = new int[35]; + int i = 0; + int j = 0; + int added = 0; + boolean leading = false; + while(i < 35 ){ + if(i % 9 == 8){ + b[i] = 0; + } + else if(leading){ + b[i] = convert(board[j]); + } + else{ + b[i] =convert(board[j+1]); + } + if(i % 9 == 8){ + + i++; + continue; + } + j = j + 2; + i++; + added++; + if(added == 4){ + added = 0; + leading = !leading; + } + if(j == 35){ + added =0; + leading = false; + } + } + return b; + } + + public String player(){ if(this.player == 1){ return "black"; } @@ -70,9 +129,9 @@ public class CheckersGameState3{ return backward || forward; } - public List actions(){ - LinkedList moves = new LinkedList(); - LinkedList jumps = new LinkedList(); + public List actions(){ + LinkedList moves = new LinkedList(); + LinkedList jumps = new LinkedList(); for(int i = 0; i < this.board.length; i++){ if(current_player(i, this.board)){ if(this.player == 1){ @@ -100,7 +159,7 @@ public class CheckersGameState3{ } - private void generate_moves(int origin, int delta1, int delta2, List moves, List jumps, boolean king){ + private void generate_moves(int origin, int delta1, int delta2, List moves, List jumps, boolean king){ calculate_jumps("" + origin, this.board, origin, delta1, delta2, jumps, king); if(jumps.isEmpty()){ if(can_move(origin, delta1, this.board)){ @@ -123,7 +182,7 @@ public class CheckersGameState3{ } - private void calculate_jumps(String path, int[] b, int orig, int delta1, int delta2, List jumps, boolean king){ + private void calculate_jumps(String path, int[] b, int orig, int delta1, int delta2, List jumps, boolean king){ if(!any_jumps(orig, delta1, delta2, b, king)){ if(path.contains(",")){ jumps.add(new Move3(path)); @@ -134,48 +193,57 @@ public class CheckersGameState3{ int jump = orig + 2 * delta1; int[] b2 = b.clone(); b2[orig + delta1] = 0; + b2[orig + 2 * delta1] = b2 [orig]; + b2[orig] = 0; calculate_jumps(path + "," + jump, b2, jump, delta1, delta2, jumps, king); } if(can_jump(orig, delta2, b)){ int jump = orig + 2 * delta2; int[] b3 = b.clone(); b3[orig + delta2] = 0; + b3[orig + 2 * delta2] = b3[orig]; + b3[orig] = 0; calculate_jumps(path + "," + jump, b3, jump, delta1, delta2, jumps, king); } if(king && can_jump(orig, -1 * delta1, b)){ int jump = orig + -2 * delta1; int[] b4 = b.clone(); b4[orig + (-1 * delta1)] = 0; + b4[orig + (-2 * delta1)] = b4[orig]; + b4[orig] = 0; calculate_jumps(path + "," + jump, b4, jump, delta1, delta2, jumps, king); } if(king && can_jump(orig, -1 * delta2, b)){ int jump = orig + -2 * delta2; int[] b5 = b.clone(); b5[orig + -1 * delta2] = 0; + b5[orig + (-2 * delta2)] = b5[orig]; + b5[orig] = 0; calculate_jumps(path + "," + jump, b5, jump, delta1, delta2, jumps, king); } } - CheckersGameState3 result(Move3 x){ + public 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){ + int type = this.board[x.source()]; + newState[x.source()] = 0; + newState[x.destination()] = type; + 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()){ + if(x.captures() != null){ + for(int k: x.captures()){ newState[k] = 0; } } return new CheckersGameState3(1 - this.player, newState); } - void printState(){ + public void printState(){ boolean leading = false; int printed = 0; for(int i = 0; i < this.board.length; i++){ diff --git a/src/Move.java b/src/Move.java index 41820a8..6f0e41b 100644 --- a/src/Move.java +++ b/src/Move.java @@ -1,7 +1,7 @@ public interface Move { - // Starting and ending Positions - public int src, dest; + public int source(); + public int destination(); // Returns a list of captured positions public int[] captures(); // Returns the string representation of a move diff --git a/src/Move3.java b/src/Move3.java index da66283..5c89bd3 100644 --- a/src/Move3.java +++ b/src/Move3.java @@ -1,6 +1,6 @@ -public class Move3{ +public class Move3 implements Move{ - int origin, destination; + int src, dest; String[] steps; int[] kills; String check; @@ -8,13 +8,13 @@ public class Move3{ public Move3(String steps){ String[] s = steps.split(","); this.steps = s; - this.origin = Integer.parseInt(s[0]); - this.destination = Integer.parseInt(s[s.length - 1]); + this.src = Integer.parseInt(s[0]); + this.dest = Integer.parseInt(s[s.length - 1]); kills = calculate_kills(s); } public int[] calculate_kills(String[] steps){ - int diff = this.origin - this.destination; + int diff = this.src - this.dest; if(Math.abs(diff) == 4 || Math.abs(diff) == 5){ return null; } @@ -25,15 +25,15 @@ public class Move3{ return k; } - public int origin(){ - return origin; + public int source(){ + return src; } public int destination(){ - return destination; + return dest; } - public int[] kills(){ + public int[] captures(){ return kills; } @@ -47,14 +47,53 @@ public class Move3{ // 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] + ")"; - } + + private String convert(int position) { + // Converts a position to the correct String + int row = position / 4; + int col = (position % 4) * 2; + if(row % 2 == 0){ + col++; } - return move; + return "("+(7-row)+":"+col+")"; } + private int shift(int position){ + int row, column; + int new_pos = position; + if(position >= 27){ + return (position - 3); + } + else if(position >= 18){ + return (position - 2); + } + else if(position >= 9){ + return (position - 1); + } + else{ + return position; + } + } + + + + +// 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; +// } +// + public String toString() { + String output = ""; + for(int i = 0; i