-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExam3.c
343 lines (279 loc) · 9.48 KB
/
Exam3.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
typedef struct DOG_struct {
char name[20];
int payoutRate;
int odds;
} DOG;
char Menu(void);
void InitializeDogs(DOG dog[]);
char BankingMenu(void);
void BankingDeposit(double* moneyInBank, double* moneyOnHand);
void BankingWithdrawal(double* moneyInBank, double* moneyOnHand);
void Banking(double* moneyInBank, double* moneyOnHand);
void GambleChoices(const DOG dog[], double* wager, int* chosenDog, double* moneyOnHand);
double Gamble(const DOG dog[], double* moneyOnHand, double result[], int numRaces);
void PrintResults(const double RESULT[], const int NUM_RACES);
int main(void) {
const char GAMBLE_OPTION = 'G';
const char BANKING_OPTION = 'B';
const char RESULTS_OPTION = 'R';
const char LEAVE_OPTION = 'L';
char menuOption;
DOG dog[9];
int numRaces = 0;
double result[50];
double moneyInBank = 10000.00;
double moneyOnHand = 0.00;
InitializeDogs(dog); //FIXME: Change dog names in InitializeDogs
menuOption = Menu();
while (menuOption != LEAVE_OPTION) {
if (menuOption == GAMBLE_OPTION) {
if (moneyOnHand <= 0) {
printf("Please first withdraw money from your bank account to wager\n\n");
menuOption = Menu();
}
else {
moneyOnHand += Gamble(dog, &moneyOnHand, result, numRaces); //Asks user for wager and dog to bet on and shows result of race
if (numRaces < 50) {
++numRaces; //numRaces increments after each iteration of Gamble function for results function, max 50 increments
}
menuOption = Menu();
}
}
else if (menuOption == BANKING_OPTION) {
Banking(&moneyInBank, &moneyOnHand); //Opens banking menu, which gives user option to deposit or withdraw money
menuOption = Menu();
}
else if (menuOption == RESULTS_OPTION) {
PrintResults(result, numRaces); //Prints betting results after max 50 races
menuOption = Menu();
}
}
return 0;
}
char Menu(void) {
char userInput;
bool isValidInput;
printf("--Dog Racing--\n");
printf("[G]amble\n");
printf("[B]anking\n");
printf("[R]esults\n");
printf("[L]eave\n");
printf("\nEnter option character: ");
do {
scanf("%c", &userInput);
if (userInput == '\n') {
scanf("%c", &userInput);
}
if (userInput == 'G' || userInput == 'B' || userInput == 'R' || userInput == 'L') {
isValidInput = true;
}
else {
printf("Please enter a valid option character (G, B, R, or L): ");
isValidInput = false;
}
} while (isValidInput == false);
printf("\n");
return userInput;
}
void InitializeDogs(DOG dog[]) {
strcpy(dog[0].name, "Ace");
dog[0].odds = 40;
dog[0].payoutRate = 2;
strcpy(dog[1].name, "Chaos");
dog[1].odds = 10;
dog[1].payoutRate = 5;
strcpy(dog[2].name, "Finn");
dog[2].odds = 8;
dog[2].payoutRate = 10;
strcpy(dog[3].name, "Apollo");
dog[3].odds = 6;
dog[3].payoutRate = 15;
strcpy(dog[4].name, "Kiva");
dog[4].odds = 1;
dog[4].payoutRate = 50;
strcpy(dog[5].name, "Flash");
dog[5].odds = 4;
dog[5].payoutRate = 20;
strcpy(dog[6].name, "Gator");
dog[6].odds = 8;
dog[6].payoutRate = 10;
strcpy(dog[7].name, "Raven");
dog[7].odds = 10;
dog[7].payoutRate = 5;
strcpy(dog[8].name, "Cooper");
dog[8].odds = 13;
dog[8].payoutRate = 3;
}
char BankingMenu(void) {
char userInput;
bool isValidInput;
printf("[D]eposit\n");
printf("[W]ithdraw\n");
printf("[L]eave\n");
printf("\nEnter option character: ");
do {
scanf("%c", &userInput);
if (userInput == '\n') {
scanf("%c", &userInput);
}
if (userInput == 'D' || userInput == 'W' || userInput == 'L') {
isValidInput = true;
}
else {
printf("Please enter a valid option character (D, W, or L): ");
isValidInput = false;
}
} while (isValidInput == false);
printf("\n");
return userInput;
}
void BankingDeposit(double* moneyInBank, double* moneyOnHand) {
double userInput;
bool isValidInput;
printf("Enter amount you wish to deposit: ");
do {
scanf("%lf", &userInput);
if (userInput > *moneyOnHand) {
printf("You cannot deposit more than what you have on hand. Try again: ");
isValidInput = false;
}
else if (userInput < 0.00) {
printf("You cannot deposit a negative value. Try again: ");
isValidInput = false;
}
else {
*moneyInBank = *moneyInBank + userInput;
*moneyOnHand = *moneyOnHand - userInput;
printf("Deposited $%0.2lf into account.\n\n", userInput);
isValidInput = true;
}
} while (isValidInput == false);
}
void BankingWithdrawal(double* moneyInBank, double* moneyOnHand) {
double userInput;
bool isValidInput;
printf("Enter amount you wish to withdraw: ");
do {
scanf("%lf", &userInput);
if (userInput > *moneyInBank) {
printf("You cannot withdraw more than what you have in your account. Try again: ");
isValidInput = false;
}
else if (userInput < 0.00) {
printf("You cannot withdraw a negative value. Try again: ");
isValidInput = false;
}
else {
*moneyInBank = *moneyInBank - userInput;
*moneyOnHand = *moneyOnHand + userInput;
printf("Withdrew $%0.2lf from account.\n\n", userInput);
isValidInput = true;
}
} while (isValidInput == false);
}
void Banking(double* moneyInBank, double* moneyOnHand) {
const char DEPOSIT_OPTION = 'D';
const char WITHDRAW_OPTION = 'W';
const char LEAVE_OPTION = 'L';
char menuOption;
double userInput;
bool isValidInput;
printf("Money available in account: $%0.2lf\n", *moneyInBank);
printf("Money currently on hand: $%0.2lf\n\n", *moneyOnHand);
menuOption = BankingMenu();
while (menuOption != LEAVE_OPTION) {
if (menuOption == DEPOSIT_OPTION) {
BankingDeposit(moneyInBank, moneyOnHand);
printf("Money available in account: $%0.2lf\n", *moneyInBank);
printf("Money currently on hand: $%0.2lf\n\n", *moneyOnHand);
menuOption = BankingMenu();
}
else if (menuOption == WITHDRAW_OPTION) {
BankingWithdrawal(moneyInBank, moneyOnHand);
printf("Money available in account: $%0.2lf\n", *moneyInBank);
printf("Money currently on hand: $%0.2lf\n\n", *moneyOnHand);
menuOption = BankingMenu();
}
}
}
void GambleChoices(const DOG dog[], double* wager, int* chosenDog, double* moneyOnHand) {
bool isValidWager;
printf("Input your desired wager: ");
do {
scanf("%lf", wager);
if (*wager < 0.00) {
printf("You cannot wager a negative value. Try again: ");
isValidWager = false;
}
else if (*wager > *moneyOnHand) {
printf("Wager cannot be greater than the money you have on hand. Try again");
isValidWager = false;
}
else {
isValidWager = true;
}
} while (isValidWager == false);
printf("\nChoose a dog:\n\n");
for (int i = 0; i < 9; ++i) {
printf("%d) %s\n", i + 1, dog[i].name);
}
printf("\nInput number of your desired dog: ");
scanf("%d", chosenDog);
while (*chosenDog < 1 || *chosenDog > 9) {
printf("Please input a number from 1 to 9: ");
scanf("%d", chosenDog);
}
*chosenDog = *chosenDog - 1;
}
double Gamble(const DOG dog[], double* moneyOnHand, double result[],int numRaces) {
double wager;
int chosenDog;
int roll;
bool wonBet;
double winnings;
srand(time(0));
GambleChoices(dog, &wager, &chosenDog, moneyOnHand);
roll = (rand() % 100) + 1;
if (roll <= dog[chosenDog].odds) {
wonBet = true;
winnings = wager * dog[chosenDog].payoutRate;
}
else {
wonBet = false;
winnings = -wager;
}
result[numRaces] = winnings;
if (wonBet == true) {
printf("You won!\n");
printf("Earnings: $%0.2lf\n\n", winnings);
*moneyOnHand = *moneyOnHand - wager;
return winnings;
}
else {
printf("You lost.\n");
printf("Losses: -$%0.2lf\n\n", wager);
return winnings;
}
}
void PrintResults(const double RESULT[], const int NUM_RACES) {
if (NUM_RACES == 0) {
printf("No race data available. Try running a few races.\n\n");
}
else {
for (int i = 0; i < NUM_RACES; ++i) {
printf("Race #%d:\n", i + 1);
if (RESULT[i] <= 0) {
printf("Bet lost.\n");
}
else {
printf("Bet won.\n");
}
printf("Earnings: $%0.2lf\n\n", RESULT[i]);
}
}
}