3월 17일 #열거형 #구조체

sejun-Lee·2025년 3월 17일
enum RockPaperScissor	// enum -> 열거형 
{   // =1 입력하면 알아서 다음 숫자들이 차례대로 바뀜
    Rock=1,		// 1
    Scissor,	//2
    paper		//3
}

enum Equipment		// enum -> 열거형
{	// 0,	1,	  2,	3,	 4
    Head, Body, Foot, Arm, Size
}

static void Main(string[] args)
{
    // 1: 가위 2 : 바위 3 : 보 게임
    RockPaperScissor commend = RockPaperScissor.paper;

    Console.WriteLine("묵찌빠!!!");
    Console.WriteLine("1. 묵, 2. 찌, 3. 빠");

    string input = Console.ReadLine();
    Enum.TryParse(input, out commend);

    //Enum.TryParse(Console.ReadLine(), out commend);   //열거형은 전부 int로 형변한 됨.

    switch (commend)
    {
        case RockPaperScissor.Scissor:
            Console.WriteLine("가위를 냅니다");
            break;
        case RockPaperScissor.Rock:
            Console.WriteLine("바위를 냅니다");
            break;
        case RockPaperScissor.paper:
            Console.WriteLine("보를 냅니다");
            break;
        default:
            Console.WriteLine("잘못 냈습니다");
            break;
    }


    RockPaperScissor commend = (RockPaperScissor)2; // 2를 RockPaperScissor로 형변환 -> Scissor
    int value = (int)RockPaperScissor.paper;    //열거형을 int로 형변환 -> 3

    Console.WriteLine(commend);
    Console.WriteLine(value);


    // 장비 유형에 따라 여러 장비를 저장하는 배열을 만든다
    string[] equipments = new string[(int)Equipment.Size];    //배열의 사이즈 수 지정 , 0부터 시작하기 때문에 딱 맞음(요령)

    // 머리 부분을 사용하고 싶다면
    // 머리 : 0 번에 해당하는 공간에 저장&불러오기 할 필요가 있음
    // 여기서 0 번으로 쓰는 경우 햇갈릴 수 있지만
    // (int)로 형변환을 이용한다면 머리라는 이름으로 명확하며 실수하지 않게 사용이 가능
    // 또한 0부터 시작한다는 열거형의 특징상 배열의 0 ~ 장비유형 갯수 만큼 사용도 가능
    equipments[(int)Equipment.Head] = "철투구";

    Console.WriteLine(equipments[0]);
    

✅ 배경 및 글자 색상 변경

    Console.BackgroundColor = ConsoleColor.DarkBlue;    // 칸의 배경색이 변경됨
    Console.WriteLine("                      ");
    Console.ResetColor();                               // 꼭 리셋해야 함. 계속 바뀜


    Console.ForegroundColor = ConsoleColor.Red;     // 해당 글자색이 변경됨
    Console.WriteLine("글자 색깔 바꾸기");
    Console.ResetColor();                           // 꼭 리셋해야 함. 계속 바뀜

✅ key 입력 열거형

  • 캐릭터가 위 방향 입력시 위로 움직이는 이유
    ConsoleKey key = Console.ReadKey().Key;

    switch (key)
    {
        case ConsoleKey.UpArrow:

            break;
    }

<구조체>


    string skillName = "파이어볼";	// 구조체X 수동 입력
    float coolTime = 2.5f;
    int cost = 10;
    float range = 3.5f;

    Skill fireball;
    fireball.name = "파이어볼";
    fireball.coolTime = 2.5f;
    fireball.cost = 10;
    fireball.range = 3.5f;


    string skillName2 = "강타";
    float coolTime2 = 3f;
    int cost2 = 30;
    float range2 = 10f;

    Skill smash;
    smash.name = "강타";
    smash.coolTime = 3f;
    smash.cost = 10;
    smash.range = 20f;

    Console.WriteLine("사용할 스킬은 {0}", fireball.name);
    Console.WriteLine("해당 스킬을 사용하여 쿨타임 {0} 을 적용합니다", fireball.coolTime);

    Console.WriteLine("사용할 스킬은 {0}", smash.name);
    Console.WriteLine("해당 스킬을 사용하여 쿨타임 {0} 을 적용합니다", smash.coolTime);

struct Skill		// struct -> 구조체
{
    public string name;
    public float coolTime;
    public int cost;
    public float range;
}

enum Type { Nomal, Elite, Boss}

struct Monster		// struct -> 구조체
{
    public string name;
    public int attack;
    public int defense;
    public float spped;
    public string[] items;
    public Type type;
    public string area;
}

enum ItemType		// enum -> 열거형
{
    Equip, Usable, Quest, Material
}

struct Item		// struct -> 구조체
{
    public string name;
    public int weight;
    public ItemType type; 
}

    Skill[] skills = new Skill[4];      // Q skill, W skill, E skill, R skill

    Skill lance;			// 구조체 등록
    lance.name = "창던지기";
    lance.coolTime = 1f;
    lance.cost = 0;
    lance.range = 1f;

    Skill ultimate;
    ultimate.name = "궁극기";
    ultimate.coolTime = 180f;
    ultimate.cost = 200;
    ultimate.range = 10f;

    skills[0] = fireball;
    skills[1] = smash;
    skills[2] = lance;
    skills[3] = ultimate;
    
    
    Monster orc;
    orc.name = "오크";
    orc.attack = 100;
    orc.defense = 50;
    orc.spped = 3.5f;
    orc.type = Type.Nomal;
    orc.items = new string[] { "포션", "방패" };
    orc.area = "늪지";

    while (true)
    {
        Console.Write("사용할 스킬 : ");
        string input = Console.ReadLine();
        int value = int.Parse(input)-1;

        Console.WriteLine("{0} 스킬을 사용합니다!!", skills[value].name);
        Console.WriteLine("마나가 {0} 감소합니다.", skills[value].cost);
        Console.WriteLine("쿨타임을 {0} 초 돌립니다", skills[value].coolTime);
        Console.WriteLine("공격 범위 {0} 을 확인합니다.", skills[value].range);
    }
profile
초보 개발자

0개의 댓글