C# 문법 종합반 개인 과제 / 던전 게임 만들기 / 상점 구현, 중복 속성 적용 시 거부 처리

2023. 8. 22. 16:48카테고리 없음

1. 상점 구현 - (구매/판매, 가능한 목록 나열)

 

1) 내가 기본적으로 가진 아이템 외에 상점에도 아이템 리스트를 세팅했다.

        static List<Item> shopInventory = new List<Item>
        {
            new Item("강철 갑옷", "방어", 8, 300),    // 상점 아이템
            new Item("대검", "공격", 5, 250),
            new Item("철 철퇴", "공격", 6, 280),
            new Item("은 목걸이", "체력", 15, 150),
            new Item("동 목걸이", "체력", 5, 100)

        };

2) 상점에 들어가면 구매 목록 / 판매 목록이 뜨고, 

1을 눌러 Buy, 2를 눌러 Sell 할 수 있도록 구현했다.

        static void ShopSetting() // 상점
        {

            Console.Clear();  // 콘솔화면 클리어
            Console.ForegroundColor = ConsoleColor.Red;

            Console.WriteLine("상점");
            Console.WriteLine("구매 혹은 판매 할수 있는 아이템의 목록입니다.");
            Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Cyan;

            Console.WriteLine("[구매 가능한 아이템 목록]");
            int itemIndex = 1;
            foreach (var item in shopInventory) 
            {
                Console.WriteLine($"{itemIndex}. {item.Name} | {item.Type} +{item.StatBonus} | 가격: {item.Price} G");
                itemIndex++;  // 아이템을 순회하면서 리스트를 출력.
            }
            Console.WriteLine();

            Console.WriteLine("[판매 가능한 아이템 목록]");
            itemIndex = 1;
            foreach (var item in player.Inventory)
            {
                string equippedText = item.IsEquipped ? "[장착중] " : ""; 
                Console.WriteLine($"{itemIndex}. {item.Name} | {item.Type} +{item.StatBonus} | 가격: {item.Price} G {equippedText}");
                itemIndex++;  // 아이템을 순회하면서 리스트를 출력.
            }
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.WriteLine("원하시는 행동을 입력해주세요");
            Console.WriteLine("1. 구매");
            Console.WriteLine("2. 판매");
            Console.WriteLine("0. 나가기");

            int input = CheckValidInput(0, 2);

            switch (input)
            {
                case 0:
                    DisplayGameIntro();  // 0을 입력하면 게임 인트로를 다시 보여주기
                    break;

                case 1:
                    BuyItem();
                    break;

                case 2:
                    SellItem();
                    break;

            }
        }

3) 구매/판매는 Buyitem / Sellitem 으로 처리했다.

        static void BuyItem()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.WriteLine("구매할 아이템의 번호를 입력해주세요");
            Console.WriteLine("(0. 나가기)");

            int input = CheckValidInput(0, shopInventory.Count);
            if (input == 0)
            {
                ShopSetting(); // 0을 입력하면 인벤토리 화면으로 돌아갑니다.
                return; // 함수를 종료합니다.
            }

            Item selected = shopInventory[input - 1];

            if (player.Gold >= selected.Price)
            {
                player.Gold -= selected.Price;
                player.Inventory.Add(selected);  // 구매한 아이템을 플레이어의 인벤토리에 추가합니다
                shopInventory.Remove(selected);  // 상점 인벤토리에서 아이템 제거
                Console.WriteLine($"{selected.Name}을(를) 구매하였습니다.");
            }
            else
            {
                Console.WriteLine("소지금이 부족합니다.");
            }

            Console.ReadLine();
            ShopSetting();

        }
        
                {
            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.WriteLine("판매할 아이템의 번호를 입력해주세요");
            Console.WriteLine("(0. 나가기)");

            int input = CheckValidInput(0, player.Inventory.Count);
            if (input == 0)
            {
                ShopSetting(); // 0을 입력하면 인벤토리 화면으로 돌아갑니다.
                return; // 함수를 종료합니다.
            }

            Item selected = player.Inventory[input - 1];

            // 아직 장착되지 않은 경우
            if (!selected.IsEquipped)  // 장착 중인 아이템은 판매하지 않도록 확인하기
            {
                player.Gold += selected.Price;  // 아이템 판매로 골드 획득
                player.Inventory.Remove(selected);  // 아이템을 플레이어의 인벤토리에서 제거
                shopInventory.Add(selected);  // 아이템을 상점의 인벤토리에 추가
                Console.WriteLine($"{selected.Name}을(를) 판매하였습니다.");
            }
            else
            {
                Console.WriteLine($"{selected.Name}은(는) 장착 중입니다. 먼저 장착을 해제해주세요.");
            }

            Console.ReadLine();
            ShopSetting();
        }

 

2. 속성 별 한 아이템만 착용 가능

무기 / 방어 / 체력 으로 3가지 속성의 아이템들이 있는데, 갑옷을 장착하고 또 갑옷을 입지 못하도록 설정했다.

(Equip 함수에 처리)

        public void Equip(Character player)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;

            if (Type == "공격" && player.Inventory.Any(i => i.IsEquipped && i.Type == "공격"))
            {
                Console.WriteLine("공격 아이템은 이미 착용중입니다!");
            }
            else if (Type == "방어" && player.Inventory.Any(i => i.IsEquipped && i.Type == "방어"))
            {
                Console.WriteLine("방어 아이템은 이미 착용중입니다!");
            }
            else if (Type == "체력" && player.Inventory.Any(i => i.IsEquipped && i.Type == "체력"))
            {
                Console.WriteLine("체력 아이템은 이미 착용중입니다!");
            }
            else
            {
                if (Type == "공격")
                {
                    player.Atk += StatBonus;
                }
                else if (Type == "방어")
                {
                    player.Def += StatBonus;
                }
                else if (Type == "체력")
                {
                    player.Hp += StatBonus;
                }

                IsEquipped = true;
                Console.WriteLine($"{Name}을(를) 장착하였습니다.");
            }
        }

        public void Unequip(Character player)
        {
            if (Type == "공격")
            {
                player.Atk -= StatBonus;
            }
            else if (Type == "방어")
            {
                player.Def -= StatBonus;
            }
            else if (Type == "체력")
            {
                player.Hp -= StatBonus;
            }

            IsEquipped = false; // 아이템 장착 상태 변경

        }
    }