From 1dae33800e9f263aeb3899130d3f106cd8839cf8 Mon Sep 17 00:00:00 2001 From: Alexander Huddleston Date: Tue, 3 Nov 2015 23:24:42 -0600 Subject: [PATCH] Final finishing touches on Client vs. Client. --- Client.java | 15 +++++++++++---- Parser.cpp | 7 ++++--- Server.cpp | 8 +++++--- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Client.java b/Client.java index 7a46775..33f4fef 100755 --- a/Client.java +++ b/Client.java @@ -318,9 +318,9 @@ public class Client { output.println(userInput); String diff = "?"; - in.read(b, 0, 256); - temp = String.valueOf(b).trim(); - if(gameMode != 3) { + if(gameMode != 3) { + in.read(b, 0, 256); + temp = String.valueOf(b).trim(); while(!diff.equals("1") && !diff.equals("2") && !diff.equals("3")) { diff = JOptionPane.showInputDialog(frame, temp);//diff = stdIn.readLine(); //diff = diff.toUpperCase(); @@ -337,8 +337,8 @@ public class Client { else if(diff.equals("3")) { userInput = "HARD"; } + output.println(userInput); } - output.println(userInput); } else { gameMode = 3; @@ -362,6 +362,13 @@ public class Client { while(!end && boardoutput.length() < 4) { if(in.ready()) { in.read(buffer, 0, 256); + for(int i = 0; i < buffer.length; ++i) { + if(Arrays.equals(Arrays.copyOfRange(buffer, i, i+9), go)) { + frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); + JOptionPane.showMessageDialog(null, "\nGAME OVER\n"); + end = true; + } + } System.out.println(String.valueOf(buffer).trim()); window.updateBoard(String.valueOf(buffer).trim()); } diff --git a/Parser.cpp b/Parser.cpp index 3db5ec7..dde4953 100755 --- a/Parser.cpp +++ b/Parser.cpp @@ -34,13 +34,14 @@ void parseAndMove(vector tokens, Board& board) { if(tokens[0].size() == 2) { if(tokens[1].size() >= 3 && tokens[1].size() <= 5) { - if(tokens[0][0] - 'A' < 0 || tokens[0][0] - 'A' > 7) { + cout << "Inside parser " << tokens[0] << "\n\n"; + if(tokens[0][0] - 'A' < 0 || tokens[0][0] - 'A' > 8) { cout << "Error. Invalid move location. (1st coord.)\n\n"; cout << tokens[0][0] << " " << (tokens[0][0] - 'A') << "\n\n"; return; } - else if(tokens[0][1] - '0' < 0 || tokens[0][1] - '0' > 7) { - cout << "Error. Invalid move location. (1st coord.)\n\n"; + else if(tokens[0][1] - '0' < 0 || tokens[0][1] - '0' > 8) { + cout << "Error. Invalid move location. (2nd coord.)\n\n"; return; } else { diff --git a/Server.cpp b/Server.cpp index 3c9cb69..7928162 100644 --- a/Server.cpp +++ b/Server.cpp @@ -76,7 +76,7 @@ int main(int argc, char *argv[]) string final_move; //Ask client about game type - string introduction = "WELCOME\n1. Play against AI?\n2. Watch AI vs. AI?3. Play Client vs. Client?\nEnter choice: \n"; + string introduction = "WELCOME\n1. Play against AI?\n2. Watch AI vs. AI?\n3. Play Client vs. Client?\nEnter choice: \n"; write(newsockfd, introduction.c_str(), introduction.length()); n = read(newsockfd,info,255); //Reads choice as a string istringstream convert(info); //Converts the read string to an integer @@ -118,7 +118,7 @@ int main(int argc, char *argv[]) { gameOver = e.getBoard()->isGameOver(); - while(e.getBoard()->getTurn() == 'O' && !e.getBoard()->isValid()) + while(!gameOver && e.getBoard()->getTurn() == 'O' && !e.getBoard()->isValid()) { e.getBoard()->displayBoard();//Display the board on the server string boardState = e.getBoard()->boardToString(); @@ -145,7 +145,7 @@ int main(int argc, char *argv[]) e.getBoard()->setValidFalse(); e.getBoard()->snapshot(record, *e.getBoard()); - while(e.getBoard()->getTurn() == 'X' && !e.getBoard()->isValid()) + while(!gameOver && e.getBoard()->getTurn() == 'X' && !e.getBoard()->isValid()) { e.getBoard()->displayBoard();//Display the board on the server string boardState = e.getBoard()->boardToString(); @@ -254,6 +254,8 @@ int main(int argc, char *argv[]) string game_over = "\n\nGAME OVER!!!\n"; write(newsockfd, final_move.c_str(), final_move.length()); //Display the board to the client (line by line) write(newsockfd, game_over.c_str(), game_over.length()); //Tell the client the game is over + write(newsockfd2, final_move.c_str(), final_move.length()); //Display the board to the client (line by line) + write(newsockfd2, game_over.c_str(), game_over.length()); //Tell the client the game is over cout << game_over; usleep(1); close(newsockfd);