-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlastCode
645 lines (591 loc) · 19.8 KB
/
lastCode
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static _13_3.Program;
using static _13_4.Player;
namespace _13_4
{
internal class Program
{
static void Main(string[] args)
{
GameManager gameManager = new GameManager();
GameMessage gameMessage = new GameMessage();
gameManager.OnGameStart += () => gameMessage.ShowMessage("게임을 시작합니다.");
gameManager.OnGamePause += () => gameMessage.ShowMessage("게임을 일시정지합니다.");
gameManager.OnGameEnd += () => gameMessage.ShowMessage("게임을 종료합니다.");
EnemyUnit enemy = new EnemyUnit();
PlayerUnit playerUnit = new PlayerUnit();
enemy.EnemyAttack += playerUnit.HandleDamage;
Item item = new Item();
gameManager.GameStart();
Console.WriteLine("당신의 이름을 입력하세요.");
string playerName = (string)Console.ReadLine();
Player player = new Player(playerName);
EnemyUnit unit = new EnemyUnit();
unit.GetRandom(player);
player.GetUnit(enemy);
player.PrintUnitList();
while(true)
{
Console.WriteLine("어떤 행동을 하실 건가요?");
Console.WriteLine("1. 새로운 포켓몬을 잡는다. 2. 정보 3. 상점 4. 게임 끝");
ConsoleKeyInfo input = Console.ReadKey();
if (input.Key == ConsoleKey.D1)
{
Console.WriteLine();
EnemyUnit enemy1 = new EnemyUnit();
enemy.GetRandom(player);
Console.WriteLine("{0}이 나타났습니다.", enemy.Name);
Console.WriteLine("사용할 아이템의 번호를 입력하세요.");
player.PrintItemList();
int useIdx = int.Parse(Console.ReadLine());
while(useIdx >= player.Items.Count)
{
Console.WriteLine("소지하고 있는 아이템이 없습니다.");
useIdx = int.Parse(Console.ReadLine());
}
player.GotchaEvent(enemy, player.Items[useIdx]);
}else if(input.Key == ConsoleKey.D2)
{
player.PlayerInfo();
}
else if(input.Key == ConsoleKey.D3)
{
gameManager.GameEnd();
break;
}
}
}
}
public delegate void GameEvent();
public class GameManager
{
public event GameEvent OnGameStart;
public event GameEvent OnGamePause;
public event GameEvent OnGameEnd;
public void GameStart()
{
OnGameStart?.Invoke();
}
public void GamePause()
{
OnGamePause?.Invoke();
}
public void GameEnd()
{
OnGameEnd?.Invoke();
}
}
public class GameMessage
{
public void ShowMessage(string message)
{
Console.WriteLine(message);
}
}
enum TypeEnum
{
Fire,
Water,
Grass
}
public class Unit
{
public string Name;
public int Level;
public int Attack;
public int Defense; //방어 Defence는 방위
public int Speed;
public bool IsFaint;
public int Health;
public void PrintStats()
{
Console.WriteLine("Unit Name : {0}", Name);
Console.WriteLine("Unit Level : {0}", Level);
Console.WriteLine("Unit Attack : {0}", Attack);
Console.WriteLine("Unit Defense : {0}", Defense);
Console.WriteLine("Unit Speed : {0}", Speed);
Console.WriteLine("Unit Health : {0}", Health);
Console.WriteLine("Unit IsFaint : {0}", IsFaint);
}
public void GetRandomUnit(Player player)
{
Random random = new Random();
int num = random.Next(1, 101);
Name = NameEnum.NoData.ToString();
int naturalExp;
if (num == 1)
{
Name = NameEnum.Mew.ToString();
naturalExp = 50;
}
else if (num <= 10)
{
Name = NameEnum.Pikachu.ToString();
naturalExp = 20;
}
else if (num <= 20)
{
Name = NameEnum.Purin.ToString();
naturalExp = 18;
}
else if (num <= 50)
{
Name = NameEnum.Tunguri.ToString();
naturalExp = 14;
}
else if (num <= 90)
{
Name = NameEnum.Snaki.ToString();
naturalExp = 10;
}
else if (num <= 100)
{
Name = NameEnum.Naoha.ToString();
naturalExp = 20;
}
else
{
Console.WriteLine("오류 발생");
naturalExp = 0;
}
if (player.Level <= 5)
{
Level = random.Next(1, player.Level + 6);
Attack = random.Next(1, 10 + player.Level);
Defense = random.Next(1,10 + player.Level);
Speed = random.Next(1, 10 + player.Level);
Health = ((Defense + Level) + random.Next(1, player.Level));
IsFaint = false;
}
else
{
Level = random.Next(player.Level - 5, player.Level + 6);
Attack = random.Next(1, 10 + player.Level);
Defense = random.Next(1, 10 + player.Level);
Speed = random.Next(1, 10 + player.Level);
Health = ((Defense + Level) + random.Next(player.Level) + random.Next(10));
IsFaint = false;
}
}
}
enum NameEnum
{
NoData=0,
Mew = 1,
Pikachu = 10,
Purin = 20,
Tunguri = 50,
Snaki=90,
Naoha=100
}
//Dictionaty사용해서 확률 계산하기
// NameEnum[] enums =
public delegate void EnemyAttackHandler(float damage);
public class EnemyUnit : Unit
{
public float GotchaPercent { get; set; }
public float GiveExp { get; set; }
public EnemyUnit()
{
Name = "UnKnown";
Health = 0;
Level = 0;
Attack = 0;
Defense = 0;
Speed = 0;
GotchaPercent = 0;
IsFaint = false;
GiveExp = 0;
}
public event EnemyAttackHandler EnemyAttack;
public void AttackPlayer(float damage)
{
EnemyAttack?.Invoke(damage);
}
public void GetRandom(Player player)
{
{
/*
* NameEnum enum =
* Rendom random = new Random();
* int
*/
Random random = new Random();
int num = random.Next(1, 101);
Name = NameEnum.NoData.ToString();
int naturalExp;
if (num == 1)
{
Name = NameEnum.Mew.ToString();
GotchaPercent = 0.1f;
naturalExp = 50;
}
else if (num <= 10)
{
Name = NameEnum.Pikachu.ToString();
GotchaPercent = 0.3f;
naturalExp = 20;
}
else if (num <= 20)
{
Name = NameEnum.Purin.ToString();
GotchaPercent = 0.5f;
naturalExp = 18;
}
else if (num <= 50)
{
Name = NameEnum.Tunguri.ToString();
GotchaPercent = 0.5f;
naturalExp = 14;
}
else if (num <= 90)
{
Name = NameEnum.Snaki.ToString();
GotchaPercent = 0.5f;
naturalExp = 10;
}
else if (num <= 100)
{
Name = NameEnum.Naoha.ToString();
GotchaPercent = 0.3f;
naturalExp = 20;
}
else
{
Console.WriteLine("오류 발생");
naturalExp = 0;
}
if (player.Level <= 5)
{
Level = random.Next(1, player.Level + 6);
Attack = random.Next(1, 10 + player.Level);
Defense = random.Next(1, 10 + player.Level);
Speed = random.Next(1, 10 + player.Level);
Health = ((Defense + Level) + random.Next(1, player.Level));
IsFaint = false;
}
else
{
Level = random.Next(player.Level - 5, player.Level + 6);
Attack = random.Next(1, 10 + player.Level);
Defense = random.Next(1, 10 + player.Level);
Speed = random.Next(1, 10 + player.Level);
Health = ((Defense + Level) + random.Next(player.Level) + random.Next(10));
IsFaint = false;
}
GiveExp = (float)naturalExp * (1 - (player.Level - Level) / Level);
}
}
}
public class PlayerUnit : Unit
{
public float Experience { get; set; }
public float NeedExp { get; set; }
public int MaxHealth { get; set; } //
public PlayerUnit()
{
Random rand = new Random();
Name = "기본캐릭터";
Level = 5;
Attack = rand.Next(1,10);
Defense = rand.Next(1, 10);
Speed = rand.Next(1, 10);
Health = rand.Next(1, 10);
Experience = rand.Next(1, 10);
}
public PlayerUnit(EnemyUnit enemy,string? nickName)
{
Name = enemy.Name;
Level = enemy.Level;
Attack = enemy.Attack;
Defense = enemy.Defense;
Speed = enemy.Speed;
Health = enemy.Health;
Experience = 0;
if (nickName!=null)
{
Name = nickName;
}
NeedExp = Level * (enemy.GiveExp) / 10+100;
}
public void ChangeName()
{
Console.WriteLine("바꾸고 싶은 이름을 입력하여 주십시오.");
string? newName = Console.ReadLine();
if (newName != null)
{
Name = newName;
}
}
public void HandleDamage(float damage)
{
Console.WriteLine("{0}은 {1}만큼의 데미지를 입었다!", Name, damage);
}
public void LevelUp()
{
Attack += (1 + Level / 10);
Defense += (1 + Level / 10);
Speed += (1 + Level / 10);
Health += (1 + Level / 5);
NeedExp = NeedExp+Level;
}
}
public class Player
{
public string Name { get; set; } = "Red";
public int Level { get; set; } = 1;
public float Exp { get; set; }
public List <PlayerUnit> MyUnits { get; set; }
public List <Item> Items { get; set; }
public Player(string? name)
{
if (name == null)
{
Name = "Red";
}
else
{
Name = name;
}
Level = 1;
Exp = 0;
MyUnits = new List<PlayerUnit>();
Items = new List<Item>();
Item item = new Item();
Items.Add(item);
}
// 공통되는 기능을 가진 애들을 호출하는 클래스를 따로 만들고, 그걸 player가 가지도록 한다.
// Item Controller 따로 만들어서 Get Use 그 안에 넣고 Items 포인터로 받기
public void ItemGet(Item item)
{
Items.Add(item);
item.Get();
}
public bool ItemUse(Item item)
{
while (Items.Contains(item))
{
if (item.UseableLevel > Level)
{
Console.WriteLine("레벨이 낮아 아이템을 사용할 수 없습니다.");
break;
}
else
{
item.Use();
int itemIdx = Items.IndexOf(item);
Items.RemoveAt(itemIdx);
if (Items.Count == 0)
{
Item newItem = new Item();
Items.Add(newItem);
}
return true;
}
}
return false;
}
public void PrintItemList()
{
foreach (Item item in Items)
{
Console.Write(Items.IndexOf(item)+". ");
Console.WriteLine($"{item.Name}");
Console.WriteLine($"{item.ExplainScript}");
}
}
public void PrintUnitList()
{
foreach (PlayerUnit myUnit in MyUnits)
{
int indexNum = MyUnits.IndexOf(myUnit);
Console.WriteLine($"{indexNum} ");
myUnit.PrintStats();
}
}
public void PrintPlayerList()
{
Console.WriteLine($"Player Name : {Name}");
Console.WriteLine($"Player Level : {Level}");
Console.WriteLine($"Player Exp : {Exp}/10");
}
public void PlayerInfo()
{
Console.WriteLine("Player");
PrintPlayerList();
Console.WriteLine();
Console.WriteLine("Units");
PrintUnitList();
Console.WriteLine();
Console.WriteLine("Items");
PrintItemList();
}
public void CheckFightable()
{
var FightableUnits = from num in MyUnits
where num.IsFaint == false
select num;
foreach (var unit in FightableUnits)
{
Console.WriteLine(unit.Name);
}
}
private void ExpCounter(EnemyUnit enemy)
{
Console.WriteLine("경험치를 {0}만큼 획득하였습니다..", enemy.GiveExp);
Exp += enemy.GiveExp /10;
Console.WriteLine("플레이어의 경험치 : {0}/10", Exp);
foreach (var unit in MyUnits)
{
Console.WriteLine(unit.Name);
unit.Experience += enemy.GiveExp;
Console.WriteLine("경험치 : {0} / {1}", unit.Experience, unit.NeedExp);
}
CheckLevelUp();
}
public void CheckLevelUp()
{
PlayerLevelUp();
UnitLevelUp();
}
private void PlayerLevelUp()
{
int levelPtr = Level;
while(Exp>10)
{
Console.WriteLine("플레이어의 레벨이 올랐습니다!");
Level++;
Exp-=10;
Console.WriteLine("플레이어의 레벨 : {0} >> {1}", levelPtr, Level);
}
}
private void UnitLevelUp()
{
foreach (var unit in MyUnits)
{
int levelPtr = unit.Level;
while (unit.Experience > unit.NeedExp)
{
unit.Level++;
unit.Experience -= unit.NeedExp;
Console.WriteLine($"{unit.Name}의 레벨이 올랐습니다!");
unit.LevelUp();
Console.WriteLine("{0}의 레벨 : {1} >> {2}", unit.Level, levelPtr, unit.Level);
}
}
}
private bool? GetYesNo()
{
bool? result = null;
do
{
Console.WriteLine("1. Yes 2. No ");
ConsoleKeyInfo key = Console.ReadKey();
if (key.Key == ConsoleKey.D1)
{
result = true;
}
else if (key.Key == ConsoleKey.D2)
{
result = false;
}
else
{
Console.WriteLine("1과 2 사이의 값을 입력해주세요.");
}
} while (result == null);
return result;
}
public string? NameUnit()
{
string? nick=null;
Console.WriteLine("이름을 지어주시겠습니까?");
if(GetYesNo()==true)
{
Console.WriteLine("이름을 입력해주세요.");
nick=Console.ReadLine();
}
return nick;
}
public void GetUnit(EnemyUnit unit)
{
Console.WriteLine("{0}을 획득하였습니다!", unit.Name);
//플레이어 경험치 획득.
ExpCounter(unit);
string? nickName = NameUnit();
PlayerUnit myUnit = new PlayerUnit(unit, nickName);
MyUnits.Add(myUnit);
}
private bool CheckGetOrNo(EnemyUnit enemy, Item item)
{
ItemUse(item);
float calculateNum = enemy.GotchaPercent * item.ItemEffect * 100;
Random rand = new Random();
int randomVal = rand.Next(1, 101);
if (randomVal < calculateNum)
{
return true;
}
else return false;
}
public void GotchaEvent(EnemyUnit enemy, Item item)
{
bool result = false;
do
{
result = CheckGetOrNo(enemy, item);
if (result == false)
{
Console.WriteLine("포획에 실패했습니다.");
Console.WriteLine("다시 시도하겠습니까?");
if (GetYesNo() == true)
{
continue;
}
else
{
Console.WriteLine("포획 실패");
break;
}
}
} while (result == false);
if(result == true)
{
GetUnit(enemy);
}
}
}
public class Item
{
public string Name { get; set; }
public int UseableLevel { get; set; }
public string ExplainScript { get; set; }
public float ItemEffect { get; set; }
// 1. Item에 타입을 넣어서 아이템을 적용시킬 때 조건문을 나눠서 처리하는 방법
// 2. 아이템 상속받아서 USE메서드를 오버라이드해서 각 하위 클래스에서 정의해주는 것
public Item()
{
Name = "Simple Ball";
UseableLevel = 0;
ExplainScript = string.Empty;
ItemEffect = 1;
}
public Item(string? name, int useLevel,string explain, float effect)
{
Name = name;
UseableLevel = useLevel;
ExplainScript = explain;
ItemEffect = effect;
}
public void Get()
{ Console.WriteLine("{0}을 획득하였다.", this.Name); }
public void Use()
{ Console.WriteLine("{0}을 사용하였다.", this.Name); }
public void Drop()
{ Console.WriteLine("{0}을 버렸다.", this.Name); }
}
}