카테고리 없음
2018.03.16 수업내용 (줄넘기, 대장장이)
일등하이
2018. 5. 15. 21:23
반응형
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; namespace ConsoleApp1 { class Program { static void Main(string[] args) { //짱구의 줄넘기 //짱구 string name1 = "짱구"; //줄넘기를 한다. JumpRope(name1, 3); //짱아 string name2 = "짱아"; //줄넘기를 한다. JumpRope(name2, 2); Console.ReadKey(); } static void JumpRope(string name, int count) { for (int i = 0; i < count; i++) { Console.WriteLine(name + "이(가) 줄넘기를 했습니다."); } } } } | 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 | using System; namespace ConsoleApp1 { class Test { //전역변수 string name; //null //constructor(생성자) public Test() { //매개변수가 없는 생성자 : 기본 생성자 Console.WriteLine(this + "의 기본 생성자"); } //매개 변수가 있는 생성자 public Test(string name) { this.name = name; Console.WriteLine("매개 변수가 있는 생성자: " + name); } //매개 변수가 있는 생성자 public Test(string name1, string name2) { Console.WriteLine("매개 변수가 있는 생성자: " + name1 + " , " + name2 ); } //메서드 생성 //[접근제한자] [반환타입] [메소드명] ([매개변수들]) {} public void SayMyName() { Console.WriteLine("제 이름은 " + this.name + "입니다."); } } } | 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 | using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Test test1 = new Test(); //기본 생성자가 호출됨 test1.SayMyName(); //메서드 호출 //제 이름은 입니다. Test test2 = new Test("홍길동"); //매개변수가 1개 있는 생성자 test1.SayMyName(); //제 이름은 입니다. test2.SayMyName(); //제 이름은 홍길동 입니다. Test test3 = new Test("홍길동", "임꺽정"); //매개변수가 2개 있는 생성자 test3.SayMyName(); Console.ReadKey(); } } } | cs |
Player가 대장장장이에게 무기를 요청하는 시뮬
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 | using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { //플레이어 생성 Player player = new Player("홍길동"); //대장장이 생성 Blacksmith blackSmith = new Blacksmith("대장장이"); //플레이어는 대장장이에게 무기를 만들어서 달라고함 Weapon weapon = blackSmith.MakeWeapon(0); //플레이어는 만들어진 무기를 소지함 player.SetWeapon(weapon); //플레이어는 소지한 무기를 착용함 player.EquipWeapon(); Console.ReadKey(); } } } | cs |
Player.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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { //플레이어 class Player { //전역 변수 Weapon weapon; Weapon equipedWeapon; string name; //생성자 public Player(string name) { this.name = name; Console.WriteLine("{0}님이 태어났습니다.", this.name); } //메서드 생성 //무기를 받는다. public void SetWeapon(Weapon weapon) { this.weapon = weapon; Console.WriteLine("{0}님이 {1}을(를) 소지 했습니다.", this.name, this.weapon.weaponName); } //무기를 착용한다. public void EquipWeapon() { if (this.weapon == null) { //무기가 없습니다. } else { this.equipedWeapon = this.weapon; string msg = string.Format("{0}님이 {1}을(를) 착용했습니다.", this.name, this.equipedWeapon.weaponName); Console.Write(msg); } } } } | cs |
Blacksmith.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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { //대장장이 class Blacksmith { //전역변수 //대장간 생성 Smithy smithy = new Smithy(); string name; //생성자 public Blacksmith(string name) { this.name = name; Console.WriteLine("{0}님이 태어났습니다.", this.name); } //메서드 생성부 //대장간에서 무기를 만든다 public Weapon MakeWeapon(int weaponId) { Console.WriteLine("대장장이가 대장간에서 무기를 만들기 시작합니다..."); Weapon weapon = smithy.CreateWeapon(weaponId); return weapon; } } } | cs |
Smithy.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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { //대장간 class Smithy { //생성자 public Smithy() { Console.WriteLine("대장간이 생겨났습니다."); } //무기를 생성한다. //생성자 public Weapon CreateWeapon(int weaponId) { Weapon weapon = new Weapon(); switch (weaponId) { case 0: weapon.SetInfo(weaponId, "지팡이"); break; case 1: weapon.SetInfo(weaponId, "단검"); break; default: break; } Console.WriteLine("대장간에서 {0}이(가) 만들어졌습니다.", weapon.weaponName); return weapon; } } } | cs |
Weapon.cs (무기)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { //무기 class Weapon { //전역 변수 int weaponId; public string weaponName; public void SetInfo(int weaponId, string weaponName) { this.weaponId = weaponId; this.weaponName = weaponName; } } } | cs |
Test.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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { class Test { //전역변수 Human jjanggu = new Human("짱구"); Human jjanga = new Human("짱아"); //메서드 생성 public void Run() { Console.WriteLine("Test 클래스의 Run메서드 실행"); Console.WriteLine("-----> " + jjanggu); if (jjanggu == null) { Console.WriteLine("jjanggu의 변수의 값은 null입니다."); } else { Console.WriteLine("jjanggu의 변수의 값은 null이 아닙니다."); } jjanggu.Walk(); jjanga.Walk(); } } } | cs |
Human.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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { //인간 class Human { //이름 string name; //생성자 public Human(string name) { this.name = name; } //걸을수있다. public void Walk() { Console.WriteLine(this.name + " 이(가) 걷고 있습니다."); } } } | cs |
Program.cs (메인)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Console.WriteLine("Program클래스의 Main메서드 실행"); Test test = new Test(); test.Run(); Console.ReadKey(); } } } | cs |
동물친구들을 만들어 보고 울게 해보세요.
Animal.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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { class Animal { string name; string strCry; public Animal(string name, string strCry) { this.name = name; this.strCry = strCry; } public void Cry() { Console.WriteLine(this.name + " , " + this.strCry + "울었습니다."); } } } | cs |
Test.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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { class Test { /* * 클래스를 사용해서 동물친구들을 만들어봅시다 사자, 고양이, 말, 개 동물친구들은 울음소리를 낼수있어요! 사자: '어흥' 고양이: '야옹' 말: '이히히힝~' 개: '멍멍' */ //동물 , 이름, 울음소리, 울수있다 //메서드 생성 public void Run() { Animal lion = new Animal("사자", "어흥"); lion.Cry(); Animal cat = new Animal("고양이", "야옹"); cat.Cry(); Animal holes = new Animal("말", "이히힣~"); holes.Cry(); Animal dog = new Animal("개", "멍멍"); dog.Cry(); } } } | cs |
Test.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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { class Test { /* * 영웅과 몬스터를 생성해주세요. 영웅과 몬스터는 이름, 체력, 공격력 을 가지고 있습니다. 영웅은 몬스터를 공격할수 있어요 몬스터또한 영웅을 공격할수 있어요 영웅이나 몬스터의 체력이 0이 되면 죽어요... [추가 요구 사항] 영웅과 몬스터는 이동 할수 있게 해주세요. 영웅이 영웅도 공격할수 있게 해주세요 */ //메서드 생성 public void Run() { Character hero1 = new Character("마린", 100, 10); Character hero2 = new Character("빠이어벳", 150, 15); Character monster1 = new Character("저글링", 35, 7); Character monster2 = new Character("히드라", 75, 18); hero1.Move(); hero2.Move(); monster1.Move(); monster2.Move(); //마린이 저글링을 공격함 hero1.Attack(monster1); //저글링이 마린을 공격함 monster1.Attack(hero1); //마린이 빠이어뱃 공격함 hero1.Attack(hero2); //쩌글링이 히드라 공격함 monster1.Attack(monster2); } } } | cs |
Character.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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { class Character { //이름, 체력, 공격력 public string name; public int damage; int hp; int maxHp; //생성자 public Character(string name, int hp, int damage) { this.name = name; this.hp = this.maxHp = hp; this.damage = damage; } //공격할수 있어요 public void Attack(Character target) { Console.WriteLine("{0} 이(가) {1} 을(를) 공격 합니다.", this.name, target.name); target.Hit(this.damage); } //공격받다 public void Hit(int damage) { Console.WriteLine("{0} 이(가) 공격을 받았습니다.", this.name); this.hp -= damage; if (this.hp <= 0) { this.Die(); } } //죽어요 public void Die() { Console.WriteLine("{0} 이(가) 죽었습니다..", this.name); } //이동 할수 있게 해주세요 public void Move() { Console.WriteLine("{0} 이(가) 이동합니다.", this.name); } } } |
반응형