Skip to content

Commit

Permalink
Now plays against server and other clients
Browse files Browse the repository at this point in the history
  • Loading branch information
dmj12004 committed Apr 30, 2017
1 parent ecad422 commit a5bddbc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
3 changes: 3 additions & 0 deletions include/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Client {
char msgbuf[512];

int timeleft = 180;
int userID;
int oppID;

const static std::string user13;
const static std::string user14;
Expand All @@ -28,6 +30,7 @@ class Client {
int getTime() { return timeleft; };
Client(int userID, int oppID);
std::string amIBlack();
bool isAgainstServer() { return oppID == 0; };
};


Expand Down
17 changes: 12 additions & 5 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ std::string Client::getMsg() {
std::cout << "Game over: \n" << msg << std::endl;
exit(-1);
}


std::cout << msg << std::endl;
// std::cout << "GETMSG: \n" << msg << std::endl;

//update time left
if(newlinePos+1 < msg.size() )
Expand Down Expand Up @@ -153,6 +154,8 @@ Client::Client(int userID, int oppID){
getMsg(); // ?Opponent:
sendMsg(std::to_string(oppID));

this->userID = userID;
this->oppID = oppID;

std::cout << getMsg(); // Game:

Expand All @@ -163,8 +166,12 @@ std::string Client::amIBlack(){

if(m[6] == 'B')
return "B";

int newlinePos = m.find('\n', 0);

return m.substr(newlinePos + 1, m.size());
else if (oppID != 0){
m = getMsg();
return m;
}
else {
int newlinePos = m.find('\n', 0);
return m.substr(newlinePos + 1, m.size());
}
}
18 changes: 12 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ int main(int argc, const char * argv[]) {

bool isFirstMove = true;

std::string player;
//if black, make first move and give to client connection
std::string firstMoveStr = c.amIBlack();
if( firstMoveStr[0] == 'B')
{
player = "Black";
isFirstMove = false;
std::cout << "Black goes first " << std::endl;
std::cout << "\n=======================================" << std::endl;
Expand All @@ -51,10 +53,10 @@ int main(int argc, const char * argv[]) {
curMove = c.getMove();
std::cout << curMove->toString() << std::endl;
}
else
{
curMove = c.getMove(firstMoveStr);
}
else{
player = "White";
curMove = c.getMove(firstMoveStr);
}

bool gameNotOver = true;

Expand All @@ -63,9 +65,9 @@ int main(int argc, const char * argv[]) {
std::cout << "\n=======================================" << std::endl;
std::cout << "waiting on " << board->player() << "..." << std::endl;

if(isFirstMove) {
if(isFirstMove)
isFirstMove = false;
} else
else
curMove = c.getMove();

if(curMove->isEmpty()) { gameNotOver = false; break; }
Expand All @@ -74,6 +76,10 @@ int main(int argc, const char * argv[]) {
std::cout << "Recieved move: " << curMove->toString() << std::endl;
std::cout << "=======================================" << std::endl;

if(!c.isAgainstServer())
c.getMove(); // getting ?Move():

std::cout << "This client - (" << player << ")";

std::cout << "\n=======================================" << std::endl;
std::cout << "generating move... time = " << c.getTime() << std::endl;
Expand Down

0 comments on commit a5bddbc

Please sign in to comment.