From e2d0473857a566d4c5f1b107ce0c8cb439dce49d Mon Sep 17 00:00:00 2001 From: William Bracho Blok Date: Wed, 14 Oct 2015 16:54:10 -0500 Subject: [PATCH 01/10] Create client.C --- client.C | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 client.C diff --git a/client.C b/client.C new file mode 100644 index 0000000..181de35 --- /dev/null +++ b/client.C @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +void error(const char *msg) +{ + perror(msg); + exit(0); +} + +int main(int argc, char *argv[]) +{ + int sockfd, portno, n; + struct sockaddr_in serv_addr; + struct hostent *server; + + char buffer[256]; + if (argc < 3) { + fprintf(stderr,"usage %s hostname port\n", argv[0]); + exit(0); + } + portno = atoi(argv[2]); + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) + error("ERROR opening socket"); + server = gethostbyname(argv[1]); + if (server == NULL) { + fprintf(stderr,"ERROR, no such host\n"); + exit(0); + } + bzero((char *) &serv_addr, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + bcopy((char *)server->h_addr, + (char *)&serv_addr.sin_addr.s_addr, + server->h_length); + serv_addr.sin_port = htons(portno); + if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) + error("ERROR connecting"); + printf("Please enter the message: "); + bzero(buffer,256); + fgets(buffer,255,stdin); + n = write(sockfd,buffer,strlen(buffer)); + if (n < 0) + error("ERROR writing to socket"); + bzero(buffer,256); + n = read(sockfd,buffer,255); + if (n < 0) + error("ERROR reading from socket"); + printf("%s\n",buffer); + close(sockfd); + return 0; +} From 66e96797c3b442c6b5091eba73719e0bc999b6ef Mon Sep 17 00:00:00 2001 From: William Bracho Blok Date: Wed, 14 Oct 2015 16:54:46 -0500 Subject: [PATCH 02/10] Create server.C --- server.C | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 server.C diff --git a/server.C b/server.C new file mode 100644 index 0000000..0c61a0e --- /dev/null +++ b/server.C @@ -0,0 +1,83 @@ +/* A simple server in the internet domain using TCP + The port number is passed as an argument */ +#include +#include +#include +#include +#include +#include +#include + +void error(const char *msg) +{ + perror(msg); + exit(1); +} + +int main(int argc, char *argv[]) +{ + int sockfd, newsockfd, portno; + socklen_t clilen; + char buffer[256]; + + + struct sockaddr_in serv_addr, cli_addr; + + int n; + + + if (argc < 2) { + fprintf(stderr,"ERROR, no port provided\n"); + exit(1); + } + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + + if (sockfd < 0) + error("ERROR opening socket"); + + bzero((char *) &serv_addr, sizeof(serv_addr)); + + portno = atoi(argv[1]); + + serv_addr.sin_family = AF_INET; + + serv_addr.sin_addr.s_addr = INADDR_ANY; + + serv_addr.sin_port = htons(portno); + + if (bind(sockfd, (struct sockaddr *) &serv_addr, + sizeof(serv_addr)) < 0) + error("ERROR on binding"); + + listen(sockfd,5); + + clilen = sizeof(cli_addr); + + newsockfd = accept(sockfd, + (struct sockaddr *) &cli_addr, + &clilen); + + if (newsockfd < 0) + error("ERROR on accept"); + + bzero(buffer,256); + + 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"); + + close(newsockfd); + + close(sockfd); + + return 0; +} From 70e99f7d4ba4c861c877c1f2a40edec44a371a35 Mon Sep 17 00:00:00 2001 From: William Bracho Blok Date: Wed, 14 Oct 2015 17:18:23 -0500 Subject: [PATCH 03/10] Update README.md --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index e5895c3..c5a8dbe 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,26 @@ # Breakthrough Reposity for the second CSCE 315 project + +I included an example of how to do socket programming; this example uses the server.C file and the client.C file attached: + +How to use these files to see example: + +1) open two different putty windows, one of these windows will be used for the client, and the other one for the server +2) compile each file on separatedly and make two object files using g++ command as follows (on either window): + +g++ server.C -o server +g++ client.C -o client + +3) start the server first, run the server object file with a port number (some number between 2000 and 65535) in the server window (just pick one of the two). For example: ./server 51717 + +once the server runs, it will wait for the client to connect. + +4) Start the client in the other putty window with two arguments: the host name (such as linux.cse.tamu.edu) and the port number used to start the server (int this case: 51717). For example: + +./client linux.cse.tamu.edu 51717 + +Once you start the client, the client can send a message to the server. Type a message, enter it, and the server will receive it. Once the server receives the message, it will display it on the server window and terminate. These files can be modified so that it won't terminate after sending only one message. + +Let me know if you have any questions, +William + From a25faded924b519354c2a4136933be7d53f11298 Mon Sep 17 00:00:00 2001 From: William Bracho Blok Date: Wed, 14 Oct 2015 18:55:17 -0500 Subject: [PATCH 04/10] Update README.md --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c5a8dbe..239712e 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,10 @@ once the server runs, it will wait for the client to connect. Once you start the client, the client can send a message to the server. Type a message, enter it, and the server will receive it. Once the server receives the message, it will display it on the server window and terminate. These files can be modified so that it won't terminate after sending only one message. -Let me know if you have any questions, -William +*Note:* +If you run the server immediately for the second time using the same port number, the server won't accept this number because it says that it is being used. This is because it takes a couple of minutes for the server to release this port number. So in this case you can either run the server using a different name, or wait a couple of minutes before running the server again with the same port number. + +I hope this helps. Let me know if you have any questions. + +-William From 524b2ac121fda902b0ff858824963eeef3caf206 Mon Sep 17 00:00:00 2001 From: William Bracho Blok Date: Wed, 14 Oct 2015 18:55:48 -0500 Subject: [PATCH 05/10] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 239712e..01831ad 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ How to use these files to see example: 2) compile each file on separatedly and make two object files using g++ command as follows (on either window): g++ server.C -o server + g++ client.C -o client 3) start the server first, run the server object file with a port number (some number between 2000 and 65535) in the server window (just pick one of the two). For example: ./server 51717 From 0d7549dd8793aee8a57e90d9d24fca18e6376d24 Mon Sep 17 00:00:00 2001 From: William Bracho Blok Date: Wed, 14 Oct 2015 18:56:36 -0500 Subject: [PATCH 06/10] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 01831ad..a779685 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ Once you start the client, the client can send a message to the server. Type a m *Note:* If you run the server immediately for the second time using the same port number, the server won't accept this number because it says that it is being used. This is because it takes a couple of minutes for the server to release this port number. So in this case you can either run the server using a different name, or wait a couple of minutes before running the server again with the same port number. + I hope this helps. Let me know if you have any questions. -William From ca090d8898ef723e2b784a006fe4012a30c536d3 Mon Sep 17 00:00:00 2001 From: William Bracho Blok Date: Sat, 17 Oct 2015 17:33:11 -0500 Subject: [PATCH 07/10] Create Example socketProgramming --- Example socketProgramming | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Example socketProgramming diff --git a/Example socketProgramming b/Example socketProgramming new file mode 100644 index 0000000..4901216 --- /dev/null +++ b/Example socketProgramming @@ -0,0 +1,28 @@ +I included an example of how to do socket programming; this example uses the server.C file and the client.C file attached: + +How to use these files to see example: + +1) open two different putty windows, one of these windows will be used for the client, and the other one for the server +2) compile each file on separatedly and make two object files using g++ command as follows (on either window): + +g++ server.C -o server + +g++ client.C -o client + +3) start the server first, run the server object file with a port number (some number between 2000 and 65535) in the server window (just pick one of the two). For example: ./server 51717 + +once the server runs, it will wait for the client to connect. + +4) Start the client in the other putty window with two arguments: the host name (such as linux.cse.tamu.edu) and the port number used to start the server (int this case: 51717). For example: + +./client linux.cse.tamu.edu 51717 + +Once you start the client, the client can send a message to the server. Type a message, enter it, and the server will receive it. Once the server receives the message, it will display it on the server window and terminate. These files can be modified so that it won't terminate after sending only one message. + +*Note:* +If you run the server immediately for the second time using the same port number, the server won't accept this number because it says that it is being used. This is because it takes a couple of minutes for the server to release this port number. So in this case you can either run the server using a different name, or wait a couple of minutes before running the server again with the same port number. + + +I hope this helps. Let me know if you have any questions. + +-William From 922d866e263820593668f8778a75411e719ea548 Mon Sep 17 00:00:00 2001 From: William Bracho Blok Date: Sat, 17 Oct 2015 17:34:19 -0500 Subject: [PATCH 08/10] Create main.cpp --- main.cpp | 595 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 595 insertions(+) create mode 100644 main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..cdcb791 --- /dev/null +++ b/main.cpp @@ -0,0 +1,595 @@ +#include +#include +#include +#include + + +using namespace std; + +// class piece +// { + // char color; //w for white, b for white, e for empty + // int xpos; + // int ypos; + + // public: + + // piece(int x, int y) + // { + // xpos = x; + // ypos = y; + // } + + // void setColor(char kolor) + // { + // color = kolor; + // } + + // char getColor() + // { + // return color; + // } +// }; + +struct moves +{ + int row; + char column; + string moveType; + + moves(int linea, int columna, string m) + { + row = linea; + column = columna; + moveType = m; + } +}; + +class Board +{ + char boardArray [8][8]; + char turn = 'O'; + //bool winOrLose; + + public: + + 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'; + } + } + + // winOrLose = false; + + } + + moves parse(string input) //needs to be idiot proof + { + + int temp1; + char temp2; + string temp3; + + temp2 = input[0]; + temp1 = input[1] - '0'; + + if (input[3] == 'L') + { + temp3 = "LEFT"; + } + + else if (input[3] == 'R') + { + temp3 = "RIGHT"; + } + + else + { + temp3 = "FWD"; + } + + moves output(temp1,temp2,temp3); + + return output; + + } + + char getTurn() + { + return turn; + } + + bool isGameOver() + { + for (int i = 0; i < 8; ++i) + { + + if (boardArray[0][i] == 'O') + { + cout<<"\n\n\nplayer O wins!\n\n\n"< 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 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; + } + + + void dumbassAI() + { + + //1) see all possible movements + + vector 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: "< input) +{ + cout<<"\n\nList of possible Moves:"<>move; + b.move(move); + } + + + vector possibleMoves = b.viewPossibleMoves(); + //displayPossibleMoves(possibleMoves); + + + b.dumbassAI(); + + gameOver = b.isGameOver(); + } + +} From cdee60611ec12cbce51d43d0ffb14cb9e7311568 Mon Sep 17 00:00:00 2001 From: William Bracho Blok Date: Sat, 17 Oct 2015 17:48:30 -0500 Subject: [PATCH 09/10] Update README.md --- README.md | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index a779685..f8be7c0 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,12 @@ # Breakthrough Reposity for the second CSCE 315 project -I included an example of how to do socket programming; this example uses the server.C file and the client.C file attached: +The main.cpp file contains all the structure necessary to play breakthrough in a local machine; it has all the classes incorporated to it, and it seems to be bug-free and bulletproof. So far, it only has a simple AI that picks random movements. -How to use these files to see example: +To test it: -1) open two different putty windows, one of these windows will be used for the client, and the other one for the server -2) compile each file on separatedly and make two object files using g++ command as follows (on either window): +- compile using c++11. For example: g++ -std=c++11 main.cpp +- An example of valid command is: B2_FWD, where B is the column letter, 2 is the row number (see board print on screen when running the program), underscore, and either "FWD" for forward, "LEFT" for moving a piece forward (relative to the piece) and left (relative to the board, not the piece), or "RIGHT" (similar to left, but moving right relative to the board). -g++ server.C -o server - -g++ client.C -o client - -3) start the server first, run the server object file with a port number (some number between 2000 and 65535) in the server window (just pick one of the two). For example: ./server 51717 - -once the server runs, it will wait for the client to connect. - -4) Start the client in the other putty window with two arguments: the host name (such as linux.cse.tamu.edu) and the port number used to start the server (int this case: 51717). For example: - -./client linux.cse.tamu.edu 51717 - -Once you start the client, the client can send a message to the server. Type a message, enter it, and the server will receive it. Once the server receives the message, it will display it on the server window and terminate. These files can be modified so that it won't terminate after sending only one message. - -*Note:* -If you run the server immediately for the second time using the same port number, the server won't accept this number because it says that it is being used. This is because it takes a couple of minutes for the server to release this port number. So in this case you can either run the server using a different name, or wait a couple of minutes before running the server again with the same port number. - - -I hope this helps. Let me know if you have any questions. - --William +As of now, the program will automatically terminate once either player wins. From 406547118b7e5dd90c6015b72ddffab63313b661 Mon Sep 17 00:00:00 2001 From: William Bracho Blok Date: Sat, 17 Oct 2015 19:18:47 -0500 Subject: [PATCH 10/10] Update main.cpp --- main.cpp | 51 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index cdcb791..e365e5b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include using namespace std; @@ -31,6 +31,35 @@ using namespace std; // } // }; + +string 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< possibleMoves = b.viewPossibleMoves(); - //displayPossibleMoves(possibleMoves); + //displayPossibleMoves(possibleMoves); for debugging purposes - AI - b.dumbassAI(); + b.easyAI(); gameOver = b.isGameOver(); }