Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Change client so it actually plays using minimax
  • Loading branch information
joesweeney committed Apr 17, 2017
1 parent 6321c44 commit cef40a5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/CheckersAI.java
Expand Up @@ -19,7 +19,7 @@ public class CheckersAI{
double v = Double.NEGATIVE_INFINITY; double v = Double.NEGATIVE_INFINITY;
double check; double check;
Move max = null; Move max = null;
System.out.println(s.actions().size()); // System.out.println(s.actions().size());
for(Move a: s.actions()){ for(Move a: s.actions()){
check = minValue(s.result(a), alpha, beta, ply, depth + 1); check = minValue(s.result(a), alpha, beta, ply, depth + 1);
if(check > v){ if(check > v){
Expand Down
14 changes: 14 additions & 0 deletions src/CheckersGameState3.java
Expand Up @@ -11,6 +11,20 @@ public class CheckersGameState3 implements CheckersGameState{
this.board = 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){ public CheckersGameState3(int player, String[] board){
this.player = player; this.player = player;
this.board = to_array(board); this.board = to_array(board);
Expand Down
80 changes: 69 additions & 11 deletions src/RmCheckersClient.java
Expand Up @@ -46,8 +46,14 @@ public class RmCheckersClient {
protected String _gameID; protected String _gameID;
protected String _myColor; protected String _myColor;


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

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


public Socket getSocket(){ public Socket getSocket(){
Expand Down Expand Up @@ -95,23 +101,27 @@ public class RmCheckersClient {


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


Expand All @@ -122,6 +132,54 @@ public class RmCheckersClient {
} }
} }


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 public String readAndEcho() throws IOException
{ {
String readMessage = _in.readLine(); String readMessage = _in.readLine();
Expand Down

0 comments on commit cef40a5

Please sign in to comment.