We revisit the slot machine from last week, this time using a range-based For lo
ID: 3804406 • Letter: W
Question
We revisit the slot machine from last week, this time using a range-based For loop and the Switch statement. The Switch gives us some flexibility in coding the payouts. The code has one compile time bug and two run-time bugs. One of the run-time errors is a typo and the other is a logic error. Fix the compile error and run the program to find the run-time errors. Neither run-time bug will show up right away. This is to give you experience with testing your program to make sure it works. Often, although a program seems to be OK, it isn’t. Hint: Look for occasional erroneous wins of $1 when the first symbol is Bar.
Here is the code.
// Week 4 Assignment- 2
// Description: Run-time errors in more loops and branching
//----------------------------------
//**begin #include files************
#include <iostream> // provides access to cin and cout
#include <iomanip>
#include <array>
#include <vector>
//--end of #include files-----------
//----------------------------------
using namespace std;
//----------------------------------
//**begin global constants**********
// number of positions on a reel (11)
const int reelPositions = 11;
//--end of global constants---------
//----------------------------------
//**begin main program**************
int main()
{
// seed random number generator
srand(time(NULL));
// create enum for symbols
enum symbol
{
Lemon, Cherry, Orange, Bell, Bar, Jackpot
};
// create a struct for slot machine wheel
struct Wheel
{
array <string, reelPositions > symbols;
array <symbol, 1 reelPositions 1> eSymbols;
int position;
string selected;
};
//create an array of three slot machine wheels
array <Wheel, 3> slotMachine =
{
{
{
{"Bar", "Orange", "Lemon", "Orange", "Bell", "Orange", "Bar", "Lemon", "Bell", "Jackpot", "Bell"},
{Bar, Orange, Lemon, Orange, Bell, Orange, Lemon, Bell, Jackpot, Bell},
0,"Cherry"
},
{
{"Lemon", "Bell", "Lemon", "Orange", "Bell", "Jackpot", "Bar", "Lemon", "Cherry", "Jackpot", "Bell"},
{Lemon, Bell, Lemon, Orange, Bell, Jackpot, Bar, Lemon, Cherry, Jackpot, Bell},
1,"Bell"
},
{
{"Cherry", "Orange", "Bar", "Lemon", "Orange", "Lemon", "Orange", "Lemon","Cherry", "Jackpot", "Bell"},
{Cherry, Orange, Bar, Lemon, Orange, Lemon, Orange, Lemon, Cherry, Jackpot, Bell},
2,"Lemon"
}
}
};
bool gameOn = true;
int thePot = 100;
int bet = 1;
bool winner = false;
int winnings = 0;
char checkKey =' ';
vector <int> combo;
cout << "Hit return to bet, space and return to quit." << endl;
while (gameOn)
{
for (auto s: slotMachine)
{
s.position =(s.position + rand()%reelPositions)% reelPositions;
s.selected = s.symbols[s.position];
cout << setw(10) << left << s.selected.c_str() ;
combo.push_back(s.eSymbols[s.position]);
}
winnings = -bet;
switch (combo[0]);
{
case Lemon:
switch (combo[1])
{
case Lemon:
if (combo[2] == Lemon) winnings = 1;
else if (combo[2] == Cherry) winnings = 1;
break;
case Cherry:
winnings = 1;
if (combo[2] == Cherry) winnings = rand()%4+2;
break;
default:
if (combo[2] == Cherry) winnings = 1;
break;
}
break;
case Cherry:
winnings = 1;
if ((combo[1] == Cherry) && (combo[2] == Cherry)) winnings = 10;
else if ((combo[1] == Cherry) || (combo[2] == Cherry)) winnings = rand()%4+2;
break;
case Orange:
switch (combo[1])
{
case Orange:
if (combo[2] == Orange) winnings = 15;
else if (combo[2] == Cherry) winnings = 1;
break;
case Cherry:
winnings = 1;
if (combo[2] == Cherry) winnings = rand()%4+2;
break;
default:
if (combo[2] == Cherry) winnings = 1;
break;
}
break;
case Bell:
switch (combo[1])
{
case Bell:
if (combo[2] == Bell) winnings = 20;
else if (combo[2] == Cherry) winnings = 1;
break;
case Cherry:
winnings = 1;
if (combo[2] == Cherry) winnings = rand()%4+2;
break;
default:
if (combo[2] = Cherry) winnings = 1;
break;
}
break;
case Bar:
switch (combo[1])
{
case Bar:
if (combo[2] == Bar) winnings = 40;
else if (combo[2] == Cherry) winnings = 1;
break;
case Cherry:
winnings = 1;
if (combo[2] == Cherry) winnings = rand()%4+2;
break;
default:
if (combo[2] == Cherry) winnings = 1;
break;
}
break; case Jackpot:
switch (combo[1])
{
case Jackpot:
if (combo[2] == Jackpot)
{
winnings = 1000;
winner = true;
gameOn = false;
cout << "You hit the Jackpot!!!" << endl;
}
else if (combo[2] == Cherry) winnings = 1;
break;
case Cherry:
winnings = 1;
if (combo[2] == Cherry) winnings = rand()%4+2;
break;
default:
if (combo[2] == Cherry) winnings = 1;
break;
}
break;
}
if (winnings > 0) cout << "You win " << winnings << endl;
thePot += winnings;
cout << "You now have $" << thePot << endl;
combo.clear();
cout << endl;
cin.get(checkKey);
if (checkKey != ' ') gameOn = false;
}
while (!cin.get()){};
if (winner) cout << "You walk away a winner." << endl;
else if (thePot < 0) cout << "Good bye." << endl;
else cout << "You have lost all your money." << endl;
// Wait for user input to close program when debugging.
cin.get();
return 0;
}
//--end of main program-------------
//----------------------------------
Explanation / Answer
#include <iostream> // provides access to cin and cout
#include <iomanip>
#include <array>
#include <vector>
//--end of #include files-----------
//----------------------------------
using namespace std;
//----------------------------------
//**begin global constants**********
// number of positions on a reel (11)
const int reelPositions = 11;
//--end of global constants---------
//----------------------------------
//**begin main program**************
int main()
{
// seed random number generator
srand(time(NULL));
// create enum for symbols
enum symbol
{
Lemon, Cherry, Orange, Bell, Bar, Jackpot
};
// create a struct for slot machine wheel
struct Wheel
{
array <string, reelPositions > symbols;
array <symbol, 1 reelPositions 1> eSymbols;
int position;
string selected;
};
//create an array of three slot machine wheels
array <Wheel, 3> slotMachine =
{
{
{
{"Bar", "Orange", "Lemon", "Orange", "Bell", "Orange", "Bar", "Lemon", "Bell", "Jackpot", "Bell"},
{Bar, Orange, Lemon, Orange, Bell, Orange, Lemon, Bell, Jackpot, Bell},
0,"Cherry"
},
{
{"Lemon", "Bell", "Lemon", "Orange", "Bell", "Jackpot", "Bar", "Lemon", "Cherry", "Jackpot", "Bell"},
{Lemon, Bell, Lemon, Orange, Bell, Jackpot, Bar, Lemon, Cherry, Jackpot, Bell},
1,"Bell"
},
{
{"Cherry", "Orange", "Bar", "Lemon", "Orange", "Lemon", "Orange", "Lemon","Cherry", "Jackpot", "Bell"},
{Cherry, Orange, Bar, Lemon, Orange, Lemon, Orange, Lemon, Cherry, Jackpot, Bell},
2,"Lemon"
}
}
};
bool gameOn = true;
int thePot = 100;
int bet = 1;
bool winner = false;
int winnings = 0;
char checkKey =' ';
vector <int> combo;
cout << "Hit return to bet, space and return to quit." << endl;
while (gameOn)
{
for (auto s: slotMachine)
{
s.position =(s.position + rand()%reelPositions)% reelPositions;
s.selected = s.symbols[s.position];
cout << setw(10) << left << s.selected.c_str() ;
combo.push_back(s.eSymbols[s.position]);
}
winnings = -bet;
switch (combo[0]);
{
case Lemon:
switch (combo[1])
{
case Lemon:
if (combo[2] == Lemon) winnings = 1;
else if (combo[2] == Cherry) winnings = 1;
break;
case Cherry:
winnings = 1;
if (combo[2] == Cherry) winnings = rand()%4+2;
break;
default:
if (combo[2] == Cherry) winnings = 1;
break;
}
break;
case Cherry:
winnings = 1;
if ((combo[1] == Cherry) && (combo[2] == Cherry)) winnings = 10;
else if ((combo[1] == Cherry) || (combo[2] == Cherry)) winnings = rand()%4+2;
break;
case Orange:
switch (combo[1])
{
case Orange:
if (combo[2] == Orange) winnings = 15;
else if (combo[2] == Cherry) winnings = 1;
break;
case Cherry:
winnings = 1;
if (combo[2] == Cherry) winnings = rand()%4+2;
break;
default:
if (combo[2] == Cherry) winnings = 1;
break;
}
break;
case Bell:
switch (combo[1])
{
case Bell:
if (combo[2] == Bell) winnings = 20;
else if (combo[2] == Cherry) winnings = 1;
break;
case Cherry:
winnings = 1;
if (combo[2] == Cherry) winnings = rand()%4+2;
break;
default:
if (combo[2] = Cherry) winnings = 1;
break;
}
break;
case Bar:
switch (combo[1])
{
case Bar:
if (combo[2] == Bar) winnings = 40;
else if (combo[2] == Cherry) winnings = 1;
break;
case Cherry:
winnings = 1;
if (combo[2] == Cherry) winnings = rand()%4+2;
break;
default:
if (combo[2] == Cherry) winnings = 1;
break;
}
break; case Jackpot:
switch (combo[1])
{
case Jackpot:
if (combo[2] == Jackpot)
{
winnings = 1000;
winner = true;
gameOn = false;
cout << "You hit the Jackpot!!!" << endl;
}
else if (combo[2] == Cherry) winnings = 1;
break;
case Cherry:
winnings = 1;
if (combo[2] == Cherry) winnings = rand()%4+2;
break;
default:
if (combo[2] == Cherry) winnings = 1;
break;
}
break;
}
if (winnings > 0) cout << "You win " << winnings << endl;
thePot += winnings;
cout << "You now have $" << thePot << endl;
combo.clear();
cout << endl;
cin.get(checkKey);
if (checkKey != ' ') gameOn = false;
}
while (!cin.get()){};
if (winner) cout << "You walk away a winner." << endl;
else if (thePot < 0) cout << "Good bye." << endl;
else cout << "You have lost all your money." << endl;
// Wait for user input to close program when debugging.
cin.get();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.