What I can do to make this code more efficient?
It is supposed to be like one of those slot machines where you gamble on it and stuff (like at a casino).
#include <iostream>
#include <conio.h>
#include <string>
#include <ctime>
#include <iomanip>
using namespace std;
int main() {
srand(time(0));
int slot1;
int slot2;
int slot3;
double total_money = 0;
double total_wins = 0;
double total_tries = 0;
double win_percentage;
char play_again;
cout << "Welcomes to Virtual Slots." << endl; // Welcome the user
cout << "Match 2, win that amount." << endl; // Explain prizes
cout << "Match 3, win 3x that amount.\n" << endl;
do {
slot1 = rand() % 10 + 1; // Get random numbers for slots
slot2 = rand() % 10 + 1;
slot3 = rand() % 10 + 1;
cout << "****SLOTS!!****" << endl;
cout << "***************" << endl;
cout << "* [" << slot1 << "] [" << slot2 << "] [" << slot3 << "] *" << endl; // Show slots
cout << "***************\n" << endl;
if (slot1 == slot2 == slot3) { // If all three slots match, user getx 3x money
cout << "THREE TIMES MATCH!!" << endl;
cout << "You win $" << (slot1 * 3) << ".\n" << endl;
total_money += slot1; // Count money
total_wins++; // Count wins
}
else if (slot1 == slot2 || slot1 == slot3 || slot2 == slot3) { // if user gets 2 slots matched, user gets that amount of money
cout << "TWO TIMES MATCH!!" << endl;
cout << "You win $" << slot1 << ".\n" << endl;
total_money += slot1; // Count money
total_wins++; // Count wins
}
else { // No match, no money
cout << "NO MATCH.\n" << endl;
}
total_tries++; // Count tries
win_percentage = (total_wins / total_tries); // Percentage of wins
cout << "Current Total: $" << setprecision(4) << total_money << endl; // Show user his statistics
cout << "Win Percentage: " << setprecision(3) << win_percentage << "%" << endl;
cout << "Play Again? (Y/N) " << endl; // Ask the user if s/he wants to play again
cin >> play_again;
system("PAUSE");
system("CLS");
} while (play_again != 'N'); // Keep going until user says No
cout << "See you next time!" << endl;
_getch();
return 0;
}
if (slot1 == slot2 == slot3)
does not do what you seem to expect. \$\endgroup\$slot1
andslot2
then compares the resultingbool
toslot3
. \$\endgroup\$if ((slot1 == slot2) && (slot2 == slot 3))
? \$\endgroup\$