From 0e326ee4081ff2c5d8d6326ea64e0886ba90f76f Mon Sep 17 00:00:00 2001 From: Alexander Huddleston Date: Tue, 20 Oct 2015 23:49:27 -0500 Subject: [PATCH 1/9] 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(); } From ef3bdea3ba6325b70e6630571153c09155d75130 Mon Sep 17 00:00:00 2001 From: Alex Huddleston Date: Tue, 20 Oct 2015 23:52:34 -0500 Subject: [PATCH 2/9] Create README.md --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..500efc5 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Breakthrough +Reposity for the second CSCE 315 project + +compile the server with the folling command: +g++ -std=c++11 -o server Board.cpp Server.cpp + +Client is still WIP. From 460bd7f3dbab6f9749bae801e772f8c3ae368e24 Mon Sep 17 00:00:00 2001 From: Alex Huddleston Date: Wed, 21 Oct 2015 00:03:46 -0500 Subject: [PATCH 3/9] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 500efc5..ac4fdc5 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,7 @@ Reposity for the second CSCE 315 project compile the server with the folling command: g++ -std=c++11 -o server Board.cpp Server.cpp +run: +./server [port] + Client is still WIP. From 07ee4ac6de83598add2a9bcc7f6dc36ddfedceb7 Mon Sep 17 00:00:00 2001 From: Brandon Jackson <1drummer@att.net> Date: Wed, 21 Oct 2015 00:30:59 -0500 Subject: [PATCH 4/9] Update Server.cpp --- Server.cpp | 78 +++++++++++++----------------------------------------- 1 file changed, 18 insertions(+), 60 deletions(-) diff --git a/Server.cpp b/Server.cpp index 604075c..de4e231 100644 --- a/Server.cpp +++ b/Server.cpp @@ -85,24 +85,14 @@ int main(int argc, char *argv[]) char buffer[256]; char info[256]; int choice; + int move_counter = 0; + string final_move; - 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 - 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()); - } - } + 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(true) { @@ -112,26 +102,16 @@ int main(int argc, char *argv[]) while(b.getTurn() == 'O' ) { - b.displayBoard(); //Display the board on the server + b.displayBoard();//Display the board on the server string boardState = b.boardToString(); - write(newsockfd, boardState.c_str(), boardState.length()); //Display the board to the client (line by line) + final_move = b.boardToString(); + 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 = 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); - - // if(!gameOver) { - // gameOver = b.isGameOver(); - // } - - if(gameOver) { + gameOver = b.isGameOver(); + if(gameOver == true) { string endGame = "Game_Over"; write(newsockfd, endGame.c_str(), endGame.length()); //Display the board to the client (line by line) break; @@ -141,42 +121,20 @@ int main(int argc, char *argv[]) write(newsockfd, continueGame.c_str(), continueGame.length()); //Display the board to the client (line by line) } } - - //gameOver = b.isGameOver(); - // if(gameOver == true) { - // string endGame = "Game_Over"; - // write(newsockfd, endGame.c_str(), endGame.length()); //Display the board to the client (line by line) - // break; - // } - // else { - // string continueGame = "Continue_Game"; - // write(newsockfd, continueGame.c_str(), continueGame.length()); //Display the board to the client (line by line) - // } - vector possibleMoves = b.viewPossibleMoves(); - if(choice == 1 && !gameOver) + if(choice == 1) b.easyAI(); + } + string final_move = b.boardToString(); + write(newsockfd, final_move.c_str(), final_move.length());//Display the board to the client (line by line) + write(newsockfd, final_move.c_str(), final_move.length()); + cout << "\n\nGAME OVER!!!"; + usleep(1); close(newsockfd); close(sockfd); break; - - /* - bzero(buffer,512); - n = read(newsockfd,buffer,255); - if (n < 0) error("ERROR reading from socket"); - printf("Here is the message: %s\n",buffer); - n = write(newsockfd,"I got your message",18); - - if (n < 0) error("ERROR writing to socket"); - - if(buffer == "y") { - close(newsockfd); - close(sockfd); - break; - } - */ } return 0; } From 95c1b20d1ef2c1351348fe285d7ea057a8f8016f Mon Sep 17 00:00:00 2001 From: Brandon Jackson <1drummer@att.net> Date: Wed, 21 Oct 2015 00:33:53 -0500 Subject: [PATCH 5/9] Update Board.cpp --- Board.cpp | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/Board.cpp b/Board.cpp index 0536ff6..e170687 100755 --- a/Board.cpp +++ b/Board.cpp @@ -104,6 +104,29 @@ void Board::displayBoard() cout<<"turn : "< Date: Wed, 21 Oct 2015 00:35:08 -0500 Subject: [PATCH 6/9] Update Client.cpp --- Client.cpp | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/Client.cpp b/Client.cpp index 73bb121..4ed70b2 100644 --- a/Client.cpp +++ b/Client.cpp @@ -48,46 +48,58 @@ int main(int argc, char *argv[]) //Client has successfully joined char buffer[256]; char info[256]; + string final_board; cout<<"WELCOME\n"; cout<<"1. Play against AI?\n"; - cout<<"2. Play against a human?\n"; - cout<<"Enter choice: \n"; - - string choice; - cin >> choice; + cout<<"2. Play AI against an AI?\n"; + string choice = ""; + while(choice != "1") + { + cout<<"Enter choice: \n"; + cin >> choice; + if(choice == "2") + cout << "AI vs. AI mode not added yet!\n"; + } //Check for a valid option cout << "OK!\n" << endl; //Writes off the choice to the server - n = write(sockfd, choice.c_str(), choice.length()); //Sends an input to the server - - + cin.clear(); + cin.ignore(10000,'\n'); while(true) { bzero(buffer,256); //resets the input stream n = read(sockfd,buffer,255); //Receives the board from server printf("%s\n",buffer);//Prints the received message + final_board = buffer; printf("Please enter a move: "); bzero(buffer,256); //resets input stream fgets(buffer,255,stdin); //Enter a move - n = write(sockfd,buffer,strlen(buffer)); //Sends an inputted move to the server + n = write(sockfd,buffer,strlen(buffer)); //Sends an input move to the server bzero(info,256); //resets input stream n = read(sockfd,info,255); //Reads from server if move was valid string ref = info; if(ref == "Game_Over") { + n = read(sockfd,info,255); //Reads from server if move was valid cout << "GAME OVER!!!" << endl; break; } else continue; - } + cout << "\nGAME WINNING MOVE: \n"; + //cout << final_board << endl; + //bzero(info,256); //resets input stream + n = read(sockfd,info,255); //Reads from server if move was valid + + printf("%s\n",info);//Prints the received message + usleep(1); close(sockfd); return 0; } From 6a94a2342577ee5d138af43b396bf59d30b59443 Mon Sep 17 00:00:00 2001 From: Brandon Jackson <1drummer@att.net> Date: Wed, 21 Oct 2015 00:35:26 -0500 Subject: [PATCH 7/9] Update Board.h --- Board.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Board.h b/Board.h index ea0a81c..87169c1 100755 --- a/Board.h +++ b/Board.h @@ -37,10 +37,10 @@ public: bool isThisMovePossible(int r, int c, string moveType); vector viewPossibleMoves(); string myToUpper(string input); + void displayPossibleMoves(vector input); void undo(Board& tablero); void interpret(string input, Board& tablero); void snapshot(vector& inputVec, Board inputBoard); void easyAI(); string boardToString(); - void displayPossibleMoves(vector input); -}; \ No newline at end of file +}; From 6355f7ad5611711f05add2aadf504d66fe58498d Mon Sep 17 00:00:00 2001 From: William Bracho Blok Date: Wed, 21 Oct 2015 17:47:33 -0500 Subject: [PATCH 8/9] Update Board.cpp --- Board.cpp | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/Board.cpp b/Board.cpp index e170687..cdde4cc 100755 --- a/Board.cpp +++ b/Board.cpp @@ -498,9 +498,8 @@ void Board::displayPossibleMoves(vector input) } } -void Board::undo(Board& tablero) +void Board::undo() { - vector record; if (record.size() < 2) { @@ -509,11 +508,14 @@ void Board::undo(Board& tablero) else { + cout<<"BAZINGA"< record; input = myToUpper(input); - if (input == "UNDO") + if (input[0] == 'U') { - undo(tablero); + undo(); } else if (input == "DISPLAYRECORD") //for debugging purposes @@ -544,22 +546,32 @@ void Board::interpret(string input, Board& tablero) //determines what kind of co else tablero.move(input); } -void Board::snapshot(vector& inputVec, Board inputBoard) +void Board::snapshot() { - if (inputVec.size() == 10) + if (record.size() == 10) { - inputVec.erase(inputVec.begin()); + record.erase(record.begin()); } - else if (inputVec.size() > 10) + else if (record.size() > 10) { cout<<"QUEUE OVERFLOW!"< Date: Wed, 21 Oct 2015 17:48:03 -0500 Subject: [PATCH 9/9] Update Board.h --- Board.h | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/Board.h b/Board.h index 87169c1..bd6ce95 100755 --- a/Board.h +++ b/Board.h @@ -17,9 +17,39 @@ struct moves { } }; +struct simpleBoard{ + + char boardStamp [8][8]; + + char elementAt(int r, int k) + { + return boardStamp[r][k]; + } + + void modifyAt(int r, int k, char c) + { + boardStamp[r][k] = c; + } + + void display() + { + for (int r = 0; r < 8; ++r) + { + cout<<'\n'; + + for (int k = 0; k < 8; ++k) + { + cout< record; public: Board(); @@ -38,9 +68,10 @@ public: vector viewPossibleMoves(); string myToUpper(string input); void displayPossibleMoves(vector input); - void undo(Board& tablero); + void undo(); void interpret(string input, Board& tablero); - void snapshot(vector& inputVec, Board inputBoard); + void snapshot(); void easyAI(); string boardToString(); + void displayRecord(); };