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 File Reference

Human-computer interactions functions. More...

#include "controls.h"
Include dependency graph for controls.cpp:

Go to the source code of this file.

Functions

void advanceConfirmation (int row, int col, bool &advance, bool &flush)
 
void buyStocks (int row, int col, float &balance, float tax, std::vector< Stock > &stocks, bool &flush)
 
int integerInput (int row, int col, const std::string &message)
 
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)
 
void quitConfirmation (int row, int col, bool &flush, bool &gameQuit)
 
void sellStocks (int row, int col, float &balance, float tax, std::vector< Stock > &stocks, bool &flush)
 
void toggleView (bool &viewMode, bool &flush)
 

Detailed Description

Human-computer interactions functions.

Definition in file controls.cpp.

Function Documentation

◆ advanceConfirmation()

void advanceConfirmation ( int row,
int col,
bool & advance,
bool & flush )

Definition at line 154 of file controls.cpp.

154 {
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}
string setCursorPosition(int offsetY, int offsetX)
Definition format.cpp:56

References setCursorPosition().

Referenced by optionsInput().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ buyStocks()

void buyStocks ( int row,
int col,
float & balance,
float tax,
std::vector< Stock > & stocks,
bool & flush )

Definition at line 90 of file controls.cpp.

91 {
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}
static void sleep(int dur)
Definition format.cpp:52
int integerInput(int row, int col, const std::string &message)
Definition controls.cpp:71
const int sleepMedium
Definition format.cpp:49
float balance
Player's balance.
Definition main.cpp:68

References balance, integerInput(), setCursorPosition(), time::sleep(), and sleepMedium.

Referenced by optionsInput().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ integerInput()

int integerInput ( int row,
int col,
const std::string & message )

Definition at line 71 of file controls.cpp.

71 {
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}

References setCursorPosition(), time::sleep(), and sleepMedium.

Referenced by buyStocks(), main(), and sellStocks().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ optionsInput()

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 at line 17 of file controls.cpp.

20 {
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}
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
void advanceConfirmation(int row, int col, bool &advance, bool &flush)
Definition controls.cpp:154
void toggleView(bool &viewMode, bool &flush)
Definition controls.cpp:149
void quitConfirmation(int row, int col, bool &flush, bool &gameQuit)
Definition controls.cpp:166
void listEvents(int row, int col, std::vector< Stock_event > events)
Definition draw.cpp:90

References advanceConfirmation(), balance, buyStocks(), listEvents(), quitConfirmation(), sellStocks(), setCursorPosition(), time::sleep(), sleepMedium, and toggleView().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ quitConfirmation()

void quitConfirmation ( int row,
int col,
bool & flush,
bool & gameQuit )

Definition at line 166 of file controls.cpp.

166 {
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}

References setCursorPosition().

Referenced by optionsInput().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sellStocks()

void sellStocks ( int row,
int col,
float & balance,
float tax,
std::vector< Stock > & stocks,
bool & flush )

Definition at line 120 of file controls.cpp.

121 {
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}

References balance, integerInput(), setCursorPosition(), time::sleep(), and sleepMedium.

Referenced by optionsInput().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toggleView()

void toggleView ( bool & viewMode,
bool & flush )

Definition at line 149 of file controls.cpp.

149 {
150 viewMode = !viewMode;
151 flush = true;
152}

Referenced by optionsInput().

Here is the caller graph for this function: