Updating Branch.

This commit is contained in:
Alexander Huddleston 2015-10-29 22:53:02 -05:00
commit 28c6a7f9f7
3 changed files with 278 additions and 0 deletions

View file

@ -11,6 +11,10 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <vector> #include <vector>
#include "Board.h" #include "Board.h"
<<<<<<< HEAD
=======
#include "Engine.h"
>>>>>>> master
using namespace std; using namespace std;
void error(const char *msg) void error(const char *msg)
@ -19,10 +23,51 @@ void error(const char *msg)
exit(1); exit(1);
} }
<<<<<<< HEAD
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int sockfd, newsockfd, portno; int sockfd, newsockfd, portno;
socklen_t clilen; socklen_t clilen;
=======
string tempParse(string move) {
string output = "";
int tempa = move[0] - '0';
int tempb = move[1] - '0';
int tempc = move[2] - '0';
int tempd = move[3] - '0';
//cout << "move: " << move << " " << tempa << " " << tempb << " " << tempc << " " << tempd << "\n";
for(int c = 0; c < 4; c++) {
if(!isdigit(move[c])) {
//cout << "\nfjdkjfjd\n";
return move;
}
}
if(tempa == (tempc + 1)) {
//cout << "\nTest\n";
if(tempb == tempd) {
output = ((char)('A' + tempb)) + to_string(8 - tempa) + "_f";
}
else if(tempb == (tempd + 1)) {
output = ((char)('A' + tempb)) + to_string(8 - tempa) + "_l";
}
else if(tempb == (tempd - 1)) {
output = ((char)('A' + tempb)) + to_string(8 - tempa) + "_r";
}
}
else {
return move;
}
cout << "Debugging: " << output << "\n\n";
return output;
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
>>>>>>> master
struct sockaddr_in serv_addr, cli_addr; struct sockaddr_in serv_addr, cli_addr;
@ -31,6 +76,7 @@ int main(int argc, char *argv[])
if (argc < 2) { if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n"); fprintf(stderr,"ERROR, no port provided\n");
exit(1); exit(1);
<<<<<<< HEAD
} }
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
@ -81,11 +127,51 @@ int main(int argc, char *argv[])
bool gameOver = false; bool gameOver = false;
b.snapshot(); b.snapshot();
=======
}
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
Engine e;
string move;
bool gameOver = false;
vector<Board> record;
e.getBoard()->snapshot(record,(*e.getBoard()));
>>>>>>> master
char buffer[256]; char buffer[256];
char info[256]; char info[256];
int choice; int choice;
int move_counter = 0; int move_counter = 0;
string final_move; string final_move;
<<<<<<< HEAD
//Waiting for client to select game type //Waiting for client to select game type
n = read(newsockfd,info,255); n = read(newsockfd,info,255);
@ -104,10 +190,47 @@ int main(int argc, char *argv[])
b.displayBoard();//Display the board on the server b.displayBoard();//Display the board on the server
string boardState = b.boardToString(); string boardState = b.boardToString();
final_move = b.boardToString(); final_move = b.boardToString();
=======
//Ask client about game type
string introduction = "WELCOME\n1. Play against AI?\n2. Watch AI vs. AI?\nEnter choice: \n";
write(newsockfd, introduction.c_str(), introduction.length());
n = read(newsockfd,info,255); //Reads choice as a string
istringstream convert(info); //Converts the read string to an integer
convert >> choice; //Sets value equal to the converted value
//Later in the project, we need to check for AI and Human modes
cout << "OK" << endl;
bzero(info,256); //Resets info back to normal
/*//Becca- once your AI code is working, use this code:
string difficulty_select = "Choose Difficulty\n1. Easy\n2. Medium\n4. Hard\nEnter choice: \n";
write(newsockfd, difficulty_select.c_str(), difficulty_select.length());
n = read(newsockfd,info,255);
istringstream convert(info);
convert >> choice_difficulty;
cout << "OK" << endl;
bzero(info,256);
*/
while(true) {
while(gameOver != true)
{
gameOver = e.getBoard()->isGameOver();
while(e.getBoard()->getTurn() == 'O' && !e.getBoard()->isValid())
{
e.getBoard()->displayBoard();//Display the board on the server
string boardState = e.getBoard()->boardToString();
final_move = e.getBoard()->boardToString();
>>>>>>> master
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line) write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
cout<<"\nWaiting for client: "; cout<<"\nWaiting for client: ";
n = read(newsockfd,buffer,255); n = read(newsockfd,buffer,255);
move = buffer; move = buffer;
<<<<<<< HEAD
b.interpret(move,b); b.interpret(move,b);
gameOver = b.isGameOver(); gameOver = b.isGameOver();
if(gameOver == true) { if(gameOver == true) {
@ -133,6 +256,47 @@ int main(int argc, char *argv[])
cout << "\n\nGAME OVER!!!"; cout << "\n\nGAME OVER!!!";
//b.displayRecord();for debugging undo/snapchot fnctions //b.displayRecord();for debugging undo/snapchot fnctions
usleep(1); usleep(1);
=======
bzero(buffer,256);
//cout << move << "\n\n";
move = tempParse(move);
//cout << move << "\n\n";
e.getBoard()->interpret(move,(*e.getBoard()));
if(e.getBoard()->isValid()) {
e.getBoard()->changeTurns();
e.getBoard()->setValidFalse();
}
}
if(e.getBoard()->isValid())
cout << e.getBoard()->getTurn();
while(e.getBoard()->getTurn() == 'X' )
{
e.easyAI();
/*Becca- once you finish your AI, uncomment this out and remove the line directly above this
if(choice_difficulty == 1)
easyAI();
else if(choice_difficulty == 2)
mediumAI();
else
hardAI();
*/
}
gameOver = e.getBoard()->isGameOver();
e.getBoard()->setValidFalse();
e.getBoard()->snapshot(record, *e.getBoard());
}
final_move = e.getBoard()->boardToString();
string game_over = "\n\nGAME OVER!!!\n";
write(newsockfd, final_move.c_str(), final_move.length()); //Display the board to the client (line by line)
write(newsockfd, game_over.c_str(), game_over.length()); //Tell the client the game is over
cout << game_over;
usleep(1);
>>>>>>> master
close(newsockfd); close(newsockfd);
close(sockfd); close(sockfd);
break; break;

114
Serverbackup.txt Executable file
View file

@ -0,0 +1,114 @@
cout << "Launching server..." << endl;
int sockfd, newsockfd, portno;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
if (port < 2) {
cout << "ERROR, no port provided\n";
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
cout << "ERROR opening socket";
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = port;
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)
cout << "ERROR on binding";
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
cout << "ERROR on accept";
string move;
bool gameOver = false;
vector<Board> record;
b->snapshot(record, *b);
char buffer[256];
char info[256];
int choice = -1;
int choice_difficulty = -1;
string final_move;
//Ask client about game type
string introduction = "WELCOME\n1. Play against AI?\n2. Watch AI vs. AI?\nEnter choice: \n";
write(newsockfd, introduction.c_str(), introduction.length());
n = read(newsockfd,info,255); //Reads choice as a string
istringstream convert(info); //Converts the read string to an integer
convert >> choice; //Sets value equal to the converted value
//Later in the project, we need to check for AI and Human modes
cout << "OK" << endl;
bzero(info,256); //Resets info back to normal
/*//Becca- once your AI code is working, use this code:
string difficulty_select = "Choose Difficulty\n1. Easy\n2. Medium\n4. Hard\nEnter choice: \n";
write(newsockfd, difficulty_select.c_str(), difficulty_select.length());
n = read(newsockfd,info,255);
istringstream convert(info);
convert >> choice_difficulty;
cout << "OK" << endl;
bzero(info,256);
*/
while (gameOver != true)
{
gameOver = b->isGameOver();
while(b->getTurn() == 'O' && !b->isValid())
{
b->displayBoard();
string boardState = b->boardToString();
final_move = b->boardToString();
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
cout<<"\nStanding by for client... \n";
n = read(newsockfd,buffer,255);//Read the client's input
move = buffer;
bzero(buffer,256);
b->interpret(move, *b);
if(b->isValid()) {
b->changeTurns();
b->setValidFalse();
}
}
if(b->isValid()) cout << b->getTurn();
while(b->getTurn() == 'X' )
{
easyAI();
/*Becca- once you finish your AI, uncomment this out and remove the line directly above this
if(choice_difficulty == 1)
easyAI();
else if(choice_difficulty == 2)
mediumAI();
else
hardAI();
*/
}
gameOver = b->isGameOver();
b->setValidFalse();
b->snapshot(record, *b);
}
final_move = b->boardToString();
string game_over = "\n\nGAME OVER!!!\n";
write(newsockfd, final_move.c_str(), final_move.length()); //Display the board to the client (line by line)
write(newsockfd, game_over.c_str(), game_over.length()); //Tell the client the game is over
cout << game_over;
usleep(1);
close(newsockfd);
close(sockfd);

View file