본문 바로가기
Development/Group Study

[Part 1] 섹션 4. TextRPG

by Mobics 2025. 7. 26.

 

목차


    C#과 유니티로 만드는 MMORPG 게임 개발 시리즈

    25.07.26

    Part 1: C# 기초 프로그래밍 입문

     

    섹션 4. TextRPG

     

    TextRPG 직업 고르기

    : 직업을 선택하라는 대사와 함께 직업이 제시되며, 제시된 직업의 번호 외에 다른 숫자를 입력했을 시, 다시 직업을 선택하도록 안내

    --> 코드를 작성할 때, 기능에 따라 함수로 묶어서 코드를 작성

    >> 전체 코드

    namespace First
    {
        class Program
        {
            enum ClassType
            {
                None = 0,
                Knight = 1,
                Archer = 2,
                Mage = 3
            }
    
            static ClassType ChooseClass()
            {
                Console.WriteLine("직업을 선택하세요!");
                Console.WriteLine("[1] 기사");
                Console.WriteLine("[2] 궁수");
                Console.WriteLine("[3] 법사");
    
                ClassType choice = ClassType.None;
                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;
            }
    
            static void Main(string[] args)
            {
                ClassType choice = ClassType.None;
    
                while (choice == ClassType.None)
                {
                    choice = ChooseClass();
                }
            }
        }
    }

     

    --> 실행한 모습

     

    TextRPG 플레이어 생성

    : 선택한 직업에 따라 HP와 공격력 설정

     

    └ 구조체

    : 데이터와 관련 기능을 캡슐화할 수 있는 값 형식이다. --> 구조체 형식의 변수는 할당 시, 매개변수를 함수에 전달할 때, 함수의 결과를 반환할 때 전부 복사된다.

     

     

     

    구조체 형식 - C# reference

    C#의 구조체 형식에 관한 자세한 정보

    learn.microsoft.com

     

    >> 전체 코드

    namespace First
    {
        class Program
        {
            enum ClassType
            {
                None = 0,
                Knight = 1,
                Archer = 2,
                Mage = 3
            }
    
            struct Player
            {
                public int hp;
                public int attack;
            }
    
            static ClassType ChooseClass()
            {
                Console.WriteLine("직업을 선택하세요!");
                Console.WriteLine("[1] 기사");
                Console.WriteLine("[2] 궁수");
                Console.WriteLine("[3] 법사");
    
                ClassType choice = ClassType.None;
                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;
            }
    
            static void CreatePlayer(ClassType choice, out Player player)
            {
                // 기사(100/10) 궁수(75/12) 법사(50/15)
                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)
                {
                    choice = ChooseClass();
    
                    // 캐릭터 생성
                    Player player;
                    CreatePlayer(choice, out player);
    
                    Console.WriteLine($"HP : {player.hp} Attack : {player.attack}");
                }
            }
        }
    }

     

    --> 실행한 모습

     

    TextRPG 몬스터 생성

    : 1~3 중에 랜덤으로 숫자를 뽑아 해당하는 몬스터를 생성하고 생성한 몬스터에 따라 HP와 공격력 설정

     

    >> 전체 코드

    namespace First
    {
        class Program
        {
            enum ClassType
            {
                None = 0,
                Knight = 1,
                Archer = 2,
                Mage = 3
            }
    
            struct Player
            {
                public int hp;
                public int attack;
            }
    
            enum MonsterType
            {
                None = 0,
                Slime = 1,
                Orc = 2,
                Skeleton = 3
            }
    
            struct  Monster
            {
                public int hp;
                public int attack;
            }
    
            static ClassType ChooseClass()
            {
                Console.WriteLine("직업을 선택하세요!");
                Console.WriteLine("[1] 기사");
                Console.WriteLine("[2] 궁수");
                Console.WriteLine("[3] 법사");
    
                ClassType choice = ClassType.None;
                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;
            }
    
            static void CreatePlayer(ClassType choice, out Player player)
            {
                // 기사(100/10) 궁수(75/12) 법사(50/15)
                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 CreateRandomMonster(out Monster monster)
            {
                Random rand = new Random();
                int randMonster = rand.Next(1, 4);
                switch (randMonster)
                {
                    case (int)MonsterType.Slime:
                        Console.WriteLine("슬라임이 스폰되었습니다!");
                        monster.hp = 20;
                        monster.attack = 2;
                        break;
                    case (int)MonsterType.Orc:
                        Console.WriteLine("오크가 스폰되었습니다!");
                        monster.hp = 40;
                        monster.attack = 5;
                        break;
                    case (int)MonsterType.Skeleton:
                        Console.WriteLine("스켈레톤이 스폰되었습니다!");
                        monster.hp = 30;
                        monster.attack = 3;
                        break;
                    default:
                        monster.hp = 0;
                        monster.attack = 0;
                        break;
                }
            }
    
            static void EnterField()
            {
                Console.WriteLine("필드에 입장했습니다!");
    
                // 랜덤으로 몬스터 생성
                Monster monster;
                CreateRandomMonster(out monster);
    
                Console.WriteLine("[1] 전투 모드로 돌입");
                Console.WriteLine("[2] 일정 확률로 마을로 도망");
            }
    
            static void EnterGame()
            {
                while (true)
                {
                    Console.WriteLine("마을에 입장했습니다!");
                    Console.WriteLine("[1] 필드로 간다.");
                    Console.WriteLine("[2] 로비로 돌아가기.");
    
                    string input = Console.ReadLine();
    
                    if (input == "1")
                    {
                        EnterField();
                    }
                    else if (input == "2")
                    {
                        break;
                    }
    
                    //switch (input)
                    //{
                    //    case "1":
                    //        EnterField();
                    //        break;
                    //    case "2":
                    //        return;
                    //}
                }
            }
    
            static void Main(string[] args)
            {
                ClassType choice = ClassType.None;
    
                while (choice == ClassType.None)
                {
                    choice = ChooseClass();
    
                    // 캐릭터 생성
                    Player player;
                    CreatePlayer(choice, out player);
    
                    EnterGame();
                }
            }
        }
    }

     

    TextRPG 전투

    - 전투에 진입하면 플레이어가 먼저 몬스터를 공격하면서 공격을 주고 받는다.

    - 마을로 도망치고 싶다면 33% 확률로 도망칠 수 있도록 하고, 도망에 실패하면 전투에 진입하도록 한다.

     

    >> 전체 코드

    namespace First
    {
        class Program
        {
            enum ClassType
            {
                None = 0,
                Knight = 1,
                Archer = 2,
                Mage = 3
            }
    
            struct Player
            {
                public int hp;
                public int attack;
            }
    
            enum MonsterType
            {
                None = 0,
                Slime = 1,
                Orc = 2,
                Skeleton = 3
            }
    
            struct  Monster
            {
                public int hp;
                public int attack;
            }
    
            static ClassType ChooseClass()
            {
                Console.WriteLine("직업을 선택하세요!");
                Console.WriteLine("[1] 기사");
                Console.WriteLine("[2] 궁수");
                Console.WriteLine("[3] 법사");
    
                ClassType choice = ClassType.None;
                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;
            }
    
            static void CreatePlayer(ClassType choice, out Player player)
            {
                // 기사(100/10) 궁수(75/12) 법사(50/15)
                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 CreateRandomMonster(out Monster monster)
            {
                Random rand = new Random();
                int randMonster = rand.Next(1, 4);
                switch (randMonster)
                {
                    case (int)MonsterType.Slime:
                        Console.WriteLine("슬라임이 스폰되었습니다!");
                        monster.hp = 20;
                        monster.attack = 2;
                        break;
                    case (int)MonsterType.Orc:
                        Console.WriteLine("오크가 스폰되었습니다!");
                        monster.hp = 40;
                        monster.attack = 5;
                        break;
                    case (int)MonsterType.Skeleton:
                        Console.WriteLine("스켈레톤이 스폰되었습니다!");
                        monster.hp = 30;
                        monster.attack = 3;
                        break;
                    default:
                        monster.hp = 0;
                        monster.attack = 0;
                        break;
                }
            }
    
            static void Fight(ref Player player, ref Monster monster)
            {
                while (true)
                {
                    // 플레이어가 몬스터 공격
                    monster.hp -= player.attack;
                    if (monster.hp <= 0)
                    {
                        Console.WriteLine("승리했습니다!");
                        Console.WriteLine($"남은 체력 : {player.hp}");
                        break;
                    }
    
                    // 몬스터가 반격
                    player.hp -= monster.attack;
                    if (player.hp <= 0)
                    {
                        Console.WriteLine("패배했습니다!");
                        break;
                    }
                }
            }
    
            static void EnterField(ref Player player)
            {
                while (true)
                {
                    Console.WriteLine("필드에 입장했습니다!");
    
                    // 랜덤으로 몬스터 생성
                    Monster monster;
                    CreateRandomMonster(out monster);
    
                    Console.WriteLine("[1] 전투 모드로 돌입");
                    Console.WriteLine("[2] 일정 확률로 마을로 도망");
    
                    string input = Console.ReadLine();
                    if (input == "1")
                    {
                        Fight(ref player, ref monster);
                    }
                    else if (input == "2")
                    {
                        // 33%
                        Random rand = new Random();
                        int randValue = rand.Next(0, 101);
                        if (randValue <= 33)
                        {
                            Console.WriteLine("도망치는데 성공했습니다!");
                            break;
                        }
                        else
                        {
                            Console.WriteLine("도망치는데 실패했습니다!");
                            Console.WriteLine("전투에 진입합니다.");
                            Fight(ref player, ref monster);
                        }
                    }
                }
            }
    
            static void EnterGame(ref Player player)
            {
                while (true)
                {
                    Console.WriteLine("마을에 입장했습니다!");
                    Console.WriteLine("[1] 필드로 간다.");
                    Console.WriteLine("[2] 로비로 돌아가기.");
    
                    string input = Console.ReadLine();
    
                    if (input == "1")
                    {
                        EnterField(ref player);
                    }
                    else if (input == "2")
                    {
                        break;
                    }
    
                    //switch (input)
                    //{
                    //    case "1":
                    //        EnterField();
                    //        break;
                    //    case "2":
                    //        return;
                    //}
                }
            }
    
            static void Main(string[] args)
            {
                while (true)
                {
                    ClassType choice = ChooseClass();
                    if (choice == ClassType.None)
                        continue;
    
                    // 캐릭터 생성
                    Player player;
                    CreatePlayer(choice, out player);
    
                    EnterGame(ref player);
                }
            }
        }
    }