전체 코드

using System.ComponentModel;
using System.Numerics;
using System.Threading;

namespace CSharp
{
    internal class Program
    {

        // 작은것을 만들어 보는 연습
        // TextRPG
        enum ClassType
        {
            None = 0,
            Knight = 1,
            Archer = 2,
            Mage = 3
        }
        struct Player
        {
            public int hp;
            public int attack;
            public ClassType type;
        }
        enum MonsterType
        {
            None = 0,
            Slime = 1,   // 슬라임
            Orc = 2,     // 오크
            Skeleton = 3 // 스켈레톤
        }
        struct Monster
        {
            public int hp;
            public int attack;
            public MonsterType type;
        }
        static void CreateRandomMonster(out Monster monster)
        {
            Random rand = new Random();
            int randMonster = rand.Next(1, 4); // 1 ~ 3 중 랜덤 정수

            switch (randMonster)
            {
                case (int)MonsterType.Slime:
                    Console.WriteLine("슬라임이 스폰 되었습니다!");
                    monster.hp = 20;
                    monster.attack = 2;
                    monster.type = MonsterType.Slime;
                    break;
                case (int)MonsterType.Orc:
                    Console.WriteLine("오크가 스폰 되었습니다!");
                    monster.hp = 40;
                    monster.attack = 4;
                    monster.type = MonsterType.Orc;
                    break;
                case (int)MonsterType.Skeleton:
                    Console.WriteLine("스켈레톤이 스폰 되었습니다!");
                    monster.hp = 30;
                    monster.attack = 3;
                    monster.type = MonsterType.Skeleton;
                    break;
                default:
                    monster.hp = 0;
                    monster.attack = 0;
                    monster.type = MonsterType.None;
                    break;
            }
        }
        static void EnterField()
        {

            // 랜덤으로 1 ~ 3 몬스터 중 하나를 리스폰
            // [1] 전투모드 돌입
            // [2] 일정 확률로 마을로 도망
            Console.WriteLine("필드에 접속했습니다.");

            // 랜덤 몬스터 생성
            Monster monster;
            CreateRandomMonster(out monster);

            Console.WriteLine("[1] 전투 모드로 돌입");
            Console.WriteLine("[2] 일정 확률로 마을로 도망");

            string input = Console.ReadLine();

            switch (input)
            {
                case "1":
                    Console.WriteLine($"당신은 {monster.type}와(과) 전투를 시작합니다!");
                    break;
                case "2":
                    Console.WriteLine("도망을 시도합니다...");
                    if (new Random().Next(0, 2) == 0)
                    {
                        Console.WriteLine("도망 성공! 마을로 돌아갑니다.");
                    }
                    else
                    {
                        Console.WriteLine("도망 실패! 전투를 시작합니다.");
                    }
                    break;
            }
        }
        static void EnterGame()
        {
            while (true)
            {
                Console.WriteLine("마을에 접속했습니다.");
                Console.WriteLine("[1] 필드로 나가기");
                Console.WriteLine("[2] 로비로 돌아가기");

                string input = Console.ReadLine();

                switch (input)
                {
                    case "1":
                        EnterField();
                        break;
                    case "2":
                        return; // 로비로 돌아가기 (메인 함수로 이동)
                }
            }
        }
        static ClassType ChooseClass()
        {
            ClassType choice = ClassType.None;

            Console.WriteLine("직업을 선택하세요");
            Console.WriteLine("[1] 기사");
            Console.WriteLine("[2] 궁수");
            Console.WriteLine("[3] 법사");

            string input = Console.ReadLine();

            // 수업
            switch (input)
            {
                case "1": choice = ClassType.Knight; break;
                case "2": choice = ClassType.Archer; break;
                case "3": choice = ClassType.Mage; break;
            }

            return choice;

            //switch (input)
            //{
            //    case "1": return ClassType.Knight;
            //    case "2": return ClassType.Archer;
            //    case "3": return ClassType.Mage;
            //    default:
            //        Console.WriteLine("잘못된 입력입니다. 다시 입력하세요.");
            //        break;
            //}

        }
        static void CreatePlayer(ClassType choice, out Player player)
        {
            player.type = choice;  // 선택한 직업 저장

            // 직업별 능력치 설정
            switch (choice)
            {
                case ClassType.Knight:
                    player.hp = 100;
                    player.attack = 10;
                    break;
                case ClassType.Archer:
                    player.hp = 75;
                    player.attack = 12;
                    break;
                case ClassType.Mage:
                    player.hp = 50;
                    player.attack = 15;
                    break;
                default:
                    player.hp = 0;
                    player.attack = 0;
                    break;
            }
        }

