Update Server.cpp

This commit is contained in:
Brandon Jackson 2015-10-21 00:30:59 -05:00
parent 460bd7f3db
commit 07ee4ac6de

View file

@ -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') {
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
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());
}
}
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<moves> 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;
}