Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
522 lines (438 loc) · 19.3 KB
/
Copy pathProgram.cs
File metadata and controls
522 lines (438 loc) · 19.3 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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace PokemonPocket
{
class Program
{
static string ReadString(string prompt)
{
while (true)
{
Console.Write(prompt);
var input = Console.ReadLine();
if (input is String && input.Length > 0)
{
return input;
} else
{
Console.WriteLine("Invalid Input: Input must be a string");
}
}
}
static int ReadInt(string prompt)
{
while (true)
{
var input = ReadString(prompt);
int output;
if (int.TryParse(input, out output))
{
return output;
} else
{
Console.WriteLine("Invalid Input: Input must be a number");
}
}
}
static string ReadOptions(string prompt, List<string> options)
{
while (true)
{
string input = ReadString(prompt);
options = options
.Select(p => p.ToLower())
.ToList();
if (options.Contains(input.ToLower())) {
return input;
} else {
Console.WriteLine($"Invalid Option: Please only input one of the following options ({String.Join(',', options)})");
}
}
}
static int ReadOptionsInt(string prompt, List<int> options)
{
while (true)
{
int input = ReadInt(prompt);
if (options.Contains(input)) {
return input;
} else {
Console.WriteLine($"Invalid Option: Please only input one of the following options ({String.Join(',', options)})");
}
}
}
static string UserInput(PokePocket db)
{
Console.WriteLine("*****************************");
Console.WriteLine("Welcome to Pokemon Pocket App");
Console.WriteLine($"Current Pity: {db.Pity.First().Value*100}%");
Console.WriteLine("*****************************");
Console.WriteLine("(1). Add Pokemon to Pocket");
Console.WriteLine("(2). List Pokemon(s) in Pocket");
Console.WriteLine("(3). Check if I can evolve Pokemon");
Console.WriteLine("(4). Evolve Pokemon");
Console.WriteLine("(5). List Pokeballs");
Console.WriteLine("(6). Find & Capture a Pokemon");
var options = new List<string>(){"1","2","3","4","5","6","q"};
string userInput = ReadOptions($"Please only enter [1, 2, 3, 4, 5, 6] or Q to quit: ", options);
return userInput.ToLower();
}
static bool CheckInMaster(string input, List<PokemonMaster> pokemonMasters)
{
var valid = pokemonMasters
.Select(p => p.Name.ToLower())
.ToList();
return valid.Contains(input.ToLower());
}
static void AddPokemon(PokePocket db, List<PokemonMaster> pokemonMasters)
{
string name;
while (true)
{
name = ReadString("Enter Pokemons's Name: ");
if (CheckInMaster(name, pokemonMasters))
{
break;
} else
{
Console.WriteLine("Not a valid Pokemon in PokeDex. Try Again!");
}
}
int hp = ReadInt("Enter Pokemons's Hp: ");
int exp = ReadInt("Enter Pokemons's Exp: ");
if (name.ToLower() == "charmander")
{
var newPoke = new Charmander(name, hp, exp);
db.Add(newPoke);
} else if (name.ToLower() == "eevee")
{
var newPoke = new Eevee(name, hp, exp);
db.Add(newPoke);
} else if (name.ToLower() == "pikachu")
{
var newPoke = new Pikachu(name, hp, exp);
db.Add(newPoke);
}
RewardBalls(db, 1, 11);
db.SaveChanges();
}
static void ListPokes(List<Pokemon> pokemons, bool id = true)
{
if (pokemons.Count ==0 )
{
Console.WriteLine("No Pokemons to show!");
return;
}
foreach (var obj in pokemons)
{
if (obj.Shiny)
{
Console.ForegroundColor = ConsoleColor.Yellow;
}
if (id)
{
Console.WriteLine($"---------ID: {obj.Id}-------------");
} else {
pokemons = pokemons
.OrderBy(obj => obj.Hp)
.ToList();
Console.WriteLine($"--------------------------");
}
Console.WriteLine($"Name: {obj.Name}");
Console.WriteLine($"Hp: {obj.Hp}");
Console.WriteLine($"Exp: {obj.Exp}");
Console.WriteLine($"Skill: {obj.Skill}");
Console.WriteLine("--------------------------");
Console.ResetColor();
}
}
static void ListPokeballs(DbSet<Pokeball> pokeballs)
{
foreach (var ball in pokeballs)
{
Console.WriteLine($"{ball.Id}. {ball.Name}: {ball.Count}");
}
}
static List<Pokemon> CheckEvolve(PokePocket db, List<PokemonMaster> pokemonMasters)
{
var i = db.Pokemons
.OrderBy(obj => obj.Name);
var evolvables = new List<Pokemon>();
var PokeCount = new Dictionary<string, int>();
foreach (var obj in pokemonMasters)
{
PokeCount.Add(obj.Name.ToLower(), 0);
}
foreach (var obj in i)
{
if (PokeCount.Keys.Contains(obj.Name.ToLower()))
{
PokeCount[obj.Name.ToLower()] += 1;
}
}
foreach (var obj in pokemonMasters)
{
if (PokeCount[obj.Name.ToLower()] >= obj.NoToEvolve)
{
foreach (var objP in i)
{
if (objP.Name.ToLower() == obj.Name.ToLower())
{
evolvables.Add(objP);
}
}
Console.WriteLine($"{obj.Name} -------> {obj.EvolveTo}");
}
}
if (evolvables.Count() == 0)
{
Console.WriteLine("No Pokemon can be evolved!");
}
return evolvables;
}
static void EvolvePoke(PokePocket db, List<PokemonMaster> pokemonMasters)
{
var evolvables = CheckEvolve(db, pokemonMasters);
evolvables = evolvables
.OrderBy(p => p.Id)
.ToList();
var evos = evolvables
.Select(obj => obj.Name.ToLower())
.Distinct()
.ToList();
if (evos.Count == 0){return;}
string evo = ReadOptions($"Select an Evolution ({String.Join(',', evos)}): ", evos).ToLower();
evolvables = evolvables
.Where(obj => obj.Name.ToLower() == evo)
.ToList();
ListPokes(evolvables);
int evolverID = ReadOptionsInt("Which pokemon do u want to evolve? (Select ID): ", evolvables.Select(p => p.Id).ToList());
var evolver = evolvables
.Where(obj => obj.Id == evolverID)
.First();
evolvables.Remove(evolver);
var evoRequirement = pokemonMasters
.Where(obj => obj.Name.ToLower() == evo)
.First()
.NoToEvolve-1;
var evolvees = new List<Pokemon>();
if (evolvables.Count() > evoRequirement)
{
for (int x = 0; x < evoRequirement; x++)
{
int evolveeID = ReadOptionsInt($"Select {x+1}th Pokemon to Consume (Select ID): ", evolvables.OrderBy(p => p.Id).Select(p => p.Id).ToList());
evolvees.Add(evolvables.Single(obj => obj.Id == evolveeID));
evolvables.Remove(evolvables.Single(obj => obj.Id == evolveeID));
}
} else
{
evolvees = evolvables;
}
evolver.Name = pokemonMasters
.Single(obj => obj.Name.ToLower() == evolver.Name.ToLower())
.EvolveTo;
evolver.Hp = 0;
evolver.Exp = 0;
for (int x = 0; x < evoRequirement; x++)
{
db.Remove(evolvees[x]);
}
Console.WriteLine($"{evo} has evolved to {evolver.Name}");
ListPokes(new List<Pokemon>() {evolver});
RewardBalls(db, 5, 16);
db.SaveChanges();
}
static WildPokemon CreateWildPokemon(List<PokemonMaster> pokemonMasters)
{
var rand = new Random();
var quality = rand.NextDouble();
int hp;
int exp;
if (quality < 0.7)
{
hp = rand.Next(10, 51);
exp = rand.Next(10, 51);
} else if (quality < 0.9) {
hp = rand.Next(60, 101);
exp = rand.Next(60, 101);
} else {
hp = rand.Next(120, 201);
exp = rand.Next(120, 201);
}
return new WildPokemon(pokemonMasters[rand.Next(3)].Name, hp, exp);
}
static WildPokemon CapturePokemon(PokePocket db, List<PokemonMaster> pokemonMasters)
{
var pokeballs = db.Pokeballs;
var pokemons = db.Pokemons;
if (pokeballs.Where(p => p.Count > 0).Count() == 0)
{
Console.WriteLine("You do not have any pokeballs to capture a pokemon");
return null;
}
while (true)
{
Console.WriteLine("Finding a Pokemon...");
var wildpoke = CreateWildPokemon(pokemonMasters);
Console.WriteLine($"A wild {wildpoke.Name} has appeared!");
Console.WriteLine("-------------------------------------");
Console.WriteLine($"Name: {wildpoke.Name}");
Console.WriteLine($"HP: {wildpoke.Hp}");
Console.WriteLine($"EXP: {wildpoke.Exp}");
bool exit = true;
while (exit)
{
Console.WriteLine("-------------------------------------");
Console.WriteLine("What do you want to do?");
Console.WriteLine("1. View Pokeballs");
Console.WriteLine("2. Use Pokeball");
Console.WriteLine("0. Run Away");
var input = ReadOptions("Enter option (1, 2 or 0): ", new List<string>(){"1", "2", "0"});
Console.WriteLine("-------------------------------------");
switch (input)
{
case "1":
ListPokeballs(pokeballs);
break;
case "2":
do
{
ListPokeballs(pokeballs);
var ballinput = ReadOptionsInt($"Which ball will you use? ({String.Join(", ", pokeballs.Where(b => b.Count > 0).Select(b => b.Id).ToList())}): ", pokeballs.Where(b => b.Count > 0).Select(b => b.Id).ToList());
if (pokeballs.Single(p => p.Id == ballinput).useBall(wildpoke))
{
var pity = db.Pity.First();
if (new Random().NextDouble() < pity.Value)
{
pity.Reset();
wildpoke.Shiny = true;
Console.WriteLine($"SUCCESS! You caught the {wildpoke.Name}. It has been added to your pocket.");
Console.WriteLine("NO WAY! ITS SHINY");
RewardBalls(db, 20, 31);
} else {
pity.Add();
db.SaveChanges();
Console.WriteLine($"SUCCESS! You caught the {wildpoke.Name}. It has been added to your pocket.");
}
return wildpoke;
} else if (wildpoke.Tries > 0) {
if (ReadOptions("Try Again? (Y/N): ", new List<string>() {"Y", "N"}).ToLower() == "n")
{
Console.WriteLine("Running away!...");
break;
} else if (wildpoke.Tries == 1) {
Console.WriteLine($"The {wildpoke.Name} grows restless! ");
}
}
} while (wildpoke.Tries > 0 && pokeballs.Where(p => p.Count > 0).Count() > 0);
if (pokeballs.Where(b => b.Count > 0).Count() == 0)
{
Console.WriteLine("You have no available Pokeballs to use. Running away and returning home!");
return null;
}
exit = false;
break;
case "0":
Console.WriteLine("Running away!...");
exit = false;
break;
}
}
if (ReadOptions("Find another Pokemon? (Y/N): ", new List<string>() {"Y", "N"}).ToLower() == "n")
{
return null;
}
}
}
static void RewardBalls(PokePocket db, int min, int max)
{
var rand = new Random();
var pokeball = db.Pokeballs.ToList()[rand.Next(4)];
var amt = rand.Next(min, max);
pokeball.addBall(amt);
Console.WriteLine($"You gained {amt} {pokeball.Name}s as a reward!");
db.SaveChanges();
}
static void Main(string[] args )
{
var db = new PokePocket();
//PokemonMaster list for checking pokemon evolution availability.
List<PokemonMaster> pokemonMasters = new List<PokemonMaster>() {
new PokemonMaster("Pikachu", 2, "Raichu"),
new PokemonMaster("Eevee", 3, "Flareon"),
new PokemonMaster("Charmander", 1, "Charmeleon")
};
if (db.Pokeballs.Count() == 0) {
Console.WriteLine("Creating Pokeball objects");
db.Add(new Pokeball("Poke Ball"));
db.Add(new Pokeball("Great Ball"));
db.Add(new Pokeball("Ultra Ball"));
db.Add(new Pokeball("Master Ball"));
db.SaveChanges();
}
if (db.Pity.Count() == 0) {
Console.WriteLine("Creating Pity object");
db.Add(new Pity());
db.SaveChanges();
}
var rand = new Random();
//Use "Environment.Exit(0);" if you want to implement an exit of the console program
// Start your assignment 1 requirements below.
do
{
string userInput = UserInput(db);
Console.WriteLine("-----------------------------------------------------------");
switch(userInput)
{
case "1":
AddPokemon(db, pokemonMasters);
break;
case "2":
ListPokes(db.Pokemons.ToList(), false);
break;
case "3":
CheckEvolve(db, pokemonMasters);
break;
case "4":
EvolvePoke(db, pokemonMasters);
break;
case "5":
ListPokeballs(db.Pokeballs);
Console.WriteLine("Get PokeBalls by adding a Pokemon to your pocket (1-10) or by Evolving one (5-15)");
break;
case "6":
var wildpoke = CapturePokemon(db, pokemonMasters);
if (wildpoke is null)
{
break;
}
var pokemons = db.Pokemons;
if (wildpoke.Name == "Charmander")
{
var newPoke = new Charmander(wildpoke.Name, wildpoke.Hp, wildpoke.Exp, wildpoke.Shiny);
pokemons.Add(newPoke);
ListPokes(new List<Pokemon>(){newPoke}, false);
} else if (wildpoke.Name == "Eevee")
{
var newPoke = new Eevee(wildpoke.Name, wildpoke.Hp, wildpoke.Exp, wildpoke.Shiny);
pokemons.Add(newPoke);
ListPokes(new List<Pokemon>(){newPoke}, false);
} else if (wildpoke.Name == "Pikachu")
{
var newPoke = new Pikachu(wildpoke.Name, wildpoke.Hp, wildpoke.Exp, wildpoke.Shiny);
pokemons.Add(newPoke);
ListPokes(new List<Pokemon>(){newPoke}, false);
}
db.SaveChanges();
break;
case "q":
Environment.Exit(0);
break;
}
} while (true);
}
}
}