Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'master' into joe
  • Loading branch information
joesweeney committed Apr 4, 2017
2 parents 9144ec8 + 7202456 commit 5bf801c
Show file tree
Hide file tree
Showing 5 changed files with 360 additions and 32 deletions.
96 changes: 82 additions & 14 deletions 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;
Expand All @@ -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";
}
Expand Down Expand Up @@ -70,9 +129,9 @@ public class CheckersGameState3{
return backward || forward;
}

public List<Move3> actions(){
LinkedList<Move3> moves = new LinkedList<Move3>();
LinkedList<Move3> jumps = new LinkedList<Move3>();
public List<Move> actions(){
LinkedList<Move> moves = new LinkedList<Move>();
LinkedList<Move> jumps = new LinkedList<Move>();
for(int i = 0; i < this.board.length; i++){
if(current_player(i, this.board)){
if(this.player == 1){
Expand Down Expand Up @@ -100,7 +159,7 @@ public class CheckersGameState3{
}


private void generate_moves(int origin, int delta1, int delta2, List<Move3> moves, List<Move3> jumps, boolean king){
private void generate_moves(int origin, int delta1, int delta2, List<Move> moves, List<Move> jumps, boolean king){
calculate_jumps("" + origin, this.board, origin, delta1, delta2, jumps, king);
if(jumps.isEmpty()){
if(can_move(origin, delta1, this.board)){
Expand All @@ -123,7 +182,7 @@ public class CheckersGameState3{
}


private void calculate_jumps(String path, int[] b, int orig, int delta1, int delta2, List<Move3> jumps, boolean king){
private void calculate_jumps(String path, int[] b, int orig, int delta1, int delta2, List<Move> jumps, boolean king){
if(!any_jumps(orig, delta1, delta2, b, king)){
if(path.contains(",")){
jumps.add(new Move3(path));
Expand All @@ -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++){
Expand Down
4 changes: 2 additions & 2 deletions 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
Expand Down
71 changes: 55 additions & 16 deletions src/Move3.java
@@ -1,20 +1,20 @@
public class Move3{
public class Move3 implements Move{

int origin, destination;
int src, dest;
String[] steps;
int[] kills;
String check;

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;
}
Expand All @@ -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;
}

Expand All @@ -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<steps.length; i++) {
output += i!=0 ? ":" : "";
output += convert(shift(Integer.parseInt(steps[i])));
}
return output;
}
}
80 changes: 80 additions & 0 deletions src/Test.java
@@ -0,0 +1,80 @@
import java.util.Arrays;

public class Test{

public static void main(String[] args){
String[] b = {"-", "b", "-", "b", "-", "b", "-", "b",
"b", "-", "b", "-", "b", "-", "b", "-",
"-", "b", "-", "b", "-", "b", "-", "b",
"-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-",
"w", "-", "w", "-", "w", "-", "w", "-",
"-", "w", "-", "w", "-", "w", "-", "w",
"w", "-", "w", "-", "w", "-", "w", "-"};

String[] b2 = {"-", "-", "-", "b", "-", "b", "-", "b",
"b", "-", "w", "-", "b", "-", "b", "-",
"-", "-", "-", "B", "-", "-", "-", "b",
"-", "-", "w", "-", "w", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-",
"w", "-", "w", "-", "w", "-", "w", "-",
"-", "w", "-", "-", "-", "w", "-", "w",
"w", "-", "w", "-", "w", "-", "w", "-"};

String[] b3 = {"-", "-", "-", "b", "-", "b", "-", "b",
"b", "-", "b", "-", "b", "-", "b", "-",
"-", "b", "-", "w", "-", "b", "-", "b",
"-", "-", "b", "-", "w", "-", "-", "-",
"-", "-", "-", "b", "-", "-", "-", "-",
"w", "-", "b", "-", "w", "-", "w", "-",
"-", "w", "-", "-", "-", "w", "-", "w",
"-", "-", "w", "-", "w", "-", "w", "-"};

String[] b4 = {"-", "-", "-", "b", "-", "b", "-", "b",
"b", "-", "-", "-", "b", "-", "b", "-",
"-", "-", "-", "B", "-", "-", "-", "b",
"-", "-", "w", "-", "w", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-",
"w", "-", "w", "-", "w", "-", "w", "-",
"-", "w", "-", "-", "-", "w", "-", "w",
"w", "-", "w", "-", "w", "-", "w", "-"};



CheckersGameState s1 = new CheckersGameState3(1, b);
CheckersGameState s2 = new CheckersGameState3(2, b);
CheckersGameState s3 = new CheckersGameState3(1, b2);
CheckersGameState s4 = new CheckersGameState3(2, b2);
CheckersGameState s5 = new CheckersGameState3(1, b3);
CheckersGameState s6 = new CheckersGameState3(2, b3);
CheckersGameState s7 = new CheckersGameState3(1, b4);
System.out.println("State 1");
printMoves(s1);
System.out.println("State 2");
printMoves(s2);
System.out.println("State 3");
printMoves(s3);
System.out.println("State 4");
printMoves(s4);
System.out.println("State 5");
printMoves(s5);
System.out.println("Result of State 5");
s5.result(s5.actions().get(0)).printState();
System.out.println("State 6");
printMoves(s6);
System.out.println("Result of State 6");
s6.result(s6.actions().get(0)).printState();
System.out.println("State 7");
printMoves(s7);
System.out.println("Result of State 7");
s7.result(s7.actions().get(0)).printState();
}

static void printMoves(CheckersGameState s){
s.printState();
for(Move m: s.actions()){
System.out.println(m);
}
System.out.println();
}
}

0 comments on commit 5bf801c

Please sign in to comment.