본문 바로가기
C# 언어/C# 기초 문법

직업 선택

by 후야- 2024. 3. 19.

1) 직업 선택 및 열거형

using System.Numerics;
using System.Security.Cryptography.X509Certificates;

namespace CShop
{
    enum ClassType
    {
        None = 0,
        Warrial = 1,
        Archer = 2,
        Magition = 3,
    }
    class Program
    {

        static void Main(string[] args)
        {
            ClassType choice = ClassType.None;

            while (true)
            {
                Console.WriteLine("******************");
                Console.WriteLine("직업을 골라주세요 !");
                Console.WriteLine("[1] 전사");
                Console.WriteLine("[2] 궁수");
                Console.WriteLine("[3] 법사");

                string input = Console.ReadLine();
                switch (input)
                {
                    case "1":
                        Console.WriteLine("전사를 선택했습니다.");
                        choice = ClassType.Warrial;
                        break;
                    case "2":
                        Console.WriteLine("궁수를 선택했습니다.");
                        choice = ClassType.Archer;
                        break;
                    case "3":
                        Console.WriteLine("법사를 선택했습니다.");
                        choice = ClassType.Magition;
                        break;
                }
                if (choice != ClassType.None)
                    break;
            }

        }

    }
}

 

2) 직업 함수화작업

using System.Numerics;
using System.Security.Cryptography.X509Certificates;

namespace CShop
{    
    class Program
    {
        enum ClassType
        {
            None = 0,
            Warrial = 1,
            Archer = 2,
            Magition = 3,
        }

        static void ChoiceClass(out ClassType choice)
        {
            Console.WriteLine("******************");
            Console.WriteLine("직업을 골라주세요 !");
            Console.WriteLine("[1] 전사");
            Console.WriteLine("[2] 궁수");
            Console.WriteLine("[3] 법사");
            
            string input = Console.ReadLine();
            switch (input)
            {
                case "1":
                    Console.WriteLine("전사를 선택했습니다.");
                    choice = ClassType.Warrial;
                    break;
                case "2":
                    Console.WriteLine("궁수를 선택했습니다.");
                    choice = ClassType.Archer;
                    break;
                case "3":
                    Console.WriteLine("법사를 선택했습니다.");
                    choice = ClassType.Magition;
                    break;
                default:
                    choice = ClassType.None;
                    break;
            }
        }

        static void Main(string[] args)
        {
            ClassType choice = ClassType.None;

            while (true)
            {
                ChoiceClass(out choice);
                if (choice != ClassType.None)
                    break;
            }

        }

    }
}

 

3) 직업 HP, Attack

using System.Numerics;
using System.Security.Cryptography.X509Certificates;

namespace CShop
{
    class Program
    {
        enum ClassType
        {
            None = 0,
            Warrial = 1,
            Archer = 2,
            Magition = 3,
        }

        static void ChoiceClass(out ClassType choice)
        {
            Console.WriteLine("******************");
            Console.WriteLine("직업을 골라주세요 !");
            Console.WriteLine("[1] 전사");
            Console.WriteLine("[2] 궁수");
            Console.WriteLine("[3] 법사");

            string input = Console.ReadLine();
            switch (input)
            {
                case "1":
                    Console.WriteLine("전사를 선택했습니다.");
                    choice = ClassType.Warrial;
                    break;
                case "2":
                    Console.WriteLine("궁수를 선택했습니다.");
                    choice = ClassType.Archer;
                    break;
                case "3":
                    Console.WriteLine("법사를 선택했습니다.");
                    choice = ClassType.Magition;
                    break;
                default:
                    choice = ClassType.None;
                    break;
            }
        }

        static void CreatePlayer(ClassType choice, out int hp, out int attack)
        {
            switch (choice)
            {
                case ClassType.Warrial:
                    hp = 100;
                    attack = 10;
                    break;
                case ClassType.Archer:
                    hp = 75;
                    attack = 7;
                    break;
                case ClassType.Magition:
                    hp = 50;
                    attack = 15;
                    break;
                default:
                    hp = 0;
                    attack = 0;
                    break;
            }
        }

