From cc7da221640e706cebaf773f0b5bf86e765fe98e Mon Sep 17 00:00:00 2001 From: David Jardim Date: Thu, 27 Apr 2017 18:59:27 -0400 Subject: [PATCH] Iteraction with server added --- include/Client.hpp | 12 ++++-- include/Coordinate.h | 3 +- include/MinimaxSearch.h | 2 +- include/Move.h | 5 ++- src/BitBoard.cpp | 1 - src/Client.cpp | 64 +++++++++++++++++++++++++------- src/Move.cpp | 43 +++++++++++++++++++++ src/main.cpp | 82 ++++++++++++++++++++++++++++++++++++----- 8 files changed, 182 insertions(+), 30 deletions(-) diff --git a/include/Client.hpp b/include/Client.hpp index f1c9a04..300aec6 100644 --- a/include/Client.hpp +++ b/include/Client.hpp @@ -10,6 +10,8 @@ class Client { int socketfd; char msgbuf[512]; + int timeleft = 180; + const static std::string user13; const static std::string user14; const static std::string password13; @@ -17,13 +19,15 @@ class Client { std::stringstream s; int StringToSockaddr(char *name, struct sockaddr_in *address); +public: std::string getMsg(); int sendMsg(std::string cmd); - Move getMove(); - int sendMove(Move m); -public: + std::unique_ptr getMove(); + std::unique_ptr getMove(std::string msgBuf); + void giveMove(Move m); + int getTime() { return timeleft; }; Client(int userID, int oppID); - + std::string amIBlack(); }; diff --git a/include/Coordinate.h b/include/Coordinate.h index 832383a..b9c906a 100644 --- a/include/Coordinate.h +++ b/include/Coordinate.h @@ -13,7 +13,8 @@ class Coordinate Coordinate(int x, int y) : m_x(x), m_y(y) {}; int getX() const { return m_x; } int getY() const { return m_y; } - + void setX(int x) { m_x = x; } + void setY(int y) { m_y = y; } std::string toString() const; }; diff --git a/include/MinimaxSearch.h b/include/MinimaxSearch.h index fa56d7e..be8d9ce 100644 --- a/include/MinimaxSearch.h +++ b/include/MinimaxSearch.h @@ -13,7 +13,7 @@ typedef std::unique_ptr mup; class MinimaxSearch { private: - static const int maxDepth = 6; + static const int maxDepth = 8; Heuristic heuristic; double minValue(std::unique_ptr &board, int currentDepth); diff --git a/include/Move.h b/include/Move.h index b5f2269..04cc92e 100644 --- a/include/Move.h +++ b/include/Move.h @@ -11,12 +11,14 @@ class Move { private: std::vector moves; - + static const std::vector coordinateMap; + static const int moveMap[8][4]; public: Move(); + Move(std::string moveStr); Move(int start); Move(const Move &move); void addMove(int move); @@ -28,6 +30,7 @@ class Move void printMoves() const; std::string toString() const; + std::string toStringRotated() const; }; #endif diff --git a/src/BitBoard.cpp b/src/BitBoard.cpp index 403fdec..6005c7c 100644 --- a/src/BitBoard.cpp +++ b/src/BitBoard.cpp @@ -240,7 +240,6 @@ bup BitBoard::result(mup &move) const } } - currBoard.m_isBlacksTurn = !m_isBlacksTurn; bup result (new BitBoard(currBoard)); return result; diff --git a/src/Client.cpp b/src/Client.cpp index 6536625..c9e9b5d 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -56,6 +56,30 @@ int Client::StringToSockaddr(char *name, struct sockaddr_in *address) std::string Client::getMsg() { bzero(msgbuf, BUFSIZE); recv(socketfd, msgbuf, BUFSIZE, 0); + + std::string msg(msgbuf); + int newlinePos = msg.find('\n', 0); + + // if 'Result' found - game over + if(msg[0] == 'R' || msg[newlinePos+1] == 'R' || msg[0] == 'E'){ + std::cout << "Game over: \n" << msg << std::endl; + exit(-1); + } + + std::cout << msg << std::endl; + + //update time left + if(newlinePos+1 < msg.size() ) + { + + int start = msg.find('(', newlinePos); + int end = msg.find(')', newlinePos); + std::string timeStr = msg.substr(start+1, end - start - 1); + + timeleft = std::stoi(timeStr); + + } + return msgbuf; } @@ -71,13 +95,20 @@ int Client::sendMsg(std::string cmd){ return 0; } -Move Client::getMove(){ - Move m; +std::unique_ptr Client::getMove(){ + std::unique_ptr m(new Move(getMsg())); return m; } -int Client::sendMove(Move m){ - return 0; +std::unique_ptr Client::getMove(std::string moveBuf){ + std::unique_ptr m(new Move(moveBuf)); + return m; +} + + + +void Client::giveMove(Move m){ + sendMsg(m.toStringRotated()); } Client::Client(int userID, int oppID){ @@ -103,29 +134,36 @@ Client::Client(int userID, int oppID){ exit(-1); } - std::cout << getMsg(); - std::cout << getMsg(); + getMsg(); // SAMv1.0 msg + getMsg(); // ?Username: if(userID == 13) sendMsg(user13); else sendMsg(user14); - std::cout << getMsg(); + getMsg(); // ?Password: if(userID == 13) sendMsg(password13); else sendMsg(password14); - std::cout << getMsg(); - // std::cout << getMsg(); + getMsg(); // ?Opponent: sendMsg(std::to_string(oppID)); - std::cout << getMsg(); - std::cout << getMsg(); - std::cout << getMsg(); - std::cout << getMsg(); + std::cout << getMsg(); // Game: + } +std::string Client::amIBlack(){ + std::string m = getMsg(); + + if(m[6] == 'B') + return "B"; + + int newlinePos = m.find('\n', 0); + + return m.substr(newlinePos + 1, m.size()); +} diff --git a/src/Move.cpp b/src/Move.cpp index b8c9887..6ff8cc2 100644 --- a/src/Move.cpp +++ b/src/Move.cpp @@ -15,8 +15,37 @@ const std::vector Move::coordinateMap = { Coordinate(6, 4), Coordinate(7, 5) }; +const int Move::moveMap[8][4] = { + {18, 12, 6, 0}, + {19, 13, 7, 1}, + {26, 20, 14, 8}, + {27, 21, 15, 9}, + { 2, 28, 22, 16}, + { 3, 29, 23, 17}, + {10, 4, 30, 24}, + {11, 5, 31, 25} +}; + Move::Move() : moves() {}; +Move::Move(std::string movesStr) // rotates board coordinates +{ + // std::cout << movesStr; + movesStr = movesStr.substr(11, movesStr.size()); + + int newlinePos = movesStr.find('\n', 0); + for(int i = 0; i < newlinePos - 1 ; i+=6) + { + int a = 7 - (movesStr[i+1] - 48); + int b = 7 - (movesStr[i+3] - 48); + int m = moveMap[a][b/2]; + moves.push_back(m); + } + + // std::cout << toString() << std::endl; + +} + Move::Move(int start) { moves = {start}; @@ -68,3 +97,17 @@ std::string Move::toString() const return result; } +std::string Move::toStringRotated() const +{ + std::string result; + int movesLength = moves.size(); + for (int i = 0; i < movesLength; ++i) { + Coordinate c = coordinateMap[moves[i]]; + c.setX(7 - c.getX()); + c.setY(7 - c.getY()); + result += c.toString(); + if (i != movesLength - 1) result+= ":"; + } + + return result; +} diff --git a/src/main.cpp b/src/main.cpp index 3cff5d9..f56fb66 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,21 +10,85 @@ #include "Client.hpp" int main(int argc, const char * argv[]) { - - Client c(13, 0); - /* - std::unique_ptr board (new BitBoard); - Heuristic heur_test; - MinimaxSearch searchDriver(heur_test); + if(argc < 3) + { + std::cout << "Please specify args: userID oppID\n"; + std::cout << "E.g. ./build/runner 13 0\n"; + return -1; + } + + // Set up telnet connection + Client c(atoi(argv[1]), atoi(argv[2])); + + std::unique_ptr board (new BitBoard); + Heuristic heur_test; + MinimaxSearch searchDriver(heur_test); + + std::unique_ptr curMove; + int isMoveRequested; + + bool isFirstMove = true; + + //if black, make first move and give to client connection + std::string firstMoveStr = c.amIBlack(); + if( firstMoveStr[0] == 'B') + { + isFirstMove = false; + std::cout << "Black goes first " << std::endl; + std::cout << "\n=======================================" << std::endl; + std::cout << "generating move... time = " << c.getTime() << std::endl; + + std::unique_ptr bestMove = searchDriver.minimaxDecision(board); + + board = board->result(bestMove); + c.giveMove(*bestMove); + board->printState(); + std::cout << "Sending move: " << bestMove->toString() << std::endl; + std::cout << "=======================================" << std::endl; - for (int i = 0; i < 100; ++i) { + // get move echo and throw it away + curMove = c.getMove(); + std::cout << curMove->toString() << std::endl; + } + else + { + curMove = c.getMove(firstMoveStr); + } + + bool gameNotOver = true; + + while(gameNotOver) + { + std::cout << "\n=======================================" << std::endl; + std::cout << "waiting on " << board->player() << "..." << std::endl; + + if(isFirstMove) { + isFirstMove = false; + } else + curMove = c.getMove(); + + if(curMove->isEmpty()) { gameNotOver = false; break; } + board = board->result(curMove); board->printState(); - std::unique_ptr bestMove = searchDriver.minimaxDecision(board); + std::cout << "Recieved move: " << curMove->toString() << std::endl; + std::cout << "=======================================" << std::endl; + + + std::cout << "\n=======================================" << std::endl; + std::cout << "generating move... time = " << c.getTime() << std::endl; + // get best move and give to client connection + std::unique_ptr bestMove = searchDriver.minimaxDecision(board); + c.giveMove(*bestMove); board = board->result(bestMove); + board->printState(); + std::cout << "Sending move: " << bestMove->toString() << std::endl; + + + // get move echo and throw it away + c.getMove(); std::cout << "=======================================" << std::endl; } - */ return 0; }