diff --git a/Client.java b/Client.java index f8b3737..d4f0de2 100755 --- a/Client.java +++ b/Client.java @@ -1,4 +1,4 @@ -// Alex Huddleston +// Alex Huddleston and Brandon Jackson // Breakthrough Client in Java import java.io.IOException; @@ -289,10 +289,6 @@ public class Client { in.read(b, 0, 256); String temp = String.valueOf(b).trim(); - System.out.print(temp); - //setBoard(temp); - //window.drawBoard(temp); - String g = "GAME OVER"; char[] go = new char[9]; for(int x = 0; x < go.length; ++x) { @@ -312,58 +308,113 @@ public class Client { frame.pack(); frame.setMinimumSize(frame.getSize()); frame.setVisible(true); - userInput = stdIn.readLine(); + //userInput = stdIn.readLine(); + int gameMode = 0; + userInput = JOptionPane.showInputDialog(frame, temp); + gameMode = Integer.parseInt(userInput); + while(!userInput.equals("1") && !userInput.equals("2")) { + userInput = JOptionPane.showInputDialog(frame, temp); + output.println(userInput); + } + gameMode = Integer.parseInt(userInput); output.println(userInput); String diff = "?"; in.read(b, 0, 256); temp = String.valueOf(b).trim(); - System.out.print(temp); while(!diff.equals("EASY") && !diff.equals("MEDIUM") && !diff.equals("HARD")) { - diff = stdIn.readLine(); + diff = JOptionPane.showInputDialog(frame, temp);//diff = stdIn.readLine(); + diff = diff.toUpperCase(); if(!diff.equals("EASY") && !diff.equals("MEDIUM") && !diff.equals("HARD")) { - System.out.print(diff + "\nInvalid difficulty.\n" + temp); + JOptionPane.showMessageDialog(null, diff + "\nInvalid difficulty.\n" + temp); } } output.println(userInput); String out = ""; - - while(!end) { - char[] buffer = new char[256]; - in.read(buffer, 0, 256); - for(int i = 0; i < buffer.length; ++i) { - if(Arrays.equals(Arrays.copyOfRange(buffer, i, i+9), go)) { - end = true; - } - } - System.out.println(String.valueOf(buffer).trim()); - window.updateBoard(String.valueOf(buffer).trim()); - while(!end && boardoutput.length() < 4) { - 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."); + if(gameMode == 1) + { + while(!end) { + char[] buffer = new char[256]; + 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) { + 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(out); + output.println(out); + output.flush(); + boardoutput = ""; } - System.out.println(out); - output.println(out); - output.flush(); - boardoutput = ""; + output.close(); + in.close(); + stdIn.close(); + echoSocket.close(); + } + if(gameMode == 2) + { + while(!end) { + char[] buffer = new char[256]; + 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) { + 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(out); + //output.println(out); + //output.flush(); + boardoutput = ""; + } + output.close(); + in.close(); + stdIn.close(); + echoSocket.close(); } - output.close(); - in.close(); - stdIn.close(); - echoSocket.close(); } catch (IOException e){ System.err.println("IOException: " + e.getMessage()); diff --git a/Server.cpp b/Server.cpp index 621aa1f..9c3759e 100644 --- a/Server.cpp +++ b/Server.cpp @@ -19,44 +19,6 @@ void error(const char *msg) perror(msg); exit(1); } -/* -string tempParse(string move) { - string output = ""; - int tempa = move[0] - '0'; - int tempb = move[1] - '0'; - int tempc = move[2] - '0'; - int tempd = move[3] - '0'; - - //cout << "move: " << move << " " << tempa << " " << tempb << " " << tempc << " " << tempd << "\n"; - - for(int c = 0; c < 4; c++) { - if(!isdigit(move[c])) { - //cout << "\nfjdkjfjd\n"; - if(move.substr(0, 4) == "UNDO") { - return "UNDO"; - } - return move; - } - } - if(tempa == (tempc + 1)) { - //cout << "\nTest\n"; - if(tempb == tempd) { - output = ((char)('A' + tempb)) + to_string(8 - tempa) + "_f"; - } - else if(tempb == (tempd + 1)) { - output = ((char)('A' + tempb)) + to_string(8 - tempa) + "_l"; - } - else if(tempb == (tempd - 1)) { - output = ((char)('A' + tempb)) + to_string(8 - tempa) + "_r"; - } - } - else { - return move; - } - cout << "Debugging: " << output << "\n\n"; - return output; -} -*/ int main(int argc, char *argv[]) { @@ -134,47 +96,78 @@ int main(int argc, char *argv[]) while(true) { - while(gameOver != true) - { - gameOver = e.getBoard()->isGameOver(); - - while(e.getBoard()->getTurn() == 'O' && !e.getBoard()->isValid()) + if(choice == 1) { - 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; - bzero(buffer,256); - //cout << move << "\n\n"; - //move = tempParse(move); - parse(move,(*e.getBoard())); - - if(e.getBoard()->isValid()) { - e.getBoard()->changeTurns(); + while(gameOver != true) + { + gameOver = e.getBoard()->isGameOver(); + + while(e.getBoard()->getTurn() == 'O' && !e.getBoard()->isValid()) + { + 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; + bzero(buffer,256); + //cout << move << "\n\n"; + //move = tempParse(move); + parse(move,(*e.getBoard())); + + if(e.getBoard()->isValid()) { + e.getBoard()->changeTurns(); + e.getBoard()->setValidFalse(); + } + } + + if(e.getBoard()->isValid()) + cout << e.getBoard()->getTurn(); + + while(e.getBoard()->getTurn() == 'X' ) + { + if(choice_difficulty == "EASY") + e.AI(3); + else if(choice_difficulty == "MEDIUM") + e.AI(5); + else + e.AI(7); + } + + gameOver = e.getBoard()->isGameOver(); e.getBoard()->setValidFalse(); + e.getBoard()->snapshot(record, *e.getBoard()); } } - if(e.getBoard()->isValid()) - cout << e.getBoard()->getTurn(); - - while(e.getBoard()->getTurn() == 'X' ) + if(choice == 2) { - if(choice_difficulty == "EASY") - e.AI(3); - else if(choice_difficulty == "MEDIUM") - e.AI(5); - else - e.AI(7); + while (gameOver != true) { + gameOver = e.getBoard()->isGameOver(); + + while(e.getBoard()->getTurn() == 'O') { + 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) + e.AI(7); + } + + e.getBoard()->displayBoard(); + sleep(1); + + while(e.getBoard()->getTurn() == 'X' ) { + 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) + e.AI(7); + } + + gameOver = e.getBoard()->isGameOver(); + } } - - gameOver = e.getBoard()->isGameOver(); - e.getBoard()->setValidFalse(); - e.getBoard()->snapshot(record, *e.getBoard()); - } final_move = e.getBoard()->boardToString(); string game_over = "\n\nGAME OVER!!!\n";