Moved server implementation into server.cpp. Changed test.cpp back to original implementation.

This commit is contained in:
Alexander Huddleston 2015-10-27 22:30:11 -05:00
parent 99073c77c5
commit fea54020ff
4 changed files with 41 additions and 96 deletions

View file

@ -15,71 +15,22 @@ Engine::Engine(){
b = brd; b = brd;
} }
void Engine::startGame(int port){ void Engine::startGame(){
cout<<"WELCOME\n";
cout << "Launching server..." << endl; cout<<"1. Play against AI?\n";
int sockfd, newsockfd, portno; cout<<"2. Play against a human?\n";
socklen_t clilen; cout<<"Enter choice: \n";
struct sockaddr_in serv_addr, cli_addr;
int n;
if (port < 2) { int choice = -1;
cout << "ERROR, no port provided\n"; cin >> choice;
exit(1); cout << "OK" << endl;
}
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; string move;
bool gameOver = false; bool gameOver = false;
vector<Board> record; vector<Board> record;
b->snapshot(record, *b); 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) while (gameOver != true)
{ {
@ -88,13 +39,9 @@ void Engine::startGame(int port){
while(b->getTurn() == 'O' && !b->isValid()) while(b->getTurn() == 'O' && !b->isValid())
{ {
b->displayBoard(); b->displayBoard();
string boardState = b->boardToString(); cout<<"\nEnter command: ";
final_move = b->boardToString(); cin>>move;
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line) cout << "\n";
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); b->interpret(move, *b);
if(b->isValid()) { if(b->isValid()) {
b->changeTurns(); b->changeTurns();
@ -107,28 +54,13 @@ void Engine::startGame(int port){
while(b->getTurn() == 'X' ) while(b->getTurn() == 'X' )
{ {
easyAI(); 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(); gameOver = b->isGameOver();
b->setValidFalse(); b->setValidFalse();
b->snapshot(record, *b); 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);
} }
void Engine::easyAI() void Engine::easyAI()

View file

@ -9,8 +9,9 @@ class Engine {
public: public:
Engine(); Engine();
void startGame(int port); void startGame();
void easyAI(); void easyAI();
void AI(); void AI();
Board* getBoard() { return b; }
moves minMax(Board* temp, moves m, int c); moves minMax(Board* temp, moves m, int c);
}; };

View file

@ -11,6 +11,7 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <vector> #include <vector>
#include "Board.h" #include "Board.h"
#include "Engine.h"
using namespace std; using namespace std;
void error(const char *msg) void error(const char *msg)
@ -75,42 +76,47 @@ int main(int argc, char *argv[])
// cin >> choice; // cin >> choice;
// cout << "OK" << endl; // cout << "OK" << endl;
Board b; Engine e;
//Board* temp = e.getBoard();
//Board b = *temp;
string move; string move;
//Brute force up in here! //Brute force up in here!
bool gameOver = false; bool gameOver = false;
vector<Board> record; vector<Board> record;
b.snapshot(record,b); e.getBoard()->snapshot(record,(*e.getBoard()));
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;
write(newsockfd, "Select a choice:\n", 18);
//Waiting for client to select game type //Waiting for client to select game type
n = read(newsockfd,info,255); n = read(newsockfd,info,255);
istringstream convert(info); istringstream convert(info);
convert >> choice; //Sets choice equal to 1 or 2, based on clients input convert >> choice; //Sets choice equal to 1 or 2, based on clients input
//cout << choice << "\n\n";
bzero(info,256); //Resets info back to normal, "choice" now contains client's value bzero(info,256); //Resets info back to normal, "choice" now contains client's value
while(true) { while(true) {
while(gameOver != true) while(gameOver != true)
{ {
gameOver = b.isGameOver(); gameOver = e.getBoard()->isGameOver();
while(b.getTurn() == 'O' ) while(e.getBoard()->getTurn() == 'O' )
{ {
b.displayBoard();//Display the board on the server e.getBoard()->displayBoard();//Display the board on the server
string boardState = b.boardToString(); string boardState = e.getBoard()->boardToString();
//final_move = b.boardToString(); //final_move = b.boardToString();
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;
b.interpret(move,b); cout << "\ntest" << move << "\n\n";
gameOver = b.isGameOver(); e.getBoard()->interpret(move,(*e.getBoard()));
gameOver = e.getBoard()->isGameOver();
if(gameOver == true) { if(gameOver == true) {
string endGame = "Game_Over"; string endGame = "Game_Over";
write(newsockfd, endGame.c_str(), endGame.length()); //Display the board to the client (line by line) write(newsockfd, endGame.c_str(), endGame.length()); //Display the board to the client (line by line)
@ -119,11 +125,14 @@ int main(int argc, char *argv[])
else { else {
string continueGame = "Continue_Game"; string continueGame = "Continue_Game";
write(newsockfd, continueGame.c_str(), continueGame.length()); //Display the board to the client (line by line) write(newsockfd, continueGame.c_str(), continueGame.length()); //Display the board to the client (line by line)
e.getBoard()->changeTurns();
} }
} }
vector<moves> possibleMoves = b.viewPossibleMoves(); vector<moves> possibleMoves = e.getBoard()->viewPossibleMoves();
if(choice == 1) if(choice == 1) {
b.easyAI(); cout << "test\n\n";
e.easyAI();
}
} }
/* /*

View file

@ -4,11 +4,14 @@ using namespace std;
int main() int main()
{ {
//Board b;
Engine e; Engine e;
/*
int pn = 0; int pn = 0;
cout << "Welcome to Breakthrough server launcher, please enter a host port number: \n"; cout << "Welcome to Breakthrough server launcher, please enter a host port number: \n";
cin >> pn; cin >> pn;
cin.clear(); cin.clear();
cin.ignore(10000,'\n'); cin.ignore(10000,'\n');
e.startGame(pn); */
e.startGame();
} }