2015-10-25 16:43:27 -05:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2015-10-27 21:30:52 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <sstream>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
2015-10-25 16:43:27 -05:00
|
|
|
#include "Engine.h"
|
|
|
|
|
|
|
|
Engine::Engine(){
|
2015-10-25 16:53:32 -05:00
|
|
|
Board* brd = new Board();
|
|
|
|
b = brd;
|
2015-10-25 16:43:27 -05:00
|
|
|
}
|
|
|
|
|
2015-10-27 21:30:52 -05:00
|
|
|
void Engine::startGame(int port){
|
|
|
|
|
|
|
|
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";
|
2015-10-25 16:43:27 -05:00
|
|
|
|
|
|
|
string move;
|
|
|
|
bool gameOver = false;
|
|
|
|
vector<Board> record;
|
2015-10-25 16:53:32 -05:00
|
|
|
b->snapshot(record, *b);
|
2015-10-27 21:30:52 -05:00
|
|
|
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);
|
|
|
|
|
|
|
|
*/
|
2015-10-25 16:43:27 -05:00
|
|
|
|
|
|
|
while (gameOver != true)
|
|
|
|
{
|
2015-10-25 16:53:32 -05:00
|
|
|
gameOver = b->isGameOver();
|
2015-10-25 16:43:27 -05:00
|
|
|
|
2015-10-27 17:30:33 -05:00
|
|
|
while(b->getTurn() == 'O' && !b->isValid())
|
2015-10-25 16:43:27 -05:00
|
|
|
{
|
2015-10-25 16:53:32 -05:00
|
|
|
b->displayBoard();
|
2015-10-27 21:30:52 -05:00
|
|
|
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);
|
2015-10-25 16:53:32 -05:00
|
|
|
b->interpret(move, *b);
|
2015-10-27 17:30:33 -05:00
|
|
|
if(b->isValid()) {
|
|
|
|
b->changeTurns();
|
|
|
|
b->setValidFalse();
|
|
|
|
}
|
2015-10-25 16:43:27 -05:00
|
|
|
}
|
2015-10-27 10:40:55 -05:00
|
|
|
|
2015-10-27 17:30:33 -05:00
|
|
|
if(b->isValid()) cout << b->getTurn();
|
2015-10-27 13:48:45 -05:00
|
|
|
|
2015-10-26 15:22:59 -05:00
|
|
|
while(b->getTurn() == 'X' )
|
2015-10-25 16:43:27 -05:00
|
|
|
{
|
2015-10-27 17:30:33 -05:00
|
|
|
easyAI();
|
2015-10-27 21:30:52 -05:00
|
|
|
/*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();
|
|
|
|
*/
|
2015-10-25 16:43:27 -05:00
|
|
|
}
|
|
|
|
|
2015-10-25 16:53:32 -05:00
|
|
|
gameOver = b->isGameOver();
|
2015-10-27 17:30:33 -05:00
|
|
|
b->setValidFalse();
|
2015-10-25 16:53:32 -05:00
|
|
|
b->snapshot(record, *b);
|
2015-10-25 16:43:27 -05:00
|
|
|
}
|
2015-10-27 21:30:52 -05:00
|
|
|
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);
|
2015-10-25 16:43:27 -05:00
|
|
|
}
|
|
|
|
|
2015-10-25 16:53:32 -05:00
|
|
|
void Engine::easyAI()
|
2015-10-25 16:43:27 -05:00
|
|
|
{
|
2015-10-25 16:53:32 -05:00
|
|
|
vector<moves> listOfMoves = b->viewPossibleMoves();
|
2015-10-27 17:30:33 -05:00
|
|
|
/*
|
|
|
|
for(int x = 0; x < listOfMoves.size(); ++x) {
|
|
|
|
cout << listOfMoves[x].row << " " << listOfMoves[x].column << " " << listOfMoves[x].moveType << "\n\n";
|
|
|
|
}
|
|
|
|
*/
|
2015-10-27 11:31:44 -05:00
|
|
|
srand(time(NULL));
|
2015-10-27 11:34:20 -05:00
|
|
|
int randomChoice = rand() % (listOfMoves.size()-1) - 0;
|
2015-10-27 11:31:44 -05:00
|
|
|
|
2015-10-27 21:19:24 -05:00
|
|
|
//int temp = randomChoice;
|
2015-10-27 14:48:03 -05:00
|
|
|
cout << "easy AI move: " << listOfMoves[randomChoice].row << listOfMoves[randomChoice].column << listOfMoves[randomChoice].moveType << "\n";
|
2015-10-27 11:31:44 -05:00
|
|
|
b->move(listOfMoves[randomChoice]);
|
|
|
|
b->changeTurns();
|
2015-10-25 16:43:27 -05:00
|
|
|
}
|
|
|
|
|
2015-10-25 16:53:32 -05:00
|
|
|
void Engine::AI(){
|
2015-10-26 17:08:48 -05:00
|
|
|
cout << "----------------------BEGIN AI FUNCTION----------------------\n";
|
2015-10-26 15:22:59 -05:00
|
|
|
vector<moves> listOfMoves = b->viewPossibleMoves();
|
2015-10-27 21:19:24 -05:00
|
|
|
Board* temp = new Board(*b);
|
2015-10-27 08:38:25 -05:00
|
|
|
|
|
|
|
//probably not needed, check later
|
2015-10-27 17:30:33 -05:00
|
|
|
/*
|
|
|
|
if (b->getTurn() != 'X'){
|
2015-10-27 08:38:25 -05:00
|
|
|
cout << "a changing of turns is needed. \n";
|
2015-10-27 17:30:33 -05:00
|
|
|
b->changeTurns();
|
2015-10-26 17:08:48 -05:00
|
|
|
}
|
2015-10-27 17:30:33 -05:00
|
|
|
*/
|
2015-10-26 17:08:48 -05:00
|
|
|
//only doing 1 branch right now because testing
|
|
|
|
/*for (int i = 0; i < listOfMoves.size(); ++i){
|
2015-10-27 17:30:33 -05:00
|
|
|
minMax(b, listOfMoves[i]);
|
2015-10-26 17:08:48 -05:00
|
|
|
}*/
|
2015-10-27 10:40:55 -05:00
|
|
|
|
2015-10-27 21:19:24 -05:00
|
|
|
b->move(minMax(temp, listOfMoves[0], 0));
|
2015-10-27 12:55:23 -05:00
|
|
|
b->changeTurns();
|
2015-10-27 10:40:55 -05:00
|
|
|
|
|
|
|
//verification of correct turn
|
2015-10-27 17:30:33 -05:00
|
|
|
/*
|
2015-10-27 10:40:55 -05:00
|
|
|
if (b->getTurn() != 'O'){
|
|
|
|
cout << "ERROR in Engine::AI: b is on the wrong turn. \n";
|
|
|
|
}
|
2015-10-27 17:30:33 -05:00
|
|
|
*/
|
2015-10-27 08:38:25 -05:00
|
|
|
b->displayBoard();
|
2015-10-26 17:08:48 -05:00
|
|
|
cout << "----------------------END AI FUNCTION----------------------\n";
|
2015-10-25 16:43:27 -05:00
|
|
|
}
|
|
|
|
|
2015-10-27 21:19:24 -05:00
|
|
|
moves Engine::minMax(Board* temp, moves m, int c){
|
2015-10-27 14:48:03 -05:00
|
|
|
//testing purposes only, c = finite depth
|
2015-10-27 21:19:24 -05:00
|
|
|
|
|
|
|
cout << "c: " << c << "\n\n";
|
|
|
|
cout << "current turn: " << temp->getTurn() << "\n";
|
|
|
|
vector<moves> listOfMoves = temp->viewPossibleMoves();
|
2015-10-27 12:55:23 -05:00
|
|
|
if (c > 5){
|
2015-10-27 08:38:25 -05:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2015-10-27 21:19:24 -05:00
|
|
|
if (temp->isGameOver() == true){
|
2015-10-26 17:08:48 -05:00
|
|
|
cout << "END OF PATH REACHED\n";
|
2015-10-27 08:38:25 -05:00
|
|
|
return m;
|
2015-10-26 17:08:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2015-10-27 21:19:24 -05:00
|
|
|
if(temp->isThisMovePossible(m.row, m.column, m.moveType) && temp->getPiece(m.row, m.column)->getType() == temp->getTurn()){
|
2015-10-27 08:38:25 -05:00
|
|
|
cout << "piece has been moved in minMax\n";
|
2015-10-27 21:19:24 -05:00
|
|
|
temp->move(m);
|
|
|
|
temp->changeTurns();
|
|
|
|
temp->displayBoard();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m = minMax(temp, listOfMoves[c+1], c+1);
|
2015-10-27 08:38:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < listOfMoves.size(); ++i){
|
2015-10-27 17:30:33 -05:00
|
|
|
//return minMax(b, listOfMoves[i]);
|
2015-10-27 08:38:25 -05:00
|
|
|
}
|
2015-10-27 10:40:55 -05:00
|
|
|
|
2015-10-27 21:19:24 -05:00
|
|
|
temp->displayBoard();
|
2015-10-27 10:40:55 -05:00
|
|
|
//limited recursion
|
2015-10-27 21:19:24 -05:00
|
|
|
minMax(temp, listOfMoves[c], ++c);
|
2015-10-27 10:40:55 -05:00
|
|
|
|
|
|
|
//testing
|
|
|
|
return m;
|
2015-10-27 21:19:24 -05:00
|
|
|
}
|
2015-10-27 21:30:52 -05:00
|
|
|
}
|