From b09e378c1209d678a68eb98da7b9263544b1b262 Mon Sep 17 00:00:00 2001 From: Alexander Huddleston Date: Tue, 3 Nov 2015 22:48:59 -0600 Subject: [PATCH] Things. --- Client.java | 70 +++++++++++++++++++++++++++++++++++++++++------------ Server.cpp | 18 ++++++++++---- 2 files changed, 67 insertions(+), 21 deletions(-) diff --git a/Client.java b/Client.java index ab936b9..12446b6 100755 --- a/Client.java +++ b/Client.java @@ -181,6 +181,7 @@ public class Client { public final void updateBoard(String board) { board = TrimBoard(board); + System.out.println(board); int count = 0; ImageIcon icon = new ImageIcon( new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB)); if(oldBoard.equals("")) { @@ -215,15 +216,6 @@ public class Client { count++; } } - if(board.charAt(count) == 'X') { - boardSquares[7][7].setIcon(new ImageIcon(pieceImages[0])); - } - else if(board.charAt(count) == 'O') { - boardSquares[7][7].setIcon(new ImageIcon(pieceImages[1])); - } - else if(board.charAt(count) == '_' && oldBoard.charAt(count) != '_') { - boardSquares[7][7].setIcon(icon); - } } oldBoard = board; } @@ -342,8 +334,58 @@ public class Client { gameMode = 3; myTurn = 'X'; } - String out = ""; - if(gameMode == 1 || gameMode == 3) + String out = ""; + if(gameMode == 3) + { + while(!end) { + char[] buffer = new char[256]; + if(in.available()) { + 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()); + while(!end && boardoutput.length() < 4) { + if(in.available()) { + in.read(buffer, 0, 256); + System.out.println(String.valueOf(buffer).trim()); + window.updateBoard(String.valueOf(buffer).trim()); + } + String tempstring; + Thread.sleep(100); + if(!undoString.equals("")) { + System.out.println(undoString); + output.println(undoString); + output.flush(); + undoString = ""; + } + if(boardoutput.length() == 4) { + out = parseOutput(boardoutput); + if(out == "Invalid") { + out = ""; + boardoutput = ""; + System.out.println("Invalid move."); + } + } + } + //System.out.println(String.valueOf(buffer).trim()); + System.out.println(out); + output.println(out); + output.flush(); + boardoutput = ""; + } + output.close(); + in.close(); + stdIn.close(); + echoSocket.close(); + } + else if(gameMode == 1) { while(!end) { char[] buffer = new char[256]; @@ -360,10 +402,6 @@ public class Client { while(!end && boardoutput.length() < 4) { String tempstring; Thread.sleep(100); - if((tempstring = in.readLine())!=null) { - System.out.println(String.valueOf(buffer).trim()); - window.updateBoard(String.valueOf(buffer).trim()); - } if(!undoString.equals("")) { System.out.println(undoString); output.println(undoString); @@ -390,7 +428,7 @@ public class Client { stdIn.close(); echoSocket.close(); } - if(gameMode == 2) + else if(gameMode == 2) { while(!end) { char[] buffer = new char[256]; diff --git a/Server.cpp b/Server.cpp index 6142768..3235930 100644 --- a/Server.cpp +++ b/Server.cpp @@ -102,10 +102,12 @@ int main(int argc, char *argv[]) clilenen = sizeof(cli_addr); newsockfd2 = accept(sockfd, (struct sockaddr *) &cli2_addr, &clilenen); - write(newsockfd2, "skip", 4);//Display the board to the client (line by line) + write(newsockfd2, "skip", 4); sleep(1); string tempboard = e.getBoard()->boardToString(); write(newsockfd2, tempboard.c_str(), tempboard.length());//Display the board to the client (line by line) + write(newsockfd, tempboard.c_str(), tempboard.length());//Display the board to the client (line by line) + bool firstturn = true; if (newsockfd2 < 0) error("ERROR on accept"); @@ -119,7 +121,6 @@ int main(int argc, char *argv[]) e.getBoard()->displayBoard();//Display the board on the server string boardState = e.getBoard()->boardToString(); final_move = e.getBoard()->boardToString(); - write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line) cout<<"\nWaiting for client: "; n = read(newsockfd,buffer,255); move = buffer; @@ -129,6 +130,10 @@ int main(int argc, char *argv[]) parse(move,(*e.getBoard())); if(e.getBoard()->isValid()) { + boardState = e.getBoard()->boardToString(); + cout << boardState << "testing\n\n"; + write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line) + write(newsockfd2, boardState.c_str(), boardState.length());//Display the board to the client (line by line) e.getBoard()->changeTurns(); e.getBoard()->setValidFalse(); } @@ -153,6 +158,9 @@ int main(int argc, char *argv[]) parse(move,(*e.getBoard())); if(e.getBoard()->isValid()) { + boardState = e.getBoard()->boardToString(); + write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line) + write(newsockfd2, boardState.c_str(), boardState.length());//Display the board to the client (line by line) e.getBoard()->changeTurns(); e.getBoard()->setValidFalse(); } @@ -199,11 +207,11 @@ int main(int argc, char *argv[]) while(e.getBoard()->getTurn() == 'X' ) { if(choice_difficulty == "EASY") - e.AI(3); + e.AI(17); else if(choice_difficulty == "MEDIUM") - e.AI(5); + e.AI(47); else - e.AI(7); + e.AI(77); } gameOver = e.getBoard()->isGameOver();