본문 바로가기

C#/수업내용

2020-04-13 수업내용

클래스 연습

-----------------------------------------------------------------------------------------

1. Character Class

-----------------------------------------------------------------------------------------------------------------------------------

캐릭터 클래스 생성, 공격 기능, 피해 기능

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_008
{
    class Character
    {
        //데이터
        public string name;
        public Character(string name)
        {
            this.name = name;
            Console.WriteLine("{0}이 생성 되었습니다."this.name);
        }
        //메서드
        public void Attack(Character target, int count) //공격 (A to B & send to this(A) Character class).
        {
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine("{0}이 {1}을 공격했습니다."this.name, target.name);
                target.Hit(this); // call to target's Hit()
                Console.WriteLine("{0}이 {1}를 {2} 회 때렸습니다.\n"this.name, target.name, i + 1);
            }
        }
        public void Hit(Character target) //피해 (B to A & received to this(A) Character class).
        {
            Console.WriteLine("{0}이 {1}으로부터 피해를 받았습니다."this.name, target.name);
        }
 
 
    }
}
 
 

---------------------------------------------------------------------------------------------------------------------

2-1. Fighter Class

-----------------------------------------------------------------------------------------------------------------------------------

아군 비행기와 적군 비행기를 생성 및 위치지정, 아군 공격 총알을 만들어 이동하면서 적군을 만나면 사라지는 내용

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_008
{
    class Fighter
    {
        //데이터
 
        public string fighterName;
        public int fighter_X = 0;
        public int fighter_Y = -6;
 
        public Fighter(string fighterName,int fighter_X,int fighter_Y)
        {
            this.fighterName = fighterName;
            this.fighter_X = fighter_X;
            this.fighter_Y = fighter_Y;
            Console.WriteLine("아군 {0} 기체가 생성되었습니다. ",this.fighterName);
            Console.WriteLine("플레이어 현재 위치 ({0} , {1})\n"this.fighter_X, this.fighter_Y);
 
        }
        //메서드
        public Bullet Shoot()
        {
            return new Bullet("노말"this.fighter_X, this.fighter_Y);
        }
 
    }
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

2-2. Monster Class

-----------------------------------------------------------------------------------------------------------------------------------

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
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_008
{
    class Monster
    {
        //데이터
        public string monsterName;
        public int monster_X = 0;
        public int monster_Y = 9;
        public Monster(string monsterName, int monster_X, int monster_Y)
        {
            this.monsterName = monsterName;
            this.monster_X = monster_X;
            this.monster_Y = monster_Y;
            Console.WriteLine("적군 {0} 기체가 생성되었습니다. "this.monsterName);
            Console.WriteLine("적군 현재 위치 ({0} , {1})\n"this.monster_X, this.monster_Y);
        }
        //메서드
 
    }
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

2-3. Bullet Class

-----------------------------------------------------------------------------------------------------------------------------------

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_008
{
    class Bullet
    {
        //데이터
        public string bulletName;
        public int bullet_X = 0;
        public int bullet_Y = 0;
        public Bullet(string bulletName, int bullet_X, int bullet_Y)
        {
            this.bulletName = bulletName;
            this.bullet_Y = bullet_Y;
            this.bullet_Y = bullet_Y;
            Console.WriteLine("{0} 총알이 생성되었습니다. ({1} , {2})"this.bulletName, this.bullet_X, this.bullet_Y);
 
        }
        //메서드
 
    }
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

3-1. Barracks Class

-----------------------------------------------------------------------------------------------------------------------------------

스타크래프트 배럭에서 마린, 메딕을 만든다, 공격메서드를 추가하여 메딕은 공격불가, 마린은 공격가능 넣기

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_008
{
    class Barracks
    {
 
        public int id = 1;
        Unit units = null;
 
        public Barracks()
        {
            Console.WriteLine("배럭스가 생성되었습니다.");
        }
 
        public Unit CreateUnit(eUnitType eUnitType)
        {
 
            switch (eUnitType)
            {
                case eUnitType.Marine:
                    new Unit(id, eUnitType.Marine);
                    id++;
                    break;
 
                case eUnitType.Medic:
                    new Unit(id, eUnitType.Medic);
                    id++;
                    break;
            }
 
            return units;
 
        }
 
 
 
    }
}
 
 

-----------------------------------------------------------------------------------------------------------------------------------

3-2. Unit Class

-----------------------------------------------------------------------------------------------------------------------------------

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_008
{
 
    public enum eUnitType
    {
        None, Marine, Medic,
    }
    //유닛 클래스 
 
    class Unit
    {
 
        public eUnitType unitType;
 
        public int id;
 
 
        //생성자 
        public Unit(int id, eUnitType unitType)
        {
            this.id = id;
            this.unitType = unitType;
            Console.WriteLine("{0}유닛({1})이 생성되었습니다"this.unitType, this.id);
        }
 
        public void Attack(Unit target)
        {
            if (this.unitType == eUnitType.Medic)
            {
                Console.WriteLine("{0}({1})은 공격 할수 없습니다."this.unitType, this.id);
                return;
            }
            Console.WriteLine("{0}({1})이 {2}({3})을 공격 했습니다."this.unitType, this.id, target.unitType, target.id);
            target.Hit(this);
        }
 
 
        public void Hit(Unit target)
        {
            Console.WriteLine("{0}({1})이 {2}({3})에게 피해를 받았습니다."this.unitType, this.id, target.unitType, target.id);
        }
    }
}
 
 

-----------------------------------------------------------------------------------------------------------------------------------

4-1. Hatchery Class

-----------------------------------------------------------------------------------------------------------------------------------

해처리클래스를 만들고, 라바를 변태시켜 드론이 나오는 내용

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_008
{
    class Hatchery
    {
        public Hatchery()
        {
            Console.WriteLine("해처리가 생성되었습니다.");
 
 
        }
 
        public Larva CreateLaba()
        {
            return new Larva();
        }
    }
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

4-2. Larva Class

-----------------------------------------------------------------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_008
{
    class Larva
    {
        public Larva()
        {
            Console.WriteLine("라바가 생성되었습니다.");
        }
 
        public Drone CreateDrone()
        {
            return new Drone();
        }
 
 
    }
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

4-3. Drone Class

-----------------------------------------------------------------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_008
{
    class Drone
    {
 
        public Drone()
        {
            Console.WriteLine("드론이 생성되었습니다.");
        }
 
    }
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

5-1. Factory Class

-----------------------------------------------------------------------------------------------------------------------------------

스타크래프트 테란 건물 팩토리에서 시즈탱크를 생성하고, 디폴트는 탱크모드, 시즈모드시 이동불가.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_008
{
    class Factory
    {
        public Factory()
        {
            Console.WriteLine("Factory가 생성되었습니다.");
        }
 
        public SiegeTank CreateSiegeTank()
        {
            return new SiegeTank();
        }
 
    }
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

5-2. SiegeTank Class

-----------------------------------------------------------------------------------------------------------------------------------

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_008
{
    public enum eModeType
    {
        TankMode,
        SiegeMode,
    }
    class SiegeTank
    {
        public eModeType modeType;
 
        public SiegeTank()
        {
            this.modeType = eModeType.TankMode;
            Console.WriteLine("SiegeTank가 생성되었습니다.");
        }
 
        public void ChangeMode()
        {
            eModeType preModType = this.modeType;
 
            if (this.modeType == eModeType.TankMode)
            {
                this.modeType = eModeType.SiegeMode;
                Console.WriteLine("SiegeTank가 {0} 에서 {1}로 변경했습니다.",preModType,eModeType.SiegeMode);
            }
            else if (this.modeType == eModeType.SiegeMode)
            {
                this.modeType = eModeType.TankMode;
                Console.WriteLine("SiegeTank가 {0} 에서 {1}로 변경했습니다.",preModType,eModeType.TankMode);
            }
        }
 
        public void MoveSiegeTank()
        {
            if (this.modeType == eModeType.TankMode)
            {
                Console.WriteLine("SiegeTank가 이동 했습니다.");
            }
            else if (this.modeType == eModeType.SiegeMode)
            {
                Console.WriteLine("SiegeMode에서는 이동할 수 없습니다.");
            }
        
        }
 
 
    }
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

App Class

-----------------------------------------------------------------------------------------------------------------------------------

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_008
{
    class App
    {
        public App()
        {
            //Console.WriteLine("오늘은 2020-04-13 입니다.");
 
            //Fighter hong = new Fighter("홍길동",0,-6);
            //Monster lim = new Monster("임꺽정",0,9);
 
            //Bullet bullet = hong.Shoot(); 
            // 반환하는이유 : 플레이어가 만들지만 앱에서 모든 객체를 관리하기 위해서.
 
            //while (true)
            //{
            //    if (bullet.bullet_X == lim.monster_X && bullet.bullet_Y == lim.monster_Y)
            //    {
            //        Console.WriteLine("적군 {0} 기체가 총알을 맞고 죽었습니다.", lim.monsterName);
            //        lim = null;
            //        Console.WriteLine("총알 소멸");
            //        bullet = null;
            //        break;
            //    }
 
            //    else if (bullet.bullet_Y != lim.monster_Y)
            //    {
            //        bullet.bullet_Y++;
            //        Console.WriteLine("현재 총알 위치 {0},{1}", bullet.bullet_X, bullet.bullet_Y);
            //    }
            //}
 
            //강사님은 bullet 클래스에 메서드를 만듬.
 
 
 
            //Character hong = new Character("홍길동");
            //Character lim = new Character("임꺽정");
 
            //Console.Write("몇 대 때리시겠습니까? : ");
            //int count = int.Parse(Console.ReadLine());
            //hong.Attack(lim,count);
 
            //////////////////////////////////
 
            //Hatchery hatchery = new Hatchery();
 
            //Larva laba = hatchery.CreateLaba();
 
            //Drone drone = laba.CreateDrone();
 
            //laba = null;
 
 
 
            //Console.WriteLine("hatchery : {0}",hatchery);
            //Console.WriteLine("laba : {0}",laba);
            //Console.WriteLine("drone : {0}",drone);
 
 
 
            Factory factory = new Factory(); 
            SiegeTank siegeTank = factory.CreateSiegeTank();
 
            Console.WriteLine("현재 모드타입 :  {0}",siegeTank.modeType);
 
            siegeTank.MoveSiegeTank();
            siegeTank.ChangeMode();
 
            Console.WriteLine("모드를 변경 : {0}",siegeTank.modeType);
            siegeTank.MoveSiegeTank();
 
 
        }
    }
}
 
 
 

 

 

'C# > 수업내용' 카테고리의 다른 글

2020-04-16 수업내용 Source  (0) 2020.04.16
2020-04-14 수업내용 (클래스,메서드)  (0) 2020.04.14
2020-04-09 수업내용 Source  (0) 2020.04.09
2020-04-08 수업내용 Source  (0) 2020.04.08
2020-04-07 수업내용 Source  (0) 2020.04.07