Error checking in server.
This commit is contained in:
parent
c41ddc9715
commit
0e326ee408
3 changed files with 615 additions and 559 deletions
62
Board.cpp
62
Board.cpp
|
@ -88,6 +88,7 @@ void Board::changeTurns()
|
|||
|
||||
void Board::displayBoard()
|
||||
{
|
||||
cout<<"\n\n";
|
||||
cout<<"; A B C D E F G H"<<endl;
|
||||
for (int i = 0; i < 8; ++i)
|
||||
{
|
||||
|
@ -100,7 +101,7 @@ void Board::displayBoard()
|
|||
cout<<"|\n";
|
||||
}
|
||||
cout<<'\n'<<endl;
|
||||
cout<<"turn : "<<turn << "\n";
|
||||
cout<<"turn : "<<turn;
|
||||
}
|
||||
|
||||
int Board::charToIntColumn(char input) //converts column number to int
|
||||
|
@ -143,6 +144,7 @@ char Board::intToCharColumn(int input) //converts column number to int
|
|||
|
||||
void Board::move(string inputMove)
|
||||
{
|
||||
|
||||
moves jugada = parse(inputMove);
|
||||
|
||||
int row = 8 - (jugada.row);
|
||||
|
@ -152,7 +154,7 @@ void Board::move(string inputMove)
|
|||
|
||||
if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0)
|
||||
{
|
||||
cout<<"ERROR: index out of bound in move()!"<<endl;
|
||||
cout<<"ERROR: index out of bound!"<<endl;
|
||||
}
|
||||
|
||||
else if (temp != turn)
|
||||
|
@ -261,7 +263,7 @@ void Board::move(moves jugada)
|
|||
|
||||
if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0)
|
||||
{
|
||||
cout<<"ERROR: index out of bound in second move()!"<<endl;
|
||||
cout<<"ERROR: index out of bound!"<<endl;
|
||||
}
|
||||
|
||||
int temp = boardArray[row][kolumn];
|
||||
|
@ -464,6 +466,15 @@ string Board::myToUpper(string input)
|
|||
|
||||
}
|
||||
|
||||
void Board::displayPossibleMoves(vector<moves> input)
|
||||
{
|
||||
cout<<"\n\nList of possible Moves:"<<endl;
|
||||
for (int i = 0; i < input.size(); ++i)
|
||||
{
|
||||
cout<<"possible move: "<<input[i].row<<" "<<input[i].column<<" "<<input[i].moveType<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Board::undo(Board& tablero)
|
||||
{
|
||||
vector<Board> record;
|
||||
|
@ -534,19 +545,40 @@ void Board::easyAI()
|
|||
|
||||
vector<moves> listOfMoves = viewPossibleMoves();
|
||||
|
||||
//obvious moves
|
||||
if (false){
|
||||
return;
|
||||
}
|
||||
//2) pick a movement
|
||||
|
||||
//random
|
||||
else {
|
||||
srand(time(NULL));
|
||||
int randomChoice = rand() % (listOfMoves.size()-1) - 0; // choose a move betwen listOfMoves[0] to last element
|
||||
srand(time(NULL));
|
||||
int randomChoice = rand() % (listOfMoves.size()-1) - 0; // choose a move betwen listOfMoves[0] to last element
|
||||
|
||||
//3) execute movement
|
||||
int temp = randomChoice;
|
||||
//3) execute movement
|
||||
|
||||
int temp = randomChoice;
|
||||
|
||||
move(listOfMoves[randomChoice]);
|
||||
|
||||
//cout<<"\n\nMove executed by AI: "<<listOfMoves[temp].column<<" "<<listOfMoves[temp].row<<" "<<listOfMoves[temp].moveType<<endl; uncomment for debugging purposes
|
||||
|
||||
move(listOfMoves[randomChoice]);
|
||||
}
|
||||
}
|
||||
|
||||
string Board::boardToString()
|
||||
{
|
||||
string temp;
|
||||
temp.append("\n");
|
||||
temp.append("\n");
|
||||
temp.append("; A B C D E F G H\n");
|
||||
for (int i = 0; i < 8; ++i)
|
||||
{
|
||||
int mango = 8 - i;
|
||||
temp.append("; " + to_string(mango) + " ");
|
||||
for (int j = 0; j < 8; ++j)
|
||||
{
|
||||
temp.append("|");
|
||||
temp.push_back(boardArray[i][j]);
|
||||
}
|
||||
temp.append("|\n");
|
||||
}
|
||||
temp.append("\n");
|
||||
temp.append("turn : " + turn);
|
||||
|
||||
return temp;
|
||||
}
|
2
Board.h
2
Board.h
|
@ -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);
|
||||
};
|
36
Server.cpp
36
Server.cpp
|
@ -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) {
|
||||
|
||||
|
@ -106,10 +118,20 @@ int main(int argc, char *argv[])
|
|||
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);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue