7 Replies - 1132 Views - Last Post: 09 November 2015 - 01:57 PM Rate Topic: -----

#1 dansken15   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-September 15

Need help with a light version of yahtzee

Posted 06 November 2015 - 06:07 PM

Hello,

I'm coding a light version of a yahtzee game. What I want to do in the program is as followed: The game starts with rolling all the dice, after that you need to get as many ones to start with and then moving on to twos after you have tossed the dices 3 times. So basicly, first 3 tosses is about getting as many ones as possible. Next three tosses is about getting as many twos as possible, you do this up to sixes. after the first toss, you are given the answer to keep dices, I have chosen to do this in a FOR LOOP, that for each dice "i" you get the question, "Do you want to keep dice" the player answers with (y/n) and the dice that the player wants to keep will be put in an array, after the the last toss the array will be filled with the rest of the dices.

I'll give you an example,
first toss, the player gets: 1. 6. 4. 3. 1.
so the player decides to keep dice number 1 and 6.
so they will be put in the array.

I'll explain what I want to do in psudocode:

while(!tossedThreeTimes, not saved 5) {
toss all.
print out all - saved.
Ask wich dice to keep.
for each keep.
nrOfToss++;

if nrOfToss done(3)
{
Fill keep
}
}

This is my code so far:

#include <iostream>
#include <string>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main()
{
	const int nrOfDice = 5;
	const int nrOfPlayer = 1;
	const int protocol = 7;

	int nrOfRolls = 0;

	int keep = 0;

	int diceLeft = nrOfDice - keep;

	char ans;

	int dice[nrOfDice];
	int result[protocol];
	string player[nrOfPlayer];
	int reRoll[nrOfDice];
	int diceKeep[nrOfDice];

	srand(time(NULL));

	cout << "Player please enter your name: ";
	getline(cin, player[0]);

	cout << "Hello " << player[0] << endl;

	int i = 0;

	dice[i] = 0;
	while (nrOfRolls != 3) {
		for (int i = 0; i < 5; i++)
		{
			dice[i] = (rand() % 6) + 1;
		}
		result[0] = 0;
		cout << "---> ";
		for (int i = 0; i < 5; i++)
		{
			cout << dice[i] << ". ";
		}

		/*cout << "Dice To Keep: ";
		cin >> keep;*/
		cout << endl;
		for (int i = 0; i < nrOfDice; i++)
		{
			cout << "Do you want to keep die: " << i + 1 << endl;
			cout << "(Y/N) - ";
			cin >> ans;

			if (ans == 'y' || 'Y')
			{
				dice[i] + i;

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

		nrOfRolls++;

		
		if (nrOfRolls == 3)
		{
			cout << "Your Final Dice is: ";
			for (int i = 0; i < 5; i++)
			{
				//dice[i] + diceLeft;
				dice[i] + i;
				cout << dice[i] << ". ";
			}

			/*dice[nrOfDice] + diceLeft;

			cout << dice[nrOfDice];*/
		}
	}
	system("pause");
	getchar();
	return 0;
}



As you can see, I havent really forfilled what i described in psudocode. The thing is that im totally stuck as this point and could really need som code help to get this program working, I know that I'm almost done I just need to get my code correct. The thing that doesn't work in program now is that i cant get the function where the user chooses which dice he/she wants to keep in an array and after that print out the final dice after three tosses. My code is what I have came up with so far, and I dont know what to change in it.

Hope you have understood what I've explained and if you didn't, just let me know and I will try to explain it "better"

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with a light version of yahtzee

#2 Night Stalker   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 99
  • Joined: 26-December 13

Re: Need help with a light version of yahtzee

Posted 06 November 2015 - 10:07 PM

Your problem is you are over thinking this... Here is a simpler version for you.
Use your loops to your advantage.



#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <string>

using std :: cout;
using std :: cin;
using std :: endl;
using std :: string;

int rolldie(int); 

int main ()
{
    srand (time(0));     // randomize numbers
    int die[5]= {0};     // assign values
    string answer="";    // assign empty string

    for (int i=0; i<5; i++)
    {
        die[i]=rolldie(i);
    }
    cout << "You rolled a "<<die[0]<<" "<<die[1]<<" "<<die[2]<<" "<<die[3]<< " "<< die[4] << endl;


    for (int i=0; i<5; i++)          //  You can use a loop like this to ask each number, and reroll them one at a time
    {
        cout << "Would you like to keep dice "<< i+1<< " ";
        cin >> answer;
        if (answer=="n" || answer=="N")
        {
            die[i] = rolldie(i);
        }
    }
    cout << "You rolled a "<<die[0]<<" "<<die[1]<<" "<<die[2]<<" "<<die[3]<< " "<< die[4] << endl;     // and redisplay them

    return 0;
}

int rolldie(int num)
{
    num=rand() % 6+1;
    return num;
}

Was This Post Helpful? -1
  • +
  • -

#3 dansken15   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-September 15

Re: Need help with a light version of yahtzee

Posted 07 November 2015 - 01:17 PM

View PostNight Stalker, on 06 November 2015 - 10:07 PM, said:

Your problem is you are over thinking this... Here is a simpler version for you.
Use your loops to your advantage.



#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <string>

using std :: cout;
using std :: cin;
using std :: endl;
using std :: string;

int rolldie(int); 

int main ()
{
    srand (time(0));     // randomize numbers
    int die[5]= {0};     // assign values
    string answer="";    // assign empty string

    for (int i=0; i<5; i++)
    {
        die[i]=rolldie(i);
    }
    cout << "You rolled a "<<die[0]<<" "<<die[1]<<" "<<die[2]<<" "<<die[3]<< " "<< die[4] << endl;


    for (int i=0; i<5; i++)          //  You can use a loop like this to ask each number, and reroll them one at a time
    {
        cout << "Would you like to keep dice "<< i+1<< " ";
        cin >> answer;
        if (answer=="n" || answer=="N")
        {
            die[i] = rolldie(i);
        }
    }
    cout << "You rolled a "<<die[0]<<" "<<die[1]<<" "<<die[2]<<" "<<die[3]<< " "<< die[4] << endl;     // and redisplay them

    return 0;
}

int rolldie(int num)
{
    num=rand() % 6+1;
    return num;
}


This was really helpfull, but i couldent get it to work with my program. I used a while loop like before with nrOfRolls that got nrOfRolls++ til 3 tosses. But I couldn't get the dice you wanted to save for 3 tosses, it just randomize for each toss and doesnt keep the ones you want to save.

Here is my code:

#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <string>

using namespace std;

int rolldie(int);

int main()
{
	srand(time(0));     // randomize numbers
	string answer = "";    // assign empty string
	int nrOfRolls = 0;
	const int nrOfDice = 5;
	const int nrOfPlayer = 1;
	string player[nrOfPlayer];
	int die[nrOfDice] = { 0 }; // assign values

	cout << "Player please enter your name: ";
	getline(cin, player[0]);
	cout << "Hello " << player[0] << endl;

	while (nrOfRolls != 3) {
		for (int i = 0; i<5; i++)
		{
			die[i] = rolldie(i);
		}

		cout << player[0]  << " You rolled --> " << die[0] << " " << die[1] << " " << die[2] << " " << die[3] << " " << die[4] << endl;

		for (int i = 0; i<5; i++)          //  You can use a loop like this to ask each number, and reroll them one at a time
		{
			cout << "Would you like to keep dice " << i + 1 << " ";
			cin >> answer;
			if (answer == "n" || answer == "N")
			{
				die[i] = rolldie(i);
			}
		}

		nrOfRolls++;
		if (nrOfRolls == 3)
		{
			cout << "Your final Dice are --> " << die[0] << " " << die[1] << " " << die[2] << " " << die[3] << " " << die[4] << endl;     // and redisplay them
		}
	}
	cout << "The program is about to quit ";
	system("pause");
	getchar();
	return 0;
}

int rolldie(int num)
{
	num = rand() % 6 + 1;
	return num;
}



Any tips?
Was This Post Helpful? 0
  • +
  • -

#4 Night Stalker   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 99
  • Joined: 26-December 13

Re: Need help with a light version of yahtzee

Posted 07 November 2015 - 08:40 PM

I'm disappointed that you didn't try a little experimentation.

A few well placed cout statements can give you a boatload of information, like on line 24 for example

while (nrOfRolls != 3) {    cout << nrOfRolls << endl;


could have told you nrOfRolls is staying at 0... That would have told you to add a nrOfRolls++; inside the while loop, not outside where you had it. When it rerolls all 5 dice, regardless of you instructing it to keep them or not... there is a reason for it.
Look for the block that rolls all 5 dice at the same time...

 for (int i = 0; i<5; i++)
        {
            die[i] = rolldie(i);
        }


Move lines 25 - 28 out of your loop.

A little experiment here and there never hurts, cout statements that can show you what your variables are doing is a must.
I'll give you a code that works, but it's sloppy. It's up to you to clean it up before you turn it in.


#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <string>

using namespace std;

int rolldie(int);

int main()
{
    srand(time(0));     // randomize numbers
    string answer = "";    // assign empty string
    int nrOfRolls = 0;
    const int nrOfDice = 5;
    const int nrOfPlayer = 1;
    string player[nrOfPlayer];
    int die[nrOfDice] = { 0 }; // assign values
    cout << "Player please enter your name: ";
    getline(cin, player[0]);
    cout << "Hello " << player[0] << endl;
    for (int i = 0; i<5; i++)
    {
        die[i] = rolldie(i);
    }
    cout << player[0]  << " You rolled --> " << die[0] << " " << die[1] << " " << die[2] << " " << die[3] << " " << die[4] << endl;
    while (nrOfRolls < 2)
    {
        for (int i = 0; i<5; i++)
        {
            cout << nrOfRolls << "Would you like to reroll this die " <<die[i] << " ";
            cin >> answer;
            if (answer == "n" || answer == "N")
            {
                cout << "Okay, we'll keep that one." << endl;
            }
            else
            {
                die[i] = rolldie(i);
            }
        }
        cout << "\nYour dice are --> " << die[0] << " " << die[1] << " " << die[2] << " " << die[3] << " " << die[4] << endl;     // and redisplay them
        nrOfRolls++;
    }
    cout << "\n After 3 turns your final Dice are --> " << die[0] << " " << die[1] << " " << die[2] << " " << die[3] << " " << die[4] << endl;     // and redisplay them
    cout << "The program is about to quit ";
    system("pause");
    return 0;
}

int rolldie(int num)
{
    num = rand() % 6 + 1;
    return num;
}

This post has been edited by Night Stalker: 07 November 2015 - 08:46 PM

Was This Post Helpful? 1
  • +
  • -

#5 dansken15   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-September 15

Re: Need help with a light version of yahtzee

Posted 08 November 2015 - 08:24 AM

View PostNight Stalker, on 07 November 2015 - 08:40 PM, said:

I'm disappointed that you didn't try a little experimentation.

A few well placed cout statements can give you a boatload of information, like on line 24 for example

while (nrOfRolls != 3) {    cout << nrOfRolls << endl;


could have told you nrOfRolls is staying at 0... That would have told you to add a nrOfRolls++; inside the while loop, not outside where you had it. When it rerolls all 5 dice, regardless of you instructing it to keep them or not... there is a reason for it.
Look for the block that rolls all 5 dice at the same time...

 for (int i = 0; i<5; i++)
        {
            die[i] = rolldie(i);
        }


Move lines 25 - 28 out of your loop.

A little experiment here and there never hurts, cout statements that can show you what your variables are doing is a must.
I'll give you a code that works, but it's sloppy. It's up to you to clean it up before you turn it in.


#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <string>

using namespace std;

int rolldie(int);

int main()
{
    srand(time(0));     // randomize numbers
    string answer = "";    // assign empty string
    int nrOfRolls = 0;
    const int nrOfDice = 5;
    const int nrOfPlayer = 1;
    string player[nrOfPlayer];
    int die[nrOfDice] = { 0 }; // assign values
    cout << "Player please enter your name: ";
    getline(cin, player[0]);
    cout << "Hello " << player[0] << endl;
    for (int i = 0; i<5; i++)
    {
        die[i] = rolldie(i);
    }
    cout << player[0]  << " You rolled --> " << die[0] << " " << die[1] << " " << die[2] << " " << die[3] << " " << die[4] << endl;
    while (nrOfRolls < 2)
    {
        for (int i = 0; i<5; i++)
        {
            cout << nrOfRolls << "Would you like to reroll this die " <<die[i] << " ";
            cin >> answer;
            if (answer == "n" || answer == "N")
            {
                cout << "Okay, we'll keep that one." << endl;
            }
            else
            {
                die[i] = rolldie(i);
            }
        }
        cout << "\nYour dice are --> " << die[0] << " " << die[1] << " " << die[2] << " " << die[3] << " " << die[4] << endl;     // and redisplay them
        nrOfRolls++;
    }
    cout << "\n After 3 turns your final Dice are --> " << die[0] << " " << die[1] << " " << die[2] << " " << die[3] << " " << die[4] << endl;     // and redisplay them
    cout << "The program is about to quit ";
    system("pause");
    return 0;
}

int rolldie(int num)
{
    num = rand() % 6 + 1;
    return num;
}


Hey Night Stalker.

First of all, i just want to say thank you for this huge amount of help, it have really helped me get going and got my program to work. The main functions of the program works now using your help. But now I want to expand to program to two players and I want the players to type what die they want to save on. But I cant get neither of these functions to work..

For the function where I keep track of the player I added this:

if (currentPlayer == player[0]) // Keeps track of current player and non playing player.
{
    currentPlayer = player[1];
    notPlaying = player[0];
}
else
{
    currentPlayer = player[0];
    notPlaying = player[1];
}



But the problem that I get, is that after each roll (there are 3 rolls) the currentPlayer turns to the other. I want it to change player after the first player have tossed 3 times(when nrOfRolls turns to 3). I cant seem to come up with another solution for this, becuase I have used this kind of code before on another program.

and for the function where the players type in what die they want to save on i added this:

	const int protocol = 6;
	int result[protocol];



I asked the players to type in what they want to save on like this:

cout << "Hello " << player[0] << " and " << player[1] << endl;
		cout << "What die do you want to save on? 1-6 ";
		cin >> saving;
		if (saving > 6 || saving < 1)
		{ 
			cout << "Must be a number between 1-6" << endl;
			cout << "What die do you want to save on? 1-6 ";
			cin >> saving;
		}
		else {
                The program starts



After the first roll i set the result[0] = 0; like this:

			for (int i = 0; i < 5; i++)
			{
				die[i] = rolldie(i);
			}

			result[0] = 0;



And at the end of the roll I want to tell the player how many of the die they type in to save is, i codeded like this:

if (die[nrOfDice] == saving)
{
    result[0]++;
}
cout << endl << "Amount of " << saving << ": " << result[0] << endl;



I do this outside the loop where we ask the player wich die they want to keep.

So the problem is that I cant get the currentPlayer to change after 3 tosses and I cant get the saving function to work.
At the saving function I have tried to change around the values and change result[0] = 0; to result[0] = saving; but I cant just get it to work...
Was This Post Helpful? 0
  • +
  • -

#6 Night Stalker   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 99
  • Joined: 26-December 13

Re: Need help with a light version of yahtzee

Posted 08 November 2015 - 05:58 PM

View Postdansken15, on 08 November 2015 - 03:24 PM, said:

First of all, i just want to say thank you for this huge amount of help, it have really helped me get going and got my program to work. The main functions of the program works now using your help. But now I want to expand to program to two players and I want the players to type what die they want to save on. But I cant get neither of these functions to work..


You shouldn't use that much grease dude... It gets messy.

Quote

For the function where I keep track of the player I added this:


if (currentPlayer == player[0]) // Keeps track of current player and non playing player.
{
    currentPlayer = player[1];
    notPlaying = player[0];
}
else
{
    currentPlayer = player[0];
    notPlaying = player[1];
}

Yeah, I'd think I may try something like...
bool turn;     // bool == 0 or 1, true or false
turn==true;
player[turn] // Player[0]
turn=false;
Player[turn] // Player[1]



you can control the player with a simple true or false statement.


Quote

But the problem that I get, is that after each roll (there are 3 rolls) the currentPlayer turns to the other. I want it to change player after the first player have tossed 3 times(when nrOfRolls turns to 3). I cant seem to come up with another solution for this, becuase I have used this kind of code before on another program.

and for the function where the players type in what die they want to save on i added this:


	const int protocol = 6;       // constant so it cant be changed
	int result[protocol];         // so this is always result[6] ??



I don't understand that. You could do something like

make loop1 from 0 to 1
make loop2 from 1 to 3
roll dice for player[loop1]
check numbers
report results
get choices from player
reroll pertinent numbers
} (loop2 advancement)
} (loop1 advancement)

loop one controls the player number, loop two controls the turns.


Quote

I asked the players to type in what they want to save on like this:


cout << "Hello " << player[0] << " and " << player[1] << endl;
		cout << "What die do you want to save on? 1-6 ";
		cin >> saving;
		if (saving > 6 || saving < 1)
		{ 
			cout << "Must be a number between 1-6" << endl;
			cout << "What die do you want to save on? 1-6 ";
			cin >> saving;
		}
		else {
                The program starts



By "Save on" I take it you mean what numbers they'd like to hold? What numbers they'd like to guess? What number are they saving and why?


Quote

After the first roll i set the result[0] = 0; like this:


			for (int i = 0; i < 5; i++)
			{
				die[i] = rolldie(i);
			}

			result[0] = 0;



You don't need result[0] at all. You should have 2 arrays p1dice and p2dice already set up... They are all you need


Quote

And at the end of the roll I want to tell the player how many of the die they type in to save is, i codeded like this:


if (die[nrOfDice] == saving)
{
    result[0]++;
}
cout << endl << "Amount of " << saving << ": " << result[0] << endl;



Yeah again... you wouldn't need that. You have result set up as an array... If you are trying to use a variable, just use result0 and result1... and I digress again, you don't need them at all.

Quote

So the problem is that I cant get the currentPlayer to change after 3 tosses and I cant get the saving function to work.
At the saving function I have tried to change around the values and change result[0] = 0; to result[0] = saving; but I cant just get it to work...



Yes, you can get it to work. You need to stop, turn off the computer. Get a pencil (I promise cool people still use them) and a piece of paper. Write down the exact steps of this program. For example, you could write...

1. Decide on preprocessors
2. Figure out using namespace is bad and use a better technique
3. Make 2 arrays
a. p1dice[5]=0
b. p2dice[5]=0
4. Greet players
5. Explain the game
6. ask how many players
7. ask player[0] name
a. assign his/her name to player[0]
8. ask player[1] for name
a. assign player[1] name (or computer)
9. Ask player 0 to pick a number between 1 - 6
10 store it in a variable p0
11. Ask player 1 to pick a number between 1 - 6
12. store it in a variable p1
make a game loop i=1;<3 continue
and so on....

Then

Think about each step. What will it take to make this happen. Do I need a single variable? No? Okay,Do I need an array? yes? Great. Write that down next to number 3 or whatever... Keep breaking it down. Before long, you'll have Pseudo code. What you need is not someone helping you code, you need help getting organized. By the way, you're not alone. I'm learning the same lesson about myself.
Colleges shouldn't teach programming before UML, and logical organization. They should be required before they ever teach you the meaning of INT, and teach a good dose of debugging while they move along. Making debugging part of your homework assignments.

I'll check back tomorrow and see how you're doing. Smile!! You got this... :)
Was This Post Helpful? 0
  • +
  • -

#7 dansken15   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-September 15

Re: Need help with a light version of yahtzee

Posted 09 November 2015 - 09:09 AM

Hey Night Stalker, I just wanted to let you know, that I have finsished the program and got it to work like I wanted, or I changed it abit. When the first player have tossed 3 times, a question will appear where it asks if you want to move on to next player wich is the nonplaying.

i solved it by putting the code where I keep track of currentplayer on nonplaying, outside the loop.

And for the function where the player choose what die they want to save on, i solved it like this

				result[0] = 0;
				for (int i = 0; i < 5; i++)
				{
					if (die[i] == saving)
					{
						result[0]++;
					}
				}



And when the nrOfRolls turns 3 it prints out how many of the saving there is :)/>

Thanks for all the help, it have helped me alot in my program and learning. :)/>
Was This Post Helpful? 0
  • +
  • -

#8 Night Stalker   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 99
  • Joined: 26-December 13

Re: Need help with a light version of yahtzee

Posted 09 November 2015 - 01:57 PM

Well I don't know how much it helped, but just remember, there are steps to programming.

1. Get organized.
2. Plan your project.
3. write smaller programs that do one function. (Helps to track down errors)
4. Merge them into your main project.
5. Drape a banner across the roof of your house that says "Dream in Code.net ROCKS!"

Follow 4 of the 5 steps above (You choose which 4 ) and programming will be easier for you. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1