-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontrols.cpp
More file actions
176 lines (165 loc) · 6.29 KB
/
controls.cpp
File metadata and controls
176 lines (165 loc) · 6.29 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/// @file controls.cpp
/// Human-computer interactions functions.
/*
This program is free software: you can redistribute it and/or modify it under the
terms of the GNU Lesser General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this
program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "controls.h"
void optionsInput(int row, int col, float & balance, float tax,
std::vector<Stock> & stocks, const std::vector<Stock_event> & events,
bool & viewMode, bool & advance, bool & overlayEvent, bool & flush,
bool & gameQuit) {
char input = '.'; // -Werror=maybe-uninitialized
while (true) {
std::cout << setCursorPosition(row, 3) << "\x1b[2K";
std::cout << "Choose an option from the bar above: ";
std::cin >> input;
switch (input) {
case 'B':
case 'b':
buyStocks(row, col, balance, tax, stocks, flush);
break;
case 'S':
case 's':
sellStocks(row, col, balance, tax, stocks, flush);
break;
case 'T':
case 't':
toggleView(viewMode, flush);
break;
case 'E':
case 'e':
if (!overlayEvent) {
overlayEvent = true;
listEvents(row, col, events);
}
else {
flush = true;
}
break;
case 'N':
case 'n':
advanceConfirmation(row, col, advance, flush);
break;
// case 'O':
// case 'o':
// break;
case 'X':
case 'x':
quitConfirmation(row, col, flush, gameQuit);
break;
default:
std::cout << setCursorPosition(row, 3) << "\x1b[2K";
std::cout << "nope";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
time::sleep(sleepMedium);
continue;
}
break;
}
}
int integerInput(int row, int col, const std::string & message) {
std::ignore = col;
int num;
while (true) {
std::cout << setCursorPosition(row, 3) << "\x1b[2K";
std::cout << message;
std::cin >> num;
if (!std::cin) {
std::cin.clear();
std::cout << setCursorPosition(row, 3) << "\x1b[2K";
std::cout << "don't";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
time::sleep(sleepMedium);
continue;
}
return num;
}
}
void buyStocks(int row, int col, float & balance, float tax,
std::vector<Stock> & stocks, bool & flush) {
int index;
int amount;
index = integerInput(row, col, "Enter the index of the stock as shown: ");
while (index < 1 || index > static_cast<int>(stocks.size())) {
std::cout << setCursorPosition(row, 3) << "\x1b[2K";
std::cout << "Index out of range!";
time::sleep(sleepMedium);
index = integerInput(row, col, "Enter the index of the stock as shown: ");
}
amount = integerInput(row, col, "Enter the amount to buy (0 to skip): ");
while (amount < 0) {
std::cout << setCursorPosition(row, 3) << "\x1b[2K";
std::cout << "You cannot purchase negative amounts!";
time::sleep(sleepMedium);
index = integerInput(row, col, "Enter the amount to buy (0 to skip): ");
}
while (amount >
static_cast<int>(stocks[index - 1].num_stocks_affordable(balance, tax))) {
std::cout << setCursorPosition(row, 3) << "\x1b[2K";
std::cout << "Cannot afford!";
time::sleep(sleepMedium);
amount = integerInput(row, col, "Enter the amount to buy (0 to skip): ");
}
stocks[index - 1].purchase(balance, amount, tax);
flush = true;
}
void sellStocks(int row, int col, float & balance, float tax,
std::vector<Stock> & stocks, bool & flush) {
int index;
int amount;
index = integerInput(row, col, "Enter the index of the stock as shown: ");
while (index < 1 || index > static_cast<int>(stocks.size())) {
std::cout << setCursorPosition(row, 3) << "\x1b[2K";
std::cout << "Index out of range!";
time::sleep(sleepMedium);
index = integerInput(row, col, "Enter the index of the stock as shown: ");
}
amount = integerInput(row, col, "Enter the amount to sell (0 to skip): ");
while (amount < 0) {
std::cout << setCursorPosition(row, 3) << "\x1b[2K";
std::cout << "You cannot sell negative amounts!";
time::sleep(sleepMedium);
index = integerInput(row, col, "Enter the amount to sell (0 to skip): ");
}
while (amount > static_cast<int>(stocks[index - 1].get_quantity())) {
std::cout << setCursorPosition(row, 3) << "\x1b[2K";
std::cout << "You do not have this many stocks!";
time::sleep(sleepMedium);
amount = integerInput(row, col, "Enter the amount to sell (0 to skip): ");
}
stocks[index - 1].sell(balance, amount, tax);
flush = true;
}
void toggleView(bool & viewMode, bool & flush) {
viewMode = !viewMode;
flush = true;
}
void advanceConfirmation(int row, int col, bool & advance, bool & flush) {
std::ignore = col;
char input = 'x'; // -Werror=maybe-uninitialized
std::cout << setCursorPosition(row, 3) << "\x1b[2K";
std::cout << "Press [Y] to confirm: ";
std::cin >> input;
if (input == 'Y' || input == 'y') {
advance = true;
flush = true;
}
}
void quitConfirmation(int row, int col, bool & flush, bool & gameQuit) {
std::ignore = col;
char input = 'x'; // -Werror=maybe-uninitialized
std::cout << setCursorPosition(row, 0) << "\x1b[2K";
std::cout << "Press [Y] to confirm: ";
std::cin >> input;
if (input == 'Y' || input == 'y') {
gameQuit = true;
flush = true;
}
}