c# 강의/과제

20200410 - 주말 과제

쪼혀 2020. 4. 12. 23:46

> 캐릭터 정보확인

 

> 아이템 생성

 

> 무기착용

 

> 무기해제

 

 

> 공격

 

> 캐릭터 사망

 


> Item.cs

더보기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200410_2
{
    class Item
    {
        public string name;
        public int damage;


        public Item() { }

        // 아이템 생성
        public Item (string getItemName, int getItemDamage)
        {
            this.name = getItemName;
            this.damage = getItemDamage;
            Console.WriteLine($" [{this.name}]이(가) 생성되었습니다.(Dmage: {this.damage})]");
        }
    }
}

 

> Character.cs

더보기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200410_2
{
    class Character
    {
        public string name;
        public int maxHp;
        public int hp;
        public int damage;
        public Item item;
        public bool checkedEquip;

        public Character() { }


        public Character(string name, int maxHp, int damage) {
            this.name = name;
            this.maxHp = maxHp;
            this.hp = this.maxHp;
            this.damage = damage;
        }

        public void ShowCharacter()
        {
            Console.WriteLine("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
            Console.WriteLine($"▶ 캐릭터 이름: {this.name}\n( { hp } / { maxHp } )\n공격력: { (this.item != null ? this.item.damage+this.damage : this.damage) }");
            Console.Write("▶ 착용 무기:");
            if(this.item !=null){
                Console.WriteLine($"{this.item.name}");
            }else{
                Console.WriteLine($" 없음");
            }
            Console.WriteLine("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
        }

        public void ViewItem()
        {
            Console.WriteLine($"무기: {this.item.name}\n데미지: {this.item.damage}");
        }

        // 아이템 착용
        public void EquipItem(Item item){
            if(checkedEquip){
                Console.WriteLine("이미 착용 중 입니다.");
            }else{
                this.item = item;
                checkedEquip = true;
                Console.WriteLine($"{this.item.name}을(를) 착용 하였습니다.");
            }
        }


        // 아이템 해제
        public void UnEquipItem(Item Item){
            if(checkedEquip){
                Console.WriteLine($"{this.item.name}을(를) 해제 하였습니다.");
                this.item = null;
                checkedEquip = false;
            }else{
                Console.WriteLine("착용중인 아이템이 없습니다.");
            }
        }


        // 공격
        public void Attack(Character target){
           // Console.WriteLine($"target: {target.name} / this.: {this.name}");

            int fullDamage = this.damage;
        
         if(this.item != null && this.checkedEquip){
            fullDamage = this.damage + this.item.damage;
            Console.WriteLine($"[{this.name}]이(가) [{target.name}]을(를) [{this.item.name}]으로 공격했습니다.");
         }else{
            Console.WriteLine($"[{this.name}]이(가) [{target.name}]을(를) [맨손]으로 공격했습니다.");
         }  
            target.Hit(this, fullDamage);
        }

        // 피해
        public void Hit(Character target, int damage){
            //Console.WriteLine($"target: {target.name} / this.: {this.name}");

            // ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ 용자 -> 마왕
            this.hp -= damage;

            if(this.hp <= 0){
                this.hp = 0;
            }

            Console.WriteLine($"{target.name}는 {this.name}에게 {damage}의 피해를 주었습니다. [{this.name}:({this.hp}/{this.maxHp})]");
            Console.WriteLine();
            
            if(this.hp <= 0){
                Die();
                return;
            }

            // ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ 마왕 -> 용자

            target.hp -= this.damage;

            if(target.hp <= 0){
                target.hp = 0;
            }

            Console.WriteLine($"{this.name}에게 {this.damage}의 피해를 받았습니다. [{target.name}:({target.hp}/{target.maxHp})]");
            Console.WriteLine();
            
            if(target.hp <= 0){
                target.Die();
                GameOver();
                return;
            }
        }

        // 죽을 수 있다.
        public void Die(){
        Console.WriteLine($"{this.name}이(가) 사망했습니다.");
        }

        // 게임 종료 - 재시작
        public void GameOver(){
            Console.WriteLine($"♬ GameOver ♬");
            Console.Write($"게임 재시작 - 1번 입력: ");
            var gameOver = Console.ReadLine();
        
            if(gameOver == "1"){
                Console.Clear();
                new App();
            }    
        }

    }
}

 

> App.cs

더보기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200410_2
{
    class App
    {
        Character hero;
        Character monster;
        Item newItem;

        Item item;

        public App()
        {
           // 캐릭터 생성
            hero  = new Character("용자", 80 , 10);
            monster  = new Character("마왕", 100 , 20);
       
            while(true){
            Console.WriteLine();
                Console.WriteLine(" [1.공격] [2.아이템 제작] [3.무기착용] [4.무기해제] [5.캐릭터 정보확인]");
                Console.Write(" 번호입력: ");
                string inputNum = Console.ReadLine();
                playView(inputNum);
            }
            Console.WriteLine();
        }


        public void playView(string inputNum){
            switch (inputNum)
	        {
		        case "1": //공격
                {
                    hero.Attack(monster);
                    break;
                }
                case "2": //아이템제작
                {
                   Console.Write("아이템 이름: ");
                   string createItemName = Console.ReadLine();     
                   Console.Write("아이템 공격력: ");
                   int createItemDamage = int.Parse(Console.ReadLine());     

                   newItem = new Item(createItemName, createItemDamage);

                   break;
                }
                case "3": // 아이템 착용
                {
                    if(newItem != null)
                    {
                        hero.EquipItem(newItem);
                    }else{
                        Console.WriteLine("착용할 아이템이 없습니다.");
                    }
                    break;
                }
                case "4": // 아이템 해제
                {
                    if(newItem != null)
                    {
                        hero.UnEquipItem(newItem);
                    }else{
                        Console.WriteLine("해제할 아이템이 없습니다.");
                    }
                    break;
                }
                case "5":  // 캐릭터 정보 확인
                {
                    hero.ShowCharacter();
                    break;
                }
                default:
                    Console.WriteLine("번호를 다시 입력해주새요");
                    break;
	        }
        }
    }
}