// Scoresheet.cc // Implements Scoresheet class #include #include #include #include "Scoresheet.h" Scoresheet::Scoresheet() { int option; for (option=0;option<13;option++) { option_played[option] = false; option_score[option] = 0; } } bool Scoresheet::check_valid_option(int op) { return !option_played[op-1]; } void Scoresheet::select_scoring_option(int d[], Scoring_option op) { int i, j, count[6], max=0, empty=0; for (i=0;i<6;i++) count[i]=0; for (j=0;j<5;j++) count[d[j]-1]++; for (i=0;i<6;i++) { if ( count[i]>max ) max = count[i]; if ( count[i]==0 ) empty++; } switch (op) { case Aces : for (i=0;i<5;i++) if (d[i]==1) option_score[0]+=1; option_played[0] = true; break; case Twos : for (i=0;i<5;i++) if (d[i]==2) option_score[1]+=2; option_played[1] = true; break; case Threes : for (i=0;i<5;i++) if (d[i]==3) option_score[2]+=3; option_played[2] = true; break; case Fours : for (i=0;i<5;i++) if (d[i]==4) option_score[3]+=4; option_played[3] = true; break; case Fives : for (i=0;i<5;i++) if (d[i]==5) option_score[4]+=5; option_played[4] = true; break; case Sixes : for (i=0;i<5;i++) if (d[i]==6) option_score[5]+=6; option_played[5] = true; break; case Three_of_a_kind : if (max>=3) for (i=0;i<5;i++) option_score[6] += d[i]; option_played[6] = true; break; case Four_of_a_kind : if (max>=4) for (i=0;i<5;i++) option_score[7] += d[i]; option_played[7] = true; break; case Full_house : if (max==3 && empty==4) option_score[8] = 25; option_played[8] = true; break; case Low_straight : if (max<=2 && count[2]!=0 && count[3]!=0) option_score[9] = 30; option_played[9] = true; break; case High_straight : if ( max==1 && (d[0]==0 || d[5]==0) ) option_score[10] = 40; option_played[10] = true; break; case Yahtzee : if (max==5) option_score[11] = 50; option_played[11] = true; break; case Chance : for (i=0;i<5;i++) { option_score[12] += d[i]; } option_played[12] = true; break; } display(); } void Scoresheet::display() { int option; char names[13][17] = { "Aces", "Twos", "Threes", "Fours", "Fives", "Sixes", "Three of a kind", "Four of a kind", "Full house", "Low straight", "High straight", "Yahtzee", "Chance" }; cout << endl << endl << "Upper Section" << endl; for (option=0;option<6;option++) { cout << setw(6) << option+1 << " " << names[option]; if (option_played[option]) cout << setw(20-strlen(names[option])) << option_score[option]; cout << endl; } cout << endl << "Lower Section" << endl; for (option=6;option<13;option++) { cout << setw(6) << option+1 << " " << names[option]; if (option_played[option]) cout << setw(20-strlen(names[option])) << option_score[option]; cout << endl; } } int Scoresheet::get_total_score() { int total=0, option, bonus=35; for (option=0;option<6;option++) total += option_score[option]; if (total >= 63) total += bonus; for (option=6;option<13;option++) total += option_score[option]; return total; }