Question
i nned help fixing this code so that each prize is printed randomly 3 times so that there are 15 squares with prizes and 10
i nned help fixing this code so that each prize is printed randomly 3 times so that there are 15 squares with prizes and 10 empty
code:
#include
using namespace std;
int randRange(int low, int high){ return rand() % (high - low + 1) + low; }
void populateBoard(string arrBoard[5][5], string prizes[5]) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { arrBoard[i][j] = " "; } }
srand(time(0)); for (int i = 0; i < 9; i++) { int count = 0; while (count < 3) { int x = randRange(0, 4); int y = randRange(0, 4); for (int i = 0; i < 15; i++){ if (arrBoard[x][y] == " ") { arrBoard[x][y] = prizes[i]; count++; } } } } }
string playGame(string arrBoard[5][5], int pennies, string prizes[5]) { int prizeCounts[5] = {0}; string winner = "NONE"; while (pennies > 0) { int x = randRange(0, 4); int y = randRange(0, 4); if (arrBoard[x][y] != " ") { int prizeIndex = -1; for (int i = 0; i < 9; i++) { if (arrBoard[x][y] == prizes[i]) { prizeIndex = i; break; } } prizeCounts[prizeIndex]++; if (prizeCounts[prizeIndex] == 3) { winner = prizes[prizeIndex]; break; } pennies--; } } return winner; }
int main() { string arrBoard[5][5]; string prizes[5] = {"PUZZLE", "GAME", "DOLL", "BALL", "POSTER"};
populateBoard(arrBoard, prizes);
for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { cout << arrBoard[i][j] << "\t"; } cout << endl; } int pennies = 10; string winner = playGame(arrBoard, pennies, prizes); cout << "Prize won: " << winner << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started