-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathProgram.cs
458 lines (425 loc) · 19.3 KB
/
Program.cs
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.Json;
namespace Oligopoly;
public class Program
{
private static bool CloseRequested { get; set; } = false;
private static List<Company> Companies { get; set; } = null!;
private static List<Event> Events { get; set; } = null!;
private static Event CurrentEvent { get; set; } = null!;
private static decimal Money { get; set; }
private const decimal LosingNetWorth = 2000.00m;
private const decimal WinningNetWorth = 50000.00m;
public static void Main()
{
#region Trim Prevention
// We need to call the default constructors so that they do not get
// trimmed when compiling with the -p:PublishTrimmed=true option.
// The default constructor is required for JSON deserialization.
_ = new Company()
{
Name = nameof(Company.Name),
Industry = nameof(Company.Industry),
SharePrice = 42.42m,
NumberOfShares = 42,
Description = nameof(Company.Description)
};
_ = new Event()
{
Title = nameof(Event.Title),
Description = nameof(Event.Description),
CompanyName = nameof(Event.CompanyName),
Effect = 42
};
#endregion
try
{
MainMenuScreen();
}
finally
{
Console.ResetColor();
Console.CursorVisible = true;
}
}
private static void LoadEmbeddedResources()
{
Assembly assembly = Assembly.GetExecutingAssembly();
{
using Stream? stream = assembly.GetManifestResourceStream("Oligopoly.Company.json");
Companies = JsonSerializer.Deserialize<List<Company>>(stream!)!;
}
{
using Stream? stream = assembly.GetManifestResourceStream("Oligopoly.Event.json");
Events = JsonSerializer.Deserialize<List<Event>>(stream!)!;
}
}
private static void MainMenuScreen()
{
while (!CloseRequested)
{
StringBuilder prompt = new();
prompt.AppendLine();
prompt.AppendLine(" ██████╗ ██╗ ██╗ ██████╗ ██████╗ ██████╗ ██████╗ ██╗ ██╗ ██╗");
prompt.AppendLine(" ██╔═══██╗██║ ██║██╔════╝ ██╔═══██╗██╔══██╗██╔═══██╗██║ ╚██╗ ██╔╝");
prompt.AppendLine(" ██║ ██║██║ ██║██║ ███╗██║ ██║██████╔╝██║ ██║██║ ╚████╔╝ ");
prompt.AppendLine(" ██║ ██║██║ ██║██║ ██║██║ ██║██╔═══╝ ██║ ██║██║ ╚██╔╝ ");
prompt.AppendLine(" ╚██████╔╝███████╗██║╚██████╔╝╚██████╔╝██║ ╚██████╔╝███████╗██║ ");
prompt.AppendLine(" ╚═════╝ ╚══════╝╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ");
prompt.AppendLine();
prompt.Append("You can exit the game at any time by pressing ESCAPE.");
prompt.AppendLine();
prompt.Append("Use up and down arrow keys and enter to select an option:");
int selectedIndex = HandleMenuWithOptions(prompt.ToString(),
[
"Play",
"About",
"Exit",
]);
switch (selectedIndex)
{
case 0: IntroductionScreen(); break;
case 1: AboutInfoScreen(); break;
case 2: CloseRequested = true; break;
}
}
Console.Clear();
Console.WriteLine("Oligopoly was closed. Press any key to continue...");
Console.CursorVisible = false;
Console.ReadKey(true);
}
private static void InitializeGame()
{
Money = 10000.00m;
LoadEmbeddedResources();
}
private static void GameLoop()
{
while (!CloseRequested)
{
int selectedOption = -1;
while (!CloseRequested && selectedOption is not 0)
{
StringBuilder prompt = RenderCompanyStocksTable();
prompt.AppendLine();
prompt.Append("Use up and down arrow keys and enter to select an option:");
selectedOption = HandleMenuWithOptions(prompt.ToString(),
[
"Wait For Market Change",
"Buy",
"Sell",
"Information About Companies",
]);
switch (selectedOption)
{
case 1: BuyOrSellStockScreen(true); break;
case 2: BuyOrSellStockScreen(false); break;
case 3: CompanyDetailsScreen(); break;
}
}
EventScreen();
switch (CalculateNetWorth())
{
case >= WinningNetWorth: PlayerWinsScreen(); return;
case <= LosingNetWorth: PlayerLosesScreen(); return;
}
}
}
private static void EventScreen()
{
CurrentEvent = Events[Random.Shared.Next(0, Events.Count)];
foreach (Company currentCompany in Companies)
{
if (currentCompany.Name == CurrentEvent.CompanyName)
{
currentCompany.SharePrice += currentCompany.SharePrice * CurrentEvent.Effect / 100;
}
}
StringBuilder prompt = RenderCompanyStocksTable();
prompt.AppendLine();
prompt.AppendLine($"{CurrentEvent.Title}");
prompt.AppendLine();
prompt.AppendLine($"{CurrentEvent.Description}");
prompt.AppendLine();
prompt.Append("Press any key to continue...");
Console.Clear();
Console.Write(prompt);
Console.CursorVisible = false;
CloseRequested = CloseRequested || Console.ReadKey(true).Key is ConsoleKey.Escape;
}
private static void BuyOrSellStockScreen(bool isBuying)
{
int[] numberOfShares = new int[Companies.Count];
int index = 0;
ConsoleKey key = default;
while (!CloseRequested && key is not ConsoleKey.Enter)
{
// calculate the current cost of the transaction
decimal cost = 0.00m;
for (int i = 0; i < Companies.Count; i++)
{
cost += numberOfShares[i] * Companies[i].SharePrice;
}
Console.Clear();
Console.WriteLine(RenderCompanyStocksTable());
Console.WriteLine();
Console.WriteLine($"Use the arrow keys and enter to confirm how many shares to {(isBuying ? "buy" : "sell")}:");
for (int i = 0; i < Companies.Count; i++)
{
if (i == index)
{
(Console.BackgroundColor, Console.ForegroundColor) = (Console.ForegroundColor, Console.BackgroundColor);
Console.WriteLine($" < {numberOfShares[i]}{(!isBuying ? $"/{Companies[index].NumberOfShares}" : "")} > {Companies[i].Name}");
Console.ResetColor();
}
else
{
Console.WriteLine($" {numberOfShares[i]}{(!isBuying ? $"/{Companies[index].NumberOfShares}" : "")} {Companies[i].Name}");
}
}
Console.WriteLine();
Console.WriteLine($"{(isBuying ? "Cost" : "Payout")}: {cost:C}");
key = Console.ReadKey(true).Key;
switch (key)
{
case ConsoleKey.Escape: CloseRequested = true; return;
case ConsoleKey.UpArrow: index = index is 0 ? Companies.Count - 1 : index - 1; break;
case ConsoleKey.DownArrow: index = index == Companies.Count - 1 ? 0 : index + 1; break;
case ConsoleKey.RightArrow:
if (isBuying)
{
if (cost + Companies[index].SharePrice <= Money)
{
numberOfShares[index]++;
}
}
else
{
if (numberOfShares[index] < Companies[index].NumberOfShares)
{
numberOfShares[index]++;
}
}
break;
case ConsoleKey.LeftArrow:
if (numberOfShares[index] > 0)
{
numberOfShares[index]--;
}
break;
}
}
if (CloseRequested)
{
return;
}
if (isBuying)
{
for (int i = 0; i < Companies.Count; i++)
{
Money -= numberOfShares[i] * Companies[i].SharePrice;
Companies[i].NumberOfShares += numberOfShares[i];
}
}
else
{
for (int i = 0; i < Companies.Count; i++)
{
Money += numberOfShares[i] * Companies[i].SharePrice;
Companies[i].NumberOfShares -= numberOfShares[i];
}
}
Console.Clear();
Console.WriteLine(RenderCompanyStocksTable());
Console.WriteLine($"You shares have been updated.");
Console.WriteLine();
Console.Write("Press any key to continue...");
Console.CursorVisible = false;
CloseRequested = CloseRequested || Console.ReadKey(true).Key is ConsoleKey.Escape;
}
private static void CompanyDetailsScreen()
{
Console.Clear();
foreach (Company company in Companies)
{
Console.WriteLine($"{company.Name} - {company.Description}");
Console.WriteLine();
}
Console.Write("Press any key to exit the menu...");
Console.CursorVisible = false;
CloseRequested = CloseRequested || Console.ReadKey(true).Key is ConsoleKey.Escape;
}
private static void IntroductionScreen()
{
Console.Clear();
Console.WriteLine();
Console.WriteLine(" ┌────────────────────────────────────────────────────────────────────────────────┐");
Console.WriteLine(" │ Dear CEO, │");
Console.WriteLine(" │ │");
Console.WriteLine(" │ Welcome to Oligopoly! │");
Console.WriteLine(" │ │");
Console.WriteLine(" │ On behalf of the board of directors of Oligopoly Investments, we would like to │");
Console.WriteLine(" │ congratulate you on becoming our new CEO. We are confident that you will lead │");
Console.WriteLine(" │ our company to new heights of success and innovation. As CEO, you now have │");
Console.WriteLine(" │ access to our exclusive internal software called Oligopoly, where you can |");
Console.WriteLine(" │ track the latest news from leading companies and buy and sell their shares. │");
Console.WriteLine(" │ This software will give you an edge over the competition and help you make │");
Console.WriteLine(" │ important decisions for our company. To access the program, simply click the │");
Console.WriteLine(" │ button at the bottom of this email. We look forward to working with you and │");
Console.WriteLine(" │ supporting you in your new role. │");
Console.WriteLine(" │ │");
Console.WriteLine(" │ Sincerely, │");
Console.WriteLine(" │ The board of directors of Oligopoly Investments │");
Console.WriteLine(" └────────────────────────────────────────────────────────────────────────────────┘");
Console.WriteLine();
Console.Write("Press any key to continue...");
Console.CursorVisible = false;
CloseRequested = CloseRequested || Console.ReadKey(true).Key is ConsoleKey.Escape;
InitializeGame();
GameLoop();
}
private static void PlayerWinsScreen()
{
Console.Clear();
Console.WriteLine();
Console.WriteLine(" ┌────────────────────────────────────────────────────────────────────────────────┐");
Console.WriteLine(" │ Dear CEO, │");
Console.WriteLine(" │ │");
Console.WriteLine(" │ On behalf of the board of directors of Oligopoly Investments, we would like to │");
Console.WriteLine(" │ express our gratitude and understanding for your decision to leave your post. │");
Console.WriteLine(" │ You have been a remarkable leader and a visionary strategist, who played the │");
Console.WriteLine(" │ stock market skillfully and increased our budget by five times. We are proud │");
Console.WriteLine(" │ of your achievements and we wish you all the best in your future endeavors. As │");
Console.WriteLine(" │ a token of our appreciation, we are pleased to inform you that the company │");
Console.WriteLine(" │ will pay you a bonus of $1 million. You deserve this reward for your hard work │");
Console.WriteLine(" │ and dedication. We hope you will enjoy it and remember us fondly. Thank you │");
Console.WriteLine(" │ for your service and your contribution to Oligopoly Investments. │");
Console.WriteLine(" │ You will be missed. │");
Console.WriteLine(" │ │");
Console.WriteLine(" │ Sincerely, │");
Console.WriteLine(" │ The board of directors of Oligopoly Investments │");
Console.WriteLine(" └────────────────────────────────────────────────────────────────────────────────┘");
Console.WriteLine();
Console.WriteLine($"Your net worth is over {WinningNetWorth:C}.");
Console.WriteLine();
Console.WriteLine("You win! Congratulations!");
Console.WriteLine();
Console.Write("Press any key (except ENTER) to continue...");
ConsoleKey key = ConsoleKey.Enter;
while (!CloseRequested && key is ConsoleKey.Enter)
{
Console.CursorVisible = false;
key = Console.ReadKey(true).Key;
CloseRequested = CloseRequested || key is ConsoleKey.Escape;
}
}
private static void PlayerLosesScreen()
{
Console.Clear();
Console.WriteLine();
Console.WriteLine(" ┌────────────────────────────────────────────────────────────────────────────────┐");
Console.WriteLine(" │ Dear former CEO, │");
Console.WriteLine(" │ │");
Console.WriteLine(" │ We regret to inform you that you are being removed from the position of CEO │");
Console.WriteLine(" │ and fired from the company, effective immediately. The board of directors of │");
Console.WriteLine(" │ Oligopoly Investments has decided to take this action because you have spent │");
Console.WriteLine(" │ the budget allocated to you, and your investment turned out to be unprofitable │");
Console.WriteLine(" │ for the company. We appreciate your service and wish you all the best in your │");
Console.WriteLine(" │ future endeavors. │");
Console.WriteLine(" │ │");
Console.WriteLine(" │ Sincerely, │");
Console.WriteLine(" │ The board of directors of Oligopoly Investments │");
Console.WriteLine(" └────────────────────────────────────────────────────────────────────────────────┘");
Console.WriteLine();
Console.WriteLine($"Your net worth dropped below {LosingNetWorth:C}.");
Console.WriteLine();
Console.WriteLine("You Lose! Better luck next time...");
Console.WriteLine();
Console.Write("Press any key (except ENTER) to continue...");
ConsoleKey key = ConsoleKey.Enter;
while (!CloseRequested && key is ConsoleKey.Enter)
{
Console.CursorVisible = false;
key = Console.ReadKey(true).Key;
CloseRequested = CloseRequested || key is ConsoleKey.Escape;
}
}
private static void AboutInfoScreen()
{
Console.Clear();
Console.WriteLine();
Console.WriteLine(" |");
Console.WriteLine(" | THANKS!");
Console.WriteLine(" |");
Console.WriteLine(" | No really, thank you for taking time to play this simple console game. It means a lot.");
Console.WriteLine(" |");
Console.WriteLine(" | This game was created by Semion Medvedev (Fuinny)");
Console.WriteLine(" |");
Console.WriteLine();
Console.WriteLine("Press any key to continue...");
Console.CursorVisible = false;
CloseRequested = CloseRequested || Console.ReadKey(true).Key is ConsoleKey.Escape;
}
private static StringBuilder RenderCompanyStocksTable()
{
// table column widths
const int c0 = 30, c1 = 10, c2 = 19, c3 = 17;
StringBuilder gameView = new();
gameView.AppendLine($"╔═{new('═', c0)}═╤═{new('═', c1)}═╤═{new('═', c2)}═╤═{new('═', c3)}═╗");
gameView.AppendLine($"║ {"Company",-c0} │ {"Industry",c1} │ {"Share Price",c2} │ {"You Have",c3} ║");
gameView.AppendLine($"╟─{new('─', c0)}─┼─{new('─', c1)}─┼─{new('─', c2)}─┼─{new('─', c3)}─╢");
foreach (Company company in Companies)
{
gameView.AppendLine($"║ {company.Name,-c0} │ {company.Industry,c1} │ {company.SharePrice,c2:C} │ {company.NumberOfShares,c3} ║");
}
gameView.AppendLine($"╚═{new('═', c0)}═╧═{new('═', c1)}═╧═{new('═', c2)}═╧═{new('═', c3)}═╝");
gameView.AppendLine($"Your money: {Money:C} Your Net Worth: {CalculateNetWorth():C}");
return gameView;
}
private static int HandleMenuWithOptions(string prompt, string[] options)
{
int index = 0;
ConsoleKey key = default;
while (!CloseRequested && key is not ConsoleKey.Enter)
{
Console.Clear();
Console.WriteLine(prompt);
for (int i = 0; i < options.Length; i++)
{
string currentOption = options[i];
if (i == index)
{
(Console.BackgroundColor, Console.ForegroundColor) = (Console.ForegroundColor, Console.BackgroundColor);
Console.WriteLine($"[*] {currentOption}");
Console.ResetColor();
}
else
{
Console.WriteLine($"[ ] {currentOption}");
}
}
Console.CursorVisible = false;
key = Console.ReadKey().Key;
switch (key)
{
case ConsoleKey.UpArrow: index = index is 0 ? options.Length - 1 : index - 1; break;
case ConsoleKey.DownArrow: index = index == options.Length - 1 ? 0 : index + 1; break;
case ConsoleKey.Escape: CloseRequested = true; break;
}
}
return index;
}
private static decimal CalculateNetWorth()
{
decimal netWorth = Money;
foreach (Company company in Companies)
{
netWorth += company.SharePrice * company.NumberOfShares;
}
return netWorth;
}
}