From 07ee4ac6de83598add2a9bcc7f6dc36ddfedceb7 Mon Sep 17 00:00:00 2001 From: Brandon Jackson <1drummer@att.net> Date: Wed, 21 Oct 2015 00:30:59 -0500 Subject: [PATCH] Update Server.cpp --- Server.cpp | 78 +++++++++++++----------------------------------------- 1 file changed, 18 insertions(+), 60 deletions(-) diff --git a/Server.cpp b/Server.cpp index 604075c..de4e231 100644 --- a/Server.cpp +++ b/Server.cpp @@ -85,24 +85,14 @@ int main(int argc, char *argv[]) char buffer[256]; char info[256]; int choice; + int move_counter = 0; + string final_move; - string choice_input = "WELCOME\n1. Play against AI?\n2. Play against a human?\nEnter choice: "; - write(newsockfd, choice_input.c_str(), choice_input.length()); - choice_input = "Enter choice: "; //Waiting for client to select game type - while(read(newsockfd,info,255)) { - if(info[0] == '1' || info[0] == '2') { - istringstream convert(info); - convert >> choice; //Sets choice equal to 1 or 2, based on clients input - bzero(info,256); //Resets info back to normal, "choice" now contains client's value - break; - } - else { - string choice_error = "Invalid choice."; - write(newsockfd, choice_error.c_str(), choice_error.length()); - write(newsockfd, choice_input.c_str(), choice_input.length()); - } - } + n = read(newsockfd,info,255); + istringstream convert(info); + convert >> choice; //Sets choice equal to 1 or 2, based on clients input + bzero(info,256); //Resets info back to normal, "choice" now contains client's value while(true) { @@ -112,26 +102,16 @@ int main(int argc, char *argv[]) while(b.getTurn() == 'O' ) { - b.displayBoard(); //Display the board on the server + b.displayBoard();//Display the board on the server string boardState = b.boardToString(); - write(newsockfd, boardState.c_str(), boardState.length()); //Display the board to the client (line by line) + final_move = b.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; - move = b.myToUpper(move); - //cout << "\n\ntesting\n" << move << endl; - if(move.substr(0,4) == "QUIT") { - cout << "TESTING" << endl; - gameOver = true; - break; - } b.interpret(move,b); - - // if(!gameOver) { - // gameOver = b.isGameOver(); - // } - - if(gameOver) { + gameOver = b.isGameOver(); + if(gameOver == true) { string endGame = "Game_Over"; write(newsockfd, endGame.c_str(), endGame.length()); //Display the board to the client (line by line) break; @@ -141,42 +121,20 @@ int main(int argc, char *argv[]) write(newsockfd, continueGame.c_str(), continueGame.length()); //Display the board to the client (line by line) } } - - //gameOver = b.isGameOver(); - // if(gameOver == true) { - // string endGame = "Game_Over"; - // write(newsockfd, endGame.c_str(), endGame.length()); //Display the board to the client (line by line) - // break; - // } - // else { - // string continueGame = "Continue_Game"; - // write(newsockfd, continueGame.c_str(), continueGame.length()); //Display the board to the client (line by line) - // } - vector possibleMoves = b.viewPossibleMoves(); - if(choice == 1 && !gameOver) + if(choice == 1) b.easyAI(); + } + string final_move = b.boardToString(); + write(newsockfd, final_move.c_str(), final_move.length());//Display the board to the client (line by line) + write(newsockfd, final_move.c_str(), final_move.length()); + cout << "\n\nGAME OVER!!!"; + usleep(1); close(newsockfd); close(sockfd); break; - - /* - bzero(buffer,512); - n = read(newsockfd,buffer,255); - if (n < 0) error("ERROR reading from socket"); - printf("Here is the message: %s\n",buffer); - n = write(newsockfd,"I got your message",18); - - if (n < 0) error("ERROR writing to socket"); - - if(buffer == "y") { - close(newsockfd); - close(sockfd); - break; - } - */ } return 0; }