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