Stock Market Simulator main e8c3612
A game that provides a realistic stock buying experience with unpredictable trends to test investment strategies.
Loading...
Searching...
No Matches
controls.cpp
Go to the documentation of this file.
1/// @file controls.cpp
2/// Human-computer interactions functions.
3/*
4This program is free software: you can redistribute it and/or modify it under the
5terms of the GNU Lesser General Public License as published by the Free Software
6Foundation, either version 3 of the License, or (at your option) any later version.
7
8This program is distributed in the hope that it will be useful, but WITHOUT ANY
9WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
10PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
11
12You should have received a copy of the GNU Lesser General Public License along with this
13program. If not, see <https://www.gnu.org/licenses/>.
14*/
15#include "controls.h"
16
17void optionsInput(int row, int col, float & balance, float tax,
18 std::vector<Stock> & stocks, const std::vector<Stock_event> & events,
19 bool & viewMode, bool & advance, bool & overlayEvent, bool & flush,
20 bool & gameQuit) {
21 char input = '.'; // -Werror=maybe-uninitialized
22 while (true) {
23 std::cout << setCursorPosition(row, 3) << "\x1b[2K";
24 std::cout << "Choose an option from the bar above: ";
25 std::cin >> input;
26 switch (input) {
27 case 'B':
28 case 'b':
29 buyStocks(row, col, balance, tax, stocks, flush);
30 break;
31 case 'S':
32 case 's':
33 sellStocks(row, col, balance, tax, stocks, flush);
34 break;
35 case 'T':
36 case 't':
37 toggleView(viewMode, flush);
38 break;
39 case 'E':
40 case 'e':
41 if (!overlayEvent) {
42 overlayEvent = true;
43 listEvents(row, col, events);
44 }
45 else {
46 flush = true;
47 }
48 break;
49 case 'N':
50 case 'n':
51 advanceConfirmation(row, col, advance, flush);
52 break;
53 // case 'O':
54 // case 'o':
55 // break;
56 case 'X':
57 case 'x':
58 quitConfirmation(row, col, flush, gameQuit);
59 break;
60 default:
61 std::cout << setCursorPosition(row, 3) << "\x1b[2K";
62 std::cout << "nope";
63 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
65 continue;
66 }
67 break;
68 }
69}
70
71int integerInput(int row, int col, const std::string & message) {
72 std::ignore = col;
73 int num;
74 while (true) {
75 std::cout << setCursorPosition(row, 3) << "\x1b[2K";
76 std::cout << message;
77 std::cin >> num;
78 if (!std::cin) {
79 std::cin.clear();
80 std::cout << setCursorPosition(row, 3) << "\x1b[2K";
81 std::cout << "don't";
82 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
84 continue;
85 }
86 return num;
87 }
88}
89
90void buyStocks(int row, int col, float & balance, float tax,
91 std::vector<Stock> & stocks, bool & flush) {
92 int index;
93 int amount;
94
95 index = integerInput(row, col, "Enter the index of the stock as shown: ");
96 while (index < 1 || index > static_cast<int>(stocks.size())) {
97 std::cout << setCursorPosition(row, 3) << "\x1b[2K";
98 std::cout << "Index out of range!";
100 index = integerInput(row, col, "Enter the index of the stock as shown: ");
101 }
102 amount = integerInput(row, col, "Enter the amount to buy (0 to skip): ");
103 while (amount < 0) {
104 std::cout << setCursorPosition(row, 3) << "\x1b[2K";
105 std::cout << "You cannot purchase negative amounts!";
107 index = integerInput(row, col, "Enter the amount to buy (0 to skip): ");
108 }
109 while (amount >
110 static_cast<int>(stocks[index - 1].num_stocks_affordable(balance, tax))) {
111 std::cout << setCursorPosition(row, 3) << "\x1b[2K";
112 std::cout << "Cannot afford!";
114 amount = integerInput(row, col, "Enter the amount to buy (0 to skip): ");
115 }
116 stocks[index - 1].purchase(balance, amount, tax);
117 flush = true;
118}
119
120void sellStocks(int row, int col, float & balance, float tax,
121 std::vector<Stock> & stocks, bool & flush) {
122 int index;
123 int amount;
124
125 index = integerInput(row, col, "Enter the index of the stock as shown: ");
126 while (index < 1 || index > static_cast<int>(stocks.size())) {
127 std::cout << setCursorPosition(row, 3) << "\x1b[2K";
128 std::cout << "Index out of range!";
130 index = integerInput(row, col, "Enter the index of the stock as shown: ");
131 }
132 amount = integerInput(row, col, "Enter the amount to sell (0 to skip): ");
133 while (amount < 0) {
134 std::cout << setCursorPosition(row, 3) << "\x1b[2K";
135 std::cout << "You cannot sell negative amounts!";
137 index = integerInput(row, col, "Enter the amount to sell (0 to skip): ");
138 }
139 while (amount > static_cast<int>(stocks[index - 1].get_quantity())) {
140 std::cout << setCursorPosition(row, 3) << "\x1b[2K";
141 std::cout << "You do not have this many stocks!";
143 amount = integerInput(row, col, "Enter the amount to sell (0 to skip): ");
144 }
145 stocks[index - 1].sell(balance, amount, tax);
146 flush = true;
147}
148
149void toggleView(bool & viewMode, bool & flush) {
150 viewMode = !viewMode;
151 flush = true;
152}
153
154void advanceConfirmation(int row, int col, bool & advance, bool & flush) {
155 std::ignore = col;
156 char input = 'x'; // -Werror=maybe-uninitialized
157 std::cout << setCursorPosition(row, 3) << "\x1b[2K";
158 std::cout << "Press [Y] to confirm: ";
159 std::cin >> input;
160 if (input == 'Y' || input == 'y') {
161 advance = true;
162 flush = true;
163 }
164}
165
166void quitConfirmation(int row, int col, bool & flush, bool & gameQuit) {
167 std::ignore = col;
168 char input = 'x'; // -Werror=maybe-uninitialized
169 std::cout << setCursorPosition(row, 0) << "\x1b[2K";
170 std::cout << "Press [Y] to confirm: ";
171 std::cin >> input;
172 if (input == 'Y' || input == 'y') {
173 gameQuit = true;
174 flush = true;
175 }
176}
static void sleep(int dur)
Definition format.cpp:52
void buyStocks(int row, int col, float &balance, float tax, std::vector< Stock > &stocks, bool &flush)
Definition controls.cpp:90
void sellStocks(int row, int col, float &balance, float tax, std::vector< Stock > &stocks, bool &flush)
Definition controls.cpp:120
int integerInput(int row, int col, const std::string &message)
Definition controls.cpp:71
void advanceConfirmation(int row, int col, bool &advance, bool &flush)
Definition controls.cpp:154
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)
Definition controls.cpp:17
void toggleView(bool &viewMode, bool &flush)
Definition controls.cpp:149
void quitConfirmation(int row, int col, bool &flush, bool &gameQuit)
Definition controls.cpp:166
(Declarations of) human-computer interactions functions.
void listEvents(int row, int col, std::vector< Stock_event > events)
Definition draw.cpp:90
const int sleepMedium
Definition format.cpp:49
string setCursorPosition(int offsetY, int offsetX)
Definition format.cpp:56
float balance
Player's balance.
Definition main.cpp:68