Error checking in server.
This commit is contained in:
parent
c41ddc9715
commit
0e326ee408
3 changed files with 615 additions and 559 deletions
2
Board.h
2
Board.h
|
@ -41,4 +41,6 @@ public:
|
||||||
void interpret(string input, Board& tablero);
|
void interpret(string input, Board& tablero);
|
||||||
void snapshot(vector<Board>& inputVec, Board inputBoard);
|
void snapshot(vector<Board>& inputVec, Board inputBoard);
|
||||||
void easyAI();
|
void easyAI();
|
||||||
|
string boardToString();
|
||||||
|
void displayPossibleMoves(vector<moves> input);
|
||||||
};
|
};
|
38
Server.cpp
38
Server.cpp
|
@ -86,11 +86,23 @@ int main(int argc, char *argv[])
|
||||||
char info[256];
|
char info[256];
|
||||||
int choice;
|
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
|
//Waiting for client to select game type
|
||||||
n = read(newsockfd,info,255);
|
while(read(newsockfd,info,255)) {
|
||||||
istringstream convert(info);
|
if(info[0] == '1' || info[0] == '2') {
|
||||||
convert >> choice; //Sets choice equal to 1 or 2, based on clients input
|
istringstream convert(info);
|
||||||
bzero(info,256); //Resets info back to normal, "choice" now contains client's value
|
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) {
|
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)
|
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;
|
||||||
|
@ -132,7 +154,7 @@ int main(int argc, char *argv[])
|
||||||
// }
|
// }
|
||||||
|
|
||||||
vector<moves> possibleMoves = b.viewPossibleMoves();
|
vector<moves> possibleMoves = b.viewPossibleMoves();
|
||||||
if(choice == 1)
|
if(choice == 1 && !gameOver)
|
||||||
b.easyAI();
|
b.easyAI();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue