Fun little program you have there. I believe most of the troubles are coming from the previous cin request, namely the one asking for a
single-character answer on whether user would like to reroll. The newline after the single character is still in play and causes problems for the next cin (inside reroll()).
Added some other tweaks to help harden things and make it easier to maintain:
- calling the rollAll() from reroll() instead of repeating that logic
- changed rollDie() to roll a die passed in by reference instead of making extra copies and assignments
- was necessary to add another boolean var to each die, "needs_roll", to see if a die needed to be rolled. When a die is first created, I set this var to true.
- no longer need a new array to keep track of reroll indices, since each die now stores this information
- a dynamic vector added just to just push_back and then show the user-input numbers that were parsed successfully
- since you're asking for an unknown number of integers to mark the rerolls, I think it makes sense to use getline() to grab the line, then throw it into an istringstream, the parse on that until it cant find anymore integers
- reduced repetition in main() by running the reroll question logic 2 times under a loop
- might've converted a bunch of tabs to 4 spaces ;D
Can test at
https://repl.it/repls/ImpossibleExemplaryResources
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
|
#include <iostream> //used for input and output
#include <cstdlib> //used for random functions
#include <ctime> //used for setting srand seed based on time
#include <string> //used for strings
#include <sstream>
#include <limits>
#include <vector>
using namespace std;
enum COLORS { red, blue, green, orange }; //enum type for face color
struct die //struct for a die
{
die() : needs_roll(true)
{
}
int faceValue;
COLORS color;
bool wild;
bool needs_roll;
};
struct player //struct for a player
{
string name;
int score;
};
const int ARRAY_SIZE = 10; //size of dice arrays
// Rolls the die d
void rollDie(die& d, int min, int max)
{
d.faceValue = rand() % (max - min + 1) + min;
d.color = static_cast<COLORS>(rand() % 4);
d.wild = false;
d.needs_roll = false;
}
// Roll all dice that have "needs_roll" set to true
void rollAll(die* dice)
{
int LowDiceMin = 1;
int LowDiceMax = 6;
int HighDiceMin = 5;
int HighDiceMax = 10;
for (int i = 0; i < 4; i++) //rolls low dice
{
if (dice[i].needs_roll)
{
rollDie(dice[i], LowDiceMin, LowDiceMax);
dice[i].wild = dice[i].faceValue == 5 || dice[i].faceValue == 6;
}
}
for (int i = 4; i < 10; i++) //rolls high dice
{
if (dice[i].needs_roll)
rollDie(dice[i], HighDiceMin, HighDiceMax);
}
}
void displayAll(die* dice)
{
char abrcolor;
for (int i = 0; i < ARRAY_SIZE; i++)
{
cout << " ____ "; //formats the top row of the dice display
}
cout << endl;
for (int i = 0; i < ARRAY_SIZE; i++)
{
switch (dice[i].color) //converts the COLORS enum results to a character variable for output
{
case 0:
abrcolor = 'R';
break;
case 1:
abrcolor = 'G';
break;
case 2:
abrcolor = 'B';
break;
case 3:
abrcolor = 'O';
break;
}
if (i < 4) //checks for wilds, then outputs low dice
{
if (dice[i].wild == true)
cout << "|" << abrcolor << " W| ";
else
cout << "|" << abrcolor << " " << dice[i].faceValue << "| ";
}
else
if (dice[i].faceValue == 10) //checks for a value of ten for spacing, then outputs high dice
cout << "|" << abrcolor << " " << dice[i].faceValue << "| ";
else
cout << "|" << abrcolor << " " << dice[i].faceValue << "| ";
}
cout << endl;
for (int i = 0; i < ARRAY_SIZE; i++) //formats the bottom of the dice
{
cout << "|____| ";
}
cout << endl;
for (int i = 0; i < ARRAY_SIZE; i++) //outputs the sequential number of each dice
{
cout << " " << i + 1 << " ";
}
cout << endl;
}
void reroll(die* dice)
{
cout << "Input space-separated numbers of the dice you wish to reroll and then press <Enter>\n";
cout << "? ";
string line;
getline(cin, line);
istringstream iss(line);
vector<int> redos;
int die_num = -1;
while (iss >> die_num)
{
if (1<=die_num && die_num<=ARRAY_SIZE)
{
redos.push_back(die_num);
dice[die_num - 1].needs_roll = true;
}
else
{
cout << "Skipping input '"<<die_num<<"' -- should be between 1 and "<<ARRAY_SIZE<<endl;
}
}
cout << "Parsed rerolls: ";
for (auto& n : redos)
cout << n << ' ';
cout << endl;
cout << "The dice after the reroll: " << endl;
rollAll(dice);
displayAll(dice);
}
int main()
{
srand(time(NULL)); //sets srand seed
die dice[ARRAY_SIZE];
cout << "Press <Enter> to roll the dice.";
cin.ignore(numeric_limits<int>::max(), '\n');
rollAll(dice);
displayAll(dice);
char answer;
for (int times=1; times<=2; ++times)
{
cout << "Would you like to reroll any dice? y/n: ";
cin >> answer;
cin.ignore(numeric_limits<int>::max(), '\n');
if (answer == 'y')
reroll(dice);
else
break;
}
cout << endl << endl;
cout << "Thanks for playing!\n";
return 0;
}
|
Example output:
Press <Enter> to roll the dice.
____ ____ ____ ____ ____ ____ ____ ____ ____ ____
|G 1| |O 2| |O 2| |O 1| |B 9| |O 10| |G 10| |R 10| |G 5| |O 10|
|____| |____| |____| |____| |____| |____| |____| |____| |____| |____|
1 2 3 4 5 6 7 8 9 10
Would you like to reroll any dice? y/n: y
Input space-separated numbers of the dice you wish to reroll and then press <Enter>
? 1 2 -7 4 5 15 6 7
Skipping input '-7' -- should be between 1 and 10
Skipping input '15' -- should be between 1 and 10
Parsed rerolls: 1 2 4 5 6 7
The dice after the reroll:
____ ____ ____ ____ ____ ____ ____ ____ ____ ____
|B W| |B 4| |O 2| |O 3| |O 10| |B 6| |R 5| |R 10| |G 5| |O 10|
|____| |____| |____| |____| |____| |____| |____| |____| |____| |____|
1 2 3 4 5 6 7 8 9 10
Would you like to reroll any dice? y/n: y
Input space-separated numbers of the dice you wish to reroll and then press <Enter>
? 1 2 3 4 5 7 8 9
Parsed rerolls: 1 2 3 4 5 7 8 9
The dice after the reroll:
____ ____ ____ ____ ____ ____ ____ ____ ____ ____
|B W| |G 2| |O W| |B 1| |O 6| |B 6| |B 6| |B 9| |O 9| |O 10|
|____| |____| |____| |____| |____| |____| |____| |____| |____| |____|
1 2 3 4 5 6 7 8 9 10
Thanks for playing! |