        static void Main(string[] args)
        {
            //// 함수로 묶어서 사용하고 싶다
            //ClassType choice = ClassType.None;
            //while (choice != ClassType.None) // 유효한 입력이 들어올 때까지 반복
            //{
            //    Console.WriteLine("직업을 선택하세요");
            //    Console.WriteLine("[1] 기사");
            //    Console.WriteLine("[2] 궁수");
            //    Console.WriteLine("[3] 법사");

            //    // enum으로 가지고 있을것
            //    string input = Console.ReadLine();

            //    //if(input == "1" || input == "2" || input == "3")
            //    //{

            //    //}

            //    switch(input)
            //    {
            //        case "1": choice = ClassType.Knight; break;
            //        case "2": choice =  ClassType.Archer; break;
            //        case "3": choice = ClassType.Mage; break;
            //        default:
            //            Console.WriteLine("잘못된 입력입니다. 다시 입력하세요.");
            //            break;
            //    }
            //    //if(choice != ClassType.None)
            //    //{
            //    //    break;
            //    //}
            //}
            //static void CreatePlayer(ClassType choice, out int hp, out int attack)
            //{
            //    switch(choice)
            //    {
            //        case ClassType.Knight:
            //            hp = 100;
            //            attack = 100;
            //            break;
            //        case ClassType.Archer:
            //            hp = 75;
            //            attack = 12;
            //            break;
            //        case ClassType.Mage:
            //            hp = 50;
            //            attack = 15;
            //            break;
            //        default:
            //            hp = 0;
            //            attack = 0;
            //            break;
            //    }
            //}
            while (true)
            {
                ClassType choice = ChooseClass();
                if (choice != ClassType.None)
                {
                    break;
                    //int hp;
                    //int attack;
                    //// 캐릭터 생성
                    //// 기사(100/10) 궁수 (75/12) 법사(50/15)
                    //CreatePlayer(choice, out hp, out attack);

                    Player player;
                    CreatePlayer(choice, out player);

                    Console.WriteLine($"당신의 직업: {player.type}");
                    Console.WriteLine($"체력(HP): {player.hp}, 공격력(Attack): {player.attack}");

                    EnterGame();
                }
            }
        }

    }
}

1️⃣ 주요 기능

  1. 플레이어 직업 선택 (기사, 궁수, 법사)
  2. 플레이어 능력치 설정 (체력, 공격력)
  3. 랜덤 몬스터 생성 (슬라임, 오크, 스켈레톤)
  4. 전투 또는 도망 선택 기능
  5. 마을과 필드를 오가는 간단한 RPG 구조

2️⃣ 코드 분석 (상세 설명)

📌 1) enum을 활용한 직업 및 몬스터 타입 관리

        enum ClassType { None = 0, Knight = 1, Archer = 2, Mage = 3 }
        enum MonsterType { None = 0, Slime = 1, Orc = 2, Skeleton = 3 }
  • enum을 사용하여 직업과 몬스터 타입을 숫자로 관리 → 가독성 증가

📌 2) 플레이어 및 몬스터 데이터 관리 (구조체 활용)

        struct Player { public int hp; public int attack; public ClassType type; }
        struct Monster { public int hp; public int attack; public MonsterType type; }
  • struct(구조체)를 사용하여 플레이어와 몬스터의 능력치를 하나의 데이터로 관리

📌 3) 플레이어 및 몬스터 생성 함수

        static void CreatePlayer(ClassType choice, out Player player) { ... }
        static void CreateRandomMonster(out Monster monster) { ... }
  • out 키워드를 활용하여 함수 내부에서 값을 반드시 초기화하도록 강제

📌 4) 전투 또는 도망 선택

        static void EnterField()
        {
            Console.WriteLine("필드에 접속했습니다.");
            Monster monster;
            CreateRandomMonster(out monster);

            Console.WriteLine("[1] 전투 모드로 돌입");
            Console.WriteLine("[2] 일정 확률로 마을로 도망");
            
            string input = Console.ReadLine();
            if (input == "2" && new Random().Next(0, 2) == 0)
                Console.WriteLine("도망 성공! 마을로 돌아갑니다.");
            else
                Console.WriteLine($"당신은 {monster.type}와 전투를 시작합니다!");
        }
  • 도망 확률 50% 설정
  • 전투 모드 추가 가능

profile
李家네_공부방

0개의 댓글