diff --git a/Board.cpp b/Board.cpp index 479dfd1..b1473b4 100755 --- a/Board.cpp +++ b/Board.cpp @@ -87,8 +87,7 @@ void Board::changeTurns() } void Board::displayBoard() -{ - cout<<"\n\n"; +{ cout<<"; A B C D E F G H"< 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!"< input) -{ - cout<<"\n\nList of possible Moves:"< record; @@ -545,17 +534,19 @@ void Board::easyAI() 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: "< 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); diff --git a/Client.cpp b/Client.cpp new file mode 100644 index 0000000..73bb121 --- /dev/null +++ b/Client.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Board.h" +using namespace std; + +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; + + + 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"); + + //Client has successfully joined + char buffer[256]; + char info[256]; + + cout<<"WELCOME\n"; + + cout<<"1. Play against AI?\n"; + cout<<"2. Play against a human?\n"; + cout<<"Enter choice: \n"; + + string choice; + cin >> choice; + + //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 + + + 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 + + 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 + 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") { + cout << "GAME OVER!!!" << endl; + break; + } + else + continue; + + } + close(sockfd); + return 0; +} diff --git a/Example socketProgramming b/Example socketProgramming deleted file mode 100644 index 4901216..0000000 --- a/Example socketProgramming +++ /dev/null @@ -1,28 +0,0 @@ -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 diff --git a/README.md b/README.md deleted file mode 100644 index f8be7c0..0000000 --- a/README.md +++ /dev/null @@ -1,12 +0,0 @@ -# Breakthrough -Reposity for the second CSCE 315 project - -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. - -To test it: - -- 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). - -As of now, the program will automatically terminate once either player wins. - diff --git a/Server.cpp b/Server.cpp new file mode 100644 index 0000000..3630ebc --- /dev/null +++ b/Server.cpp @@ -0,0 +1,160 @@ +/* A simple server in the internet domain using TCP + The port number is passed as an argument */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Board.h" +using namespace std; + +void error(const char *msg) +{ + perror(msg); + exit(1); +} + +int main(int argc, char *argv[]) +{ + int sockfd, newsockfd, portno; + socklen_t clilen; + + 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"); + + //After all the server setup crap is done, we start the board + + // cout<<"WELCOME\n"; + + // cout<<"1. Play against AI?\n"; + // cout<<"2. Play against a human?\n"; + // cout<<"Enter choice: \n"; + + + // cin >> choice; + // cout << "OK" << endl; + + Board b; + string move; + + //Brute force up in here! + bool gameOver = false; + vector record; + b.snapshot(record,b); + char buffer[256]; + char info[256]; + int 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(true) { + + while(gameOver != true) + { + gameOver = b.isGameOver(); + + while(b.getTurn() == 'O' ) + { + 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) + cout<<"\nWaiting for client: "; + n = read(newsockfd,buffer,255); + move = buffer; + b.interpret(move,b); + + 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) + } + } + + //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) + b.easyAI(); + } + + 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; +} diff --git a/a.out b/a.out index 20d22fc..8a730d5 100755 Binary files a/a.out and b/a.out differ diff --git a/client.C b/client.C deleted file mode 100644 index 181de35..0000000 --- a/client.C +++ /dev/null @@ -1,57 +0,0 @@ -#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; -} diff --git a/server.C b/server.C deleted file mode 100644 index 0c61a0e..0000000 --- a/server.C +++ /dev/null @@ -1,83 +0,0 @@ -/* 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; -} diff --git a/test.cpp b/test.cpp index 368e689..7dd63ca 100755 --- a/test.cpp +++ b/test.cpp @@ -12,7 +12,7 @@ int main() cout<<"1. Play against AI?\n"; cout<<"2. Play against a human?\n"; - cout<<"CHANGE THIS TO PARSE THINGS\n"; + //cout<<"CHANGE THIS TO PARSE THINGS\n"; cout<<"Enter choice: \n"; int choice; @@ -40,7 +40,6 @@ int main() vector possibleMoves = b.viewPossibleMoves(); - //displayPossibleMoves(possibleMoves); for debugging purposes - AI if (choice == 1) { @@ -53,6 +52,7 @@ int main() { b.displayBoard(); cout<<"\nEnter command: "; + cout<<"OK\n"; cin>>move; b.interpret(move,b); }