게임 알고리즘

3일차 람다식 연습문제6

park-gom 2024. 8. 16. 17:29

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study06
{
    public class App
    {
        //생성자 
        public App()
        {
            Console.WriteLine("App클래스 생성자 호출됨");

            Item item = new Item();

            Enchant(item, 50, (success) => {    //람다식의 매개변수 타입은 bool
                if (success)
                {
                    Console.WriteLine("강화 성공 이펙트를 보여 준다.");
                }
                else
                {
                    Console.WriteLine("강화 실패 이펙트를 보여 준다.");
                }
            });
        }

        public void Enchant(Item item, int probability, Action<bool> action)
        {

            Random rand = new Random();
            int dice = rand.Next(0, 100);

            if (probability > dice)
            {
                Console.WriteLine("강화 성공!");
                action(true);
            }
            else
            {
                Console.WriteLine("강화 실패!");
                action(false);
            }
        }
    }
}