Oct 10, 2012 at 2:30am UTC
I have to make a Yahtzee program for class. what i have so far is down below. it is basically finished except that i cant get the ones through sixes to display correct points. in yahtzee if you want to score points in the ones spot you add up all the ones on your dice. i cant figure out a way to add only the ones on the dice and not all the dice. this is the same with twos threes fours fives and sixes.
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main () {
//Decalre all variables
int rand ( void );
int category;
int finalscore = 0;
int round;
int dice[5];
int turn;
int threeofakind=0;
int fourofakind=0;
int fullHouse=0;
int smallStraight=0;
int largeStraight=0;
int yahtzee=0;
int chance=0;
int scratch=0;
int totalones=0, totaltwos=0, totalthrees=0, totalfours=0, totalfives=0, totalsixs=0;
char reroll[5];
srand((unsigned int) time(0));
//Randomize each dice
dice[0]=rand() % 6+1;
dice[1]=rand() % 6+1;
dice[2]=rand() % 6+1;
dice[3]=rand() % 6+1;
dice[4]=rand() % 6+1;
round=1;
//Begin Yahtzee
cout << "Welcome to Yahtzee.\n " ;
while (round<=13) {
for (turn=1;turn<=3; turn++) {
cout << "Round" <<" " << round << " " << "Roll#" << " " << turn << "\n";
cout << "Your roll is: " << dice[0] << dice[1] << dice[2] << dice[3] << dice[4] << " (Type 'y' to re-roll or 'n' to stay)";
cin >> reroll[0] >> reroll[1] >> reroll[2] >> reroll[3] >> reroll[4];
//User decides which dice to keep and which to reroll
if (reroll[0]=='y'){
dice[0]=rand() % 6+1;}
if(reroll[0]=='n'){
dice[0];}
if (reroll[1]=='y'){
dice[1]=rand() % 6+1;}
if (reroll[2]=='y'){
dice[2]=rand() % 6+1;}
if (reroll[3]=='y'){
dice[3]=rand() % 6+1;}
if (reroll[4]=='y'){
dice[4]=rand() % 6+1;}
}
//User picks category for scoring
cout << "Under what category would you like to record your score:\n";
cout << "1 - Ones: \t" << " \n";
cout << "2 - Twos: \t" << " \n";
cout << "3 - Threes: \t" << " \n";
cout << "4 - Fours: \t" << " \n";
cout << "5 - Fives: \t" << " \n";
cout << "6 - Sixes: \t" << " \n";
cout << "7 - 3 of a Kind: \t" << " \n";
cout << "8 - 4 of a Kind: \t" << " \n";
cout << "9 - Full House: \t" << " \n";
cout << "10 -Small Straight: \t" << " \n";
cout << "11 -Large Straight: \t" << " \n";
cout << "12 -Yahtzee: \t" << " \n";
cout << "13 -Chance: \t" << " \n";
cout << "14 - Scratch: \t" << "\n";
cout << "\n";
cout << "Choice (1-14):\n";
cin >> category ;
cout << "\n";
//////here is my trouble
//Determine addition of ones
if (category == 1) {
if (dice[0]==1 || dice[1]==1 || dice[2]==1 || dice[3]==1 || dice[4]==1){
totalones+= dice[0] + dice[1] + dice[2] + dice[3] + dice[4];
}
}
//Determine addition of twos
else if (category ==2) {
if (dice[0]==2 || dice[1]==2 || dice[2]==2 || dice[3]==2 || dice[4]==2){
totaltwos+= dice[0] + dice[1] + dice[2] + dice[3] + dice[4];
}
}
//Determine addition of threes
else if (category ==3) {
if (dice[0]==3 || dice[1]==3 || dice[2]==3 || dice[3]==3 || dice[4]==3){
totalthrees+= dice[0] + dice[1] + dice[2] + dice[3] + dice[4];
}
}
//Determine addition of fours
else if (category ==4) {
if (dice[0]==4 || dice[1]==4 || dice[2]==4 || dice[3]==4 || dice[4]==4){
totalfours+= dice[0] + dice[1] + dice[2] + dice[3] + dice[4];
}
}
//Determine addition of fives
else if (category ==5) {
if (dice[0]==5 || dice[1]==5 || dice[2]==5 || dice[3]==5 || dice[4]==5){
totalfives+= dice[0] + dice[1] + dice[2] + dice[3] + dice[4];
}
}
//Determine addition of sixes
else if (category ==6) {
if (dice[0]==6 || dice[1]==6 || dice[2]==6 || dice[3]==6 || dice[4]==6){
totalsixs+= dice[0] + dice[1] + dice[2] + dice[3] + dice[4];
}
}
//Determine addition if three of a kind
else if (category ==7) {
threeofakind += dice[0]+dice[1]+dice[2]+dice[3]+dice[4];
}
//Determine addition if four of a kind
else if (category ==8) {
fourofakind += dice[0]+dice[1]+dice[2]+dice[3]+dice[4];
}
//Assign 25 points to full house
else if (category ==9) {
fullHouse += 25;
}
//Assign 30 points to small straight
else if (category ==10) {
smallStraight += 30;
}
//Assign 40 points to large straight
else if (category ==11) {
largeStraight += 40;
}
//Assign 50 points to yahtzee
else if (category ==12) {
yahtzee += 50;
}
//Determine addition of all dice for chance
else if (category ==13) {
chance += dice[0]+dice[1]+dice[2]+dice[3]+dice[4];
}//Assign zero to scratch
else if (category ==14) {
scratch += 0;
}
//Displays the current points
cout << "1 - Ones: \t" << totalones << " \n";
cout << "2 - Twos: \t" << totaltwos << " \n";
cout << "3 - Threes: \t" << totalthrees << " \n";
cout << "4 - Fours: \t" << totalfours << " \n";
cout << "5 - Fives: \t" << totalfives << " \n";
cout << "6 - Sixes: \t" << totalsixs << " \n";
cout << "7 - 3 of a Kind: \t" << threeofakind << " \n";
cout << "8 - 4 of a Kind: \t" << fourofakind << " \n";
cout << "9 - Full House: \t" << fullHouse << " \n";
cout << "10 -Small Straight: \t" << smallStraight << " \n";
cout << "11 -Large Straight: \t" << largeStraight << " \n";
cout << "12 -Yahtzee: \t" << yahtzee << " \n";
cout << "13 -Chance: \t" << chance << " \n";
cout << "14 - Scratch: \t" << scratch << "\n";
cout << "\n";
round++; }
//Determines the final score with bonus
if ( (totalones + totaltwos + totalthrees + totalfours + totalfives + totalsixs) > 62 ){
finalscore = totalones + totaltwos + totalthrees + totalfours + totalfives + totalsixs + threeofakind + fourofakind + fullHouse + smallStraight + largeStraight + yahtzee + chance + 35 ;
}
//Determine the final score without bonus
else if ( (totalones + totaltwos + totalthrees + totalfours + totalfives + totalsixs) < 63 ) {
finalscore = totalones + totaltwos + totalthrees + totalfours + totalfives + totalsixs + threeofakind + fourofakind + fullHouse + smallStraight + largeStraight + yahtzee + chance;
}
cout << "Congratulations, your final score is " << " " << finalscore << " \n" ;
return 0;
}
//THE END
Oct 10, 2012 at 4:16am UTC
Notice I havn't tested this but something like this should help
1 2 3 4 5 6
for (int i = 0, i <= 4, i++)
{
int sum;
if (dice[i] == 1)
sum = sum + 1;
}
Last edited on Oct 10, 2012 at 4:18am UTC