From 0e326ee4081ff2c5d8d6326ea64e0886ba90f76f Mon Sep 17 00:00:00 2001 From: Alexander Huddleston Date: Tue, 20 Oct 2015 23:49:27 -0500 Subject: [PATCH] Error checking in server. --- Board.cpp | 1134 +++++++++++++++++++++++++++------------------------- Board.h | 2 + Server.cpp | 38 +- 3 files changed, 615 insertions(+), 559 deletions(-) diff --git a/Board.cpp b/Board.cpp index b1473b4..0536ff6 100755 --- a/Board.cpp +++ b/Board.cpp @@ -1,552 +1,584 @@ -#include -#include -#include "Board.h" - -using namespace std; - -Board::Board() { - for (int i = 0; i < 2; ++i) { - for (int j = 0; j < 8; ++j) { - boardArray[i][j] = 'X'; - } - } - - - for (int i = 2; i < 6; ++i) { - for (int j = 0; j < 8; ++j) { - boardArray[i][j] = '_'; - } - } - - - for (int i = 6; i <= 7; ++i) { - for (int j = 0; j < 8; ++j) { - boardArray[i][j] = 'O'; - } - } -} - -moves Board::parse(string input) -{ - - input = myToUpper(input); - - cout< 8 || row < 0 || kolumn > 8 || kolumn < 0) - { - cout<<"ERROR: index out of bound in move()!"< 8 || row < 0 || kolumn > 8 || kolumn < 0) - { - cout<<"ERROR: index out of bound in second move()!"<= 0) && (r+reflector <= 7) && (c+1 <= 7) ) return true; - else return false; - } - - else if (moveType == "LEFT") - { - if (boardArray[r+reflector][c-1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0) ) return true; - else return false; - } - - else return false; - } -} - -vector Board::viewPossibleMoves() -{ - vector output; - - for (int r = 0; r < 8; ++r) - { - for (int c = 0; c < 8; ++c) - { - if (boardArray[r][c] == turn) - { - if (isThisMovePossible(r,c,"FWD")) - { - moves temp(8-r,intToCharColumn(c+1),"FWD"); - output.push_back(temp); - } - - if (isThisMovePossible(r,c,"LEFT")) - { - moves temp(8-r,intToCharColumn(c+1),"LEFT"); - output.push_back(temp); - } - - if (isThisMovePossible(r,c,"RIGHT")) - { - moves temp(8-r,intToCharColumn(c+1),"RIGHT"); - output.push_back(temp); - } - - } - } - } - - return output; -} - -string Board::myToUpper(string input) -{ - string output; - - for (int i = 0 ; i < input.size(); ++i) - { - int numeric; - - if ((input[i] - 0 >= 97) && (input[i] - 0 <= 122)) - { - numeric = input[i] - 32; - output.push_back((char)numeric);// = 'Q';//(char) numeric; - } - else output.push_back(input[i]); - } - - for (int i = 0; i < output.size(); ++i) - { - cout< record; - - if (record.size() < 2) - { - cout<<"nothing to undo"< record; - input = myToUpper(input); - - if (input == "UNDO") - { - undo(tablero); - } - - else if (input == "DISPLAYRECORD") //for debugging purposes - { - cout<<"record: "<& inputVec, Board inputBoard) -{ - if (inputVec.size() == 10) - { - inputVec.erase(inputVec.begin()); - } - - else if (inputVec.size() > 10) - { - cout<<"QUEUE OVERFLOW!"< listOfMoves = viewPossibleMoves(); - - //obvious moves - if (false){ - return; - } - - //random - else { - 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; - - move(listOfMoves[randomChoice]); - } +#include +#include +#include "Board.h" + +using namespace std; + +Board::Board() { + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 8; ++j) { + boardArray[i][j] = 'X'; + } + } + + + for (int i = 2; i < 6; ++i) { + for (int j = 0; j < 8; ++j) { + boardArray[i][j] = '_'; + } + } + + + for (int i = 6; i <= 7; ++i) { + for (int j = 0; j < 8; ++j) { + boardArray[i][j] = 'O'; + } + } +} + +moves Board::parse(string input) +{ + + input = myToUpper(input); + + cout< 8 || row < 0 || kolumn > 8 || kolumn < 0) + { + cout<<"ERROR: index out of bound!"< 8 || row < 0 || kolumn > 8 || kolumn < 0) + { + cout<<"ERROR: index out of bound!"<= 0) && (r+reflector <= 7) && (c+1 <= 7) ) return true; + else return false; + } + + else if (moveType == "LEFT") + { + if (boardArray[r+reflector][c-1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0) ) return true; + else return false; + } + + else return false; + } +} + +vector Board::viewPossibleMoves() +{ + vector output; + + for (int r = 0; r < 8; ++r) + { + for (int c = 0; c < 8; ++c) + { + if (boardArray[r][c] == turn) + { + if (isThisMovePossible(r,c,"FWD")) + { + moves temp(8-r,intToCharColumn(c+1),"FWD"); + output.push_back(temp); + } + + if (isThisMovePossible(r,c,"LEFT")) + { + moves temp(8-r,intToCharColumn(c+1),"LEFT"); + output.push_back(temp); + } + + if (isThisMovePossible(r,c,"RIGHT")) + { + moves temp(8-r,intToCharColumn(c+1),"RIGHT"); + output.push_back(temp); + } + + } + } + } + + return output; +} + +string Board::myToUpper(string input) +{ + string output; + + for (int i = 0 ; i < input.size(); ++i) + { + int numeric; + + if ((input[i] - 0 >= 97) && (input[i] - 0 <= 122)) + { + numeric = input[i] - 32; + output.push_back((char)numeric);// = 'Q';//(char) numeric; + } + else output.push_back(input[i]); + } + + for (int i = 0; i < output.size(); ++i) + { + cout< input) +{ + cout<<"\n\nList of possible Moves:"< record; + + if (record.size() < 2) + { + cout<<"nothing to undo"< record; + input = myToUpper(input); + + if (input == "UNDO") + { + undo(tablero); + } + + else if (input == "DISPLAYRECORD") //for debugging purposes + { + cout<<"record: "<& inputVec, Board inputBoard) +{ + if (inputVec.size() == 10) + { + inputVec.erase(inputVec.begin()); + } + + else if (inputVec.size() > 10) + { + cout<<"QUEUE OVERFLOW!"< listOfMoves = viewPossibleMoves(); + + //2) pick a movement + + 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; + + move(listOfMoves[randomChoice]); + + //cout<<"\n\nMove executed by AI: "<& inputVec, Board inputBoard); void easyAI(); + string boardToString(); + void displayPossibleMoves(vector input); }; \ No newline at end of file diff --git a/Server.cpp b/Server.cpp index 3630ebc..604075c 100644 --- a/Server.cpp +++ b/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) { @@ -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 possibleMoves = b.viewPossibleMoves(); - if(choice == 1) + if(choice == 1 && !gameOver) b.easyAI(); }