C#

스타듀밸리 캐릭터 클래스

s0002 2023. 1. 5. 23:13

실제 게임과는 다르지만 간단하게 설계하고 구현해보았다

using System;

namespace Homework
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace Homework
{
    class App
    {
        public App()
        {
            Character character = new Character("농부");

            character.Move("왼쪽");
            character.Sprinkle("딸기");
            character.Sprinkle("딸기");
            character.Sprinkle("딸기");
            character.Sprinkle("딸기"); 
            character.Sprinkle("딸기");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace Homework
{
    class Character
    {
        //멤버변수
        string name;
        int energy = 100;
        int maxEnergy = 100;
        int money=500;
        

        //생성자
        public Character(string name)
        {
            this.name = name;
            Console.WriteLine("캐릭터가 생성되었습니다\n이름:{0}\n소지금:{1}\n에너지:{2}",this.name,this.money,this.energy);
            Console.WriteLine();
        }

        //멤버메서드
        public void Move(string direction)
        {
            Console.WriteLine("{0}(으)로 이동합니다",direction);
            Console.WriteLine();
        }
        public void Sprinkle(string crops)
        {
            if (this.energy > 20)
            {
                Console.WriteLine("{0}에 물을 줍니다", crops);
                this.energy = this.energy - 20;
                Console.WriteLine("에너지가 떨어집니다\n에너지:{0}/{1}", this.energy,maxEnergy);
                Console.WriteLine();
            }
            else if(this.energy<=20 && this.energy>0)
            {
                Console.WriteLine("{0}에 물을 줍니다", crops);
                this.energy = 0;
                Console.WriteLine("에너지가 떨어집니다\n에너지:{0}/{1}", this.energy, maxEnergy);
                this.ToFaint();
                Console.WriteLine("기절했습니다");
                Console.WriteLine();
            }
            else
            {
                if (this.state == "기절")
                    Console.WriteLine("기절 상태에서 시도할 수 없습니다");

                else
                    Console.WriteLine("물을 줄 수 없습니다");
            }   
        }
        string state;
        public string ToFaint()
        {
            this.state = "기절";

            return this.state;
        }
    }
}