-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
1012 lines (939 loc) · 37.4 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
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Game
{
class Game
{
public string Player { get; set; }
public int NumberOfWins { get; set; }
public int NumberOfLoses { get; set; }
public List<Hero> Heroes { get; set; }
public List<Monster> Monsters { get; set; }
public List<Weapon> Weapons { get; set; }
public List<Armor> Armors { get; set; }
public List<Fight> Fights { get; set; }
public Game()
{
Heroes = new List<Hero>();
Monsters = new List<Monster>();
Weapons = new List<Weapon>();
Armors = new List<Armor>();
Fights = new List<Fight>();
}
public Game(List<Hero> heroes, List<Monster> monsters, List<Weapon> weapons, List<Armor> armors)
{
Heroes = heroes;
Monsters = monsters;
Weapons = weapons;
Armors = armors;
Fights = new List<Fight>();
}
public void Start()
{
try
{
Console.WriteLine("Welcome to the game, please input your name...");
Player = Console.ReadLine();
Console.WriteLine("Welcome {0}, the game is starting...", Player);
// Choose hero module.
Console.WriteLine("---------------------------------------");
Console.WriteLine();
if (Heroes.Count == 0)
{
throw new Exception("No hero in the game, cannot play, quit the game...");
}
Console.WriteLine("Choose one hero or get a random one.");
Console.WriteLine("Press 0 to quit the Game.");
Hero chosenHero = ChooseHero();
Console.WriteLine("---------------------------------------");
Console.WriteLine();
bool stillGame = true;
while (stillGame)
{
// Choose monster module
if (Monsters.Count == 0)
{
throw new Exception("No monster in the game, cannot play, quit the game...");
}
Console.WriteLine("Choose one monster or get a random one.");
Console.WriteLine("Press 0 to quit the Game.");
Monster chosenMonster = ChooseMonster();
Console.WriteLine("---------------------------------------");
Console.WriteLine();
// Main menu to switch statements.
bool inMenu = true;
while (inMenu)
{
Console.WriteLine("Press 0 to quit the Game.");
Console.WriteLine("Press 1 to show the statistics.");
Console.WriteLine("Press 2 to show the inventory.");
Console.WriteLine("Press 3 to select the weapons.");
Console.WriteLine("Press 4 to select the armors.");
Console.WriteLine("Press any key to FIGHT.");
char statValue = Console.ReadKey().KeyChar;
Console.WriteLine();
try
{
if (int.Parse(statValue.ToString()) == 0)
{
throw new Exception("You quit the Game...");
}
else if (int.Parse(statValue.ToString()) == 1)
{
chosenHero.ShowStats();
inMenu = true;
}
else if (int.Parse(statValue.ToString()) == 2)
{
chosenHero.ShowInventory();
inMenu = true;
}
else if (int.Parse(statValue.ToString()) == 3)
{
// Select weapons.
Console.WriteLine("Choose the weapons for the hero...");
GetWeapons(chosenHero);
inMenu = true;
}
else if (int.Parse(statValue.ToString()) == 4)
{
// Select armors.
Console.WriteLine("Choose the armors for the hero...");
GetArmors(chosenHero);
inMenu = true;
}
else
{
Console.WriteLine("Quiting from the menu, you can fight now.");
inMenu = false;
}
}
catch
{
Console.WriteLine("Quiting from the menu, you can fight now.");
inMenu = false;
}
}
// Fight module.
Console.WriteLine("You are joining a fight now...");
Fight fight = new Fight(chosenHero, chosenMonster);
Fights.Add(fight);
fight.FightStart();
Console.WriteLine("----------------------------");
Console.WriteLine();
Console.WriteLine("Do you want to start a new game?");
Console.WriteLine("Press Y to start, any other key to quit the game.");
char input = Console.ReadKey().KeyChar;
Console.WriteLine();
if (input == 'Y' || input == 'y')
{
stillGame = true;
}
else
{
stillGame = false;
}
}
Console.WriteLine("You are quiting the game, bye!");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public Hero ChooseHero()
{
Console.WriteLine("The heroes are in the following list...");
Console.WriteLine("---------------------------------------");
foreach (Hero hero in Heroes)
{
Console.WriteLine(hero);
}
Console.WriteLine("Please input the NUMBER of the hero to choose, or press ANY KEY to get a random one.");
int heroID = 0;
string inputValue = Console.ReadLine();
try
{
heroID = int.Parse(inputValue);
if (heroID < 0 || heroID > Heroes.Count)
{
throw new Exception();
}
}
catch
{
Console.WriteLine("Get a random hero...");
Random ran = new Random();
heroID = ran.Next(1, Heroes.Count);
}
if (heroID == 0)
{
throw new Exception("You quit the game.");
}
Console.WriteLine("Your hero is number {0}", heroID);
return GetHeroByID(heroID);
}
public Monster ChooseMonster()
{
Console.WriteLine("The monsters are in the following list...");
Console.WriteLine("---------------------------------------");
int topStrength = 0;
Monster topDamageMonster = Monsters[0];
int topDefense = 0;
Monster topDefenseMonster = Monsters[0];
foreach (Monster monster in Monsters)
{
// Get the top damage monster.
if (monster.Strength > topStrength)
{
topStrength = monster.Strength;
topDamageMonster = monster;
}
// Get the top defense monster.
if (monster.Defense > topDefense)
{
topDefense = monster.Defense;
topDefenseMonster = monster;
}
Console.WriteLine(monster);
}
Console.WriteLine("------------------------------------");
Console.WriteLine();
Console.WriteLine("Do you want to choose the strongest monster to play?");
Console.WriteLine("Press Y to choose, any other key to enter normal selection.");
char choosestrongest = Console.ReadKey().KeyChar;
Console.WriteLine();
if (choosestrongest == 'Y' || choosestrongest == 'y')
{
bool getMonster = false;
while (!getMonster)
{
Console.WriteLine("{0}, it is the top damage monster, press 1 to choose", topDamageMonster);
Console.WriteLine("{0}, it is the top defense monster, press 2 to choose", topDefenseMonster);
char choose = Console.ReadKey().KeyChar;
Console.WriteLine();
if (choose == '1')
{
Console.WriteLine("You chose top damage monster");
Console.WriteLine(topDamageMonster);
return topDamageMonster;
}
else if (choose == '2')
{
Console.WriteLine("You chose top defense monster");
Console.WriteLine(topDefenseMonster);
return topDefenseMonster;
}
else
{
Console.WriteLine("Incorrect input try again.");
getMonster = false;
}
}
}
Console.WriteLine("The monsters are listed above.");
Console.WriteLine("Please input the NUMBER of the monster to choose, or press ANY KEY to get a random one.");
int monsterID = 0;
string inputValue = Console.ReadLine();
try
{
monsterID = int.Parse(inputValue);
if (monsterID < 0 || monsterID > Monsters.Count)
{
throw new Exception();
}
}
catch
{
Console.WriteLine("Get a random monster...");
Random ran = new Random();
monsterID = ran.Next(1, Monsters.Count);
}
if (monsterID == 0)
{
throw new Exception("You quit the game.");
}
Console.WriteLine("The monster is number {0}.", monsterID);
return GetMonsterByID(monsterID);
}
public void GetWeapons(Hero hero)
{
Console.WriteLine("The weapons are in the following list...");
Console.WriteLine("---------------------------------------");
foreach (Weapon weapon in Weapons)
{
Console.WriteLine(weapon);
}
bool finish = false;
while (!finish)
{
Console.WriteLine("Get the weapon by the NUMBER, press any key to quit selecting weapons.");
string inputValue = Console.ReadLine();
Console.WriteLine();
try
{
int weaponID = int.Parse(inputValue);
Console.WriteLine();
Weapon selectedWeapon = GetWeaponByID(weaponID);
Console.WriteLine("You select the {0}", selectedWeapon.Name);
if (weaponID > 0 && weaponID <= Weapons.Count)
{
if (hero.EquippedWeapon == null)
{
hero.EquippedWeapon = selectedWeapon;
}
else
{
hero.WeaponsBag.Add(hero.EquippedWeapon);
hero.EquippedWeapon = selectedWeapon;
}
finish = false;
}
else
{
throw new Exception();
}
}
catch
{
Console.WriteLine("Quit selecting weapons...");
Console.WriteLine("--------------------------------------");
Console.WriteLine();
finish = true;
}
}
}
public void GetArmors(Hero hero)
{
Console.WriteLine("The armors are in the following list...");
Console.WriteLine("---------------------------------------");
foreach (Armor armor in Armors)
{
Console.WriteLine(armor);
}
bool finish = false;
while (!finish)
{
Console.WriteLine("Get the armor by the NUMBER, press any key to quit selecting armors.");
string inputValue = Console.ReadLine();
Console.WriteLine();
try
{
int armorID = int.Parse(inputValue);
if (armorID > 0 && armorID <= Armors.Count)
{
if (hero.EquippedArmor == null)
{
hero.EquippedArmor = GetArmorByID(armorID);
}
else
{
hero.ArmorsBag.Add(hero.EquippedArmor);
hero.EquippedArmor = GetArmorByID(armorID);
}
finish = false;
}
else
{
throw new Exception();
}
}
catch
{
Console.WriteLine("Quit selecting armors...");
Console.WriteLine("--------------------------------------");
Console.WriteLine();
finish = true;
}
}
}
public Hero GetHeroByID(int heroID)
{
foreach (Hero hero in Heroes)
{
if (hero.ID == heroID)
{
return hero;
}
}
throw new Exception("Can't find the hero");
}
public Monster GetMonsterByID(int monsterID)
{
foreach (Monster monster in Monsters)
{
if (monster.ID == monsterID)
{
return monster;
}
}
throw new Exception("Can't find the monster");
}
public Weapon GetWeaponByID(int weaponID)
{
foreach (Weapon weapon in Weapons)
{
if (weapon.ID == weaponID)
{
return weapon;
}
}
throw new Exception("Can't find the weapon");
}
public Armor GetArmorByID(int armorID)
{
foreach (Armor armor in Armors)
{
if (armor.ID == armorID)
{
return armor;
}
}
throw new Exception("Can't find the armor");
}
public int checkInputValue(string inputValue, int maxNum)
{
// Check the inputValue until it is a number.
bool checkIfInt = false;
while (!checkIfInt)
{
try
{
int.Parse(inputValue);
checkIfInt = true;
}
catch
{
Console.WriteLine("Your input is not a number, please choose again.");
checkIfInt = false;
}
}
while (int.Parse(inputValue) < 0 || int.Parse(inputValue) > maxNum)
{
Console.WriteLine("Sorry, the number you inputed is out of range, please choose again.");
inputValue = Console.ReadLine();
}
return int.Parse(inputValue);
}
}
class Hero
{
public static int Counter { get; set; }
public int ID { get; set; }
public string Name { get; set; }
public int wins { get; set; }
public int loses { get; set; }
public int Strength { get; set; }
public int Defense { get; set; }
public int OriginalHealth { get; set; }
public int CurrentHealth { get; set; }
public int Coins { get; set; }
public Weapon EquippedWeapon { get; set; }
public Armor EquippedArmor { get; set; }
public List<Weapon> WeaponsBag { get; set; }
public List<Armor> ArmorsBag { get; set; }
public Hero(string name, int strength, int defense)
{
Name = name;
ID = ++Counter;
Strength = strength;
Defense = defense;
OriginalHealth = int.Parse(ConfigurationManager.AppSettings.Get("OriginalHealthOfHero"));
CurrentHealth = OriginalHealth;
Coins = 10;
ArmorsBag = new List<Armor>();
WeaponsBag = new List<Weapon>();
}
public void ShowStats()
{
Console.WriteLine("The current status of the hero:");
Console.WriteLine("The hero name is {0}, the strength is {1}, the defense is {2}", Name, Strength, Defense);
Console.WriteLine("Your current health is {0}", CurrentHealth);
Console.WriteLine("he current equipments of the hero:");
if (EquippedWeapon == null)
{
Console.WriteLine("No weapon of the hero.");
}
else
{
Console.WriteLine(EquippedWeapon);
}
if (EquippedArmor == null)
{
Console.WriteLine("No weapon of the hero.");
}
else
{
Console.WriteLine(EquippedArmor);
}
Console.WriteLine(" The hero has won {0} times in the game, and lost {1} in the game.", wins, loses);
Console.WriteLine(" The hero has {0} coins to use.", Coins);
Console.WriteLine("------------------------------------------------------------------");
Console.WriteLine();
}
public void ShowInventory()
{
Console.WriteLine("The current equipments of the hero:");
if (EquippedWeapon == null)
{
Console.WriteLine("No equiped weapon of the hero.");
Console.WriteLine("------------------------------");
}
else
{
Console.WriteLine(EquippedWeapon);
}
if (WeaponsBag.Count == 0)
{
Console.WriteLine("You don't have weapon in your bag.");
Console.WriteLine("------------------------------");
Console.WriteLine();
}
else
{
Console.WriteLine("You have the following weapons in your bag.");
foreach (Weapon weapon in WeaponsBag)
{
int num = WeaponsBag.IndexOf(weapon) + 1;
Console.WriteLine("Number {0}:", num);
Console.WriteLine(weapon);
Console.WriteLine();
}
}
if (EquippedArmor == null)
{
Console.WriteLine("No Armor of the hero.");
Console.WriteLine("------------------------------");
}
else
{
Console.WriteLine(EquippedArmor);
}
if (ArmorsBag.Count == 0)
{
Console.WriteLine("You don't have armor in your bag.");
Console.WriteLine("------------------------------");
Console.WriteLine();
}
else
{
Console.WriteLine("You have the following weapons in your bag.");
foreach (Armor armor in ArmorsBag)
{
int num = ArmorsBag.IndexOf(armor) + 1;
Console.WriteLine("Number {0}:", num);
Console.WriteLine(armor);
Console.WriteLine();
}
}
Console.WriteLine("----------------------------");
Console.WriteLine();
}
public void GetWeapon()
{
if (WeaponsBag.Count == 0)
{
Console.WriteLine("You don't have weapon in your bag.");
Console.WriteLine();
}
else
{
Console.WriteLine("You have the following weapons in your bag.");
foreach (Weapon weapon in WeaponsBag)
{
int num = WeaponsBag.IndexOf(weapon) + 1;
Console.WriteLine("Number {0}:", num);
Console.WriteLine(weapon);
}
Console.WriteLine("Press the number to choose the weapon:");
string inputValue = Console.ReadLine();
Console.WriteLine();
int inputresult = checkInputValue(inputValue, WeaponsBag.Count);
Weapon chosenWeapon = WeaponsBag[inputresult - 1];
Console.WriteLine("You choose the number of {0} weapon in your bag.", inputresult);
Console.WriteLine(chosenWeapon);
Console.WriteLine("-----------------------------------");
Console.WriteLine();
if (EquippedWeapon == null)
{
EquippedWeapon = chosenWeapon;
}
else
{
WeaponsBag.Add(EquippedWeapon);
EquippedWeapon = chosenWeapon;
WeaponsBag.Remove(chosenWeapon);
}
}
}
public void GetArmor()
{
if (ArmorsBag.Count == 0)
{
Console.WriteLine("You don't have armor in your bag.");
Console.WriteLine();
}
else
{
Console.WriteLine("You have the following armors in your bag.");
foreach (Armor armor in ArmorsBag)
{
int num = ArmorsBag.IndexOf(armor) + 1;
Console.WriteLine("Number {0}:", num);
Console.WriteLine(armor);
}
Console.WriteLine("Press the number to choose the armor:");
string inputValue = Console.ReadLine();
int inputresult = checkInputValue(inputValue, WeaponsBag.Count);
Armor chosenArmor = ArmorsBag[inputresult - 1];
Console.WriteLine("You choose the number of {0} armor in your bag.", inputresult);
Console.WriteLine(chosenArmor);
Console.WriteLine("---------------------------------");
Console.WriteLine();
if (EquippedArmor == null)
{
EquippedArmor = chosenArmor;
}
else
{
ArmorsBag.Add(EquippedArmor);
EquippedArmor = chosenArmor;
ArmorsBag.Remove(chosenArmor);
}
}
}
public void BuyHealth()
{
if (Coins == 0)
{
Console.WriteLine("Sorry, you have 0 coin, you can't buy health.");
Console.WriteLine();
}
else
{
Console.WriteLine("You have {0} coins.", Coins);
int lostHealth = OriginalHealth - CurrentHealth;
Console.WriteLine("You lost {0} health", lostHealth);
Console.WriteLine("Press 0 to quit buying health...");
string input;
bool success = false;
while (!success)
{
if (lostHealth > Coins)
{
Console.WriteLine("You can buy {0} health, cost you {0} coins.", Coins);
Console.WriteLine("Input the number of health you want to buy, or press 0 to quit buying health... ");
input = Console.ReadLine();
int inputvalue = checkInputValue(input, Coins);
if (inputvalue == 0)
{
Console.WriteLine("You quit buying health.");
success = true;
}
else
{
Coins -= inputvalue;
CurrentHealth += inputvalue;
Console.WriteLine("You bought {0} health.", inputvalue);
success = true;
}
}
else
{
Console.WriteLine("You can buy {0} health to full health, only cost you {0} coins.", lostHealth);
Console.WriteLine("Input the number of health you want to buy, or press 0 to quit buying health... ");
input = Console.ReadLine();
Console.WriteLine();
int inputvalue = checkInputValue(input, lostHealth);
if (inputvalue == 0)
{
Console.WriteLine("You quit buying health.");
success = true;
}
else
{
Coins -= inputvalue;
CurrentHealth += inputvalue;
Console.WriteLine("You bought {0} health.", inputvalue);
success = true;
}
}
}
}
}
/// <summary>
/// This function is to check the input value whether it is in the range of the indexes of the list
/// </summary>
/// <param name="inputValue">this is the value that use input</param>
/// <param name="maxNum">the max number of the range, usually it is the count of a list</param>
/// <returns>it will return the int type of the input, sothat we can use it to catch the value in a list</returns>
public int checkInputValue(string inputValue, int maxNum)
{
// Check the inputValue until it is a number.
bool checkIfInt = false;
while (!checkIfInt)
{
try
{
while (int.Parse(inputValue) < 0 || int.Parse(inputValue) > maxNum)
{
Console.WriteLine("Sorry, the number you inputed is out of range, please choose again.");
inputValue = Console.ReadLine();
}
checkIfInt = true;
}
catch
{
Console.WriteLine("Your input is not a number, please choose again.");
inputValue = Console.ReadLine();
checkIfInt = false;
}
}
return int.Parse(inputValue);
}
public override string ToString()
{
return string.Format("The hero number is {0} and the name is {1}, with the strength of {2}, and defense of {3}", ID, Name, Strength, Defense);
}
}
class Monster
{
public static int Counter { get; set; }
public int ID { get; set; }
public string Name { get; set; }
public int Strength { get; set; }
public int Defense { get; set; }
public int OriginalHealth { get; set; }
public int CurrentHealth { get; set; }
public int Coins { get; set; }
public Monster(string name, int strength, int defense, int originalHealth)
{
ID = ++Counter;
Name = name;
Strength = strength;
Defense = defense;
OriginalHealth = originalHealth;
CurrentHealth = originalHealth;
Coins = 20;
}
public override string ToString()
{
return string.Format("The monster number is {0} and the name is {1}, with the strength of {2}, and defense of {3}", ID, Name, Strength, Defense);
}
}
class Weapon
{
public static int Counter { get; set; }
public int ID { get; set; }
public string Name { get; set; }
public int Power { get; set; }
public Weapon(string name, int power)
{
ID = ++Counter;
Name = name;
Power = power;
}
public override string ToString()
{
return string.Format("The weapon number is {0}, name is {1}, with the power of {2}.", ID, Name, Power);
}
}
class Armor
{
public static int Counter { get; set; }
public int ID { get; set; }
public string Name { get; set; }
public int Power { get; set; }
public Armor(string name, int power)
{
ID = ++Counter;
Name = name;
Power = power;
}
public override string ToString()
{
return string.Format("The armor number is {0}, name is {1}, with the power of {2}.", ID, Name, Power);
}
}
enum FightResult
{
Win,
Lose
}
class Fight
{
public Hero Hero { get; set; }
public Monster Monster { get; set; }
public FightResult Result { get; set; }
public Fight(Hero hero, Monster monster)
{
Hero = hero;
Monster = monster;
}
public void FightStart()
{
try
{
Console.WriteLine("The fight is starting...");
while (Hero.CurrentHealth > 0 && Monster.CurrentHealth > 0)
{
HeroFight();
if (Monster.CurrentHealth > 0)
{
MonsterFight();
}
}
if (Hero.CurrentHealth <= 0)
{
Console.WriteLine("Sad! You lost the fight.....");
Lose();
}
else if (Monster.CurrentHealth <= 0)
{
Console.WriteLine("Great! You won the fight...");
Win();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public void HeroFight()
{
bool fight = false;
while (!fight)
{
Console.WriteLine("Hero turn");
Console.WriteLine("-----------------------------");
Console.WriteLine();
Console.WriteLine("Choose the option you want: ");
Console.WriteLine("Press 1 to show status.");
Console.WriteLine("Press 2 to show inventory.");
Console.WriteLine("Press 3 to choose the weapon.");
Console.WriteLine("Press 4 to choose the armor.");
Console.WriteLine("Press 5 to buy health.");
Console.WriteLine("Press 0 to quit the fight.");
Console.WriteLine("Press any key to fight directly.");
char inputValue = Console.ReadKey().KeyChar;
Console.WriteLine();
if (inputValue == '0')
{
Hero.CurrentHealth = Hero.OriginalHealth;
Monster.CurrentHealth = Monster.OriginalHealth;
throw new Exception("You quit the fight.");
}
else if (inputValue == '1')
{
Hero.ShowStats();
fight = false;
}
else if (inputValue == '2')
{
Hero.ShowInventory();
fight = false;
}
else if (inputValue == '3')
{
Hero.GetWeapon();
fight = false;
}
else if (inputValue == '4')
{
Hero.GetArmor();
fight = false;
}
else if (inputValue == '5')
{
Hero.BuyHealth();
}
else
{
fight = true;
}
}
int lostHealth = 0;
if (Hero.EquippedWeapon == null)
{
lostHealth = Hero.Strength - Monster.Defense;
}
else
{
lostHealth = Hero.Strength + Hero.EquippedWeapon.Power - Monster.Defense;
}
if (lostHealth <= 0) lostHealth = 0;
Monster.CurrentHealth -= lostHealth;
Console.WriteLine();
Console.WriteLine("Your attack made the monstor lose {0} health.", lostHealth);
Console.WriteLine();
}
public void MonsterFight()
{
Console.WriteLine("Monster turn:");
Console.WriteLine("-----------------------------");
int lostHealth = 0;
if (Hero.EquippedArmor == null)
{
lostHealth = Monster.Strength - Hero.Defense;
}
else
{
lostHealth = Monster.Strength - (Hero.Defense + Hero.EquippedArmor.Power);
}
if (lostHealth <= 0) lostHealth = 0;
Hero.CurrentHealth -= lostHealth;
Console.WriteLine("The attack of the monstor made you lose {0} health.", lostHealth);
Console.WriteLine();
}
public void Win()
{
Result = FightResult.Win;
Hero.wins++;
Hero.Coins += Monster.Coins;
Hero.CurrentHealth = Hero.OriginalHealth;
Monster.CurrentHealth = Monster.OriginalHealth;
Console.WriteLine("You got {0} coins!", Monster.Coins);
}
public void Lose()
{
Result = FightResult.Lose;
Hero.loses++;
Hero.CurrentHealth = Hero.OriginalHealth;
Monster.CurrentHealth = Monster.OriginalHealth;
}
}
class Program
{
static void Main(string[] args)
{
Hero hero1 = new Hero("hero1", 6, 6);
Hero hero2 = new Hero("hero2", 7, 8);
Hero hero3 = new Hero("hero3", 8, 7);
Hero hero4 = new Hero("hero4", 5, 5);
Hero hero5 = new Hero("hero5", 9, 4);
List<Hero> heroes = new List<Hero>() { hero1, hero2, hero3, hero4, hero5 };
Monster monster1 = new Monster("monster1", 10, 10, 100);
Monster monster2 = new Monster("monster2", 12, 7, 100);
Monster monster3 = new Monster("monster3", 13, 5, 100);
Monster monster4 = new Monster("monster4", 11, 6, 100);
Monster monster5 = new Monster("monster5", 15, 8, 100);
List<Monster> monsters = new List<Monster>() { monster1, monster2, monster3, monster4, monster5 };
Weapon Sword = new Weapon("Sword", 10);
Weapon Dagger = new Weapon("Dagger", 6);
Weapon Axe = new Weapon("Axe", 10);
Weapon Bow = new Weapon("Bow", 9);
List<Weapon> weapons = new List<Weapon>() { Sword, Dagger, Axe, Bow };