Dice Challenge

This dice game program works fine but i need it to repeat each turn seven times for each 'player' then add the total score added after the seven 'rolls'.

This is it so far:

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <cstdlib>
#include <iostream>
using namespace std;

int getRand();

 void Rolldie( int num0=0,int num1=0, int num2=0, int num3=0, int num4=0, int num5=0,int num6=0,int num7=0);
 void results (int num0,int num1, int num2, int num3, int num4, int num5,int num6, int num7);
 void score( int num0,int num1, int num2, int num3, int num4, int num5,int num6, int num7); 
 int main()
{
 int num0,num1, num2, num3, num4, num5, num6,num7;
 srand(time(NULL));
 string player1;
 string player2;

 cout << "Welcome to the Dice Roller!  The simple rule of this game is to roll"<< endl;
 cout << "the die and finish on a total score of 10,000 or more points."<< endl << endl;


 cout << "Player 1, please enter your name: ";
 cin >> player1;
 cout<< endl;
 cout << "Welcome, " << player1 << "."<< endl << endl;
	
 cout << "Player 2, please enter your name: "; 
 cin >> player2;
 cout<< endl;
 cout << "Welcome, " << player2 << "."<< endl << endl;
	
 cout << "Let's begin!" << endl;
 system ("Pause");
 cout<<endl;
 cout<< player1 <<" rolled :";

 num0 = getRand();
 num1 = getRand();
 num2 = getRand();
 num3 = getRand();
 num4 = getRand();
 num5 = getRand();
 num6 = getRand();
 num7 = getRand();
    
 Rolldie(num0=0,num1=0, num2=0, num3=0, num4=0, num5=0, num6=0, num7=0);
    
 system("PAUSE");
 return EXIT_SUCCESS;
}
 int getRand()
{
 return (rand()%6 + 1);
}
 void Rolldie (int num0,int num1, int num2, int num3, int num4, int num5,int num6,int num7)
{

 int num;
 int counter = 0;
	
// loop which allows code to be executed repeatedly	
 while (counter <= 7)
{       
 for (int i = 0; i < 6; i++)
{	
 num = rand() % 6+1;//gives the random generated number (num)between 1 to 6
  
 cout <<" "<< num <<" ";
 
 // if else statements that determines the numbers generated
 if (num==0)
     num0++;
    else if(num == 1)
            num1++;
	else if(num == 2)
		    num2++;
	else if(num == 3)
		    num3++;
	else if(num == 4)
		    num4++;
	else if(num == 5)
		    num5++;
	else if(num == 6)
		    num6++;
	else
            num7++;
	counter++;
}
 int results (int num0,int num1, int num2, int num3, int num4, int num5,int num6, int num7);
{
 //results will return the amount of 1's to 6's that are randomly generated
 cout << num << endl;
 
}
 int score (int num0,int num1, int num2, int num3, int num4, int num5,int num6, int num7);
{
string player1; 
int total1=num1*1*100;
int total2=num2*2*100;
int total3=num3*3*100;
int total4=num4*4*100;
int total5=num5*5*100;
int total6=num6*6*100;
int T_total;
T_total=total1+total2+total3+total4+total5+total6;

cout<<" Roll points is: "<<T_total<<endl;//returns the overall score

}
}
}

Last edited on
I'm certainly not an expert or anything, but you might want to add a loop for the game so that it continues until the score is reached. Also doesn't the bracket on line 49 close main?
Thanks for the response.

How would i write the loop for the score? Ive been trying a number of things but its just not working. Could you write a sample loop so i can get an idea of how it should be scored and when that loop is put in place where should it go?

Thanks
Try to avoid to make your code too long and complicated. I believe that is bad practice. It looks like alot of code for what it's really doing.

Simplicity makes perfect. While your program may work, it looks like complicated code. I know you're a beginner like most of us on this forum, but a tip would be, to find the best possible way to make your code simple.

Some for-loops and do-while loops should do the trick ;)

Good luck.
On top of that, it would be a rather good idea to use arrays instead of the massive number of variables you have going on. With your rolldie function, especially so- rather than having to up the counter and do a random value for 8 variables, you can just have a single array (num[7]) and rather than having that complicated loop that you're using there, you can simply make nm[i] increment i and set whatever rolled value to num[i] where i is a number from 0 through 7. Basically, what you did exactly but with half of the code.
Im not allowed to use arrays in this. I hav'nt learnt that yet.
Well, in that case... with the same process that you used to determine what each die rolled in the rolldie function, you could make a loop that increments from 1 to 7 that, once it reaches 7, stops. Every time the loop completes once, it stores the total value (from your score function) into either total1, total2, total3, up to total7. This loop function would then return all 7 totals, which in main would be summed up, and displayed.
I was thinking that as well but im not sure how to write the loop so it stops at seven rolls could you write a part of it so i can know what to do?

And how do I get the second name to apear when the random numbers appear? When the program compiles is shows for example:

1
2
 Pat  rolled: 5 6 3 2 1 4 3 
 (nothing)  5 3 2 4 4 1 2 


in the (nothing) spot how can i get the second inputted name there?
Well, if you want it to display both player's names, make the proc rolldie only calculate the values for one player, not both. Then call the proc twice:

1
2
3
4
cout << player1 << "rolled:";
Rolldie(num0,num1,num2,num3,num4,num5.num6,num7);
cout << player2 << "rolled:";
Rolldie(num0,num1,num2,num3,num4,num5.num6,num7);


Also, there is absolutely no need to get a random number for num0-7 if when you pass the values to Rolldie, you immediately define them all to be 0. Also, if you're going to use srand(time(NULL)), be sure to #include <ctime>.

Also, feel free to replace that series of if-else statements with 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
 // switch that determines the numbers generated
 switch(num){
case 0:
    num0++;
    break;
case 1:
    num1++;
    break;
case 2:
    num2++;
    break;
case 3:
    num3++;
    break;
case 4:
    num4++;
    break;
case 5:
    num5++;
    break;
case 6:
    num6++;
    break;
case 7:
    num7++;
    break;
}	counter++;


If you want to make it easier to read and follow.
Last edited on
Ok. So I did what you said but now the score is giving negatives, for example it would say:

roll points is : -32765

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

int getRand();

 void Rolldie( int num0=0,int num1=0, int num2=0, int num3=0, int num4=0, int num5=0,int num6=0,int num7=0);
 void results (int num0,int num1, int num2, int num3, int num4, int num5,int num6, int num7);
 void score( int num0,int num1, int num2, int num3, int num4, int num5,int num6, int num7); 
 int main()
{
 int num0,num1, num2, num3, num4, num5, num6,num7;
 srand(time(NULL));
 string player1;
 string player2;

 cout << "Welcome to the Dice Roller!  The simple rule of this game is to roll"<< endl;
 cout << "the die and finish on a total score of 10,000 or more points."<< endl << endl;


 cout << "Player 1, please enter your name: ";
 cin >> player1;
 cout<< endl;
 cout << "Welcome, " << player1 << "."<< endl << endl;
	
 cout << "Player 2, please enter your name: "; 
 cin >> player2;
 cout<< endl;
 cout << "Welcome, " << player2 << "."<< endl << endl;
	
 cout << "Let's begin!" << endl;
 system ("Pause");
 cout<<endl;
 
 cout << player1 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
 
 cout << player2 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
   
 system("PAUSE");
 return EXIT_SUCCESS;

}
 void Rolldie (int num0,int num1, int num2, int num3, int num4, int num5,int num6,int num7)
{
 int num;
 int counter = 0;
	
 while (counter <= 5)// loop which allows code to be executed repeatedly	
{       
 num = rand() % 6+1;//gives the random generated number (num)between 1 to 6
 
 cout << " " << num << " " ;
 
 if (num==0)// if else statements that determines the numbers generated
     num0++;
    else if(num == 1)
            num1++;
	else if(num == 2)
		    num2++;
	else if(num == 3)
		    num3++;
	else if(num == 4)
		    num4++;
	else if(num == 5)
		    num5++;
	else if(num == 6)
		    num6++;
	else
            num7++;
	counter++;
}
 int results (int num0,int num1, int num2, int num3, int num4, int num5,int num6, int num7);
 //results will return the amount of 1's to 6's that are randomly generated
{
 
 cout << num << endl;
}
 int score (int num0,int num1, int num2, int num3, int num4, int num5,int num6, int num7);
{
 
int total0=num0*0;
int total1=num1*1*100;
int total2=num2*2*100;
int total3=num3*3*100;
int total4=num4*4*100;
int total5=num5*5*100;
int total6=num6*6*100;
int T_total;
T_total=total0+total1+total2+total3+total4+total5+total6;

cout<<endl;
cout<<" Roll points is: "<<"  "<< T_total <<endl;//returns the overall score

}
}
1
2
3
4
 int main()
{
 int num0,num1, num2, num3, num4, num5, num6,num7;
 srand(time(NULL));


Should be:

1
2
3
4
 int main()
{
 int num0=0,num1=0, num2=0, num3=0, num4=0, num5=0, num6=0,num7=0;
 srand(time(NULL));


And...

void Rolldie( int num0=0,int num1=0, int num2=0, int num3=0, int num4=0, int num5=0,int num6=0,int num7=0);

Should be:

void Rolldie( int num0,int num1, int num2, int num3, int num4, int num5,int num6,int num7);
It worked thank you :). Now how do i get it to repeat 7 times and show who wins with the highest final added score?

do i just repeat
1
2
3
4
5
cout << player1 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);
 
 cout << player2 << " rolled:";
 Rolldie(num0,num1,num2,num3,num4,num5,num6,num7);

