Update Server.cpp
This commit is contained in:
parent
460bd7f3db
commit
07ee4ac6de
1 changed files with 18 additions and 60 deletions
68
Server.cpp
68
Server.cpp
|
@ -85,24 +85,14 @@ int main(int argc, char *argv[])
|
||||||
char buffer[256];
|
char buffer[256];
|
||||||
char info[256];
|
char info[256];
|
||||||
int choice;
|
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
|
//Waiting for client to select game type
|
||||||
while(read(newsockfd,info,255)) {
|
n = read(newsockfd,info,255);
|
||||||
if(info[0] == '1' || info[0] == '2') {
|
|
||||||
istringstream convert(info);
|
istringstream convert(info);
|
||||||
convert >> choice; //Sets choice equal to 1 or 2, based on clients input
|
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
|
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) {
|
while(true) {
|
||||||
|
|
||||||
|
@ -114,24 +104,14 @@ int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
b.displayBoard();//Display the board on the server
|
b.displayBoard();//Display the board on the server
|
||||||
string boardState = b.boardToString();
|
string boardState = b.boardToString();
|
||||||
|
final_move = b.boardToString();
|
||||||
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
||||||
cout<<"\nWaiting for client: ";
|
cout<<"\nWaiting for client: ";
|
||||||
n = read(newsockfd,buffer,255);
|
n = read(newsockfd,buffer,255);
|
||||||
move = buffer;
|
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);
|
b.interpret(move,b);
|
||||||
|
gameOver = b.isGameOver();
|
||||||
// if(!gameOver) {
|
if(gameOver == true) {
|
||||||
// gameOver = b.isGameOver();
|
|
||||||
// }
|
|
||||||
|
|
||||||
if(gameOver) {
|
|
||||||
string endGame = "Game_Over";
|
string endGame = "Game_Over";
|
||||||
write(newsockfd, endGame.c_str(), endGame.length()); //Display the board to the client (line by line)
|
write(newsockfd, endGame.c_str(), endGame.length()); //Display the board to the client (line by line)
|
||||||
break;
|
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)
|
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();
|
vector<moves> possibleMoves = b.viewPossibleMoves();
|
||||||
if(choice == 1 && !gameOver)
|
if(choice == 1)
|
||||||
b.easyAI();
|
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(newsockfd);
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
break;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue