I have many things done so far in my project. However, in my switch statement it does not generate random numbers like it did earlier. You'll see what I mean. Other than that I need help with the scoring part of the assignment. Can anyone please help me? I feel like I am close to being finished with my programming I class final...Yahtzee.

Here is the code:

#include <iostream> 
#include <string>
#include <algorithm>
#include <ctime>

using namespace std;

void Welcome_Screen()
{
	cout << "                 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ " << endl;
	cout << endl;     
	cout << "                      Yahtzee Scorecard Edition " << endl;
	cout << endl;
	cout << "                   \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ " << endl;
	cout << endl; 
	cout << endl;
	cout << endl;
	cout << "Brought to you by Subway.....Eat Fresh!" << endl;
	cout << endl;
	cout << endl;
	system("pause");
}

void Board()
{
	string name, letter;
	int randomnumber = rand();

	system("cls");

	cout << "================================================" << endl;
	cout << endl;
	cout << "             Player Scorecard" << endl;
	cout << endl;
	cout << "================================================" << endl;
	cout << endl;
	cout << "Type in your name: ";
	getline(cin,name);
	cout << endl;

	cout << "Enter d to roll the die: ";
	cin >> letter;
	cout << endl;
	cout << "_________________________________________________" << endl;
	cout << endl;

}

void Roll_Again_With_Features()
{
	char numbersave = 'y';
    char rollAgain = 'y';
	char savenum = 'y';
	const int LOW = 1, HIGH = 6;
	int Die[5], die[5], choice;

	
	die[0] = rand() % (HIGH - LOW + 1) + LOW; 
    die[1] = rand() % (HIGH - LOW + 1) + LOW; 
    die[2] = rand() % (HIGH - LOW + 1) + LOW; 
    die[3] = rand() % (HIGH - LOW + 1) + LOW; 
    die[4] = rand() % (HIGH - LOW + 1) + LOW; 

	cout << "Your numbers are: " << die[0] << endl;
	cout << "Your numbers are: " << die[1] << endl;
	cout << "Your numbers are: " << die[2] << endl;
	cout << "Your numbers are: " << die[3] << endl;
	cout << "Your numbers are: " << die[4] << endl;
	cout << "_________________________________________________" << endl;
	cout << endl;

	do
	{
	cout << "Roll Again?? (y/n): ";
	cin >> rollAgain;
	cout << endl;
	
		if (rollAgain != 'y')
		{
			cout << "Do you want to save any numbers? (y/n): ";
			cin >> savenum;

			if (savenum != 'y')
			{
				cout << " End of Turn" << endl;
			}

			else if (savenum == 'y')
			{
				cout << "What number do you want to save? (1-6): ";
				cin >> Die[0];
				cout << endl;
				cout << "Do you want to save any other number? (y/n): ";
				cin >> numbersave;

				if (numbersave != 'y')
				{
					cout << endl;
				}

				else if (numbersave == 'y')
				{
					cout << "What number do you want to save? (1-6): ";
					cin >> Die[1];
					cout << endl;
				}

				cout << "Save another number? (y/n): ";
				cin >> savenum;

				if (savenum != 'y')
				{
					cout << endl;
				}

				else if (savenum == 'y')
				{
					cout << "What number do you want to save? (1-6): ";
					cin >> Die[2];
					cout << endl;
					cout << "Do you want to save another? (y/n): ";
					cin >> numbersave;

					if (numbersave != 'y')
					{
						cout << endl;
					}

					else if (numbersave == 'y')
					{
						cout << "What number do you want to save? (1-6): ";
						cin >> Die[3];
						cout << endl;
					}
				}
					cout << "Save another number? (y/n): ";
					cin >> savenum;

					if (savenum != 'y')
					{
						cout << endl;
					}

					else if (savenum == 'y')
					{
						cout << "What number do you want to save? (1-6): ";
						cin >> Die[4];
						cout << endl;
						cout << "Your Die are: " << Die[0] << " " << Die[1] << " " << Die[2] << " " << Die[3] << " " << Die[4] << endl;
					}
				}
			}

		else if (rollAgain == 'y')
		{
			cout << "Do you want to save any numbers before you re roll? (y/n): ";
			cin >> savenum;

			if (savenum != 'y')
			{
				cout << "How many die do you want to re roll? ";
				cin >> choice;

				switch (choice)
				{
				case 1: 
					cout << "You rolled a: " << die[0] << endl;
					break;
				case 2: 
					cout << "You rolled a : " << die[0] << " , " << die[1] << endl;
					break;
				case 3:
					cout << "You rolled a: " << die[0] << " , " << die[1] << " , " << die[2] << endl;
					break;
				case 4:
					cout << "You rolled a: " << die[0] << " , " << die[1] << " , " << die[2] << " , " << die[3] << " , " << die[4] << endl;
					break;
				case 5:
					cout << "You rolled a: " << die[0] << " , " << die[1] << " , " << die[2] << " , " << die[3] << " , " << die[4] << " , " << die[5] << endl;
					break;
				}
			}

			else if (savenum == 'y')
			{
				cout << "What number do you want to save? (1-6): ";
				cin >> Die[0];
				cout << endl;
				cout << "Do you want to save any other number? (y/n): ";
				cin >> numbersave;

				if (numbersave != 'y')
				{
					cout << "How many die do you want to re roll? ";
					cin >> choice;

					switch (choice)
					{
					case 1: 
						cout << "You rolled a: " << die[0] << endl;
						break;
					case 2: 
						cout << "You rolled a : " << die[0] << " , " << die[1] << endl;
						break;
					case 3:
						cout << "You rolled a: " << die[0] << " , " << die[1] << " , " << die[2] << endl;
						break;
					case 4:
						cout << "You rolled a: " << die[0] << " , " << die[1] << " , " << die[2] << " , " << die[3] << " , " << die[4] << endl;
						break;
					case 5:
						cout << "You rolled a: " << die[0] << " , " << die[1] << " , " << die[2] << " , " << die[3] << " , " << die[4] << " , " << die[5] << endl;
						break;
					}
				}

				else if (numbersave == 'y')
				{
					cout << "What number do you want to save? (1-6): ";
					cin >> Die[1];
					cout << endl;
				}

				cout << "Save another number?? (y/n): ";
				cin >> numbersave;

				if (numbersave != 'y')
				{
					cout << "How many die do you want to re roll? ";
					cin >> choice;

					switch (choice)
					{
					case 1: 
						cout << "You rolled a: " << die[0] << endl;
						break;
					case 2: 
						cout << "You rolled a : " << die[0] << " , " << die[1] << endl;
						break;
					case 3:
						cout << "You rolled a: " << die[0] << " , " << die[1] << " , " << die[2] << endl;
						break;
					case 4:
						cout << "You rolled a: " << die[0] << " , " << die[1] << " , " << die[2] << " , " << die[3] << " , " << die[4] << endl;
						break;
					case 5:
						cout << "You rolled a: " << die[0] << " , " << die[1] << " , " << die[2] << " , " << die[3] << " , " << die[4] << " , " << die[5] << endl;
						break;
					}
				}

				else if (numbersave == 'y')
				{
					cout << "What number do you want to save? (1-6): ";
					cin >> Die[2];
					cout << endl;
				}
				cout << "Do you want to save any other number? (y/n): ";
				cin >> numbersave;

				if (numbersave != 'y')
				{
					cout << "How many die do you want to re roll? ";
					cin >> choice;

					switch (choice)
					{
					case 1: 
						cout << "You rolled a: " << die[0] << endl;
						break;
					case 2: 
						cout << "You rolled a : " << die[0] << " , " << die[1] << endl;
						break;
					case 3:
						cout << "You rolled a: " << die[0] << " , " << die[1] << " , " << die[2] << endl;
						break;
					case 4:
						cout << "You rolled a: " << die[0] << " , " << die[1] << " , " << die[2] << " , " << die[3] << " , " << die[4] << endl;
						break;
					case 5:
						cout << "You rolled a: " << die[0] << " , " << die[1] << " , " << die[2] << " , " << die[3] << " , " << die[4] << " , " << die[5] << endl;
						break;
					}
				}

				else if (numbersave == 'y')
				{
					cout << "What number do you want to save? (1-6): ";
					cin >> Die[3];
					cout << endl;
				}

				cout << "Save another number?? (y/n): ";
				cin >> numbersave;

				if (numbersave != 'y')
				{
					cout << "How many die do you want to re roll? ";
					cin >> choice;

					switch (choice)
					{
					case 1: 
						cout << "You rolled a: " << die[0] << endl;
						break;
					case 2: 
						cout << "You rolled a : " << die[0] << " , " << die[1] << endl;
						break;
					case 3:
						cout << "You rolled a: " << die[0] << " , " << die[1] << " , " << die[2] << endl;
						break;
					case 4:
						cout << "You rolled a: " << die[0] << " , " << die[1] << " , " << die[2] << " , " << die[3] << " , " << die[4] << endl;
						break;
					case 5:
						cout << "You rolled a: " << die[0] << " , " << die[1] << " , " << die[2] << " , " << die[3] << " , " << die[4] << " , " << die[5] << endl;
						break;
					}
				}

				else if (numbersave == 'y')
				{
					cout << "What number do you want to save? (1-6): ";
					cin >> Die[4];
					cout << endl;
					cout << "Your Die are: " << Die[0] << " , " << Die[1] << " , " << Die[2] << " , " << Die[3] << " , " << Die[4] << endl;
				}
			}
			}
	} while (rollAgain == 'y');
	cout << endl;
}

int main()
{
	srand(time(0));

	Welcome_Screen();
	Board();
	Roll_Again_With_Features();

	system("pause");
	return 0;
}

Thank you again for your help guys =)

Recommended Answers

All 3 Replies

I created a version of "Farkle" a while back and I learned a few things while doing so.

The biggest thing that I learned was this:
To prevent many scoring errors you should start by searching for the larger/more difficult combinations first. For example, to prevent inadvertently scoring a YAHTZEE! as a 3-of-a-kind, you should detect a 5-of-a-kind first, then a 4-of-a-kind, then the 3-of-a-kind.

To easily facilitate this, I found a "distribution" array/vector to be beneficial. A distribution vector would keep track of how many of a particular value appeared on the roll. For example, if the roll were to produce 1,1,3,6,2 the distribution vector would be:

distribution[0] = 0;
distribution[1] = 2;
distribution[2] = 1;
distribution[3] = 1;
distribution[4] = 0;
distribution[5] = 0;
distribution[6] = 1;

You would then base your roll analysis on the distribution rather than the actual dice values.

However, in my switch statement it does not generate random numbers like it did earlier. You'll see what I mean.

There are only 5 switch statements. And how would we see what you mean? You didn't bother to describe it.

Other than that I need help with the scoring part of the assignment. Can anyone please help me? I feel like I am close to being finished with my programming I class final...Yahtzee.

With what? Without a description of what you are having problems with, we can't very well help, can we?

I can see a few things that could be better designed to make it easier to change your program. By creating code that can be easily re-used, it is possible to modify/maintain your software much more efficiently. When you successfully create code that can be re-used, you only have to edit it once and it's changed everywhere you use that code.

Do you know anything about classes? I think it would behoove you to consider implementing a "Die" class. This class would handle everything related to your dice such as rolling, keeping, and value recording/reporting. Then, create either an array or vector of Die objects and use a loop for any repetitive actions, such as rolling the set of dice.

For example, you could replace this:

//...
int main() {

  //...

  die[0] = rand() % (HIGH - LOW + 1) + LOW;
  die[1] = rand() % (HIGH - LOW + 1) + LOW;     
  die[2] = rand() % (HIGH - LOW + 1) + LOW;     
  die[3] = rand() % (HIGH - LOW + 1) + LOW;     
  die[4] = rand() % (HIGH - LOW + 1) + LOW;

  //...
} //end main()

with a loop that tells a Die to roll itself:

const int LOW = 1, HIGH = 6;
const int RANGE = ((HIGH - LOW) + 1);

int main() {

  //...

  for (int i = 0; i < DIE_SET_SIZE; ++i) {
    die[i] = (rand() % RANGE) + LOW;
  }

  //...

} //end main()

Do you see how the change causes the maintenance/modification labor to be dramatically reduced? It's easier and more efficient to change one (1) statement or function than it is to change multiple copies. What happens if you miss one? You start getting errors and you have to track down the line that you forgot to change, which can be quite time-consuming.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.