Oct 25, 2008 at 2:23am UTC
Hello. I have made a dice game (that is also in the General C++ Programming section) but there is a problem with it.
After each game, you get points according to what you roll. Your points is saved and then forwarded to
overallScorePlayer
and
overallScoreComputer
. At the end, it will tell you who is leading the competition with the overall score.
The problem is - the overall score for the computer is always 239189213983121 when the points is no where near that and so, it always wins.
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
char endGame;
int rollAgain;
int diceThrow1;
int diceThrow2;
int computerThrow1;
int computerThrow2;
int diceScorePlayer;
int diceScoreComputer;
int overallScorePlayer;
int overallScoreComputer;
cout << "\nWelcome to Dice Game Mania! The objective of this game is to score the highest points." ;
cout << "\nYou roll your die at the start and you get points according to what you get." ;
cout << "\nIf you or the computer rolls a 1 and a 2, the roller scores 500 points!" ;
cout << "\nIf you or the computer rolls 2 numbers that are the same, you get the number of points equal to the first dice roll multiplied by 33." ;
cout << "\nIf you or the computer rolls any other numbers, you score the first roll multiplied by 10 plus the second roll." ;
cout << "\nAfter one roll, you can choose to re-roll dice 1, dice 2 or just leave it as it is." ;
cout << "\nDoing so can help you win, but remember - it also resets the computer's dice you chose as well!" ;
diceGame:
srand(time(0));
diceThrow1 = rand() % 6 + 1;
diceThrow2 = rand() % 6 + 1;
computerThrow1 = rand() % 6 + 1;
computerThrow2 = rand() % 6 + 1;
cout << "\nComputer Dice Throw 1: " << computerThrow1;
cout << "\nComputer Dice Throw 2: " << computerThrow2;
if (computerThrow1 + computerThrow2 == 3)
{
diceScoreComputer = 1000;
cout << "\nComputer Score is " << diceScoreComputer;
}
else if (computerThrow1 == computerThrow2)
{
diceScoreComputer = computerThrow1 * 110;
cout << "\nComputer Score is " << diceScoreComputer;
}
else
{
diceScoreComputer = computerThrow1 * 10 + computerThrow2;
cout << "\nComputer Score is " << diceScoreComputer;
}
cout << "\nUser Dice Throw 1: " << diceThrow1;
cout << "\nUser Dice Throw 2: " << diceThrow2 << endl;
if (diceThrow1 + diceThrow2 == 3)
{
diceScorePlayer = 500;
cout << "\nUser Score is " << diceScorePlayer;
}
else if (diceThrow1 == diceThrow2)
{
diceScorePlayer = diceThrow1 * 33;
cout << "User Score is " << diceScorePlayer;
}
else
{
diceScorePlayer = diceThrow1 * 10 + diceThrow2;
cout << "User Score is " << diceScorePlayer;
}
cout << "\nType '1' to reroll dice 1, '2' to reroll dice 2 or '3' to leave it." ;
cin >> rollAgain;
if (rollAgain == 1)
{
diceThrow1 = rand() % 6 + 1;
computerThrow1 = rand() % 6 + 1;
cout << "\nUser Dice Throw 1: " << diceThrow1;
cout << "\nUser Dice Throw 2: " << diceThrow2;
cout << "\nComputer Dice Throw 1: " << computerThrow1;
cout << "\nComputer Dice Throw 2: " << computerThrow2;
if (diceThrow1 + diceThrow2 == 3)
{
diceScorePlayer = 500;
cout << "\nUser Score is " << diceScorePlayer;
}
else if (diceThrow1 == diceThrow2)
{
diceScorePlayer = diceThrow1 * 33;
cout << "\nUser Score is " << diceScorePlayer;
}
else
{
diceScorePlayer = diceThrow1 * 10 + diceThrow2;
cout << "\nUser Score is " << diceScorePlayer;
}
if (computerThrow1 + computerThrow2 == 3)
{
diceScoreComputer = 500;
cout << "\nComputer Score is " << diceScoreComputer;
}
else if (computerThrow1 == computerThrow2)
{
diceScoreComputer = diceThrow1 * 33;
cout << "\nComputer Score is " << diceScoreComputer;
}
else
{
diceScoreComputer = diceThrow1 * 10 + diceThrow2;
cout << "\nComputer Score is " << diceScoreComputer;
}
}
else if (rollAgain == 2)
{
diceThrow2 = rand() % 6 + 1;
computerThrow2 = rand() % 6 + 1;
cout << "\nUser Dice Throw 1: " << diceThrow1;
cout << "\nUser Dice Throw 2: " << diceThrow2;
cout << "\nComputer Dice Throw 1: " << computerThrow1;
cout << "\nComputer Dice Throw 2: " << computerThrow2;
if (diceThrow1 + diceThrow2 == 3)
{
diceScorePlayer = 500;
cout << "\nUser Score is " << diceScorePlayer;
}
else if (diceThrow1 == diceThrow2)
{
diceScorePlayer = diceThrow1 * 33;
cout << "\nUser Score is " << diceScorePlayer;
}
else
{
diceScorePlayer = diceThrow1 * 10 + diceThrow2;
cout << "\nUser Score is " << diceScorePlayer;
}
if (computerThrow1 + computerThrow2 == 3)
{
diceScoreComputer = 500;
cout << "\nComputer Score is " << diceScoreComputer;
}
else if (computerThrow1 == computerThrow2)
{
diceScoreComputer = computerThrow1 * 33;
cout << "\nComputer Score is " << diceScoreComputer;
}
else
{
diceScoreComputer = computerThrow1 * 10 + computerThrow2;
cout << "\nComputer Score is " << diceScoreComputer;
}
}
else
{
cout << "\nDie left as it was thrown." ;
}
if (diceScorePlayer < diceScoreComputer)
{
cout << "\nThe computer has won this round." ;
}
else if (diceScorePlayer > diceScoreComputer)
{
cout << "\nCongratulations! You have won this round!" ;
}
else
{
cout << "\nWOW! You both had the same scores! TIE!" ;
}
overallScorePlayer + diceScorePlayer;
overallScoreComputer + diceScoreComputer;
if (overallScorePlayer > overallScoreComputer)
{
cout << "\nCongratulations! You are currently leading the game at " << overallScorePlayer << " points!" ;
}
else if (overallScorePlayer < overallScoreComputer)
{
cout << "\nThe Computer is currently leading the game at " << overallScoreComputer << " points." ;
}
else
{
cout << "\nIt's a tie at " << overallScorePlayer << " points! Amazing!" ;
}
cout << "Do you wish to continue playing? ('Y' or 'N')" ;
cin >> endGame;
if (endGame == 'N' || endGame == 'n' )
{
cout << "You have left the game with " << overallScorePlayer << " points." << endl;
system("PAUSE" );
return (0);
}
else if (endGame == 'Y' || endGame == 'y' )
{
system("CLS" );
goto diceGame;
cout << "\n" ;
}
}
Thanks for any help!
Last edited on Oct 25, 2008 at 2:24am UTC
Oct 25, 2008 at 3:26am UTC
I think most people here check both forums, so it really doesn't need to be posted twice. Plus, you REALLY need to indent this, it's a mess. You should check and see if your IDE does auto indent, like in VS2005, its <ctrl><A><K><F>
give me a minute to look at it
Last edited on Oct 25, 2008 at 3:47am UTC
Oct 25, 2008 at 3:33am UTC
1 2 3
overallScorePlayer + diceScorePlayer;
overallScoreComputer + diceScoreComputer;
these two lines do nothing
when you add two variables without assigning them to anything, the value vanishes into the ether...if you want to calculate a running total, you need to use something like
overallScoreComputer=overallScoreComputer+diceScoreComputer
and you should set overall scores to 0 at the beginning of the program.
I just ran it with those two changes and it works fine, except that when you exit the game, it only tells you your score. It should give the final scores for each player.
Last edited on Oct 25, 2008 at 3:45am UTC
Oct 25, 2008 at 4:20am UTC
This is the new one. This copy was never posted in the General C++ Programming Forum, just the question about it.
Sorry about the non-indenting. It was because in the first version, I had a do {..}while (..)
loop that was included in the question. So after it was removed, I had indented like, every line? I just had to try to make it as neat as possible (hey, it's not that easy! 200 lines of code to re-indent and fix.)
Thanks for your help. It worked good now.
Thanks again! :)
Last edited on Oct 25, 2008 at 4:24am UTC