#include #include using namespace std; /* * given a sequence of rolls determine the best possible score in sequential yahtzee */ class Evaluator { public: virtual int eval(int rolls[]) { return 0;} protected: int evaln(int rolls[], int n) { int ans = 0; for(int i=0; i<5; i++) if (rolls[i] == n) ans += n; return ans; } int countPairs(int rolls[]) { int ans = 0; for(int i=0; i<4; i++) for(int j=i+1; j<5; j++) if (rolls[i] == rolls[j]) ans++; return ans; } int sort(int rolls[]) // sort and remove duplicates { for(int i=0; i<4; i++) for(int j=0; j<4; j++) if(rolls[j] > rolls[j+1]) { int tmp = rolls[j]; rolls[j] = rolls[j+1]; rolls[j+1] = tmp; } int k=1; for(int i=1; i<5; i++) { if (rolls[i] != rolls[i-1]) rolls[k++] = rolls[i]; } for(;k<5; k++) rolls[k] = -1; } }; class Ones : public Evaluator { public: virtual int eval(int rolls[]) { return evaln(rolls, 1); } }; class Twos : public Evaluator { public: virtual int eval(int rolls[]) { return evaln(rolls, 2); } }; class Threes : public Evaluator { public: virtual int eval(int rolls[]) { return evaln(rolls, 3); } }; class Fours : public Evaluator { public: virtual int eval(int rolls[]) { return evaln(rolls, 4); } }; class Fives : public Evaluator { public: virtual int eval(int rolls[]) { return evaln(rolls, 5); } }; class Sixes : public Evaluator { public: virtual int eval(int rolls[]) { return evaln(rolls, 6); } }; class ThreeOfAKind : public Evaluator { public: virtual int eval(int rolls[]) { int good = rolls[0]+rolls[1] + rolls[2] + rolls[3] + rolls[4]; int numPairs = countPairs(rolls); if (numPairs >= 3) return good; else return 0; } }; class FourOfAKind : public Evaluator { public: virtual int eval(int rolls[]) { int good = rolls[0]+rolls[1] + rolls[2] + rolls[3] + rolls[4]; int numPairs = countPairs(rolls); if (numPairs >= 6) return good; else return 0; } }; class FullHouse : public Evaluator { public: virtual int eval(int rolls[]) { int numPairs = countPairs(rolls); if (numPairs == 4) return 25; else return 0; } }; class SmallStraight : public Evaluator { public: virtual int eval(int rolls[]) { int tmp[5]; for(int i=0; i<5; i++) tmp[i] = rolls[i]; sort(tmp); if (tmp[0]+1 == tmp[1] && tmp[1]+1 == tmp[2] && tmp[2]+1 == tmp[3]) return 30; if (tmp[1]+1 == tmp[2] && tmp[2]+1 == tmp[3] && tmp[3]+1 == tmp[4]) return 30; return 0; } }; class LargeStraight : public Evaluator { public: virtual int eval(int rolls[]) { int tmp[5]; for(int i=0; i<5; i++) tmp[i] = rolls[i]; sort(tmp); if (tmp[0]+1 == tmp[1] && tmp[1]+1 == tmp[2] && tmp[2]+1 == tmp[3] && tmp[3]+1 == tmp[4]) return 40; return 0; } }; class Chance : public Evaluator { public: virtual int eval(int rolls[]) { return rolls[0]+rolls[1] + rolls[2] + rolls[3] + rolls[4]; } }; class Yahtzee : public Evaluator { public: virtual int eval(int rolls[]) { int numPairs = countPairs(rolls); if (numPairs == 10) return 50; else return 0; } }; const int MAXR = 195; // max number of rolls = (15 rolls per category)*(13 categories) int diceRolls[MAXR]; int nRolls; string masks[6][10] = { {"yyyyy"}, {"nyyyy", "ynyyy", "yynyy", "yyyny", "yyyyn"}, {"nnyyy", "nynyy", "nyyny", "nyyyn", "ynnyy", "ynyny", "ynyyn", "yynny", "yynyn", "yyynn"}, {"nnnyy", "nnyny", "nnyyn", "nynny", "nynyn", "nyynn", "ynnny", "ynnyn", "ynynn", "yynnn"}, {"nnnny", "nnnyn", "nnynn", "nynnn", "ynnnn"}, {"nnnnn"} }; int numMask[6] = {1,5,10,10,5,1}; int start[13] = {0, 5, 10, 15, 20, 25, 30, 35, 40, 45 , 50, 55, 60}; // start location in rolls for each category int stop[13] = {0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180}; // stop locations in rolls for each category Evaluator *evaluators[13]; struct { int bestVal; int numDice; } table[13][MAXR]; void findBest(int rnum, Evaluator *e, int vals[5], int len, int index, int best[], int nRolls) { int val = e->eval(vals); if (best[len] < val) best[len] = val; if (rnum == 3) return; for(int r=1; r<=5; r++) { for(int i=0; i> nRolls; for(int i=0; i> diceRolls[i]; int best[16]; int vals[5]; for(int i=0; i<5; i++) vals[i] = diceRolls[i]; // update stop points based on number of dice rolls int newstop = nRolls-5; for(int i=12; i>=0; i--) { if (newstop < stop[i]) stop[i] = newstop; newstop -= 5; } for(int i=0; i<13; i++) for(int j=0; j