경일게임아카데미 실습/12월
2024 - 12 - 02 (열거형과 구조체)
mynote6590
2024. 12. 2. 15:41
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
internal class Program
{
enum Example
{
a, b, c, d = 5, e
}
static void Main(string[] args)
{
Example examp = Example.e;
int inputInt = 0;
// false , examp = Example 첫번째 값
// true
bool check = Enum.TryParse(Console.ReadLine(), out examp);
Console.WriteLine("__________");
Console.WriteLine("check : " + check);
Console.WriteLine("inputInt : " + examp);
Console.WriteLine("__________");
Console.WriteLine(Enum.IsDefined(typeof(SelectMap), 3));
}
}
}
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("이동 할 장소를 설정해주세요");
Console.WriteLine("1. 마을");
Console.WriteLine("2. 사냥터");
Console.WriteLine("3. 상점");
int toDetermine;
int.TryParse(Console.ReadLine(), out toDetermine);
Console.Clear(); //화면을 지워줍니다
switch (toDetermine)
{
case 1:
Console.WriteLine("마을로 이동합니다");
break;
case 2:
Console.WriteLine("사냥터로 이동합니다");
break;
case 3:
Console.WriteLine("상점으로 이동합니다");
break;
default:
Console.WriteLine("1,2,3 어느것도 아니에요");
break;
}
}
}
위 코드는 과거 스위치문을 배울 때, 적었던 코드입니다. 해당 코드는 스위치 문에서 1,2와 같이 숫자로만 적혀있어 가독성이 떨어지는 문제를 가지고 있습니다. 열거형을 활용하여 해당 코드를 더욱 알아보기 쉽게 수정하여주세요.
using System;
class Program
{
enum MapSelect
{
기본,마을,사냥터,상점
}
static void Main(string[] args)
{
Console.WriteLine("이동 할 장소를 설정해주세요");
Console.WriteLine("1. 마을");
Console.WriteLine("2. 사냥터");
Console.WriteLine("3. 상점");
Console.Clear(); //화면을 지워줍니다
MapSelect selectMap1 = MapSelect.기본;
Enum.TryParse(Console.ReadLine(), out selectMap1);
switch (selectMap1)
{
case MapSelect.마을:
Console.WriteLine("마을로 이동합니다");
break;
case MapSelect.사냥터:
Console.WriteLine("사냥터로 이동합니다");
break;
case MapSelect.상점:
Console.WriteLine("상점으로 이동합니다");
break;
default:
Console.WriteLine("1,2,3 어느것도 아니에요");
break;
}
}
}
using System;
class Program
{
enum PlayerStats
{
defalt, idle, run, walk, die = 9
}
static void Main(string[] args)
{
// 이넘 초기값 설정
PlayerStats stats = PlayerStats.defalt;
while (true)
{
stats = Repeat(stats);
switch (stats)
{
case PlayerStats.idle:
Console.WriteLine("1번 상태입니다.");
break;
case PlayerStats.run:
Console.WriteLine("달리는 중입니다");
break;
case PlayerStats.walk:
Console.WriteLine("걷는 중입니다");
break;
//case PlayerStats.die:
// Console.WriteLine("죽었습니다.");
// break;
}
int result = (int)stats;
if (result == 9)
{
break;
}
}
}
static PlayerStats Repeat(PlayerStats stats)
{
while (true)
{
// 문자열을 이넘의 값으로
// 만약 1,2,3을 누르면 1번상태 2번상태 3번상태로 stats에 저장됨
Enum.TryParse(Console.ReadLine(), out stats);
// stats는 1번상태, 2번상태 , 3번상태 로 저장 되어있기 때문에 숫자로 형변환 해야함
if ((int)stats == 1 || (int)stats == 2 || (int)stats == 3 || (int)stats == 9)
{
break;
}
Console.WriteLine("잘못입력하였습니다.");
}
return stats;
}
}
using System;
using System.Runtime.InteropServices;
class Program
{
// Weapon 구조체에 name 타입 선언
// Soilder 구조체에 Weapon구조체를 담는 배열 및 정수형 변수 선언
// 솔져 무기 목록 배열에 Weapon구조체를 3개 담을 수 있게 초기화
// 3가지 무기 기입 (초기에 1번 들고있음)
public struct Weapon
{
public string name;
}
public struct Soilder
{
// {"aa" , "bb" , "cc"}
public Weapon[] weaponArray;
public int index;
}
static void Main(string[] args)
{
Soilder soilder = new Soilder();
soilder.weaponArray = new Weapon[3];
soilder.weaponArray[0].name = "총";
soilder.weaponArray[1].name = "권총";
soilder.weaponArray[2].name = "단검";
ChangeWeapon(soilder);
}
static void ChangeWeapon(Soilder soilder)
{
for(int i=0; i<3; i++)
{
Console.WriteLine(i+1 + "." + soilder.weaponArray[i].name);
}
int inpuBox = 0;
int result = 1;
while (true)
{
Console.WriteLine("어떤 무기로 바꾸시겠습니까");
int.TryParse(Console.ReadLine(), out inpuBox);
if (result == inpuBox)
{
Console.WriteLine("동일한 무기를 가지고 있습니다");
continue;
}
else if (1 <= inpuBox && inpuBox <= 3)
{
break;
}
}
result = inpuBox;
Console.WriteLine(soilder.weaponArray[inpuBox].name +" 로 변경되었습니다.");
}
}