Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi all I am createing a game which is in c++ . Its a yahtzee game. However I am having some problems with my code

Hi all I am createing a game which is in c++ . Its a yahtzee game. However I am having some problems with my code and would need help with edits onto it. The problem I have with the code is from "case" option. From case "2" till case "13" I have to manually calculate the results and cin>> results[2]. But however I do not want to manually input the value into the code. As for case "1" I have done this but the result does not seem to reflect my results after calling the function . Thats the first problem, the second problem is that, What if there are more then 1 player who wants to play the game, how do I alow multiple player to play this game, at the end of the game, the program would be able to determine the winner from this game. Final it will promot user do you want to play again. Much help would be appreciated.

#include

#include

#include

#include

using namespace std;

int ace (int dices[]);

int main ()

{

int count = 0;

const int MAX_ROUNDS = 13;

const int scores = 13;

int results[scores] = {0,0,0,0,0,0,0,0,0,0,0,0,0};

int rounds = 0;

int die[5];

string keepers;

bool catUsed[13];

catUsed[1] = false;

catUsed[2] = false;

catUsed[3] = false;

catUsed[4] = false;

catUsed[5] = false;

catUsed[6] = false;

catUsed[7] = false;

catUsed[8] = false;

catUsed[9] = false;

catUsed[10] = false;

catUsed[11] = false;

catUsed[12] = false;

catUsed[13] = false;

int upperScore;

int lowerScore;

int upperBonus = 0;

while(rounds < MAX_ROUNDS)

{

rounds +=1;

srand(time(0));

cout << "This will be your first roll" << endl;

for(int i = 0; i < 5; i++)

{

die[i] = rand()%6+1;

}

cout << die[0] << ' ' << die[1] << ' ' << die[2] << ' ' << die[3] << ' ' << die[4] << endl;

cout << "1 for no-reroll or 0 for re-roll,Example: 01011 then press enter" << endl;

while(true)

{

cin >> keepers;

bool go = false;

for(int i = 0; i < keepers.size(); i++)

if(keepers[i] < '0' || keepers[i] > '1')

go = true;

break;

}

cout << "This will be your second roll" << endl;

for(int i = 0; i < 5; i++)

if(keepers[i] != '1')

die[i] = rand()%6+1;

cout << die[0] << ' ' << die[1] << ' ' << die[2] << ' ' << die[3] << ' ' << die[4] << endl;

cout << "This will be your final roll" << endl;

while(true)

{

cin >> keepers;

bool go = false;

for(int i = 0; i < keepers.size(); i++)

if(keepers[i] < '0' || keepers[i] > '1')

go = true;

break;

}

cout << "Your final rolls are: " << endl;

for(int i = 0; i < 5; i++)

if(keepers[i] != '1')

die[i] = rand()%6+1;

cout << die[0] << ' ' << die[1] << ' ' << die[2] << ' ' << die[3] << ' ' << die[4] << endl;

cout << "Please enter number from 1-13 for category to input data. " << endl;

cout << "1 - Score Aces: " << results[0] << " ";

cout << "2 - Score Twos: " << results[1] << " ";

cout << "3 - Score Threes: " << results[2] << " ";

cout << "4 - Score Fours: " << results[3] << " ";

cout << "5 - Score Fives: " << results[4] << " ";

cout << "6 - Score Sixes: " << results[5] << " ";

cout << "7 - Score 3 of a Kind: " << results[6] << " ";

cout << "8 - Score 4 of a Kind: " << results[7] << " ";

cout << "9 - Score Full House: " << results[8] << " ";

cout << "10 - Score Small Straight: " << results[9] << " ";

cout << "11 - Score Large Straight: " << results[10] << " ";

cout << "12 - Score All straight: " << results[11] << " ";

cout << "13 - Score Chance: " << results[12] << " ";

int option;

cout << "Option - ";

cin >> option;

switch (option)

{

case 1:

if (catUsed[1] == true)

cout << "pick another category" << endl;

else

cout << "Enter Ace Score. ";

catUsed[1] = true;

results[0] == ace(die); // I am having problems here, my result does not relay back to results[0] after calling the function

break;

case 2:

if (catUsed[2] == true)

cout << "pick another category" << endl;

else

cout << "Enter Twos Score. ";

cin >> results[1];

catUsed[2] = true;

break;

case 3:

if (catUsed[3] == true)

cout << "pick another category" << endl;

else

cout << "Enter Threes Score. ";

cin >> results[2];

catUsed[3] = true;

break;

case 4:

if (catUsed[4] == true)

cout << "pick another category" << endl;

else

cout << "Enter Fours score. ";

cin >> results[3];

catUsed[4] = true;

break;

case 5:

if (catUsed[5] == true)

cout << "pick another category" << endl;

else

cout << "Enter Fives score. ";

cin >> results[4];

cin >> option;

catUsed[5] = true;

break;

case 6:

if (catUsed[6] == true)

cout << "pick another category" << endl;

else

cout << "Enter Sixes score. ";

cin >> results[5];

catUsed[6] = true;

break;

case 7:

if (catUsed[7] == true)

cout << "pick another category" << endl;

else

cout << "Enter 3 of a Kind score. ";

cin >> results[6];

catUsed[7] = true;

break;

case 8:

if (catUsed[8] == true)

cout << "pick another category" << endl;

else

cout << "Enter 4 of a Kind score. ";

cin >> results[7];

catUsed[8] = true;

break;

case 9:

if (catUsed[9] == true)

cout << "pick another category" << endl;

else

cout << "Enter Full House score. ";

cin >> results[8];

catUsed[9] = true;

break;

case 10:

if (catUsed[10] == true)

cout << "pick another category" << endl;

else

cout << "Enter Small Straight score. ";

cin >> results[9];

catUsed[10] = true;

break;

case 11:

if (catUsed[11] == true)

cout << "pick another category" << endl;

else

cout << "Enter Large Straight score. ";

cin >> results[10];

catUsed[11] = true;

break;

case 12:

if (catUsed[12] == true)

cout << "pick another category" << endl;

else

cout << "Enter Yahtzee score. ";

cin >> results[11];

catUsed[12] = true;

break;

case 13:

if (catUsed[13] == true)

cout << "pick another category" << endl;

else

cout << "Enter Chance score. ";

cin >> results[12];

catUsed[13] = true;

break;

default:

cout << "Illegal choice made. Please choose another." << endl;

}

}

upperScore = results[0] + results[1] + results[2] + results[3] + results[4] + results[5];

lowerScore = results[6] + results[7] + results[8] + results[9] + results[10] + results[11] + results[12];

if (upperScore >= 63)

upperBonus = 35;

cout << "You have finished the game. Let's see how you did. ";

int totalScore = upperScore + upperBonus + lowerScore;

cout << "Congratulations. Your total score is " << totalScore << ". Not too shabby.";

system("pause");

return 0;

}

int ace(int die[])

{

int count=0;

int results=0;

for (int m=0; m<5; m++)

if (die[m]==1);

{

count=count+1;

}

results=count;

return results;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago