Yahtzee Program

switch statement

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 8307 Views - Last Post: 23 May 2010 - 09:42 AM Rate Topic: -----

#1 matthew_perry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-May 10

Yahtzee Program

Posted 23 May 2010 - 04:33 AM

I have created a Yahtzee program for my college programming course in C++. It runs properly, but I have one flaw. I set a bool expression catUsed, to check to see if a value has been entered for each category. I can get the program to tell me that the categoy has been used, but it still enters the new score in the category. I am trying to figure out how to get it back to the switch statement. I have tried putting switch, default, and cin >> option, after the statement, but none of them seem to work. I was hoping I could get someone to tell me what I am doing wrong.

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	const int MAX_ROUNDS = 13;
	int rounds = 0;
	int die[5];
	string keepers;
	
	int aces = -1;
	int twos = -1;
	int threes = -1;
	int fours = -1;
	int fives = -1;
	int sixes = -1;
	int threeofaKind = -1;
	int fourofaKind = -1;
	int fullHouse = -1;
	int smallStraight = -1;
	int largeStraight = -1;
	int yahtzee = -1;
	int chance = -1;

	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 << "\n\nYour first rolls are:" << 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 << "Which dice would you like to reroll?\n1s keep dice, while 0s reroll.\nExample: 01011 to reroll first and third." << 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 second 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 << "Last Chance... Which dice would you like to reroll?\n" << 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 << "\n\nScoring Options: Enter number for category to enter score.\n" << endl;
	cout << "1 - Score Aces: " << aces << " \n";
	cout << "2 - Score Twos: " << twos << " \n";
	cout << "3 - Score Threes: " << threes << " \n";
	cout << "4 - Score Fours: " << fours << " \n";
	cout << "5 - Score Fives: " << fives << " \n";
	cout << "6 - Score Sixes: " << sixes << " \n";
	cout << "7 - Score 3 of a Kind: " << threeofaKind << " \n";
	cout << "8 - Score 4 of a Kind: " << fourofaKind << " \n";
	cout << "9 - Score Full House: " << fullHouse << " \n";
	cout << "10 - Score Small Straight: " << smallStraight << " \n";
	cout << "11 - Score Large Straight: " << largeStraight << " \n";
	cout << "12 - Score Yahtzee: " << yahtzee << " \n";
	cout << "13 - Score Chance: " << chance << " \n";
	

	int option;
	cout << "Option - ";
	cin >> option;


	switch (option)
	{
	
	case 1:
		if (catUsed[1] == true)
			cout << "pick another category" << endl;
		else
			cout << "Enter Ace Score.\n";
			cin >> aces;
			catUsed[1] = true;
			break;
	case 2:
		if (catUsed[2] == true)
			cout << "pick another category" << endl;	
		else
			cout << "Enter Twos Score.\n";
			cin >> twos;
			catUsed[2] = true;
			break;
	case 3:
		if (catUsed[3] == true)
			cout << "pick another category" << endl;
		else
			cout << "Enter Threes Score.\n";
			cin >> threes;
			catUsed[3] = true;
			break;
	case 4:
		if (catUsed[4] == true)
			cout << "pick another category" << endl;
		else
			cout << "Enter Fours score.\n";
			cin >> fours;
			catUsed[4] = true;
			break;
	case 5:
		if (catUsed[5] == true)
			cout << "pick another category" << endl;
		else
			cout << "Enter Fives score.\n";
			cin >> fives;
			cin >> option;
			catUsed[5] = true;
			break;
	case 6:
		if (catUsed[6] == true)
			cout << "pick another category" << endl;
		else
			cout << "Enter Sixes score.\n";
			cin >> sixes;
			catUsed[6] = true;		
			break;
	case 7:
		if (catUsed[7] == true)
			cout << "pick another category" << endl;
		else
			cout << "Enter 3 of a Kind score.\n";
			cin >> threeofaKind;
			catUsed[7] = true;
			break;
	case 8:
		if (catUsed[8] == true)
			cout << "pick another category" << endl;
		else
			cout << "Enter 4 of a Kind score.\n";
			cin >> fourofaKind;
			catUsed[8] = true;	
			break;
	case 9:
		if (catUsed[9] == true)
			cout << "pick another category" << endl;
		else
			cout << "Enter Full House score.\n";
			cin >> fullHouse;
			catUsed[9] = true;
			break;
	case 10:
		if (catUsed[10] == true)
			cout << "pick another category" << endl;
		else
			cout << "Enter Small Straight score.\n";
			cin >> smallStraight;
			catUsed[10] = true;
			break;
	case 11:
		if (catUsed[11] == true)
			cout << "pick another category" << endl;
		else
			cout << "Enter Large Straight score.\n";
			cin >> largeStraight;
			catUsed[11] = true;	
			break;
	case 12:
		if (catUsed[12] == true)
			cout << "pick another category" << endl;
		else
			cout << "Enter Yahtzee score.\n";
			cin >> yahtzee;
			catUsed[12] = true;		
			break;
	case 13:
		if (catUsed[13] == true)
			cout << "pick another category" << endl;
		else
			cout << "Enter Chance score.\n";
			cin >> chance;
			catUsed[13] = true;	
			break;
	default:
		cout << "Illegal choice made. Please choose another." << endl;
	}
	}
	
	upperScore = aces + twos + threes + fours + fives + sixes;
	lowerScore = threeofaKind + fourofaKind + fullHouse + smallStraight + largeStraight + yahtzee + chance;

	if (upperScore >= 63)
		upperBonus = 35;

	cout << "You have finished the game. Let's see how you did.\n\n";
	int totalScore = upperScore + upperBonus + lowerScore;
	cout << "Congratulations. Your total score is " << totalScore << ".\n Not too shabby.";
	

	system("pause");
	return 0;
}

This post has been edited by matthew_perry: 23 May 2010 - 05:24 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Yahtzee Program

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Yahtzee Program

Posted 23 May 2010 - 05:14 AM

Please edit your posting (see the "EDIT" button in lower right).
( a ) Delete all your code.
( b ) Get a fresh copy of the code with formatting in place from your editor / IDE.
( c ) Paste the formatted code between code tags like this :code:
( d ) Use the "Preview Post" button to check it's all good.
( e ) Use the "Submit Modified Post" button to finish the editing.


Use some braces around your if and else blocks to make it clear what happens when the test is passed or failed.
For example what happens here?:
case 1:
if (catUsed[1] == true)
cout << "pick another category" << endl;
else
cout << "Enter Ace Score.\n";
cin >> aces;
catUsed[1] = true;
break;


This post has been edited by janotte: 23 May 2010 - 05:20 AM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Yahtzee Program

Posted 23 May 2010 - 07:18 AM

Oh, and DIDN'T you do the post tags correctly when asked? You rock! :)
Was This Post Helpful? 0
  • +
  • -

#4 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Yahtzee Program

Posted 23 May 2010 - 07:41 AM

Ummmm aren't you accessing your array out of bounds??

 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;


yeah, array indexes start at ZERO.

there are only catUsed[0] through catUsed[12] indexes in that array, you're accessing some memory out of your array's bounds, may be the cause of your problem even. I've had things like that cause weird stuff to happen.


Also, call srand() ONCE, usually at the beginning for best results.

This post has been edited by taylorc8: 23 May 2010 - 07:47 AM

Was This Post Helpful? 1
  • +
  • -

#5 matthew_perry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-May 10

Re: Yahtzee Program

Posted 23 May 2010 - 07:47 AM

View Posttaylorc8, on 23 May 2010 - 06:41 AM, said:

Ummmm aren't you accessing your array out of bounds??

 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;


yeah, array indexes start at ZERO.

there are only catUsed[0] through catUsed[12] indexes in that array.


I tried it your way, but that does not work. It still does the same exact thing that it did when I write the code my way. The way I do it might not be proper programming, but it works just the same with the way I write the code. My professor even agreed with this. ummmm, yeah, please don't belittle people.
Was This Post Helpful? 0
  • +
  • -

#6 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Yahtzee Program

Posted 23 May 2010 - 07:50 AM

Sorry, I didn't mean to, but if you don't want to change all of your code, just add another element to the array, and it shouldn't mess up that memory near the end of your array. Also, you may need to hit "rebuild" if you're using visual studio.
Was This Post Helpful? 0
  • +
  • -

#7 matthew_perry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-May 10

Re: Yahtzee Program

Posted 23 May 2010 - 07:59 AM

View Posttaylorc8, on 23 May 2010 - 06:50 AM, said:

Sorry, I didn't mean to, but if you don't want to change all of your code, just add another element to the array, and it shouldn't mess up that memory near the end of your array. Also, you may need to hit "rebuild" if you're using visual studio.


my bool expression is not what it in question. It works properly. It tells me that the category has been used. I am wondering how to get back to the switch option. That is not dealing with my boolean expression, I just want it to circle back to the beginning. The boolean expression does its job.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Yahtzee Program

Posted 23 May 2010 - 08:07 AM

If you need to get back to the switch statement, then you need to wrap it in a loop.
Was This Post Helpful? 0
  • +
  • -

#9 matthew_perry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-May 10

Re: Yahtzee Program

Posted 23 May 2010 - 08:30 AM

View PostJackOfAllTrades, on 23 May 2010 - 07:07 AM, said:

If you need to get back to the switch statement, then you need to wrap it in a loop.


I don't need the entire case structure to loop, just if a category already has a value. If there has been no value added, it should add a value and automatically start rolling dice again. If I loop it, the case structure will not do this
Was This Post Helpful? 0
  • +
  • -

#10 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Yahtzee Program

Posted 23 May 2010 - 08:48 AM

Quote

I don't need the entire case structure to loop, just if a category already has a value. If there has been no value added, it should add a value and automatically start rolling dice again. If I loop it, the case structure will not do this
So...write additional logic to do this. You need X. Figure out the logic on paper. Once you believe you have the correct algorithm on paper, consider how to represent it in code. This process is called programming.
Was This Post Helpful? 0
  • +
  • -

#11 matthew_perry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-May 10

Re: Yahtzee Program

Posted 23 May 2010 - 09:03 AM

View PostOler1s, on 23 May 2010 - 07:48 AM, said:

Quote

I don't need the entire case structure to loop, just if a category already has a value. If there has been no value added, it should add a value and automatically start rolling dice again. If I loop it, the case structure will not do this
So...write additional logic to do this. You need X. Figure out the logic on paper. Once you believe you have the correct algorithm on paper, consider how to represent it in code. This process is called programming.


Do you honestly think I have not done this? Do you think I pulled all the code above from out of mid-air? I promise I would not come to a site, where anybody could give you a hoax answer, if I had not stormed my brain with all sorts of logic. I know what programming is. I have only been doing this for five weeks, but I think I know how to prepare my logic. It's not like I just started writing out random code. I don't need someone to talk to me like a child. If you are not willing to help, then just don't answer.
Was This Post Helpful? 0
  • +
  • -

#12 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Yahtzee Program

Posted 23 May 2010 - 09:05 AM

Also, if you don't want to write your own loop to initialize that array of bool's you can just use memset() in the <cstring> header file.

memset( catUsed, false, 13 );




Okay, what problems are you having again exactly? Also, do you have a reference for how the game of yahtzee is supposed to be played?

I'm a little uncertain as to what you're trying to do :( Sorry if I sounded like a douche earlier, Sarmanu is the only douche-bag on DIC, truthfully.

This post has been edited by taylorc8: 23 May 2010 - 09:07 AM

Was This Post Helpful? 0
  • +
  • -

#13 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Yahtzee Program

Posted 23 May 2010 - 09:16 AM

Are you aware that your indentation makes this look a little weird?

               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 << "Last Chance... Which dice would you like to reroll?\n" << endl;



If it does what it should, then I guess it's fine, but does it?
Was This Post Helpful? 0
  • +
  • -

#14 matthew_perry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-May 10

Re: Yahtzee Program

Posted 23 May 2010 - 09:18 AM

View Posttaylorc8, on 23 May 2010 - 08:05 AM, said:

Also, if you don't want to write your own loop to initialize that array of bool's you can just use memset() in the <cstring> header file.

memset( catUsed, false, 13 );




Okay, what problems are you having again exactly? Also, do you have a reference for how the game of yahtzee is supposed to be played?

I'm a little uncertain as to what you're trying to do :( Sorry if I sounded like a douche earlier, Sarmanu is the only douche-bag on DIC, truthfully.


You are fine, don't worry about it. I just need the code to check to see if a value has been entered in a category. My code does this properly, but it does not let me go back to the option of picking a category. I don't need the entire case to loop, just if a value has already been entered. If a value has not been entered, it should let you enter a value and automatically start rolling dice again, which it does. The code just won't let me go back to the options if a value has been entered.
Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,594
  • Joined: 08-August 08

Re: Yahtzee Program

Posted 23 May 2010 - 09:20 AM

You aren't using braces after your "if" and "else" statements, so only the next line is part of them. The following lines are executed regardless of the results of the test.
I'd do something like this:
if(catUsed[option])
	cout << "pick another category"<< endl;
else
{
	switch(option)
	{
		case 1:
			cout << "Enter Ace Score.\n";
			cin >> aces;
			catUsed[option] = true;
		break;
		case 2:
			cout << "Enter Twos Score.\n";
			cin >> twos;
			catUsed[option] = true;
		break;
		// and so on...
	}
}

If you were to use an array for the input you could do this without using a switch statement at all:
if(catUsed[option])
	cout << "pick another category"<< endl;
else
{
	cout << "Enter "<<kind[option] << " Score.\n";
	cin >> Num[option];
	catUsed[option] = true;
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2