Slot Machine in C++ Assignment Sample
You’ve been asked to program a slot machine for a mafia-operated casino, so make sure you get it right or you can try out your new concrete shoes when you go swimming. The slot machine displays 3 letters representing the different reels, and you get money if 2 or 3 are the same (more money for 3). The user should be able to transfer funds to the game, check their balance, play a game, or cash out. The first 5 rolls are fair, but after the 5th roll, if you have won more than you have spent, it starts cheating, making losing more likely. You should use lots of functions to avoid repeating code. For more C++ programming assignments contact us for a quote.
Solution:
functions.h
struct slot_machine{
int balance=0;
int roll_1=0;
int roll_2=0;
int roll_3=0;
};
void loop();
void printMenu();
void transferFunds();
void spin(slot_machine machine);
void spin(slot_machine machine,int rerolls);
void checkFunds();
void exit();
functions.cpp
#include “functions.h”
#include
#include /* srand, rand */
using namespace std;
char values[]={‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’,’k’};
int funds=500;
slot_machine the_machine;
int option=0;
int clean_rolls=5;
void loop(){
while(option!=4){
printMenu();
if(option==1)
checkFunds();
if(option==2)
transferFunds();
if(option==3){
if(clean_rolls>0)
spin(the_machine);
else
spin(the_machine,40);
clean_rolls–;
}
if(option==4)
exit();
}
}
void printMenu(){
cout<
cout<<“1. Check Bankk Balance”<
cout<<“2. Transfer funds to game”<
cout<<“3. Play”<
cout<<“4. Exit”<
cout<
cin>>option;
}
void spin(slot_machine machine){
cout<
machine.roll_1=(rand() % 10);
machine.roll_2=(rand() % 10);
machine.roll_3=(rand() % 10);
if(machine.roll_1==machine.roll_2 && machine.roll_2==machine.roll_3)
{
cout<
cout<< ” win $30″ <
machine.balance+=30;
}else{
if(machine.roll_1==machine.roll_2 || machine.roll_2==machine.roll_3 ||machine.roll_1==machine.roll_2 && machine.roll_1==machine.roll_3){
cout<
cout<< ” win $1″ <
machine.balance+=1;
}else{
cout<
cout<< ” lose $1″ <
machine.balance-=1;
}
}
}
void spin(slot_machine machine, int rerolls){
cout<
machine.roll_1=(rand() % 10);
machine.roll_2=(rand() % 10);
machine.roll_3=(rand() % 10);
while((machine.roll_1==machine.roll_2 && machine.roll_2==machine.roll_3)
&& (machine.roll_1==machine.roll_2 || machine.roll_2==machine.roll_3 ||machine.roll_1==machine.roll_2 && machine.roll_1==machine.roll_3)
&& rerolls>0){
machine.roll_1=(rand() % 10);
machine.roll_2=(rand() % 10);
machine.roll_3=(rand() % 10);
rerolls–;
}
if(machine.roll_1==machine.roll_2 && machine.roll_2==machine.roll_3)
{
cout<
cout<< ” win $30″ <
machine.balance+=30;
}else{
if(machine.roll_1==machine.roll_2 || machine.roll_2==machine.roll_3 ||machine.roll_1==machine.roll_2 && machine.roll_1==machine.roll_3){
cout<
cout<< ” win $1″ <
machine.balance+=1;
}else{
cout<
cout<< ” lose $1″ <
machine.balance-=1;
return;
}
}
}
void transferFunds()
{
int toTransfer=0;
cout<
cout<
cin>>toTransfer;
if(toTransfer>funds)
cout<<“Not enough funds!”<
else
cout<<“Funds transferred”<
the_machine.balance+=toTransfer;
funds-=toTransfer;
}
void checkFunds(){
cout<
}
void exit()
{
cout<<“Thanks for playing!..”<
exit(0);
}
main.cpp
#include
#include “functions.h”
using namespace std;
int main()
{
cout << “Slot machine program” << endl;
loop();
return 0;
}