Hey Guys
Im writing a program to mimic the game 'yatzee' and I have found that int main() is not reading the functions for each category I have assigned a function to, can you see what ive done wrong?
The functions start on line 146
and my call out to the function starts on line 86 for each function
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int aces (int dice[]);
int twos (int dice[]);
int threes (int dice[]);
int fours (int dice[]);
int fives (int dice[]);
int sixes (int dice[]);
int three_of_a_kind (int dice[]);
int four_of_a_kind (int dice[]);
int full_house (int dice[]);
int small_straight (int dice[]);
int large_straight (int dice[]);
int yahtzee (int dice[]);
int chance (int dice[]);
int main()
{
int total=0;
int score[13] = {0};
int bonus[6] = {0};
int dice[4] = {0};
int numbers[5];
int category;
bool used_category[13]={0};
int nums[13];
for (int round=0; round<13; round++) //generates the 13 rounds
{
cout<<"\nRound "<<round+1<<endl;
cout<<"Yahtzee Program"<<endl;
cout<<"1. Aces: Category score is "<<score[0]<<endl;
cout<<"2. Twos: Category score is "<<score[1]<<endl;
cout<<"3. Threes: Category score is "<<score[2]<<endl;
cout<<"4. Fours: Category score is "<<score[3]<<endl;
cout<<"5. Fives: Category score is "<<score[4]<<endl;
cout<<"6. Sixes: Category score is "<<score[5]<<endl;
cout<<"7. 3 of a kind: Category score is "<<score[6]<<endl;
cout<<"8. 4 of a kind: Category score is "<<score[7]<<endl;
cout<<"9. Full House: Category score is "<<score[8]<<endl;
cout<<"10. Small straight: Category score is "<<score[9]<<endl;
cout<<"11. Large straight: Category score is "<<score[10]<<endl;
cout<<"12. Yahtzee: Category score is "<<score[11]<<endl;
cout<<"13. Chance: Category score is "<<score[12]<<endl;
int seed;
char reroll[5];
//initiate dice rolls
cout<<"Enter a number to start dice roll"<<endl;
cin>>seed;
for (int i=0; i<6; i++)
{
dice[i]=(rand()%6 + 1);
}
cout<<"Dice rolls are "<<dice[0]<<" "<<dice[1]<<" "<<dice[2]<<" "<<dice[3]<<" "<<dice[4]<<endl;
//following enables user to hold and reroll die
cout<<"Enter h to hold die or r to roll die: "<<endl;
cin>>reroll[0]>>reroll[1]>>reroll[2]>>reroll[3]>>reroll[4];
for (int i=0; i<6; i++)
{
if (reroll[i]=='r')
{
dice[i]=(rand()%6 + 1);
}
}
cout<<"The new dice after the re-rolls are: "<<dice[0]<<" "<<dice[1]<<" "<<dice[2]<<" "<<dice[3]<<" "<<dice[4]<<endl;
score[0]= aces(dice);
score[1]= twos(dice);
score[2]= threes(dice);
score[3]= fours(dice);
score[4]= fives(dice);
score[5]= sixes(dice);
score[6]= three_of_a_kind(dice);
score[7]= four_of_a_kind(dice);
//score[8]= full_house(dice);
//score[9]= small_straight(dice);
//score[10]= large_straight(dice);
//score[11]= yahtzee(dice);
//score[12]= chance(dice);
//Selection of the Category to store the rolls in
//while loop testing for errors
int error_test=1;
while (error_test==1)
{
cout<<"Enter the number of the category in which you want to store your roll"<<endl;
cin>> category;
if(category<1 || category>13) //tests for invalid category selection
{
cout<<"Please choose a category between 1-13"<<endl;
cin>>category;
}
elseif (used_category[category-1]==1) //tests for if category has been used
{
cout<<"This category has already been assigned a score"<<endl;
cin>>category;
}
else
{
used_category[category-1]=1; //else assigns category bool 1 so cant be reused
total=total+score[category-1]; //incrememnts the toatl score
error_test=0; //ends the while error check loop
if (category<=6) //bonus points
{
bonus[category-1]=score[category-1];
}
}
if(round==13)
{
if(bonus[0]+bonus[1]+bonus[2]+bonus[3]+bonus[4]+bonus[5]>62)
total=total=35;
}
cout<<"total score is: "<<total<<endl;
}
}
return 0;
}
/* Start function for each of the categories*/
int aces(int dice[]) //ones function
{
int count=0;
int score=0;
for (int i=0; i<5; i++)
if (dice[i]==1); //if any of the rolls of the dice=1 increase count, score is count
{count=count+1;}
score=count;
return score;
}
int twos(int dice[]) //twos function
{
int count=0;
int score=0;
int x;
for (int i=0; i<5; i++)
if (dice[i]==2); //if any of the rolls of the dice=2 increase count, score is 2*count
{count=count+1;}
x=count*2;
score=x;
return score;
}
int threes(int dice[]) //Threes function
{
int count=0;
int score=0;
for (int i=0; i<5; i++)
if (dice[i]==3); //if any of the rolls of the dice=3 increase count, score is 3*count
{count=count+1;}
score=count*3;
return score;
}
int fours(int dice[]) //Fours Function
{
int count=0;
int score=0;
for (int i=0; i<5; i++)
if (dice[i]==4); //if any of the rolls of the dice=4 increase count, score is 4*count
{count=count+1;}
score=count*4;
return score;
}
int fives(int dice[]) //Fives Function
{
int count=0;
int score=0;
for (int i=0; i<5; i++)
if (dice[i]==5); //if any of the rolls of the dice=5 increase count, score is 5*count
{count=count+1;}
score=count*5;
return score;
}
int sixes(int dice[]) //Sixes Function
{
int count=0;
int score=0;
for (int i=0; i<5; i++)
if (dice[i]==6); //if any of the rolls of the dice=6 increase count score=6*count
{count=count+1;}
score=count*6;
return score;
}
int three_of_a_kind (int dice[]) //3 of a kind function- addition of all 5 dice if 3 are the same
{
int count[5]={0};
bool statement;
int score=0;
int k;
for (int i=1; i<7; i++) //the dice numbers 1-6
{
for (k=0; k<5; k++)
if(i==dice[k]) //check by incrementing if the dice have the same number
{
count[i]=count[i]+1;
if(count[i]>=3)
statement=true;
break; // exit loop by break function if 3 numbers are the same
}
}
if (statement==1)
score=score+dice[0]+dice[1]+dice[2]+dice[3]+dice[4];
if (statement==0) //if 3 numbers are not the same statement=0 and score remains the same
score=score;
return score;
}
int four_of_a_kind (int dice[]) //3 of a kind function- addition of all 5 dice if 4 are the same
{
int count[5]={0};
bool statement;
int score=0;
int k;
for (int i=1; i<7; i++) //the dice numbers 1-6
{
for (k=0; k<5; k++)
if(i==dice[k]) //check by incrementing if the dice have the same number
{
count[i]=count[i]+1;
if(count[i]>=4)
statement=true;
break; // exit loop by break function if 4 numbers are the same
}
}
if (statement==1)
score=score+dice[0]+dice[1]+dice[2]+dice[3]+dice[4];
if (statement==0) //if 4 numbers are not the same statement=0 and score remains the same
score=score;
return score;
}
}
Could you please describe the problem(s) in more detail?
main() is not reading the functions for each category I have assigned a function to, can you see what ive done wrong?
This doesn't make much sense to me. What results are you expecting? What results are you getting?
I do see a couple of problems with arrays that aren't large enough and/or array indexes too high.
1) You have 5 dice so line 27 should declare dice[5], not dice[4].
2) line 74 for (int i=0; i<6; i++) should be i<5 not i<6
3) In your three_of_a_kind() and four_of_a_kind() you declare int count[5]={0}; and then use index values from 1 to 6 in a for loop for (int i=1; i<7; i++)
4) There may be other instances I haven't found. Look for other such errors.
These problems may account for some strange behavior.
Ok thanks ive fixed these errors up, however I think calling my functions is the major error
I have to make a computer code mimicking the game yahtzee. Basically you roll 5 dice you can hold however many you want and roll the others. After this second roll out of the 5 dice you can store them in a category... 3 of a kind, 4 of a kind, 5 of a kind (known as yahtzee), etc etc (these are my functions) each category is assigned a score depending upon the dice roll and category you store them in. Each category can only be used once.
In these functions declared after int main() I am trying to use the random numbers generated by dice[] in the main function, to be used in the functions to perform specific tasks and output a score. However to me it seems that the functions are not being read out, any ideas???
Again, your description of the problem lacks meaning.
However to me it seems that the functions are not being read out, any ideas???
What does that mean?? Are you thinking that the functions are not being called?
If so, what leads you to believe this?
I asked:
What results are you expecting? What results are you getting?
Are you getting unexpected values in score[0], score[1], etc.? If the values are remaining 0 maybe your functions aren't written to work right.
You could put a cout statement in each function to see if it is getting called, or better yet, step through program execution using a debugger.
WAIT! I see it:
1 2
if (dice[i]==4); <- this semi-colon prevents the next line from being in the loop
{count=count+1;}
Maybe that's the problem. Try removing the ; at the end of the if statements