Slot Machine in C++

Slot Machine in C++ Homework 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 <iostream>
#include <stdlib.h> /* 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<<endl<<endl<<“Options”<<endl<<endl;
cout<<“1. Check Bankk Balance”<<endl;
cout<<“2. Transfer funds to game”<<endl;
cout<<“3. Play”<<endl;
cout<<“4. Exit”<<endl;
cout<<endl<<“option:”;
cin>>option;
}

void spin(slot_machine machine){
cout<<endl;

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<<values[machine.roll_1]<<values[machine.roll_2]<<values[machine.roll_3];
cout<< ” win $30″ <<endl;
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<<values[machine.roll_1]<<values[machine.roll_2]<<values[machine.roll_3];
cout<< ” win $1″ <<endl;
machine.balance+=1;
}else{
cout<<values[machine.roll_1]<<values[machine.roll_2]<<values[machine.roll_3];
cout<< ” lose $1″ <<endl;
machine.balance-=1;
}
}
}

void spin(slot_machine machine, int rerolls){
cout<<endl;
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<<values[machine.roll_1]<<values[machine.roll_2]<<values[machine.roll_3];
cout<< ” win $30″ <<endl;
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<<values[machine.roll_1]<<values[machine.roll_2]<<values[machine.roll_3];
cout<< ” win $1″ <<endl;
machine.balance+=1;
}else{
cout<<values[machine.roll_1]<<values[machine.roll_2]<<values[machine.roll_3];
cout<< ” lose $1″ <<endl;
machine.balance-=1;
return;
}
}
}

void transferFunds()
{
int toTransfer=0;
cout<<endl;
cout<<endl<<“Funds to transfer:”;
cin>>toTransfer;
if(toTransfer>funds)
cout<<“Not enough funds!”<<endl;
else
cout<<“Funds transferred”<<endl;
the_machine.balance+=toTransfer;
funds-=toTransfer;
}

void checkFunds(){
cout<<endl<<“Current funds: $”<<funds<<endl;
}

void exit()
{
cout<<“Thanks for playing!..”<<endl;
exit(0);
}

main.cpp

#include <iostream>
#include “functions.h”

using namespace std;

int main()
{
cout << “Slot machine program” << endl;
loop();
return 0;
}