Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added server communication and execution loop to listen()
Listen now takes a username, password and opponent option as arguments

Edited makeMove() to accept an arraylist of moves, and make them. Needs
review to be sure that it functions as intended, as well as make sure
that it can move first as well. Easy as just taking out the null
stipulations?

Edited Player to allow for changing color of the player, since it's
determined by the server.

NOTE: I don't know if I got better or the AI got worse, but I can beat
it pretty easily
  • Loading branch information
cdk10001 committed May 2, 2016
1 parent 9b207cf commit 8151371
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/controller/CheckersMain.java
Expand Up @@ -61,7 +61,7 @@ public class CheckersMain {

@Override
public void run() {
server.listen();
server.listen("1", "010101", "2");
}

}).start();
Expand Down
6 changes: 6 additions & 0 deletions src/player/Player.java
Expand Up @@ -18,6 +18,12 @@ public class Player {
this.inJumpSequence = false;
}

//Sorry that I had to make this, it's needed to make life easier on
//The server side
public void setColor(Color newColor){
this.color = newColor;
}

public boolean isInJumpSequence() {
return inJumpSequence;
}
Expand Down
132 changes: 106 additions & 26 deletions src/player/ServerPlayer.java
@@ -1,50 +1,51 @@
package player;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import controller.Game;
import model.Board;
import model.Color;
import model.Move;
import controller.Communication;

public class ServerPlayer extends Player {
private Game game;
private static String IP = "137.99.164.83";
private static int PORT = 3499;
private Socket _socket = null;
private PrintWriter _out = null;
private BufferedReader _in = null;

public ServerPlayer(Color color, Board board, Game game) {
super(color, board);
this.game = game;
}

public ArrayList<Move> makeMove(Move move) {
/* If this is the first jump of the jump sequence */
if (move.isJump() && !inJumpSequence) {
inJumpSequence = true;
}

if (board.movePromotesPiece(move)) {
inJumpSequence = false;
}

/* Request to move the piece */
game.requestMove(move);

if(getAvailableMoves(move.destination).isEmpty()) {
inJumpSequence = false;
public ArrayList<Move> makeMove(ArrayList<Move> moves) {
//Does full sequence of moves, then computer responds. This might cause bugs
for (Move move : moves){
game.requestMove(move);
}

ArrayList<Move> computerMoves = new ArrayList<Move>();

/* Make the computer move */
Move computerMove = game.makeComputerMove();

if (move != null) {
if (moves != null) {
computerMoves.add(computerMove);
}

/* While the computer can still move, ask it to move */
while (game.getComputer().isInJumpSequence()) {
computerMove = game.makeComputerMove();
if (move != null) {
if (moves != null) {
computerMoves.add(computerMove);
}
}
Expand All @@ -53,16 +54,59 @@ public class ServerPlayer extends Player {
return computerMoves;
}

public void listen() {
/* Test code */
while(true) {
System.out.println("Listening");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
public void listen(String name, String pass, String opponent) {
_socket = openSocket();
ArrayList<Move> moves;
ArrayList<Move> computerMoves;
try{
readAndEcho(); //Start message
readAndEcho(); //User query
writeMessageAndEcho(name); //Send Username
readAndEcho(); //Password query
writeMessageAndEcho(pass); //Send Password
readAndEcho(); //Opponent query
writeMessageAndEcho(opponent); //Send opponent
System.out.println("Waiting on opponent: " + opponent);
String gameID = (readAndEcho().substring(5,9)); //Game ID
String selectedColor = readAndEcho().substring(6,11); //Color
System.out.println("I am playing as "+color+" in game number "+ gameID);
if (selectedColor.equalsIgnoreCase("white")){ //Change colors accordingly
this.setColor(color.BLACK); //Almost caused a bug here. whoops!
game.getComputer().setColor(color.WHITE);
//Proceed to main loop
}
else{
this.setColor(color.WHITE);
game.getComputer().setColor(color.BLACK);
computerMoves = makeMove(null); //Act first, probably won't work now
String moveString = Communication.moveToString(computerMoves); //Put move into string
readAndEcho(); //Read move query
writeMessageAndEcho(moveString); //Send move string
readAndEcho();
}

boolean over = false;
//Main execution loop, stops when someone wins. Will probably need to write in a Tie recognizer
//Because the AI likes to dance in the corner
while(!over) { //While game isn't over
String moveMsg = readAndEcho(); //Move query or result
if (moveMsg.contains("Result")){
over = true; //Cut it if it's result
}
else {
moves = Communication.stringToMove(moveMsg); //Otherwise update board
computerMoves = makeMove(moves); //Make moves and computer moves
String moveString = Communication.moveToString(computerMoves);
readAndEcho(); //Read move query
writeMessageAndEcho(moveString); //Send moves
readAndEcho(); //Read the move echo'd back
}

}
}
catch (Exception e){
System.out.println("Exception occured:\n" + e);
System.exit(1);
}
/* Listen to server and wait for incoming messages.
* and GameConstants.SERVER_COLOR according to who the server says
Expand All @@ -82,4 +126,40 @@ public class ServerPlayer extends Player {
* the string to send to the server;
*/
}

public String readAndEcho() throws IOException
{
String readMessage = _in.readLine();
System.out.println("read: "+readMessage);
return readMessage;
}

public void writeMessage(String message) throws IOException
{
_out.print(message+"\r\n");
_out.flush();
}

public void writeMessageAndEcho(String message) throws IOException
{
_out.print(message+"\r\n");
_out.flush();
System.out.println("sent: "+ message);
}

public Socket openSocket(){
//Create socket connection, adapted from Sun example
try{
_socket = new Socket(IP, PORT);
_out = new PrintWriter(_socket.getOutputStream(), true);
_in = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
} catch (UnknownHostException e) {
System.out.println("Unknown host: " + IP);
System.exit(1);
} catch (IOException e) {
System.out.println("No I/O");
System.exit(1);
}
return _socket;
}
}

1 comment on commit 8151371

@Aaron
Copy link
Collaborator

@Aaron Aaron commented on 8151371 May 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Please sign in to comment.