### 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:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112`` ``````#include #include 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<
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:

 ``12`` `````` 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:

 ``1234`` ``````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:
 ``123456789101112131415161718192021222324252627`` `````` // 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

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798`` ``````#include #include #include 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<
 ``1234`` `````` int main() { int num0,num1, num2, num3, num4, num5, num6,num7; srand(time(NULL));``````

Should be:

 ``1234`` `````` 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
 ``12345`` ``````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:

 ``1234`` ``````if (T_total>=10000) Cout<<"player1 wins<
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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122`` ``````#include #include #include #include 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.