I need some help with the Dice game. Could someone help me with this, please?

Hello. I am trying to figure out a dice game so it would output the type plus how many it output. I need to use vectors and arrays.

For example:
Five of a kind with 3's and 1's
Four of a kind with 2's and 5's

When running the program it needs to look like this:
Enter seed (1 - 10,000): 57
Using seed of 57
The rolled dice: [3] [3] [1] [1] [3]
The frequencies: 1-> 2 2-> 0 3-> 3 4-> 0 5-> 0 6-> 0
The dice totaled 11—sum of the dice.
Full house with three 3s and two 1s----this is what I am stuck on and need help with.

I do not need a small straight. Could someone me help me, as I am stuck on this?


#include <iostream>
#include <vector>
#include <cstdlib>


using namespace std;

void seeding();
int rollDie();
void rollrept();
int sumOfdice=0;



int main()
{
seeding();
rollrept();
system ("PAUSE");
return EXIT_SUCCESS;
}

void rollrept()
{
vector <unsigned> frequency(7);
cout<< "The rolled dice: ";

for (unsigned i=0; i<5; ++i)
{
int roll = rollDie();
std:: cout<<'[' <<roll<<"] ";//brackets for the # dice rolled
++frequency[roll];
sumOfdice += roll;

}
cout<<"\nThe Frequencies: ";

for (unsigned i=1; i < frequency.size(); ++i)

std::cout<< i<<"->"<< frequency[i]<< " ";
cout<<"\nThe sum of the dice: "<<sumOfdice<<endl;


}
void seeding()//asks for the user input
{
int seed;
std::cout<<"Enter a number between 1 and 10000: ";
std::cin>>seed;
srand(seed);
}

int rollDie()
{
return (rand() % 6)+1;
}
Last edited on
1: Press tab to indent in a function or loop so we can see more clearly what goes where.

2: I don't actually see you using your 'seed' function for anything; delete it.

3: For your problem, I would try making an array of numbers. Each element of the array represents a possible outcome. You would make your array first zeroes, and then increment accordingly based on your outcomes.

Like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void rollrept() {
    int outcomeAmount[5] = {0,0,0,0,0,0};
    
    for (int i = 0; i < 5; i++) {
        int roll = rollDie() - 1;
        outcomeAmount[roll]++;
        cout << "["+roll+"]";
        sumOfDice += roll;
    }
    
    cout << "\nThe Frequencies: ";
    
    for (i = 0; i < outcomeAmount.size(); i++) {
        cout << i + "->" + outcomeAmount[i] + " ";
    }
    
    //echo sumOfDie here
    
    //'a' holds the amount each number occured (for example: 1st element of a = amount of 
    //times one occured in the set of die)

    int a[] = {count(outcomeAmount, outcomeAmount+6, 1), count(outcomeAmount, 
outcomeAmount+6, 2) ,count(outcomeAmount, outcomeAmount+6, 3),
 count(outcomeAmount, outcomeAmount+6, 4), count(outcomeAmount, 
outcomeAmount+6, 5), count(outcomeAmount, outcomeAmount+6, 6);

    for (i = 0; i < a.size(); i++) {
         cout << "\n" + (i+1) + " occured " + a[i] + " times."

    }


Let me know if this works for you.

Way to interpret: for the example you gave (3,3,1,1,3), it should spit out "3 occured 3 times" (new line) "1 occured 2 times".

You can use this information to determine patterns, such as full houses.

Good Luck.

Note: You can combine strings with the plus sign.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
bool Full_House (int& first, int& second, vector<unsigned>& frequency);

int main()
{
    system ("cls");
	srand(time(NULL));
    //seeding();
    for (int i = 1 ; i<100;i++)
		rollrept();
    system ("PAUSE");
    return EXIT_SUCCESS;
}

bool Full_House (int& first, int& second, vector<unsigned>& frequency)
{
    bool b_two (false);
    bool b_three (false);
    for (unsigned i=1; i<frequency.size(); ++i)
    {
        if (frequency[i] == 3)
        {
            b_three = true;
            first = i;
        }
        if (frequency[i] == 2)
        {
            b_two = true;
            second = i;
        }
    } // for loop
    return (b_two && b_three);
}

void rollrept()
{
    vector <unsigned> frequency(7);
    cout<< "The rolled dice: ";

.
.
.
	
	int fh_first(0);
    int fh_second(0);
    
    if (Full_House(fh_first, fh_second, frequency))
	{
		std::cout << "-----------Full house with three " << fh_first << 
		"s and two " << fh_second << "s" << endl; 
	}
}


This is how I approached the problem. Let me know if this works for you.
Thank you so much! I got it to work.
Topic archived. No new replies allowed.