• 카테고리

    질문 & 답변
  • 세부 분야

    게임 프로그래밍

  • 해결 여부

    미해결

TextRPG EnterField 부분

23.01.30 22:13 작성 조회수 193

0

using System;

//ref : 기존 변수를 메서드에서 수정할 때 사용
//out : 메서드 내에서 생성된 값을 반환할 때 사용
namespace TextRPG_1
{

    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)
        {
            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;
            }
        }

        enum MonsterType
        {
            None = 0,
            Slime = 1,
            Orc = 2,
            Skeleton = 3
        }
        
        struct Monster
        {
            public int hp;
            public int attack;
        }

        static void CreateRandomMonster(out Monster monster)
        {
            Random rand = new Random();
            int type = rand.Next(1, 4);

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

        }

        static void EnterGame(ref Player player)
        {
            while (true)
            {
                Console.WriteLine("마을에 접속했습니다");
                Console.WriteLine("[1] 필드로 간다");
                Console.WriteLine("[2] 로비로 돌아가기");

                string input = Console.ReadLine();

                switch (input)
                {
                    case "1":
                        EnterField(ref player);
                        break;
                    case "2":
                        return;
                }
                
            }
        }

        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")
                {
                    Random rand = new Random();
                    int per = rand.Next(0, 101);
                    if (per > 33)
                    {
                        Console.WriteLine("도망에 실패했습니다. 전투 모드로 돌입합니다.");
                        Fight(ref player, ref monster);
                    }
                    else
                    {
                        Console.WriteLine("도망치는 데 성공했습니다");
                        EnterGame(ref player);
                    }
                }
            }
        }

        static void Fight(ref Player player, ref Monster monster)
        {
            while (true)
            {
                monster.hp -= player.attack;
                if (monster.hp <= 0)
                {
                    Console.WriteLine("승리했습니다!");
                    Console.WriteLine($"hp:{player.hp}");
                    break;
                }

                player.hp -= monster.attack;
                if (player.hp <= 0)
                {
                    Console.WriteLine("패배했습니다!");
                    break;
                }
            }
            
            
        }

        static void Main(string[] args)
        {
            while (true)
            {

                ClassType choice = ChooseClass();
                if(choice != ClassType.None)
                {
                    Player player;
                    CreatePlayer(choice, out player);
                    EnterGame(ref player);
                }
                
            }
        }
    }
}

해당 코드를 실행했을 때 처음 직업 선택 후 마을 접속 -> 로비로 돌아가기를 누르면 정상적으로 직업 선택 창으로 이동할 수 있는데,

한번 필드에 접속하면 [2] 일정확률로 마을로 돌아가기 <를 성공해서 EnterGame()에 다시 접속하는 경우에는 로비를 돌아가기를 눌러도 계속해서 몬스터가 생성이 됩니다.

코드에서 어떤 부분을 고쳐야 해결할 수 있을까요? ㅠㅠ

답변 1

답변을 작성해보세요.

0

oort_cloud98님의 프로필

oort_cloud98

2023.01.31

break; 구문을 추가해보세요