Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improve client to now specify user and opponent in args
  • Loading branch information
joesweeney committed Apr 19, 2017
1 parent 60763a3 commit aa8c3ce
Showing 1 changed file with 62 additions and 39 deletions.
101 changes: 62 additions & 39 deletions src/RmCheckersClient.java
Expand Up @@ -32,11 +32,12 @@ import java.net.*;


public class RmCheckersClient { public class RmCheckersClient {


protected final static String _user1 = "9"; public final static String _user1 = "9";
protected final static String _password1 = "919747"; public final static String _password1 = "919747";
protected final static String _user2 = "10"; public final static String _user2 = "10";
protected final static String _password2 = "183220"; public final static String _password2 = "183220";
protected final static String _opponent = "0"; public String user, password,opponent;
public final static String _opponent = "0";
protected final String _machine = "icarus.engr.uconn.edu"; protected final String _machine = "icarus.engr.uconn.edu";
protected int _port = 3499; protected int _port = 3499;
protected Socket _socket = null; protected Socket _socket = null;
Expand All @@ -54,6 +55,18 @@ public class RmCheckersClient {
_socket = openSocket(); _socket = openSocket();
e = new Evaluator00(); e = new Evaluator00();
currentState = new CheckersGameState3(); currentState = new CheckersGameState3();
user = _user1;
password = _password1;
opponent = _opponent;
}

public RmCheckersClient(int player, String opponent){
_socket = openSocket();
e = new Evaluator00();
currentState = new CheckersGameState3();
user = player==1 ? _user1 : _user2;
password = player==1 ? _password1 : _password2;
this.opponent = opponent;
} }


public Socket getSocket(){ public Socket getSocket(){
Expand Down Expand Up @@ -84,47 +97,49 @@ public class RmCheckersClient {
return _myColor; return _myColor;
} }


public static void main(String[] argv){ public int connectGetPlayer() {
String readMessage; String readMessage;
RmCheckersClient myClient = new RmCheckersClient();


try{ try{
myClient.readAndEcho(); // start message this.readAndEcho(); // start message
myClient.readAndEcho(); // ID query this.readAndEcho(); // ID query
myClient.writeMessageAndEcho(_user1); // user ID this.writeMessageAndEcho(this.user); // user ID


myClient.readAndEcho(); // password query this.readAndEcho(); // password query
myClient.writeMessage(_password1); // password this.writeMessage(this.password); // password


myClient.readAndEcho(); // opponent query this.readAndEcho(); // opponent query
myClient.writeMessageAndEcho(_opponent); // opponent this.writeMessageAndEcho(this.opponent); // opponent


myClient.setGameID(myClient.readAndEcho()); // game this.setGameID(this.readAndEcho()); // game
myClient.setColor(myClient.readAndEcho().substring(6,11)); // color this.setColor(this.readAndEcho().substring(6,11)); // color
System.out.println("I am playing as "+myClient.getColor()+ " in "+ myClient.getGameID()); System.out.println("I am playing as "+this.getColor()+ " in "+ this.getGameID());
// readMessage = myClient.readAndEcho(); if (this.getColor().equals("White")) {
// depends on color--a black move if i am white, Move:Black:i:j this.ai = new CheckersAI(this.e, 2);
// otherwise a query to move, ?Move(time): return 2;
if (myClient.getColor().equals("White")) {
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 { else {
myClient.ai = new CheckersAI(myClient.e, 1); this.ai = new CheckersAI(this.e, 1);
myClient.playGame(1); return 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
} }
} catch (IOException e) {
System.out.println("Failed in read/close");
return 0;
}
}


public static void main(String[] argv){
String readMessage;
if(argv.length != 2) {
System.out.println("error! please specify your player(1 or 2) and the opponent");
return;
}
int myUser = Integer.parseInt(argv[0]);
RmCheckersClient myClient = new RmCheckersClient(myUser, argv[1]);

try{
int player = myClient.connectGetPlayer();
myClient.playGame(player);
myClient.getSocket().close(); myClient.getSocket().close();
} catch (IOException e) { } catch (IOException e) {
System.out.println("Failed in read/close"); System.out.println("Failed in read/close");
Expand Down Expand Up @@ -153,12 +168,20 @@ public class RmCheckersClient {
} }
msg = readAndEcho(); // my move msg = readAndEcho(); // my move
msg = readAndEcho(); // their move msg = readAndEcho(); // their move
if(msg.contains("Result")) {
System.out.println("Done.");
break;
}
boolean success = applyMove(parseMove(msg)); // apply their move boolean success = applyMove(parseMove(msg)); // apply their move
if(!success){ if(!success){
System.out.println("couldn't apply their move"); System.out.println("couldn't apply their move");
break; break;
} }
msg = readAndEcho(); // move query msg = readAndEcho(); // move query
if(msg.contains("Result")) {
System.out.println("Done.");
break;
}
} }
} catch (IOException e) { } catch (IOException e) {
System.out.println("Failed in read/close"); System.out.println("Failed in read/close");
Expand Down

0 comments on commit aa8c3ce

Please sign in to comment.