TextRPG 몬스터 생성하기
enum ClassType
{
None = 0,
Knight,
Archer,
Mage,
}
enum MonsterType
{
None = 0,
Slime,
Orc,
Skeleton
}
struct Player
{
public string job;
public int hp;
public int attack;
public int def;
}
struct Monster
{
public int hp;
public int attack;
public int def;
}
static ClassType ChooseClass()
{
Console.WriteLine("이곳은 로비 입니다");
Console.WriteLine("직업을 선택하세요!");
Console.WriteLine("[1] 기사");
Console.WriteLine("[2] 궁수");
Console.WriteLine("[3] 법사");
ClassType choice = ClassType.None;
string input = Console.ReadLine();
switch (input)
{
case "1":
choice = ClassType.Knight;
break;
case "2":
choice = ClassType.Archer;
break;
case "3":
choice = ClassType.Mage;
break;
}
return choice;
}
static void CreatePlayer(ClassType choice, out Player player)
{
switch (choice)
{
case ClassType.Knight:
player.job = "바바리안";
player.hp = 100;
player.attack = 10;
player.def = 10;
break;
case ClassType.Archer:
player.job = "아마존";
player.hp = 75;
player.attack = 12;
player.def = 7;
break;
case ClassType.Mage:
player.job = "소서리스";
player.hp = 50;
player.attack = 15;
player.def = 5;
break;
default:
player.job = "일반인";
player.hp = 0;
player.attack = 0;
player.def = 0;
break;
}
}
static void CreateRandomMonster(out Monster monster)
{
Random rand = new Random();
int randMonster = rand.Next(1, 4);
MonsterType type = (MonsterType)rand.Next(1, 4);
switch (randMonster)
{
case (int)MonsterType.Slime:
Console.WriteLine("\n슬라임이 스폰 되었습니다!");
monster.hp = 20;
monster.attack = 2;
monster.def = 2;
break;
case (int)MonsterType.Orc:
Console.WriteLine("\n오크가 스폰 되었습니다!");
monster.hp = 40;
monster.attack = 4;
monster.def = 3;
break;
case (int)MonsterType.Skeleton:
Console.WriteLine("\n스켈레톤이 스폰 되었습니다!");
monster.hp = 30;
monster.attack = 3;
monster.def = 4;
break;
default:
monster.hp = 0;
monster.attack = 0;
monster.def = 0;
break;
}
}
static void EnterField()
{
Console.Clear();
Console.WriteLine("필드에 접속했습니다!");
Monster monster;
CreateRandomMonster(out monster);
Console.WriteLine();
Console.WriteLine("[1] 전투 모드 돌입");
Console.WriteLine("[2] 일정 확률로 마을로 도망");
string input = Console.ReadLine();
switch (input)
{
case "1":
break;
case "2":
break;
}
}
static void EnterGame(Player player)
{
Console.Clear();
while (true)
{
Console.WriteLine($"{player.job}이 선택 되었습니다.");
Console.WriteLine("마을에 접속했습니다!");
Console.WriteLine("[1] 필드로 간다");
Console.WriteLine("[2] 로비로 돌아가기");
string input = Console.ReadLine();
switch (input)
{
case "1":
EnterField();
break;
case "2":
return;
}
}
}
static void Main(string[] args)
{
while (true)
{
Console.Clear();
ClassType choice = ChooseClass();
if (choice != ClassType.None)
{
Player player;
CreatePlayer(choice, out player);
EnterGame(player);
}
else
Console.Clear();
}
}
}
- 위는 몬스터가 추가된 코드이다. 기존의 Player 를 생성한 방식과 유사하지만
랜덤하게 몬스터를 생성하기 때문에 Random 함수를 사용하여 몬스터를 랜덤하게 만든 부분이 다른점이다.