Skip to content
Permalink
Browse files

Change client so it actually plays using minimax

  • Loading branch information
joesweeney committed Apr 17, 2017
1 parent 6321c44 commit cef40a57cff773e5eaa1ae92c9835db776f6cc36
Showing with 84 additions and 12 deletions.
  1. +1 −1 src/CheckersAI.java
  2. +14 −0 src/CheckersGameState3.java
  3. +69 −11 src/RmCheckersClient.java
@@ -19,7 +19,7 @@ public Move minimax(CheckersGameState s, int ply){
double v = Double.NEGATIVE_INFINITY;
double check;
Move max = null;
System.out.println(s.actions().size());
// System.out.println(s.actions().size());
for(Move a: s.actions()){
check = minValue(s.result(a), alpha, beta, ply, depth + 1);
if(check > v){
@@ -11,6 +11,20 @@ public CheckersGameState3(int player, int[] board){
this.board = board;
}

public CheckersGameState3() {
this.player = 1;
this.board = new int[]{
1,1,1,1,
1,1,1,1,0,
1,1,1,1,
0,0,0,0,0,
0,0,0,0,
2,2,2,2,0,
2,2,2,2,
2,2,2,2
};
}

public CheckersGameState3(int player, String[] board){
this.player = player;
this.board = to_array(board);
@@ -46,8 +46,14 @@
protected String _gameID;
protected String _myColor;

public Evaluator e;
public CheckersAI ai;
public CheckersGameState currentState;

public RmCheckersClient(){
_socket = openSocket();
e = new Evaluator00();
currentState = new CheckersGameState3();
}

public Socket getSocket(){
@@ -95,23 +101,27 @@ public static void main(String[] argv){

myClient.setGameID(myClient.readAndEcho()); // game
myClient.setColor(myClient.readAndEcho().substring(6,11)); // color
System.out.println("I am playing as "+myClient.getColor()+ " in game number "+ myClient.getGameID());
readMessage = myClient.readAndEcho();
System.out.println("I am playing as "+myClient.getColor()+ " in "+ myClient.getGameID());
// readMessage = myClient.readAndEcho();
// depends on color--a black move if i am white, Move:Black:i:j
// otherwise a query to move, ?Move(time):
if (myClient.getColor().equals("White")) {
readMessage = myClient.readAndEcho(); // move query
myClient.writeMessageAndEcho("(2:4):(3:5)");
readMessage = myClient.readAndEcho(); // white move
readMessage = myClient.readAndEcho(); // black move
readMessage = myClient.readAndEcho(); // move query
myClient.ai = new CheckersAI(myClient.e, 2);
myClient.playGame(2);
// readMessage = myClient.readAndEcho(); // move query
// myClient.writeMessageAndEcho("(2:4):(3:5)");
// readMessage = myClient.readAndEcho(); // white move
// readMessage = myClient.readAndEcho(); // black move
// readMessage = myClient.readAndEcho(); // move query
// here you would need to move again
}
else {
myClient.writeMessageAndEcho("(5:3):(4:4)");
readMessage = myClient.readAndEcho(); // black move
readMessage = myClient.readAndEcho(); // white move
readMessage = myClient.readAndEcho(); // move query
myClient.ai = new CheckersAI(myClient.e, 1);
myClient.playGame(1);
// myClient.writeMessageAndEcho("(5:3):(4:4)");
// readMessage = myClient.readAndEcho(); // move query
// readMessage = myClient.readAndEcho(); // black move
// readMessage = myClient.readAndEcho(); // white move
// here you would need to move again
}

@@ -122,6 +132,54 @@ public static void main(String[] argv){
}
}

public void playGame(int player) {
try {
String msg = readAndEcho(); // initial message
if(player == 1) { // black
// dont do anything cause it was a move query
}
else if(player == 2) { // white
// first apply blacks move then read move query
applyMove(parseMove(msg));
readAndEcho(); // move query
}
while(currentState.actions().size()>0){
Move myMove = ai.minimax(currentState, 3);
writeMessageAndEcho(myMove.toString());
if(!applyMove(myMove.toString())) {
System.out.println("couldn't apply my move");
break;
}
msg = readAndEcho(); // my move
msg = readAndEcho(); // their move
boolean success = applyMove(parseMove(msg)); // apply their move
if(!success){
System.out.println("couldn't apply their move");
break;
}
msg = readAndEcho(); // move query
}
} catch (IOException e) {
System.out.println("Failed in read/close");
System.exit(1);
}
}

public String parseMove(String msg) {
return msg.substring(11,msg.length());
}

public boolean applyMove(String move) {
for(Move m : currentState.actions()) {
System.out.println(move+" equals? "+m.toString());
if(move.equals(m.toString())) {
currentState = currentState.result(m);
return true;
}
}
return false;
}

public String readAndEcho() throws IOException
{
String readMessage = _in.readLine();

0 comments on commit cef40a5

Please sign in to comment.
You can’t perform that action at this time.