Compare commits
No commits in common. "dev/client-server" and "master" have entirely different histories.
dev/client
...
master
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1 @@
|
|||||||
bin/*
|
bin/*
|
||||||
*.swp
|
|
||||||
|
6
Makefile
6
Makefile
@ -1,6 +1,2 @@
|
|||||||
azool:
|
azool:
|
||||||
mkdir -p bin
|
g++ src/*.cc -I./include -o bin/azool -Werror -Weffc++ -std=c++11
|
||||||
g++ src/*.cc -I./include -o bin/azool -Werror -Weffc++ -std=c++11 -DBOOST_ALLOW_DEPRECATED_HEADERS -DBOOST_BIND_GLOBAL_PLACEHOLDERS
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm bin/*
|
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <boost/property_tree/ptree.hpp>
|
|
||||||
#include "tile_utils.h"
|
#include "tile_utils.h"
|
||||||
|
|
||||||
class GameBoard {
|
class GameBoard {
|
||||||
@ -18,17 +17,13 @@ public:
|
|||||||
|
|
||||||
GameBoard(int nPlayers=2);
|
GameBoard(int nPlayers=2);
|
||||||
friend std::ostream& operator<<(std::ostream& out, const GameBoard& board);
|
friend std::ostream& operator<<(std::ostream& out, const GameBoard& board);
|
||||||
bool validFactoryRequest(int factoryIdx, azool::TileColor color) const;
|
bool validFactoryRequest(int factoryIdx, azool::TileColor color);
|
||||||
bool takeTilesFromFactory(int factoryIdx, azool::TileColor color, int& numTiles);
|
bool takeTilesFromFactory(int factoryIdx, azool::TileColor color, int& numTiles);
|
||||||
bool takeTilesFromPool(azool::TileColor color, int& numTiles, bool& poolPenalty);
|
bool takeTilesFromPool(azool::TileColor color, int& numTiles, bool& poolPenalty);
|
||||||
void returnTilesToBag(int numTiles, azool::TileColor color);
|
void returnTilesToBag(int numTiles, azool::TileColor color);
|
||||||
void dealTiles();
|
void dealTiles();
|
||||||
std::string handleRequest(const std::string&);
|
int numFactories() { return tileFactories.size(); }
|
||||||
boost::property_tree::ptree serializeBoard() const;
|
bool endOfRound() {
|
||||||
int numFactories() {
|
|
||||||
return tileFactories.size();
|
|
||||||
}
|
|
||||||
bool endOfRound() const {
|
|
||||||
// round ends when the pool and tile factories are empty
|
// round ends when the pool and tile factories are empty
|
||||||
for (int ii = 0; ii < azool::NUMCOLORS; ++ii) {
|
for (int ii = 0; ii < azool::NUMCOLORS; ++ii) {
|
||||||
if (pool[ii] > 0) return false;
|
if (pool[ii] > 0) return false;
|
||||||
@ -42,7 +37,6 @@ private:
|
|||||||
// - vector of factories?
|
// - vector of factories?
|
||||||
std::vector<Factory> tileFactories;
|
std::vector<Factory> tileFactories;
|
||||||
int maxNumFactories;
|
int maxNumFactories;
|
||||||
int myNumPlayers;
|
|
||||||
int pool[azool::NUMCOLORS]; // stores the count of each color currently in the pool; initialize to 0s
|
int pool[azool::NUMCOLORS]; // stores the count of each color currently in the pool; initialize to 0s
|
||||||
bool whiteTileInPool; // initialize to true
|
bool whiteTileInPool; // initialize to true
|
||||||
std::vector<azool::TileColor> tileBag; // initialize to 20 of each color
|
std::vector<azool::TileColor> tileBag; // initialize to 20 of each color
|
||||||
|
@ -15,19 +15,10 @@ public:
|
|||||||
void placeTiles(int rowIdx, azool::TileColor color, int numTiles);
|
void placeTiles(int rowIdx, azool::TileColor color, int numTiles);
|
||||||
void endRound(bool& fullRow);
|
void endRound(bool& fullRow);
|
||||||
void finalizeScore();
|
void finalizeScore();
|
||||||
std::string sendRequest(const std::string&);
|
int getScore() const { return myScore; }
|
||||||
int getScore() const {
|
|
||||||
return myScore;
|
|
||||||
}
|
|
||||||
std::string printMyBoard() const;
|
std::string printMyBoard() const;
|
||||||
bool tookPenalty() const {
|
bool tookPenalty() const { return myTookPoolPenaltyThisRound; }
|
||||||
return myTookPoolPenaltyThisRound;
|
const std::string getPlayerName() const { return myName; }
|
||||||
}
|
|
||||||
int getScorePenalty() const;
|
|
||||||
const std::string getPlayerName() const {
|
|
||||||
return myName;
|
|
||||||
}
|
|
||||||
void sendRequest(/*string or stringstream?*/);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Player(const Player&) = delete;
|
Player(const Player&) = delete;
|
||||||
|
@ -19,11 +19,5 @@ const std::string TileColorStrings[NUMCOLORS] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const char TileColorSyms[NUMCOLORS] = { 'r', 'b', 'g', 'y', 'k' };
|
const char TileColorSyms[NUMCOLORS] = { 'r', 'b', 'g', 'y', 'k' };
|
||||||
const int MAXPLAYERS = 4;
|
|
||||||
|
|
||||||
const std::string REQ_TYPE_DRAW_FROM_FACTORY = "DRAW_FROM_FACTORY";
|
|
||||||
const std::string REQ_TYPE_DRAW_FROM_POOL = "DRAW_FROM_POOL";
|
|
||||||
const std::string REQ_TYPE_RETURN_TO_BAG = "RETURN_TO_BAG";
|
|
||||||
const std::string REQ_TYPE_GET_BOARD = "GET_BOARD";
|
|
||||||
}
|
}
|
||||||
#endif // TILE_UTILS_H_
|
#endif // TILE_UTILS_H_
|
||||||
|
100
src/GameBoard.cc
100
src/GameBoard.cc
@ -1,14 +1,9 @@
|
|||||||
#include "GameBoard.h"
|
#include "GameBoard.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <boost/property_tree/json_parser.hpp>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
namespace pt = boost::property_tree;
|
|
||||||
|
|
||||||
GameBoard::GameBoard(int numPlayers) :
|
GameBoard::GameBoard(int numPlayers) :
|
||||||
tileFactories(),
|
tileFactories(),
|
||||||
myNumPlayers(numPlayers),
|
|
||||||
maxNumFactories(numPlayers*2+1),
|
maxNumFactories(numPlayers*2+1),
|
||||||
pool(),
|
pool(),
|
||||||
whiteTileInPool(true),
|
whiteTileInPool(true),
|
||||||
@ -42,10 +37,9 @@ std::ostream& operator<<(std::ostream& out, const GameBoard& board) {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GameBoard::validFactoryRequest(int factoryIdx, azool::TileColor color) const {
|
bool GameBoard::validFactoryRequest(int factoryIdx, azool::TileColor color) {
|
||||||
// check if color exists on specified factory
|
// check if color exists on specified factory
|
||||||
bool retVal = factoryIdx < tileFactories.size() and
|
bool retVal = factoryIdx < tileFactories.size() and
|
||||||
factoryIdx > -1 and
|
|
||||||
tileFactories[factoryIdx].tileCounts[color] > 0;
|
tileFactories[factoryIdx].tileCounts[color] > 0;
|
||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
@ -78,12 +72,12 @@ bool GameBoard::takeTilesFromPool(azool::TileColor color, int& numTiles, bool& p
|
|||||||
poolPenalty = whiteTileInPool;
|
poolPenalty = whiteTileInPool;
|
||||||
whiteTileInPool = false;
|
whiteTileInPool = false;
|
||||||
return true;
|
return true;
|
||||||
} // GameBoard::takeTilesFromPool
|
}
|
||||||
void GameBoard::returnTilesToBag(int numTiles, azool::TileColor color) {
|
void GameBoard::returnTilesToBag(int numTiles, azool::TileColor color) {
|
||||||
for (int ii = 0; ii < numTiles; ++ii) {
|
for (int ii = 0; ii < numTiles; ++ii) {
|
||||||
tileBag.emplace_back(color);
|
tileBag.emplace_back(color);
|
||||||
}
|
}
|
||||||
} // GameBoard::returnTilesToBag
|
}
|
||||||
|
|
||||||
// random shuffle then read from the beginning of the vector?
|
// random shuffle then read from the beginning of the vector?
|
||||||
void GameBoard::dealTiles() {
|
void GameBoard::dealTiles() {
|
||||||
@ -108,94 +102,6 @@ void GameBoard::dealTiles() {
|
|||||||
}
|
}
|
||||||
} // GameBoard::dealTiles
|
} // GameBoard::dealTiles
|
||||||
|
|
||||||
pt::ptree GameBoard::serializeBoard() const {
|
|
||||||
pt::ptree outTree;
|
|
||||||
// factories
|
|
||||||
pt::ptree factTree;
|
|
||||||
int factCt = 1;
|
|
||||||
for (auto factory : tileFactories) {
|
|
||||||
std::stringstream factSS;
|
|
||||||
for (int ii = 0; ii < azool::NUMCOLORS; ++ii) {
|
|
||||||
for (int jj = 0; jj < factory.tileCounts[ii]; ++jj) {
|
|
||||||
factSS << azool::TileColorStrings[ii] << ",";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
factTree.put(std::to_string(factCt), factSS.str());
|
|
||||||
factCt++;
|
|
||||||
}
|
|
||||||
outTree.add_child("factories", factTree);
|
|
||||||
outTree.put("num_factories", tileFactories.size());
|
|
||||||
// pool
|
|
||||||
pt::ptree poolTree;
|
|
||||||
for (int ii = 0; ii < azool::NUMCOLORS; ++ii) {
|
|
||||||
poolTree.put(azool::TileColorStrings[ii], pool[ii]);
|
|
||||||
}
|
|
||||||
if (whiteTileInPool) {
|
|
||||||
poolTree.put("penalty", -1);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
poolTree.put("penalty", 0);
|
|
||||||
}
|
|
||||||
outTree.add_child("pool", poolTree);
|
|
||||||
outTree.put("end_of_round", endOfRound());
|
|
||||||
return outTree;
|
|
||||||
} // GameBoard::serializeBoard
|
|
||||||
|
|
||||||
std::string GameBoard::handleRequest(const std::string& instring) {
|
|
||||||
std::stringstream iss(instring);
|
|
||||||
pt::ptree inTree;
|
|
||||||
pt::read_json(iss, inTree);
|
|
||||||
pt::ptree outTree;
|
|
||||||
std::string req_type = inTree.get<std::string>("req_type");
|
|
||||||
// check type of request; check that required parameters are present; etc
|
|
||||||
// return: json with results, status, error msg if any
|
|
||||||
if (req_type.compare(azool::REQ_TYPE_DRAW_FROM_FACTORY) == 0) {
|
|
||||||
int idx = inTree.get<int>("factory_idx", -1);
|
|
||||||
int numTiles = 0;
|
|
||||||
azool::TileColor color = static_cast<azool::TileColor>(inTree.get<int>("tile_color"));
|
|
||||||
bool success = takeTilesFromFactory(idx, color, numTiles);
|
|
||||||
outTree.put("status", success ? "success" : "fail");
|
|
||||||
outTree.put("success", success);
|
|
||||||
outTree.put("num_tiles_returned", numTiles);
|
|
||||||
if (!success) {
|
|
||||||
outTree.put("error_msg", "INVALID FACTORY REQUEST");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (req_type.compare(azool::REQ_TYPE_DRAW_FROM_POOL) == 0) {
|
|
||||||
azool::TileColor color = static_cast<azool::TileColor>(inTree.get<int>("tile_color"));
|
|
||||||
int numTiles = 0;
|
|
||||||
bool penalty = false;
|
|
||||||
bool success = takeTilesFromPool(color, numTiles, penalty);
|
|
||||||
outTree.put("success", success);
|
|
||||||
outTree.put("status", success ? "success" : "fail");
|
|
||||||
outTree.put("num_tiles_returned", numTiles);
|
|
||||||
outTree.put("pool_penalty", penalty);
|
|
||||||
if (!success) {
|
|
||||||
outTree.put("error_msg", "INVALID POOL REQUEST");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (req_type.compare(azool::REQ_TYPE_RETURN_TO_BAG) == 0) {
|
|
||||||
int numTiles = inTree.get<int>("num_tiles_returned", 0);
|
|
||||||
azool::TileColor color = static_cast<azool::TileColor>(inTree.get<int>("tile_color"));
|
|
||||||
returnTilesToBag(numTiles, color);
|
|
||||||
outTree.put("success", true); // no reason for this to fail
|
|
||||||
outTree.put("status", "success");
|
|
||||||
}
|
|
||||||
else if (req_type.compare(azool::REQ_TYPE_GET_BOARD) == 0) {
|
|
||||||
outTree = serializeBoard();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// ERROR, BAD THINGS
|
|
||||||
outTree.put("error_msg", "INVALID REQUEST TYPE");
|
|
||||||
}
|
|
||||||
outTree.put("req_type", req_type); // include request type in returned data
|
|
||||||
std::stringstream oss;
|
|
||||||
pt::write_json(oss, outTree);
|
|
||||||
std::string output = oss.str();
|
|
||||||
return output;
|
|
||||||
// send output string over socket
|
|
||||||
} // GameBoard::handleRequest
|
|
||||||
|
|
||||||
void GameBoard::resetBoard() {
|
void GameBoard::resetBoard() {
|
||||||
memset(pool, 0, azool::NUMCOLORS*sizeof(int));
|
memset(pool, 0, azool::NUMCOLORS*sizeof(int));
|
||||||
tileBag.clear();
|
tileBag.clear();
|
||||||
|
126
src/Player.cc
126
src/Player.cc
@ -2,8 +2,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <boost/property_tree/json_parser.hpp>
|
|
||||||
namespace pt = boost::property_tree;
|
|
||||||
|
|
||||||
Player::Player(GameBoard* const board, std::string name) :
|
Player::Player(GameBoard* const board, std::string name) :
|
||||||
myGrid(),
|
myGrid(),
|
||||||
@ -43,24 +41,10 @@ bool Player::checkValidMove(azool::TileColor color, int rowIdx) const {
|
|||||||
} // Player::checkValidMove
|
} // Player::checkValidMove
|
||||||
|
|
||||||
bool Player::takeTilesFromFactory(int factoryIdx, azool::TileColor color, int rowIdx) {
|
bool Player::takeTilesFromFactory(int factoryIdx, azool::TileColor color, int rowIdx) {
|
||||||
// checkValidMove first - can't undo request to boardptr
|
// call game board -> takeTiles(factoryIdx, color)
|
||||||
if (!checkValidMove(color, rowIdx)) {
|
int numTiles = 0;
|
||||||
return false;
|
if (checkValidMove(color, rowIdx) and
|
||||||
}
|
myBoardPtr->takeTilesFromFactory(factoryIdx, color, numTiles)) {
|
||||||
pt::ptree request;
|
|
||||||
request.put("req_type", azool::REQ_TYPE_DRAW_FROM_FACTORY);
|
|
||||||
request.put("tile_color", color);
|
|
||||||
request.put("factory_idx", factoryIdx);
|
|
||||||
std::stringstream oss;
|
|
||||||
pt::write_json(oss, request);
|
|
||||||
// send request to board, recieve response
|
|
||||||
std::stringstream iss(sendRequest(oss.str()));
|
|
||||||
pt::ptree response;
|
|
||||||
pt::read_json(iss, response);
|
|
||||||
// std::cout << iss.str() << std::endl;
|
|
||||||
bool success = response.get<bool>("success", false);
|
|
||||||
int numTiles = response.get<int>("num_tiles_returned", 0);
|
|
||||||
if (success) {
|
|
||||||
placeTiles(rowIdx, color, numTiles);
|
placeTiles(rowIdx, color, numTiles);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -69,22 +53,12 @@ bool Player::takeTilesFromFactory(int factoryIdx, azool::TileColor color, int ro
|
|||||||
|
|
||||||
bool Player::takeTilesFromPool(azool::TileColor color, int rowIdx) {
|
bool Player::takeTilesFromPool(azool::TileColor color, int rowIdx) {
|
||||||
// call game board -> takeTilesFromPool
|
// call game board -> takeTilesFromPool
|
||||||
|
int numTiles = 0;
|
||||||
|
bool poolPenalty = false;
|
||||||
if (!checkValidMove(color, rowIdx)) {
|
if (!checkValidMove(color, rowIdx)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
pt::ptree request;
|
if (!myBoardPtr->takeTilesFromPool(color, numTiles, poolPenalty)) {
|
||||||
request.put("req_type", azool::REQ_TYPE_DRAW_FROM_POOL);
|
|
||||||
request.put("tile_color", color);
|
|
||||||
std::stringstream oss;
|
|
||||||
pt::write_json(oss, request);
|
|
||||||
// send request to board, recieve response
|
|
||||||
std::stringstream iss(sendRequest(oss.str()));
|
|
||||||
pt::ptree response;
|
|
||||||
pt::read_json(iss, response);
|
|
||||||
bool success = response.get<bool>("success", false);
|
|
||||||
int numTiles = response.get<int>("num_tiles_returned", 0);
|
|
||||||
bool poolPenalty = response.get<bool>("pool_penalty", false);
|
|
||||||
if (!success) {
|
|
||||||
return false; // couldn't get that tile from the pool
|
return false; // couldn't get that tile from the pool
|
||||||
}
|
}
|
||||||
if (poolPenalty) {
|
if (poolPenalty) {
|
||||||
@ -124,22 +98,18 @@ void Player::endRound(bool& fullRow) {
|
|||||||
myGrid[rowIdx][col] = true;
|
myGrid[rowIdx][col] = true;
|
||||||
myScore += scoreTile(rowIdx, col);
|
myScore += scoreTile(rowIdx, col);
|
||||||
// return extra tiles -- rowIdx = the number of leftover tiles
|
// return extra tiles -- rowIdx = the number of leftover tiles
|
||||||
pt::ptree request;
|
myBoardPtr->returnTilesToBag(rowIdx, myRows[rowIdx].second);
|
||||||
request.put("req_type", azool::REQ_TYPE_RETURN_TO_BAG);
|
|
||||||
request.put("num_tiles_returned", rowIdx);
|
|
||||||
request.put("tile_color", myRows[rowIdx].second);
|
|
||||||
std::stringstream oss;
|
|
||||||
pt::write_json(oss, request);
|
|
||||||
// send request to gameboard...don't care abt response here (?)
|
|
||||||
sendRequest(oss.str());
|
|
||||||
// myBoardPtr->returnTilesToBag(rowIdx, myRows[rowIdx].second);
|
|
||||||
// reset rows for next turn
|
// reset rows for next turn
|
||||||
myRows[rowIdx].first = 0;
|
myRows[rowIdx].first = 0;
|
||||||
myRows[rowIdx].second = azool::NONE;
|
myRows[rowIdx].second = azool::NONE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
myScore -= getScorePenalty();
|
if (myNumPenaltiesForRound >= PenaltyPoints.size()) {
|
||||||
myScore = std::max(myScore, 0); // let's not allow negatives
|
myScore -= PenaltyPoints[PenaltyPoints.size() - 1];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
myScore -= PenaltyPoints[myNumPenaltiesForRound];
|
||||||
|
}
|
||||||
// reset for next turn
|
// reset for next turn
|
||||||
// FOR THIS REASON
|
// FOR THIS REASON
|
||||||
// main loop needs to check who took penalty BEFORE calling this function
|
// main loop needs to check who took penalty BEFORE calling this function
|
||||||
@ -164,12 +134,6 @@ void Player::endRound(bool& fullRow) {
|
|||||||
} // iter over rows in grid
|
} // iter over rows in grid
|
||||||
} // Player::endRound
|
} // Player::endRound
|
||||||
|
|
||||||
int Player::getScorePenalty() const {
|
|
||||||
if (myNumPenaltiesForRound >= PenaltyPoints.size()) {
|
|
||||||
return PenaltyPoints[PenaltyPoints.size() - 1];
|
|
||||||
}
|
|
||||||
return PenaltyPoints[myNumPenaltiesForRound];
|
|
||||||
}
|
|
||||||
int Player::scoreTile(int tileRow, int tileCol) {
|
int Player::scoreTile(int tileRow, int tileCol) {
|
||||||
// search horizontally and vertically for points
|
// search horizontally and vertically for points
|
||||||
int tileScore = 1;
|
int tileScore = 1;
|
||||||
@ -271,8 +235,6 @@ void Player::finalizeScore() {
|
|||||||
} // Player::finalizeScore
|
} // Player::finalizeScore
|
||||||
|
|
||||||
std::string Player::printMyBoard() const {
|
std::string Player::printMyBoard() const {
|
||||||
// pt::ptree request;
|
|
||||||
// request.put("req_type", azool::REQ_TYPE_GET_BOARD);
|
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << "*******************************\n";
|
oss << "*******************************\n";
|
||||||
oss << "PLAYER: " << myName << "\n";
|
oss << "PLAYER: " << myName << "\n";
|
||||||
@ -307,25 +269,13 @@ std::string Player::printMyBoard() const {
|
|||||||
} // iterate over rows
|
} // iterate over rows
|
||||||
oss << "Penalties: " << myNumPenaltiesForRound << "\n";
|
oss << "Penalties: " << myNumPenaltiesForRound << "\n";
|
||||||
oss << "Score: " << myScore << "\n";
|
oss << "Score: " << myScore << "\n";
|
||||||
oss << "-------------------------------\n";
|
|
||||||
return oss.str();
|
return oss.str();
|
||||||
// TODO(feature) - print penalty tiles (?)
|
// TODO(feature) - print penalty tiles (?)
|
||||||
} // Player::printMyBoard
|
} // Player::printMyBoard
|
||||||
|
|
||||||
bool Player::discardFromFactory(int factoryIdx, azool::TileColor color) {
|
bool Player::discardFromFactory(int factoryIdx, azool::TileColor color) {
|
||||||
pt::ptree request;
|
int numTiles = -1;
|
||||||
request.put("req_type", azool::REQ_TYPE_DRAW_FROM_FACTORY);
|
if (myBoardPtr->takeTilesFromFactory(factoryIdx, color, numTiles)) {
|
||||||
request.put("factory_idx", factoryIdx);
|
|
||||||
request.put("tile_color", color);
|
|
||||||
std::stringstream oss;
|
|
||||||
pt::write_json(oss, request);
|
|
||||||
// send request to board, recieve response
|
|
||||||
std::stringstream iss(sendRequest(oss.str()));
|
|
||||||
pt::ptree response;
|
|
||||||
pt::read_json(iss, response);
|
|
||||||
bool success = response.get<bool>("success", false);
|
|
||||||
int numTiles = response.get<int>("num_tiles_returned", 0);
|
|
||||||
if (success) {
|
|
||||||
myNumPenaltiesForRound += numTiles;
|
myNumPenaltiesForRound += numTiles;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -333,19 +283,9 @@ bool Player::discardFromFactory(int factoryIdx, azool::TileColor color) {
|
|||||||
} // Player::discardFromFactory
|
} // Player::discardFromFactory
|
||||||
|
|
||||||
bool Player::discardFromPool(azool::TileColor color) {
|
bool Player::discardFromPool(azool::TileColor color) {
|
||||||
pt::ptree request;
|
bool poolPenalty = false;
|
||||||
request.put("req_type", azool::REQ_TYPE_DRAW_FROM_POOL);
|
int numTiles = -1;
|
||||||
request.put("tile_color", color);
|
if (myBoardPtr->takeTilesFromPool(color, numTiles, poolPenalty)) {
|
||||||
std::stringstream oss;
|
|
||||||
pt::write_json(oss, request);
|
|
||||||
// send request to board, recieve response
|
|
||||||
std::stringstream iss(sendRequest(oss.str()));
|
|
||||||
pt::ptree response;
|
|
||||||
pt::read_json(iss, response);
|
|
||||||
bool success = response.get<bool>("success", false);
|
|
||||||
int numTiles = response.get<int>("num_tiles_returned", 0);
|
|
||||||
bool poolPenalty = response.get<bool>("pool_penalty", false);
|
|
||||||
if (success) {
|
|
||||||
if (poolPenalty) {
|
if (poolPenalty) {
|
||||||
myNumPenaltiesForRound++;
|
myNumPenaltiesForRound++;
|
||||||
}
|
}
|
||||||
@ -355,20 +295,14 @@ bool Player::discardFromPool(azool::TileColor color) {
|
|||||||
return false;
|
return false;
|
||||||
} // Player::discardFromPool
|
} // Player::discardFromPool
|
||||||
|
|
||||||
std::string Player::sendRequest(const std::string& inStr) {
|
|
||||||
// do the socket stuff here
|
|
||||||
std::string rxStr = myBoardPtr->handleRequest(inStr);
|
|
||||||
return rxStr;
|
|
||||||
} // Player::sendRequest
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
int promptForFactoryIdx(int maxFactIdx) {
|
int promptForFactoryIdx(int maxNumFactories) {
|
||||||
static const char* promptFactoryIdxDraw = "Which factory? enter index\n";
|
static const char* promptFactoryIdxDraw = "Which factory? enter index\n";
|
||||||
char factInput; // TODO can we safely say there will never be more than 9 possible?
|
char factInput; // TODO can we safely say there will never be more than 9 possible?
|
||||||
std::cout << promptFactoryIdxDraw << std::flush;
|
std::cout << promptFactoryIdxDraw << std::flush;
|
||||||
std::cin >> factInput;
|
std::cin >> factInput;
|
||||||
int factIdx = std::atoi(&factInput);
|
int factIdx = std::atoi(&factInput);
|
||||||
if (factIdx < 1 or factIdx > maxFactIdx) {
|
if (factIdx < 1 or factIdx > maxNumFactories) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return factIdx;
|
return factIdx;
|
||||||
@ -414,19 +348,7 @@ int promptForRow() {
|
|||||||
|
|
||||||
void Player::takeTurn() {
|
void Player::takeTurn() {
|
||||||
// print game board, handle user input
|
// print game board, handle user input
|
||||||
pt::ptree request;
|
if (myBoardPtr->endOfRound()) return;
|
||||||
request.put("req_type", azool::REQ_TYPE_GET_BOARD);
|
|
||||||
std::stringstream reqss;
|
|
||||||
pt::write_json(reqss, request);
|
|
||||||
// send request to board, recieve response
|
|
||||||
std::stringstream iss(sendRequest(reqss.str()));
|
|
||||||
pt::ptree response;
|
|
||||||
pt::read_json(iss, response);
|
|
||||||
bool endOfRound = response.get<bool>("end_of_round");
|
|
||||||
// max idx for input - users use 1-indexing
|
|
||||||
int maxFactIdx = response.get<int>("num_factories");
|
|
||||||
if (endOfRound) return;
|
|
||||||
std::cout << *myBoardPtr << "\n\n";
|
|
||||||
std::cout << printMyBoard();
|
std::cout << printMyBoard();
|
||||||
static const char* promptDrawInput = "What would you like to do?\n"
|
static const char* promptDrawInput = "What would you like to do?\n"
|
||||||
"[f] take from factory "
|
"[f] take from factory "
|
||||||
@ -444,7 +366,7 @@ void Player::takeTurn() {
|
|||||||
char drawType;
|
char drawType;
|
||||||
std::cin >> drawType;
|
std::cin >> drawType;
|
||||||
if (drawType == 'f') {
|
if (drawType == 'f') {
|
||||||
int factIdx = promptForFactoryIdx(maxFactIdx);
|
int factIdx = promptForFactoryIdx(myBoardPtr->numFactories());
|
||||||
// draw from factory
|
// draw from factory
|
||||||
if (factIdx == -1) {
|
if (factIdx == -1) {
|
||||||
std::cout << invalidEntryMessage << std::flush;
|
std::cout << invalidEntryMessage << std::flush;
|
||||||
@ -491,7 +413,7 @@ void Player::takeTurn() {
|
|||||||
char discardFrom = '\0';
|
char discardFrom = '\0';
|
||||||
std::cin >> discardFrom;
|
std::cin >> discardFrom;
|
||||||
if (discardFrom == 'f') {
|
if (discardFrom == 'f') {
|
||||||
int factIdx = promptForFactoryIdx(maxFactIdx);
|
int factIdx = promptForFactoryIdx(myBoardPtr->numFactories());
|
||||||
// draw from factory
|
// draw from factory
|
||||||
if (factIdx == -1) {
|
if (factIdx == -1) {
|
||||||
std::cout << invalidEntryMessage << std::flush;
|
std::cout << invalidEntryMessage << std::flush;
|
||||||
|
14
src/main.cc
14
src/main.cc
@ -26,7 +26,7 @@ void playGame(GameBoard* game) {
|
|||||||
secondPlayer->takeTurn();
|
secondPlayer->takeTurn();
|
||||||
}
|
}
|
||||||
// check who took penalty
|
// check who took penalty
|
||||||
// needs to be done before calling player->endRound()
|
// nees to be done before claling player->endRound()
|
||||||
if (players[0]->tookPenalty()) {
|
if (players[0]->tookPenalty()) {
|
||||||
firstPlayer = players[0];
|
firstPlayer = players[0];
|
||||||
secondPlayer = players[1];
|
secondPlayer = players[1];
|
||||||
@ -44,14 +44,12 @@ void playGame(GameBoard* game) {
|
|||||||
players[0]->endRound(p0EndsGame);
|
players[0]->endRound(p0EndsGame);
|
||||||
players[1]->endRound(p1EndsGame);
|
players[1]->endRound(p1EndsGame);
|
||||||
}
|
}
|
||||||
std::cout << " Final scores:\n";
|
players[0]->finalizeScore();
|
||||||
|
players[1]->finalizeScore();
|
||||||
|
std::cout << " Final scores:\n" << players[0]->getScore() << "\n" << players[1]->getScore() << "\n" << std::flush;
|
||||||
|
std::cout << players[0]->printMyBoard();
|
||||||
|
std::cout << players[1]->printMyBoard();
|
||||||
for (auto player : players) {
|
for (auto player : players) {
|
||||||
player->finalizeScore();
|
|
||||||
std::cout << player->getPlayerName() << ": " << player->getScore() << "\n" << std::flush;
|
|
||||||
}
|
|
||||||
// separate loops b/c we want scores to print before printing boards
|
|
||||||
for (auto player : players) {
|
|
||||||
std::cout << player->printMyBoard() << std::flush;
|
|
||||||
if (player) delete player;
|
if (player) delete player;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user