Skip to content

Commit

Permalink
Iteraction with server added
Browse files Browse the repository at this point in the history
  • Loading branch information
dmj12004 committed Apr 27, 2017
1 parent c77d88c commit cc7da22
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 30 deletions.
12 changes: 8 additions & 4 deletions include/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ 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;
const static std::string password14;

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<Move> getMove();
std::unique_ptr<Move> getMove(std::string msgBuf);
void giveMove(Move m);
int getTime() { return timeleft; };
Client(int userID, int oppID);

std::string amIBlack();
};


Expand Down
3 changes: 2 additions & 1 deletion include/Coordinate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
2 changes: 1 addition & 1 deletion include/MinimaxSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ typedef std::unique_ptr<Move> mup;
class MinimaxSearch
{
private:
static const int maxDepth = 6;
static const int maxDepth = 8;
Heuristic heuristic;

double minValue(std::unique_ptr<BitBoard> &board, int currentDepth);
Expand Down
5 changes: 4 additions & 1 deletion include/Move.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ class Move
{
private:
std::vector<int> moves;

static const std::vector<Coordinate> coordinateMap;
static const int moveMap[8][4];

public:

Move();
Move(std::string moveStr);
Move(int start);
Move(const Move &move);
void addMove(int move);
Expand All @@ -28,6 +30,7 @@ class Move
void printMoves() const;

std::string toString() const;
std::string toStringRotated() const;
};

#endif
1 change: 0 additions & 1 deletion src/BitBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ bup BitBoard::result(mup &move) const
}
}


currBoard.m_isBlacksTurn = !m_isBlacksTurn;
bup result (new BitBoard(currBoard));
return result;
Expand Down
64 changes: 51 additions & 13 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -71,13 +95,20 @@ int Client::sendMsg(std::string cmd){
return 0;
}

Move Client::getMove(){
Move m;
std::unique_ptr<Move> Client::getMove(){
std::unique_ptr<Move> m(new Move(getMsg()));
return m;
}

int Client::sendMove(Move m){
return 0;
std::unique_ptr<Move> Client::getMove(std::string moveBuf){
std::unique_ptr<Move> m(new Move(moveBuf));
return m;
}



void Client::giveMove(Move m){
sendMsg(m.toStringRotated());
}

Client::Client(int userID, int oppID){
Expand All @@ -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());
}
43 changes: 43 additions & 0 deletions src/Move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,37 @@ const std::vector<Coordinate> 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};
Expand Down Expand Up @@ -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;
}
82 changes: 73 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,85 @@
#include "Client.hpp"

int main(int argc, const char * argv[]) {

Client c(13, 0);

/*
std::unique_ptr<BitBoard> 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<BitBoard> board (new BitBoard);
Heuristic heur_test;
MinimaxSearch searchDriver(heur_test);

std::unique_ptr<Move> 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<Move> 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<Move> 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<Move> 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;
}

0 comments on commit cc7da22

Please sign in to comment.