게임 알고리즘

2일차 과일 관리(Hard)

park-gom 2024. 8. 13. 14:40

namespace Study00
{
    internal class Program
    {
        static void Main(string[] args)
        {
            new App();
        }
    }
}
using System;

namespace Study00
{
    public class App
    {

        //생성자
        public App() 
        {
            Store store = new Store();
            Fruit apple = new Fruit(Fruit.FruitType.apple, 1500);
            Fruit orange = new Fruit(Fruit.FruitType.orange, 3000);
            Fruit banana = new Fruit(Fruit.FruitType.banana, 5000);

            store.AddFruit(apple);
            store.AddFruit(apple);
            store.AddFruit(orange);
            store.AddFruit(banana);
            store.AddFruit(banana);
            store.AddFruit(banana);

            store.SumPrice();

            int cnt = store.FindFruit(Fruit.FruitType.banana);
            Console.WriteLine($"바나나의 갯수 : {cnt}");

            store.ComparePrice(2000);

            Console.WriteLine("바나나 두개 제거");
            store.Remove(Fruit.FruitType.banana, 2);
        }
    }
}
using System;
using System.Diagnostics;
using static Study00.Fruit;

namespace Study00
{
    public class Store
    {
        Fruit[] cage;
        private int idx;
        

        //생성자
        public Store() 
        {
            Console.WriteLine("가게가 생성되었습니다.");
            cage = new Fruit[10];
        }

        //과일을 추가하는 메서드
        public void AddFruit(Fruit fruit)
        {
            if (idx < 10)
            {
                cage[idx++] = fruit;
                Console.WriteLine("과일을 저장했습니다.");
            }
            else
            {
                Console.WriteLine("공간이 부족합니다.");
            }
        }

        //모든 과일의 가격을 합산하는 메서드
        public void SumPrice()
        {
            int total = 0;
            for (int i = 0; i < cage.Length; i++)
            {
                Fruit fruit = cage[i];
                if (fruit != null)
                {
                    total += fruit.price;
                }
            }
            Console.WriteLine($"금액 합산 : {total}");
        }

        //타입으로 과일의 갯수를 반환하는 메서드
        public int FindFruit(Fruit.FruitType fruitType)
        {
            int cnt = 0;
            for (int i = 0; i < idx; i++)
            {
                Fruit fruit = cage[i];
                if (fruit.fruitType == fruitType)
                {
                    cnt++;
                }
            }
            return cnt;
        }

        //매개변수로 받은 가격보다 비싼 과일을 찾는 메서드
        public void ComparePrice(int price)
        {
            int[] arr = new int[3];
            for (int i = 0; i < idx; i++)
            {
                Fruit fruit = cage[i];
                if (fruit.price > price)
                {
                    if(fruit.fruitType == FruitType.apple)
                    {
                        arr[0]++;
                    }
                    else if(fruit.fruitType == FruitType.orange)
                    {
                        arr[1]++;
                    }
                    else
                    {
                        arr[2]++;
                    }
                }
            }
            Console.WriteLine($"{price}원보다 비싼 과일 목록");
            if (arr[0] != 0)
            {
                Console.WriteLine($"apple x{arr[0]}");
            }
            if (arr[1] != 0)
            {
                Console.WriteLine($"orange x{arr[1]}");
            }
            if (arr[2] != 0)
            {
                Console.WriteLine($"banana x{arr[2]}");
            }
        }

        public void Remove(FruitType fruitType, int n)
        {
            int cnt = 0;

            //먼저 과일의 갯수를 센다
            for (int i = 0; i < idx; i++)
            {
                Fruit fruit = cage[i];
                if (fruit.fruitType == fruitType)
                {
                    cnt++;
                }
            }

            //없애려는 수보다 보유수가 적으면 취소
            if (cnt < n)
            {
                Console.WriteLine("과일 수가 부족합니다.");
            }

            for(cnt = 0; cnt < n;cnt++)
            {
                for (int i = 0; i < idx; i++)
                {
                    Fruit fruit = cage[i];
                    if (fruit.fruitType == fruitType)
                    {
                        cage[i] = null;

                        // 삭제 후 빈공간을 당긴다
                        for (int j = i; j < idx; j++)
                        {
                            cage[i] = cage[i+1];
                        }
                        break;
                    }
                }
                idx--;
            }

            

            int[] arr = new int[3];
            int[] arr2 = new int[3];
            for (int i = 0; i < idx; i++)
            {
                Fruit fruit = cage[i];
                if (fruit.fruitType == FruitType.apple)
                {
                    arr[0]++;
                    arr2[0] = fruit.price;
                }
                else if (fruit.fruitType == FruitType.orange)
                {
                    arr[1]++;
                    arr2[1] = fruit.price;
                }
                else
                {
                    arr[2]++;
                    arr2[2] = fruit.price;
                }
            }

            Console.WriteLine($"남은 과일 목록");
            if (arr[0] != 0)
            {
                Console.WriteLine($"apple x{arr[0]} ({arr2[0]})");
            }
            if (arr[1] != 0)
            {
                Console.WriteLine($"orange x{arr[1]} ({arr2[1]})");
            }
            if (arr[2] != 0)
            {
                Console.WriteLine($"banana x{arr[2]} ({arr2[2]})");
            }
        }



    }
}
using System;

namespace Study00
{
    public class Fruit
    {
        public enum FruitType
        {
            apple,
            orange,
            banana
        }

        public FruitType fruitType;
        public int price;

        //생성자
        public Fruit(FruitType fruitType, int price)
        {
            this.fruitType = fruitType;
            this.price = price;
            Console.WriteLine($"과일({fruitType})이 생성됬습니다.");
        }
    }
}

'게임 알고리즘' 카테고리의 다른 글

2일차 과제2 (Luker)  (0) 2024.08.13
2일차 과제 (Siege Tank)  (0) 2024.08.13
2일차 과일 관리(Normal)  (0) 2024.08.13
2일차 과일 관리(Easy)  (0) 2024.08.13
1일차 과제2 (player vs monster)  (0) 2024.08.12