Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created January 10, 2020 20:00
Show Gist options
  • Save misterpoloy/eea7b8b4e1925f6d1523f2db2bffb3d2 to your computer and use it in GitHub Desktop.
Save misterpoloy/eea7b8b4e1925f6d1523f2db2bffb3d2 to your computer and use it in GitHub Desktop.
Casino game exercise (Math random and srand) with <ctime>
#include <iostream>
#include <ctime>
struct Player {
int cash = 10;
};
class Game {
public:
void Play(Player* player) {
srand(time(0));
char mano[] = {'Q', 'K', 'J'};
while (player->cash > 0) {
std::cout << "Enter bid:" << std::endl;
int bid;
std::cin >> bid;
for (int i = 0; i < 5; i++) { // this is needed in case consecutve x == y
std::cout << "Shuffle..." << std::endl;
int x = rand() % 3;
int y = rand() % 3;
char temp = mano[x];
mano[x] = mano[y];
mano[y] = temp;
}
std::cout << "Guess from 1, 2, 3 where's Q:" << std::endl;
int guess;
std::cin >> guess;
std::cout << "RESULT = [" << mano[0] << ", " << mano[1] << ", " << mano[2] << "]"<< std::endl;
if (mano[guess - 1] == 'Q') {
player->cash = player->cash + bid * 3;
std::cout << "WIN!! total cash:" << player->cash << std::endl;
} else {
player->cash = player->cash - bid;
std::cout << "LOST!! total cash:" << player->cash << std::endl;
}
std::cout << "=====================" << std::endl;
}
}
};
int main() {
// insert code here...
Player *user = new Player;
Game* casinoShuffle = new Game;
std::cout << "Welcome to casino game!\n";
casinoShuffle->Play(user);
std::cout << "Welcome to casino game!\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment