게임 알고리즘

3일차 주말과제 (벌처, 드랍쉽 + 역직렬화)

park-gom 2024. 8. 16. 16:40

using System;
using System.Collections.Generic;

namespace Study00
{
    public class Vulture : AttackUnit
    {
        public UnitData data;
        public List<Mine> mines = new List<Mine>();
        public int mineCnt = 0;

        public Vulture(UnitData unitData) 
        {
            //Console.WriteLine("Vulture 클래스 생성자");
            this.name = "벌처";
            this.weapon = "깡통";

            this.data = unitData;
            this.armor = data.armor;
            this.hp = data.maxHp;
            this.damage = data.damage;
            this.maxHp = data.maxHp;

            Console.WriteLine($"현재체력 : {this.hp}/{this.maxHp}");
        }

        public override void Attack(Unit unit)
        {
            Console.WriteLine($"{this.name}이 {unit.name}을 {this.weapon}으로 공격합니다.");
            unit.HitDamege(this.damage);
        }

        public void makeMine(Dictionary<int,GameData> dic)
        {
            if (mineCnt < 3)
            {
                Console.WriteLine($"{this.name}가 마인을 설치합니다. ({2 - mineCnt}개 남음)");
                mines.Add(new Mine(new UnitData(104,dic)));
                mineCnt++;
            }
            else
            {
                Console.WriteLine("마인이 부족합니다.");
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study00
{
    public class UnitData
    {
        public string name;
        public int maxHp;
        public int armor;
        public int damage;
        public int energy;
        public int maxEnergy;

        public UnitData(int id, Dictionary<int, GameData> dic)
        {
            GameData data = dic[id];

            this.name = data.name;
            this.maxHp = data.maxHp;
            this.armor = data.armor;
            this.damage = data.damage;
            this.energy = data.energy;
            this.maxEnergy = data.maxEnergy;
        }
    }

    
}
using System;

namespace Study00
{
    public class Unit
    {
        public string name;
        public int hp;
        public int maxHp;
        public int armor;

        public Unit()
        {
            //Console.WriteLine("Unit 클래스 생성자");
        }

        public void Movd(int x, int y)
        {

        }

        public void HitDamege(int damage)
        {
            
            this.hp -= damage;
            if (this.hp < 0 ) this.hp = 0;
            Console.WriteLine($"{this.name}이 피해({damage})를 받았습니다. ({this.hp}/{this.maxHp})");
        }

        public void IncreasHp(int addHp)
        {
            if (this.hp+addHp >= this.maxHp)
            {
                this.hp = this.maxHp;
            }
            else
            {
                this.hp += addHp;
            }
            
            Console.WriteLine($"{this.name}의 체력이 {addHp} 회복되었습니다. ({this.hp}/{this.maxHp})");
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study00
{
    public class Tank : AttackUnit
    {
        public bool siegeMode = false;

        public Tank(UnitData data)
        {
            this.name = "탱크";
            this.weapon = "대포";

            this.armor = data.armor;
            this.maxHp = data.maxHp;
            this.hp = data.maxHp;
            this.damage = data.damage;
            
            Console.WriteLine($"현재체력 : {this.hp}/{this.maxHp}");
        }

        public void ModeChange()
        {
            if (this.siegeMode)
            {
                this.siegeMode = false;
                this.damage = 30;
                Console.WriteLine($"{this.name}가 일반 모드가 되었습니다.");
            }
            else
            {
                this.siegeMode = true;
                this.damage = 70;
                Console.WriteLine($"{this.name}가 시즈 모드가 되었습니다.");
            }
        }

        public override void Attack(Unit unit)
        {
            Console.WriteLine($"{this.name}이 {unit.name}을 {this.weapon}으로 공격합니다.");
            unit.HitDamege(this.damage);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study00
{
    public class Mine : AttackUnit
    {
        public UnitData data;

        public Mine(UnitData unitData) 
        {

            //Console.WriteLine("Mine 클래스 생성자");
            this.name = "마인";
            this.weapon = "자폭";

            this.data = unitData;
            this.armor = data.armor;
            this.maxHp = data.maxHp;
            this.hp = data.maxHp;
            this.damage = data.damage;
        }

        public override void Attack(Unit unit)
        {
            Console.WriteLine($"{this.name}이 {unit.name}을 {this.weapon}으로 공격합니다.");
            unit.HitDamege(this.damage);
        }
    }
}
using System;

namespace Study00
{
    public class Medic : Unit
    {
        public int energy;
        public int maxEnergy;

        public Medic(UnitData unitData)
        {
            Console.WriteLine("Medic 클래스 생성자");
            this.name = "메딕";
            this.maxHp=unitData.maxHp;
            this.hp = this.maxHp;
            this.armor = unitData.armor;
            this.maxEnergy = unitData.energy;
            this.energy = unitData.energy;
        }

        public void Heal(Unit unit)
        {
            Console.WriteLine($"{this.name}이 {unit.name}을 힐했습니다.");
            bool isSuccess = this.DecreaseEnergy(1);
            if (isSuccess) unit.IncreasHp(1);
        }

        public bool DecreaseEnergy(int val)
        {
            if (this.energy > 0)
            {
                this.energy -= val;
                Console.WriteLine($"{this.name}의 에너지가 {val} 만큼 감소되었습니다. ({this.energy}/{this.maxEnergy})");
                return true;
            }
            else
            {
                Console.WriteLine("에너지가 부족합니다");
                return false;
            }
        }
    }
}
using System;
using System.Globalization;

namespace Study00
{
    public class Marine : AttackUnit
    {
        public UnitData data;

        public Marine(UnitData unitData) 
        {
            Console.WriteLine("Marine 클래스 생성자");
            this.name = "마린";
            this.weapon = "총";

            this.data = unitData;
            this.armor = data.armor;
            this.hp = data.maxHp;
            this.damage = data.damage;
            this.maxHp = data.maxHp;

            Console.WriteLine($"현재체력 : {this.hp}/{this.maxHp}");
        }

        //부모클래스에서 정의된 메서드를 재정의
        public override void Attack(Unit unit)
        {
            Console.WriteLine($"{this.name}이 {unit.name}을 {this.weapon}으로 공격합니다.");
            unit.HitDamege(this.damage);
        }
    }
}
namespace Study00
{
    public class GameData
    {
        public int id;
        public string name;
        public int maxHp;
        public int armor;
        public int damage;
        public int energy;
        public int maxEnergy;
    }
}
using System;

namespace Study00
{
    public class Firebat : AttackUnit
    {
        public UnitData data;

        public Firebat(UnitData unitData)
        {
            Console.WriteLine("Firebat 클래스 생성자");
            this.name = "파이어벳";
            this.weapon = "화염방사기";

            this.data = unitData;
            this.maxHp = data.maxHp;
            this.hp = this.maxHp;
            this.armor = data.armor;
            this.damage = data.damage;

            Console.WriteLine($"현재체력 : {this.hp}/{this.maxHp}");
        }

        public override void Attack(Unit unit)
        {
            base.Attack(unit);
            Console.WriteLine($"{this.name}이 {unit.name}을 {this.weapon}으로 공격합니다.");
            unit.HitDamege(this.damage);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;


namespace Study00
{
    public class DropShip : Unit
    {
        public int onBoradCnt = 0;
        public List<Unit> units = new List<Unit>();

        public DropShip(UnitData data)
        {
            this.name = "드롭쉽";
            this.maxHp = data.maxHp;
            this.hp = this.maxHp;
            this.armor = data.armor;
            Console.WriteLine($"{this.name}생성 ({this.hp}/{this.maxHp})");
        }

        public void onBorad(Unit unit)
        {
            bool isSuccsee = false;
            if (unit.name == "마린" || unit.name == "메딕" || unit.name == "파이어벳")
            {
                if (onBoradCnt < 8)
                {
                    onBoradCnt += 1;
                    units.Add(unit);
                    isSuccsee = true;
                }
            }
            else if (unit.name == "벌처")
            {
                if(onBoradCnt < 7)
                {
                    onBoradCnt += 2;
                    units.Add(unit);
                    isSuccsee = true;
                }
            }
            else if (unit.name == "탱크")
            {
                if(onBoradCnt < 5)
                {
                    onBoradCnt += 4;
                    units.Add(unit);
                    isSuccsee = true;
                }
            }

            if (isSuccsee)
            {
                Console.WriteLine($"{this.name}에 {unit.name}이 탑승했습니다. 남은자리 : ({this.onBoradCnt}/8)");
            }
            else
            {
                Console.WriteLine($"공간이 부족합니다. {unit.name}을 태울수 없습니다.");
            }
        }

        public void DropUnit()
        {
            while (true)
            {
                Unit unit;
                try
                {
                    unit = units.Last();
                }
                catch (Exception e)
                {
                    break;
                }
                Console.WriteLine($"{this.name}에서 {unit.name}이 하차합니다.");
                units.RemoveAt(units.Count - 1);
            }
        }
    }
}
using System;

namespace Study00
{
    public class AttackUnit : Unit
    {
        public int damage;
        public string weapon;

        public AttackUnit() 
        {
            //Console.WriteLine("AttackUnit 클래스 생성자");
        
        }

        public virtual void Attack(Unit unit)
        {
            
        }
    }
}
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;

namespace Study00
{
    public class App
    {
        public App()
        {
            Console.WriteLine("App 클래스 생성자");

            string json = File.ReadAllText("./game_data.json");
            //Console.WriteLine(json);

            GameData[] gameDatas = JsonConvert.DeserializeObject<GameData[]>(json);
            Dictionary<int, GameData> dic = new Dictionary<int, GameData>();

            for (int i = 0; i < gameDatas.Length; i++)
            {
                GameData gameData = gameDatas[i];
                dic.Add(gameData.id, gameData);
            }

            Marine marine = new Marine(new UnitData(100, dic));
            Firebat firebat = new Firebat(new UnitData(101, dic));
            Medic medic = new Medic(new UnitData(102, dic));
            Vulture vulture = new Vulture(new UnitData(103,dic));
            DropShip dropship = new DropShip(new UnitData(104,dic));
            Tank tank = new Tank(new UnitData(105, dic));
            Console.WriteLine();

            marine.Attack(firebat);
            firebat.Attack(marine);
            medic.Heal(firebat);
            Console.WriteLine();

            vulture.makeMine(dic);
            vulture.makeMine(dic);
            vulture.makeMine(dic);
            vulture.makeMine(dic);
            vulture.mines[0].Attack(firebat);
            Console.WriteLine();


            dropship.onBorad(marine);
            dropship.onBorad(tank);
            dropship.onBorad(vulture);
            dropship.onBorad(medic);
            dropship.onBorad(tank);
            dropship.onBorad(marine);
            dropship.onBorad(vulture);
            dropship.onBorad(marine);
            dropship.onBorad(marine);
            Console.WriteLine();

            dropship.DropUnit();
            Console.WriteLine();
        }
    }
}

'게임 알고리즘' 카테고리의 다른 글

3일차 람다식 연습문제2  (0) 2024.08.16
3일차 람다식 연습문제 1  (0) 2024.08.16
엑셀에서 JSON파일 만들기  (0) 2024.08.14
2일차 과제2 (Luker)  (0) 2024.08.13
2일차 과제 (Siege Tank)  (0) 2024.08.13