Merge branch 'beccadev' of https://github.tamu.edu/brj2013/Breakthrough into beccadev

This commit is contained in:
Alexander Huddleston 2015-10-20 22:09:03 -05:00
commit 09dd424f7d
10 changed files with 274 additions and 211 deletions

View file

@ -87,8 +87,7 @@ void Board::changeTurns()
}
void Board::displayBoard()
{
cout<<"\n\n";
{
cout<<"; A B C D E F G H"<<endl;
for (int i = 0; i < 8; ++i)
{
@ -101,7 +100,7 @@ void Board::displayBoard()
cout<<"|\n";
}
cout<<'\n'<<endl;
cout<<"turn : "<<turn;
cout<<"turn : "<<turn << "\n";
}
int Board::charToIntColumn(char input) //converts column number to int
@ -144,17 +143,16 @@ char Board::intToCharColumn(int input) //converts column number to int
void Board::move(string inputMove)
{
moves jugada = parse(inputMove);
int row = 8 - (jugada.row);
int kolumn = charToIntColumn(jugada.column);
int temp = boardArray[row][kolumn];
int reflector = 1;
if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0)
{
cout<<"ERROR: index out of bound!"<<endl;
cout<<"ERROR: index out of bound in move()!"<<endl;
}
else if (temp != turn)
@ -263,7 +261,7 @@ void Board::move(moves jugada)
if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0)
{
cout<<"ERROR: index out of bound!"<<endl;
cout<<"ERROR: index out of bound in second move()!"<<endl;
}
int temp = boardArray[row][kolumn];
@ -466,15 +464,6 @@ 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)
{
vector<Board> record;
@ -545,17 +534,19 @@ void Board::easyAI()
vector<moves> 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: "<<listOfMoves[temp].column<<" "<<listOfMoves[temp].row<<" "<<listOfMoves[temp].moveType<<endl; uncomment for debugging purposes
//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]);
}
}

View file

@ -37,7 +37,6 @@ public:
bool isThisMovePossible(int r, int c, string moveType);
vector<moves> viewPossibleMoves();
string myToUpper(string input);
void displayPossibleMoves(vector<moves> input);
void undo(Board& tablero);
void interpret(string input, Board& tablero);
void snapshot(vector<Board>& inputVec, Board inputBoard);

93
Client.cpp Normal file
View file

@ -0,0 +1,93 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#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;
}

View file

@ -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

View file

@ -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.

160
Server.cpp Normal file
View file

@ -0,0 +1,160 @@
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include <unistd.h>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <vector>
#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<Board> 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<moves> 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;
}

BIN
a.out

Binary file not shown.

View file

@ -1,57 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
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;
}

View file

@ -1,83 +0,0 @@
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
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;
}

View file

@ -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<moves> 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);
}