Error checking in server.

This commit is contained in:
Alexander Huddleston 2015-10-20 23:49:27 -05:00
parent c41ddc9715
commit 0e326ee408
3 changed files with 615 additions and 559 deletions

1134
Board.cpp

File diff suppressed because it is too large Load diff

View file

@ -41,4 +41,6 @@ public:
void interpret(string input, Board& tablero);
void snapshot(vector<Board>& inputVec, Board inputBoard);
void easyAI();
string boardToString();
void displayPossibleMoves(vector<moves> input);
};

View file

@ -86,11 +86,23 @@ int main(int argc, char *argv[])
char info[256];
int choice;
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
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(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());
}
}
while(true) {
@ -105,11 +117,21 @@ int main(int argc, char *argv[])
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 = 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);
gameOver = b.isGameOver();
if(gameOver == true) {
// if(!gameOver) {
// gameOver = b.isGameOver();
// }
if(gameOver) {
string endGame = "Game_Over";
write(newsockfd, endGame.c_str(), endGame.length()); //Display the board to the client (line by line)
break;
@ -132,7 +154,7 @@ int main(int argc, char *argv[])
// }
vector<moves> possibleMoves = b.viewPossibleMoves();
if(choice == 1)
if(choice == 1 && !gameOver)
b.easyAI();
}