Skip to content
Permalink
0dff06b5b4
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
201 lines (179 sloc) 6.24 KB
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 current_player(int sq, int[] board){
return (board[sq] == this.player || board[sq] - 2 == this.player);
}
private boolean can_kill(int sq, int[] board){
return valid_square(sq) && !empty(board, sq) && !current_player(sq, board);
}
private boolean valid_square(int x){
return (x >= 0 && x <= 34 && x % 9 != 8);
}
private boolean empty(int[] board , int sq){
return board[sq] == 0;
}
private boolean can_move(int orig, int delta, int[] board){
return (valid_square(orig + delta) && empty(board, orig + delta));
}
private boolean can_jump(int orig, int delta, int[] board){
return (can_kill(orig+delta, board) && valid_square(orig + (2 * delta)) && empty(board, orig + 2 * delta));
}
private boolean any_jumps(int orig, int delta1, int delta2, int[] board, boolean king){
boolean forward = can_jump(orig, delta1, board) || can_jump(orig, delta2, board);
if(!king)
return forward;
boolean backward = can_jump(orig, delta1 * -1, board) || can_jump(orig, delta2 * -1, board);
return backward || forward;
}
public List<Move3> actions(){
LinkedList<Move3> moves = new LinkedList<Move3>();
LinkedList<Move3> jumps = new LinkedList<Move3>();
for(int i = 0; i < this.board.length; i++){
if(current_player(i, this.board)){
if(this.player == 1){
if(this.board[i] == 3){
generate_moves(i, 4, 5, moves, jumps, true);
}
else{
generate_moves(i, 4, 5, moves, jumps, false);
}
}
else{
if(this.board[i] == 4){
generate_moves(i, -4, -5, moves, jumps, true);
}
else{
generate_moves(i, -4, -5, moves, jumps,false);
}
}
}
}
if(jumps.isEmpty()){
return moves;
}
return jumps;
}
private void generate_moves(int origin, int delta1, int delta2, List<Move3> moves, List<Move3> jumps, boolean king){
calculate_jumps("" + origin, this.board, origin, delta1, delta2, jumps, king);
if(jumps.isEmpty()){
if(can_move(origin, delta1, this.board)){
String act = origin + "," + (origin + delta1);
moves.add(new Move3(act));
}
if(can_move(origin, delta2, this.board)){
String act = origin + "," + (origin + delta2);
moves.add(new Move3(act));
}
if(king && can_move(origin, -1 * delta1, this.board)){
String act = origin + "," + (origin - delta1);
moves.add(new Move3(act));
}
if(king && can_move(origin, -1 * delta2, this.board)){
String act = origin + "," + (origin - delta2);
moves.add(new Move3(act));
}
}
}
private void calculate_jumps(String path, int[] b, int orig, int delta1, int delta2, List<Move3> jumps, boolean king){
if(!any_jumps(orig, delta1, delta2, b, king)){
if(path.contains(",")){
jumps.add(new Move3(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, jumps, 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, jumps, 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, jumps, 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, jumps, king);
}
}
CheckersGameState3 result(Move3 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");
}
}