        static void Main(string[] args)
        {
            ClassType choice = ClassType.None;

            while (true)
            {
                ChoiceClass(out choice);
                if (choice != ClassType.None)
                {
                    // 캐릭터 생성
                    int hp;
                    int attack;
                    CreatePlayer(choice , out hp, out attack);

                    Console.WriteLine($"hp: {hp}, 공격력: {attack}");

                    break;
                }
            }

        }

    }
}

 

4) 구조체 사용 / Player 만들기

using System.Numerics;
using System.Security.Cryptography.X509Certificates;

namespace CShop
{
    class Program
    {
        enum ClassType
        {
            None = 0,
            Warrial = 1,
            Archer = 2,
            Magition = 3,
        }

        struct Player
        {
            public int hp;
            public int attack;
        }

        static void ChoiceClass(out ClassType choice)
        {
            Console.WriteLine("******************");
            Console.WriteLine("직업을 골라주세요 !");
            Console.WriteLine("[1] 전사");
            Console.WriteLine("[2] 궁수");
            Console.WriteLine("[3] 법사");

            string input = Console.ReadLine();
            switch (input)
            {
                case "1":
                    Console.WriteLine("전사를 선택했습니다.");
                    choice = ClassType.Warrial;
                    break;
                case "2":
                    Console.WriteLine("궁수를 선택했습니다.");
                    choice = ClassType.Archer;
                    break;
                case "3":
                    Console.WriteLine("법사를 선택했습니다.");
                    choice = ClassType.Magition;
                    break;
                default:
                    choice = ClassType.None;
                    break;
            }
        }

        static void CreatePlayer(ClassType choice, out Player player)
        {
            switch (choice)
            {
                case ClassType.Warrial:
                    player.hp = 100;
                    player.attack = 10;
                    break;
                case ClassType.Archer:
                    player.hp = 75;
                    player.attack = 7;
                    break;
                case ClassType.Magition:
                    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 (true)
            {
                ChoiceClass(out choice);
                if (choice != ClassType.None)
                {
                    // 캐릭터 생성
                    Player player;
                    CreatePlayer(choice , out player);

                    Console.WriteLine($"hp: {player.hp}, 공격력: {player.attack}");

                    break;
                }
            }

        }

    }
}

 

5) 게임 접속

using System.Numerics;
using System.Security.Cryptography.X509Certificates;

namespace CShop
{
    class Program
    {
        enum ClassType
        {
            None = 0,
            Warrial = 1,
            Archer = 2,
            Magition = 3,
        }

        struct Player
        {
            public int hp;
            public int attack;
        }

        static void ChoiceClass(out ClassType choice)
        {
            Console.WriteLine("******************");
            Console.WriteLine("직업을 골라주세요 !");
            Console.WriteLine("[1] 전사");
            Console.WriteLine("[2] 궁수");
            Console.WriteLine("[3] 법사");

            string input = Console.ReadLine();
            switch (input)
            {
                case "1":
                    Console.WriteLine("전사를 선택했습니다.");
                    choice = ClassType.Warrial;
                    break;
                case "2":
                    Console.WriteLine("궁수를 선택했습니다.");
                    choice = ClassType.Archer;
                    break;
                case "3":
                    Console.WriteLine("법사를 선택했습니다.");
                    choice = ClassType.Magition;
                    break;
                default:
                    choice = ClassType.None;
                    break;
            }
        }

        static void CreatePlayer(ClassType choice, out Player player)
        {
            switch (choice)
            {
                case ClassType.Warrial:
                    player.hp = 100;
                    player.attack = 10;
                    break;
                case ClassType.Archer:
                    player.hp = 75;
                    player.attack = 7;
                    break;
                case ClassType.Magition:
                    player.hp = 50;
                    player.attack = 15;
                    break;
                default:
                    player.hp = 0;
                    player.attack = 0;
                    break;
            }
        }
        
        static void EnterGame()
        {
            while (true)
            {
                Console.WriteLine("**********************");
                Console.WriteLine("마을에 접속했습니다.");
                Console.WriteLine("[1] 필드로 이동");
                Console.WriteLine("[2] 돌아가기");

                string input_first = Console.ReadLine();
                switch (input_first)
                {
                    case "1":
                        EnterFild();
                        break;
                    case "2":
                        return;
                }
            }
        }
        

        static void Main(string[] args)
        {
            ClassType choice = ClassType.None;

            while (true)
            {
                ChoiceClass(out choice);
                if (choice != ClassType.None)
                {
                    // 캐릭터 생성
                    Player player;
                    CreatePlayer(choice , out player);

                    Console.WriteLine($"hp: {player.hp}, 공격력: {player.attack}");

                    EnterGame();
                    break;
                }
            }

        }

    }
}

 

6) 완성본

using System.Numerics;
using System.Security.Cryptography.X509Certificates;

namespace CShop
{
    class Program
    {
        enum ClassType
        {
            None = 0,
            Warrial = 1,
            Archer = 2,
            Magition = 3,
        }

        struct Player
        {
            public int hp;
            public int attack;
        }

        enum MonsterType
        {
            None = 0,
            Slime = 1,
            Skul = 2,
            Oak = 3,
        }

        struct Monster
        {
            public int hp;
            public int attack;
        }

        static void ChoiceClass(out ClassType choice)
        {
            Console.WriteLine("******************");
            Console.WriteLine("직업을 골라주세요 !");
            Console.WriteLine("[1] 전사");
            Console.WriteLine("[2] 궁수");
            Console.WriteLine("[3] 법사");

            string input = Console.ReadLine();
            switch (input)
            {
                case "1":
                    Console.WriteLine("전사를 선택했습니다.");
                    choice = ClassType.Warrial;
                    break;
                case "2":
                    Console.WriteLine("궁수를 선택했습니다.");
                    choice = ClassType.Archer;
                    break;
                case "3":
                    Console.WriteLine("법사를 선택했습니다.");
                    choice = ClassType.Magition;
                    break;
                default:
                    choice = ClassType.None;
                    break;
            }
        }

        static void CreatePlayer(ClassType choice, out Player player)
        {
            switch (choice)
            {
                case ClassType.Warrial:
                    player.hp = 100;
                    player.attack = 10;
                    break;
                case ClassType.Archer:
                    player.hp = 75;
                    player.attack = 7;
                    break;
                case ClassType.Magition:
                    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 randomMonster = rand.Next(1, 4);
            switch (randomMonster)
            {
                case (int)MonsterType.Slime:
                    Console.WriteLine("슬라임이 소환되었습니다.");
                    monster.hp = 30;
                    monster.attack = 2;
                    break;
                case (int)MonsterType.Skul:
                    Console.WriteLine("스컬이 소환되었습니다.");
                    monster.hp = 40;
                    monster.attack = 3;
                    break;
                case (int)MonsterType.Oak:
                    Console.WriteLine("오크가 소환되었습니다.");
                    monster.hp = 50;
                    monster.attack = 5;
                    break;
                default:
                    monster.hp = 0;
                    monster.attack = 0;
                    break;
            }
        }

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

                string input_first = Console.ReadLine();
                switch (input_first)
                {
                    case "1":
                        EnterFild(ref player);
                        break;
                    case "2":
                        return;
                }
            }
        }

        static void EnterFild(ref Player player)
        {
            while (true)
            {
                Console.WriteLine("필드로 이동했습니다.");
                // 몬스터 생성
                Monster monster;
                CreateRandomMonster(out monster);
                Console.WriteLine("[1] 싸운다.");
                Console.WriteLine("[2] 일정확률로 도망간다.");

                string input_Two = Console.ReadLine();
                switch (input_Two)
                {
                    case "1":
                        Fight(ref monster, ref player);
                        break;
                    case "2":
                        // 일정확률로 도망간다.
                        Random rand = new Random();
                        int randBack = rand.Next(1, 101);
                        if (randBack <= 33)
                        {
                            Console.WriteLine("도망 성공");
                            return;
                        }
                        else
                        {
                            Console.WriteLine("도망 실패");
                            Fight(ref monster, ref player);
                        }
                        break;
                }                
            }
        }

        static void Fight(ref Monster monster, ref Player player)
        {
            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)
        {
            ClassType choice = ClassType.None;

            while (true)
            {
                ChoiceClass(out choice);
                if (choice != ClassType.None)
                {
                    // 캐릭터 생성
                    Player player;
                    CreatePlayer(choice, out player);
                    Console.WriteLine($"hp: {player.hp}, 공격력: {player.attack}");

                    EnterGame(ref player);
                }
            }

        }

    }
}

 

 

7) 수정본

namespace ClassPractice
{
    internal class Program
    {
        // 클래스 열거형
        enum ClassType
        {
            None = 0,
            Warriar = 1,
            Archar = 2,
            Magition = 3,
        }

        // 플레이어 생성 구조체
        struct Player
        {
            public int Hp;
            public int Attack;
        }

        // 몬스터 열거형
        enum MonsterType
        {
            None = 0,
            Slime = 1,
            Oak = 2,
            Skull = 3,
        }

        // 몬스터 구조체
        struct Monster
        {
            public int Hp;
            public int Attack;
        }

        // 직업 함수화  (인자가 하나밖에 없을 때는 반환타임을넘기는게 좋다!)
        static ClassType ChoiceClass()
        {

            Console.WriteLine("----------------------------------------------");
            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.Warriar;
                    Console.WriteLine("기사를 선택했습니다.");
                    break;
                case "2":
                    choice = ClassType.Archar;
                    Console.WriteLine("궁수를 선택했습니다.");
                    break;
                case "3":
                    choice = ClassType.Magition;
                    Console.WriteLine("법사를 선택했습니다.");
                    break;
            }
            return choice;
        }

        // 플레이어 능력치
        static void CreatePlayer(ClassType choice, out Player player)
        {
            // 기사 (100/10) 궁수 (75/12) 법사(50/15)
            switch (choice)
            {
                case ClassType.Warriar:
                    player.Hp = 100;
                    player.Attack = 10;
                    break;
                case ClassType.Archar:
                    player.Hp = 75;
                    player.Attack = 12;
                    break;
                case ClassType.Magition:
                    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);  // 1 ~ 3

            switch (randMonster)
            {
                case (int)MonsterType.Slime:
                    Console.WriteLine("[슬라임]이 스폰되었습니다.");
                    monster.Hp = 20;
                    monster.Attack = 3;
                    break;
                case (int)MonsterType.Oak:
                    Console.WriteLine("[오크]가 스폰되었습니다.");
                    monster.Hp = 40;
                    monster.Attack = 5;
                    break;
                case (int)MonsterType.Skull:
                    Console.WriteLine("[해골]이 스폰되었습니다.");
                    monster.Hp = 30;
                    monster.Attack = 4;
                    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 EnterGame(ref Player player)
        {
            while (true)
            {
                Console.WriteLine("마을에 입장했습니다.");
                Console.WriteLine("[1] 필드로 이동하기");
                Console.WriteLine("[2] 로비로 돌아가기");

                string input = Console.ReadLine();
                // swhich 가 아닌 if 인 이유 break 로 바로 빠져나가기 위해서
                if (input == "1")
                {
                    // 필드 이동
                    EnterFild(ref player);
                }
                else if (input == "2")
                {
                    // 로비로 돌아가기
                    break;
                }
            }

        }

        // 필드 이동
        static void EnterFild(ref Player player)
        {
            while (true)
            {
                Console.WriteLine("----------------------------------------------");
                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 randValue = rand.Next(0, 101);   // 1 ~ 100
                    if (randValue <= 33)
                    {
                        Console.WriteLine("도망치는데 성공했습니다.");
                        break;
                    }
                    else
                    {
                        Fight(ref player, ref monster);
                    }
                }

            }

        }

        static void Main(string[] args)
        {
            while (true)
            {
                ClassType choice = ChoiceClass();
                if (choice != ClassType.None)
                {
                    // 캐릭터 생성
                    Player player;

                    CreatePlayer(choice, out player);

                    Console.WriteLine($"체력 :{player.Hp}, 공격력 :{player.Attack} ");
                    Console.WriteLine("----------------------------------------------");

                    // 게임 접속
                    EnterGame(ref player);
                }
            }
        }
    }
}

 

'C# 언어 > C# 기초 문법' 카테고리의 다른 글

[C#] 객체지향 직업선택  (0) 2024.03.30
[C#] 객체지향  (0) 2024.03.22
[C#] 함수  (0) 2024.03.19
[C#] 열거형  (0) 2024.03.19
[C#] 반복문 / break / continue  (0) 2024.03.19