amount of possibilities. dice lots of nesting

hello i have a function that should get the amount of ways you can throw a specific number (sum off all dice) with 9 four sided dice and 6 six sided dice. the problem is i have in total about 15 for loops all nested and they are really annoying me. so could someone tell me how i could reduce the loops (by some formula to get it immediately or if side=6 then it only does 6 loops and if side=4 then it does 9 loops.)

my code is below:

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <cmath>

int amount_of_possibilities(int total,int sides) {
	int amount=0;
	if(sides==4) {
		for (int a1=1;a1<5;a1++) {
			for (int a2=1;a2<5;a2++) {
				for (int a3=1;a3<5;a3++) {
					for (int a4=1;a4<5;a4++) {
						for (int a5=1;a5<5;a5++) {
							for (int a6=1;a6<5;a6++) {
								for (int a7=1;a7<5;a7++) {
									for (int a8=1;a8<5;a8++) {
										for (int a9=1;a9<5;a9++) {
											if (a1+a2+a3+a4+a5+a6+a7+a8+a9==total) {
												amount++;
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	if(sides==6) {
		for (int a1=1;a1<7;a1++) {
			for (int a2=1;a2<7;a2++) {
				for (int a3=1;a3<7;a3++) {
					for (int a4=1;a4<7;a4++) {
						for (int a5=1;a5<7;a5++) {
							for (int a6=1;a6<7;a6++) {
								if (a1+a2+a3+a4+a5+a6==total) {
												amount++;
								}
							}
						}
					}
				}
			}
		}
	}
	return amount;
}


int main() {
	int pyramid_sides=4;
	const int pyramid_dices=9;
	int pyramid_chance[36-pyramid_dices];//shows how many combinations can be made with index+9 as total
	int cube_sides=6;
	const int cubic_dices=6;
	int cubic_chance[36-cubic_dices];//shows how many combinations can be made with index+6 as total
	for(int i=0;i<28;i++) {
		pyramid_chance[i]=amount_of_possibilities(i+pyramid_dices,pyramid_sides);	
	}
	for(int i=0;i<31;i++) {
		cubic_chance[i]=amount_of_possibilities(i+cubic_dices,cube_sides);	
	}
	for(int i=0;i<31;i++) {
		std::cout<<cubic_chance[i]<<std::endl;
	}
	system("pause");
	return 0;
}
Last edited on
I'm a bit confused. So you want to know how many combinations or rolls there are for each set of dice?
example: if i want to know in how many ways i can throw an 8 in total (sum) when you have 3 four-sided dice.
i could make a table and i should find that there are 12 ways to get an
8 in total. i listed all the 8 ways below. note that the sum of 3 numbers below each other is always 8.
1 1 2 2 2 3 3 3 3 4 4 4
3 4 2 3 4 1 2 3 4 1 2 3
4 3 4 3 2 4 3 2 1 3 2 1
Last edited on
Topic archived. No new replies allowed.