C#/수업내용

2020-04-20 [1차원 배열 활용]

NexTin 2020. 4. 20. 18:55
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_012
{
    class App
    {
        public App()
        {
 
            //캐릭터 데이터 객체 생성
            //캐릭터 데이터 멤버 : name, hp, damage
 
            // 캐릭터 객체 생성시 매개변수로 캐릭터 데이터 객체를 전달
            // 캐릭터 이름, 체력, 공격력 출력 
 
            CharacterData data = new CharacterData("고길동"1002);
 
            Character dong = new Character(data);
            dong.PrintState();
 
            ////캐릭터 기능
            ////무기를 획득하다
 
            ////무기 데이터 객체 생성 
            ////멤버변수 : [naem, damage]
            //WeaponData weaponData = new WeaponData("롱소드", 5);
            ////무기 객체 생성 (무기데이터)
            //Weapon sword = new Weapon(weaponData);
 
            ////캐릭터 기능
            ////무기를 획득하다
            //dong.Getweapon(sword);
 
            //WeaponData weaponData1 = new WeaponData("숏소드", 3);
            //Weapon weapon1 = new Weapon(weaponData1);
            //dong.Getweapon(weapon1);
 
            //dong.PrintWeapons();
 
            //WeaponData weaponData2 = new WeaponData("도끼", 7);
            //Weapon weapon2 = new Weapon(weaponData2);
            //dong.Getweapon(weapon2);
            //dong.PrintWeapons();
 
            //dong.GetAllWeapons();
            //dong.PrintWeapons();
 
            //var weapons = dong.GetAllWeapons();
 
            //Console.WriteLine(weapons.Length);
 
            //Weapon foundweapon = null;
            //foreach (var weaponElement in weapons)
            //{
            //    if (weaponElement != null)
            //    {
            //        if (weaponElement.weapondate.name == "롱소드")
            //        {
            //            Console.WriteLine("{0}을 찾았습니다.", weaponElement.weapondate.name);
            //            foundweapon = weaponElement;
            //        }
            //    }
            //}
 
            //dong.EquipWeapon(foundweapon);
 
            //dong.EquipWeapon("숏소드"); //문자열로
 
            //dong.PrintEquipWeapon();
 
            Weapon[] arrDropItems = new Weapon[2];
            arrDropItems[0= new Weapon(new WeaponData("창"11));
            arrDropItems[1= new Weapon(new WeaponData("롱보우"10));
 
            dong.GetAllWeapons(arrDropItems);
            dong.PrintWeapons();
 
 
            Weapon[] arrDropItems1 = new Weapon[5];
            arrDropItems1[0= new Weapon(new WeaponData("창"11));
            arrDropItems1[1= new Weapon(new WeaponData("롱보우"10));
            arrDropItems1[2= new Weapon(new WeaponData("그레이트소드"13));
            arrDropItems1[3= new Weapon(new WeaponData("목검"3));
            arrDropItems1[4= new Weapon(new WeaponData("너클"4));
 
 
            dong.GetAllWeapons(arrDropItems1);
 
            Console.WriteLine("");
            foreach (var item in arrDropItems1)
            {
                Console.WriteLine("=====>" + item);
            }
         
         
             // 이게 무시되고 다음게 실행됨 무엇이 잘못됬는지?
            
            //dong.PrintWeapons();
 
 
        }
   
    }
}
 
======================================================================================================================
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_012
{
 
    class Character
    {
        public CharacterData data;
        public Weapon[] arrweapons;
        public int arrweaponindex = 0;
        public Weapon[] characterweapons;
        public int characterweaponsindex;
 
        public Weapon equipedweapon;
 
 
 
        public Character(CharacterData data)
        {
            this.data = data;
            this.arrweapons = new Weapon[5];
            this.characterweapons = new Weapon[7];
            //this.equipedweapon.isEquiped = false;
        }
 
        public void PrintState()
        {
            Console.WriteLine("이름 : {0}"this.data.name);
            Console.WriteLine("체력: {0}"this.data.hp);
            Console.WriteLine("공격력 : {0}"this.data.damage);
        }
 
        public void Getweapon(Weapon weapon)
        {
            this.arrweapons[arrweaponindex] = weapon;
            Console.WriteLine("/////{0}은 {1}을(를) 습득했다./////"this.data.name, this.arrweapons[arrweaponindex].weapondate.name);
            arrweaponindex++;
        }
 
        public void GetAllWeapons(Weapon[] arrWeapons)
        {
            arrweaponindex = 0;
            for (int i = 0; i < arrWeapons.Length; i++)
            {
                if (this.arrweapons[arrweaponindex] == null)
                {
                    this.arrweapons[arrweaponindex] = arrWeapons[i];
                    Console.WriteLine("/////{0}을(를) 습득했다./////"this.arrweapons[arrweaponindex].weapondate.name);
                    arrweaponindex++;
                }
            }
        }
 
        public void PrintWeapons()
        {
            Console.WriteLine("***소지중인 무기***");
            foreach (Weapon weapon in arrweapons)
            {
                if (weapon != null)
                {
                    Console.WriteLine("이름: {0} / 공격력: {1}"weapon.weapondate.name, weapon.weapondate.damage);
                }
                else
                {
                    Console.WriteLine("[     ]");
                }
            }
        }
 
        public Weapon[] GetAllWeapons()
        {
            Weapon foundweapon = null;
            for (int i = 0; i < arrweapons.Length; i++)
            {
                if (arrweapons[i] != null)
                {
                    foundweapon = arrweapons[i];
                    arrweapons[i] = null;
 
                    this.characterweapons[characterweaponsindex] = foundweapon;
 
                    characterweaponsindex++;
                }
                else
                {
                    Console.WriteLine("[    ]");
                }
 
 
            }
 
            Console.WriteLine("****옮겨낸 무기**** ");
            foreach (Weapon weapon in characterweapons)
            {
                if (weapon != null)
                {
                    Console.WriteLine(weapon.weapondate.name);
                }
                else
                {
                    Console.WriteLine("[     ]");
                }
            }
 
 
            return characterweapons;
 
            //int length = 0;
            //length++;
            //Weapon[] rtnArrwepons = new Weapon[length];
 
 
        }
 
 
        //장비하다를 표현하고 싶다 
        // 무기는 한손에 하나니까, 배열을 써서  표현 ?
        //널이라면 그냥 들어가고 널이 아니라면 바뀌는 형식?
 
        public void EquipWeapon(Weapon weapon)
        {
            this.equipedweapon = weapon;
 
            this.equipedweapon.isEquiped = true//착용했다는 부분으로 변경 
 
            Console.WriteLine("{0}을 장비합니다."this.equipedweapon.weapondate.name);
 
        }
 
        public void EquipWeapon(string WeaponName)
        {
            Weapon foundweapon = null;
            foreach (Weapon weapon in characterweapons)
            {
                if (weapon != null)
                {
                    if (weapon.weapondate.name == WeaponName)
                    {
                        Console.WriteLine("소지품에서 {0} 발견"weapon.weapondate.name);
                        foundweapon = weapon;
                        break;
                    }
 
                }
            }
 
            PrintEquipWeapon();
 
            switch (this.equipedweapon.isEquiped)
            {
                case true:
                    {
                        Console.WriteLine("{0} 장비 해제"this.equipedweapon.weapondate.name);
                        this.equipedweapon.isEquiped = false;
 
                        this.equipedweapon = foundweapon;
 
                        this.equipedweapon.isEquiped = true//착용했다는 부분으로 변경 
 
                        Console.WriteLine("{0}을 장비합니다."this.equipedweapon.weapondate.name);
                    }
                    break;
 
                case false:
                    this.equipedweapon = foundweapon;
                    this.equipedweapon.isEquiped = true;
                    Console.WriteLine("{0}을 장비합니다."this.equipedweapon.weapondate.name);
                    break;
            }
        }
 
        public void PrintEquipWeapon()
        {
            switch (this.equipedweapon.isEquiped)
            {
                case true:
                    Console.WriteLine("장비 착용중 : {0}, {1}"this.equipedweapon.weapondate.name, this.equipedweapon.isEquiped);
                    break;
                case false:
                    Console.WriteLine("장비하지 않았습니다. {0}"this.equipedweapon.isEquiped);
                    break;
            }
        }
 
 
   
 
    }
}
==============================================================================================================
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_012
{
    class Inventory
    {
        public List<Item> itemlist;
        public Inventory()
        {
            this.itemlist = new List<Item>();
        }
 
        public void Add(Item item)
        {
            //itemlist에 item.name이 있는지 확인 
            Item foundItem = null;
            foreach (Item element in this.itemlist)
            {
                if (element.name == item.name)
                {
                    foundItem = element;
                    break;
                }
            }
 
            if (foundItem != null)
            {
                //찾은 아이템이 있다면 amount를 매개변수 Item 객체의 amount 만큼 합산(+)
                foundItem.amount += item.amount;
            }
            else
            {
                this.itemlist.Add(item);
            }
 
        }
 
        public Item GetItemByName(string name) // 이름으로 아이템을 뺄때(가져올떄)
        {
            Item foundItem = null;
            foreach (var item in itemlist)
            {
                if (item.name == name)
                {
                    foundItem = item;
                    break;
                }
            }
            // 찾은 아이템이 null이 아니라면 
            // 수량 1 감소 
            // 새로운 아이템 생성 
            // 아이템이름은 찾은 아이템
            // 수량은 1이다.
            Item rtnItem = null;
            if (foundItem != null)
            {
                foundItem.amount -= 1;
                rtnItem =  new Item(foundItem.name, 1);
            }
            if (foundItem.amount <= 0)
            {
                foundItem.amount -= 1;
                this.itemlist.Remove(foundItem);
                Console.WriteLine("수량이 1에서 빼내므로 여기서는 사라짐 ");
                Console.WriteLine("카운트 : {0}"this.itemlist.Count);
            }
 
            return rtnItem;
        }
 
 
        public void PrintAllItems()
        {
            foreach (Item item in this.itemlist)
            {
                Console.WriteLine("이름 : {0} / 수량 : {1}"item.name, item.amount);
            }
 
            if(this.itemlist == null)
            {
                Console.WriteLine("가방에 아이템이 없습니다.");
            }
        }
 
        //public int GetItemCount() // 아이템 카운트 표현을 위해 
        //{
        //    return itemlist.Count;
        //}
 
    }
}
========================================================================================================
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_012
{
    class Weapon
    {
        public WeaponData weapondate;
        public bool isEquiped;
 
        public Weapon(WeaponData weapondate)
        {
            this.weapondate = weapondate;
 
        }
    }
}
===================================================================================================
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_012
{
    class WeaponData
    {
        public string name;
        public int damage;
        public WeaponData(string name, int damage)
        {
            this.name = name;
            this.damage = damage;
        }
    }
}
 
===================================================================================================
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_012
{
    class CharacterData
    {
        public string name;
        public int hp;
        public int damage;
        
        public CharacterData(string name, int hp, int damage)
        {
            this.name = name;
            this.hp = hp;
            this.damage = damage;
        }
 
 
 
    }
}
 
==========================================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_012
{
    class Item
    {
        public string name;
        public int amount;
        public Item(string name, int amount)
        {
            this.name = name;
            this.amount = amount;
           
        }
    }
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter