HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Text-based casino game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
textgamecasinobased

Problem

I have made this small text-based casino game in C++. How could I improve it? Be as picky as you'd like.

```
#include
#include
#include

using namespace std;

// General functions
void bankrupt();

// Roulette functions
void roulette01();
void roulette02();
void rouletteWin();
void rouletteLoss();

// Slots functions
void slots01();
void slots02();

// General variables
int invalid = 1;
int input;
int dollars = 50;

// Roulette variables
int bet = 0;
char cColour;
int iColour;
int actualColour;

// Slots variables
int randSlots01;
int randSlots02;
int randSlots03;

int main() {

srand (time(NULL));

while (invalid == 1) {
system("cls");
cout ";
cin >> input;
switch (input) {

case 1:
input = 1;
roulette01();

case 2:
slots01();

case 3:
exit(0);

default:
cout ";
cin >> input;
switch (input) {

case 1:
roulette02();

case 2:
main();

}
}

void roulette02() {

cout ";
cin >> bet;

if (bet > dollars) {
cout ";
cin >> cColour;

if (cColour == 'r') {
iColour = 1;
}

else {
iColour = 2;
}

actualColour = rand() % 2;

if (actualColour == iColour) {
rouletteWin();
}

else {
rouletteLoss();
}

}

void rouletteWin() {

dollars = dollars + bet;
cout ";
cin >> input;
switch (input) {

case 1:
slots02();

case 2:
main();

}
}

void slots02() {

randSlots01 = rand() % 3;
randSlots02 = rand() % 3;
randSlots03 = rand() % 3;

if (randSlots01 == randSlots02 && randSlots02 == randSlots03) {
dollars = dollars + 10;
cout << "\n You won 10 dollars!" << endl;
cout << "\n ";
system("pause");
slots01();
}

else {
dollars--;
cout << "\n Y

Solution


  • Your naming needs a lot of work. In particular it's a very bad idea to name your entities with numeric suffixes, as that doesn't explain at all what they're doing (it shows they're related but not how, and definitely not how they differ from each other in function).



  • All your variables are global. This is very bad form. Variables should have the smallest possible scope, so it's plainly evident where they're used and what they're used for. There are a number of ways to deal with global state; but at this level there probably should just be a struct that gets passed around.



  • You've got some unnecessary code. There's no reason for input = 1; in the first case.



  • You shouldn't use exit(0) unless you have a reason to. return works perfectly well for returning from main().



  • Don't rely on system() for controlling the user interface. It makes your code impossible to port. At the very least, make helper functions for each of the tasks so that you just have to replace the code once in each helper function.



  • Your program crashes if the user does not input an integer at the main menu.



  • The sub-functions, roulette01() and slots01(), should not call main(). main() already has a loop in it, so you can just return; from them to get back to main().



  • Similarly, roulette01() and slots01() should be structured to include a loop, so that you don't have functions recursively calling each other.



  • Ideally you ought to completely separate the logic of each game from the user interface. One function does the logic, another function calls it and displays the results.

Context

StackExchange Code Review Q#85355, answer score: 12

Revisions (0)

No revisions yet.