Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / 07-Practice-05.04.2022 / source.cpp
blob: 1a3f4ba6107aa12e002f26fb86d3fcae378b4822 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "Game.h"
#include "NumberHelpers.h"

bool gameCase_evenOrOdd(double& bet) {
	int rnd = getRandomBetween(1, 100);
	std::cout << "Is the number [e]ven or [o]dd? ";
	char buffer;
	std::cin >> buffer;

	while (buffer != 'e' && buffer != 'o') {
		std::cout << "Unknown option '" << buffer << "', choice must be either 'e' or 'o'." << std::endl << "Is the number [e]ven or [o]dd? ";
		std::cin >> buffer;
	}

	bool result = isEven(rnd) == (buffer == 'e');
	bet = (result) ? bet / 5 : 0;
	std::cout << "The number was: " << rnd << std::endl;
	return result;
}

bool gameCase_isPrime(double& bet) {
	int rnd = getRandomBetween(1, 100);

	bool result = isPrime(rnd);
	bet = (result) ? bet / 2 : 0;
	std::cout << "The number was: " << rnd << std::endl;
	return result;
}

bool gameCase_isPowerOfTwo(double& bet) {
	int rnd = getRandomBetween(1, 100);

	bool result = isPowerOfTwo(rnd);
	bet = (result) ? bet / 2 : 0;
	std::cout << "The number was: " << rnd << std::endl;
	return result;
}

bool gameCase_higherOrLower(double& bet) {
	int rnd = getRandomBetween(1, 100);

	std::cout << "Your number [20...60] ";
	int num;
	std::cin >> num;

	while (num < 20 || num > 60) {
		std::cout << "Number not in range! Enter number [20...60] ";
		std::cin >> num;
	}

	std::cout << num << " will be [h]igher or [l]ower than random? ";
	char guess;
	std::cin >> guess;

	while (guess != 'h' && guess != 'l') {
		std::cout << "Invalid option '" << guess << "', must be either 'h' or 'l'." << std::endl << num << " will be [h]igher or [l]ower than random? ";
		std::cin >> guess;
	}

	bool result = (rnd > num) == (guess == 'h');
	bet = (result) ? bet / 4 : 0;
	std::cout << "The number was: " << rnd << std::endl;
	return result;
}

bool gameCase_guess(double& bet) {
	int rnd = getRandomBetween(1, 100);

	std::cout << "Your guess: ";
	int num;
	std::cin >> num;

	bool result = num == rnd;
	bet = (result) ? bet * 10 : 0;
	std::cout << "The number was: " << rnd << std::endl;
	return result;
}

int main() {
	GameCase numberGambleCases[] = { 
		GameCase('e', gameCase_evenOrOdd),
		GameCase('p', gameCase_isPrime),
		GameCase('t', gameCase_isPowerOfTwo),
		GameCase('h', gameCase_higherOrLower),
		GameCase('g', gameCase_guess)
	};
	Game numberGamble(numberGambleCases, 5);

	Player yorgus("Yorgus", "yorgus", 23, 57741.39);

	char buffer = '\0';
	while (buffer != 'n' && buffer != 'N') {
		numberGamble.play(yorgus);
		std::cout << "Do you want to play again? [y/n] ";
		std::cin >> buffer;
	}
	std::cout << "Your balance: " << yorgus.get_balance() << std::endl;
}