seven times?

And for the score something like:

1
2
3
4
if (T_total>=10000)
Cout<<"player1 wins<<endl;
else
Cout<<"player 2 wins"<<endl; 
I didn't add anything much, except that now player2 cannot choose the same name as player1, but I removed so much clutter...

Don't use system("pause") in fact it's better not to use system("anything"), because it slows down the program a lot and for most things there are other solutions.

Ok enough talk, here's the full code:
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <cstdlib>
#include <string>
#include <windows.h>

void rolldice (int num1, int num2, int num3, int num4, int num5, int num6);
void ClearScreen();

int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0;
int player1score, player2score;
char playagain;

int main()
{
  do
  {
    srand(time(0));
    std::string player1;
    std::string player2;

    ClearScreen();
    std::cout << "Welcome to the Dice Roller!  The simple rule of this game is to roll \n";
    std::cout << "the die and score 10,000 or more points. \n\n";

    std::cout << "Player 1, please enter your name: ";
    std::cin >> player1;
    std::cout << "\nWelcome, " << player1 << ". \n\n";

    std::cout << "Player 2, please enter your name: ";
    std::cin >> player2;

        while (player2 == player1)
        {
            std::cout << "Player 1 has already chosen this name!\n\n";
            std::cout << "Player 2, please enter your name: ";
            std::cin >> player2;
        }

    std::cout << "\nWelcome, " << player2 << ". \n\n";

    std::cout << "Let's begin!\n\n";
    std::cout << "Press any key to continue...\n";
    std::cout << "(Now where's the 'any' key?)\n\n\n";
    std::cin.get();
    std::cin.ignore();

    std::cout << player1 << " rolled: ";
    rolldice (num1, num2, num3, num4, num5, num6);
    std::cout << "He/She scored " << player1score << " points!\n\n";

    std::cout << player2 << " rolled: ";
    rolldice (num1, num2, num3, num4, num5, num6);
    std::cout << "He/She scored " << player2score << " points!\n\n";

    std::cout << "Would you like to play again? (y/n)\n";
    std::cin >> playagain;

  } while (playagain == 'y' || playagain == 'Y');

    return 0;
}

void rolldice (int num1, int num2, int num3, int num4, int num5, int num6)
{
    num1 = rand() % 6 + 1; //randomly generates numbers from 1 to 6 for all 6 dice
    num2 = rand() % 6 + 1;
    num3 = rand() % 6 + 1;
    num4 = rand() % 6 + 1;
    num5 = rand() % 6 + 1;
    num6 = rand() % 6 + 1;

    //results will return the amount of 1's to 6's that are randomly generated
    std::cout << " " << num1 << " " << num2 << " " << num3 << " " << num4
              << " " << num5 << " " << num6 << "\n";

    int total1 = num1 * 1 * 100;
    int total2 = num2 * 2 * 100;
    int total3 = num3 * 3 * 100;
    int total4 = num4 * 4 * 100;
    int total5 = num5 * 5 * 100;
    int total6 = num6 * 6 * 100;

    player1score = total1 + total2 + total3 + total4 + total5 + total6;
    player2score = total1 + total2 + total3 + total4 + total5 + total6;
}

void ClearScreen()
{
  HANDLE                     hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD                      count;
  DWORD                      cellCount;
  COORD                      homeCoords = { 0, 0 };

  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;

  /* Get the number of cells in the current buffer */
  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;

  /* Fill the entire buffer with spaces */
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Fill the entire buffer with the current colors and attributes */
  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Move the cursor home */
  SetConsoleCursorPosition( hStdOut, homeCoords );
}
Last edited on
You could also add an easter egg, where say if a player chose the name "asdf", then they would instantly win, or if they chose a name, which was only one letter, the program would tell them to go away :)
Last edited on
Topic archived. No new replies allowed.