#include "swineplayer.hpp" #include #include "diceroll.hpp" #include "swine.hpp" namespace cs427_527 { CategoryStrategy::~CategoryStrategy() { } RollStrategy::~RollStrategy() { } ISwinePlayer::~ISwinePlayer() { } SwinePlayer::SwinePlayer(std::shared_ptr c, std::shared_ptr r) : catStrat{c}, rollStrat{r} { } int SwinePlayer::chooseCategory(const SwineGame& g, const DiceRoll& r, const SwineScoresheet& s) { return catStrat->chooseCategory(g, r, s); } bool SwinePlayer::rollAgain(const SwineGame& g, const DiceRoll& r, const SwineScoresheet& s, int cat, int sub) { return rollStrat->rollAgain(g, r, s, cat, sub); } int GreedyCategoryStrategy::chooseCategory(const SwineGame& g, const DiceRoll& r, const SwineScoresheet& s) { int bestCat; int bestScore= -1; for (int c = 0; c < g.countCategories(); c++) { int catScore = g.categoryScore(r, c); if (catScore > bestScore || (catScore == bestScore && s.getScore(c) > s.getScore(bestCat))) { bestCat = c; bestScore = catScore; } } return bestCat; } int GreedyProgressCategoryStrategy::chooseCategory(const SwineGame& g, const DiceRoll& r, const SwineScoresheet& s) { int bestCat; int bestScore= -1; for (int c = 0; c < g.countCategories(); c++) { int catScore = g.categoryScore(r, c); if (catScore > 0) { int subScore = s.getScore(c) + catScore; if (subScore > bestScore || (subScore == bestScore && catScore > g.categoryScore(r, bestCat))) { bestCat = c; bestScore = catScore; } } } return bestCat; } FixedLimitRollStrategy::FixedLimitRollStrategy(int l) { limit = l; } bool FixedLimitRollStrategy::rollAgain(const SwineGame& g, const DiceRoll& r, const SwineScoresheet& s, int cat, int sub) { return sub < limit && s.getScore(cat) + sub < g.targetScore(); } DynamicLimitRollStrategy::DynamicLimitRollStrategy(int l, int d) { limit = l; denom = d; } bool DynamicLimitRollStrategy::rollAgain(const SwineGame& g, const DiceRoll& r, const SwineScoresheet& s, int cat, int sub) { int max = 0; for (int c = 0; c < g.countCategories(); c++) { if (s.getScore(c) > max) { max = s.getScore(c); } } int dynamicLimit = limit + (max - s.getScore(cat)) / denom; return sub < dynamicLimit && s.getScore(cat) + sub < g.targetScore(); } }