흐름
1. Main
- 플레이어 직업 선택(ChooseClass)
- 플레이어 생성(CreatePlayer)
- 마을 접속(EnterGame) => 2로 이동
2. 마을 접속(EnterGame)
- 선택지
- 1. 필드(EnterField) => 3으로 이동
- 2. 로비로 돌아가기 => 1로 이동
3. 필드(EnterField)
- 몬스터 랜덤 생성(CreateRandomMonster)
- 선택지
- 1. 전투 => 4로 이동
- 2. 일정확률로 도망 => 2로 이동
4. 전투
using System;
using System.Numerics;
namespace Rookiss_CSharp
{
class Program
{
enum ClassType
{
None = 0,
Knight = 1,
Archer = 2,
Mage = 3
}
struct Player
{
public int hp;
public int attack;
}
enum MonsterType
{
None = 0,
Slime = 1,
Orc = 2,
Skeleton = 3
}
struct Monster
{
public int hp;
public int attack;
}
static ClassType ChooseClass()
{
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.hp = 100;
player.attack = 10;
break;
case ClassType.Archer:
player.hp = 75;
player.attack = 12;
break;
case ClassType.Mage:
player.hp = 50;
player.attack = 15;
break;
default:
player.hp = 0;
player.attack = 0;
break;
}
}
static void CreateRandomMonster(out Monster monster)
{
Random Rand = new Random();
int randMonster = Rand.Next(1, 4); // 1~3
switch (randMonster)
{
case (int)MonsterType.Slime:
Console.WriteLine("슬라임이 스폰되었습니다!");
monster.hp = 20;
monster.attack = 2;
break;
case (int)MonsterType.Orc:
Console.WriteLine("오크가 스폰되었습니다!");
monster.hp = 40;
monster.attack = 4;
break;
case (int)MonsterType.Skeleton:
Console.WriteLine("스켈레톤이 스폰되었습니다!");
monster.hp = 30;
monster.attack = 3;
break;
default:
monster.hp = 0;
monster.attack = 0;
break;
}
}
static void Fight(ref Player player, ref Monster monster)
{
while(true)
{
// 플레이어가 몬스터 공격
monster.hp -= player.attack;
if (monster.hp <= 0)
{
Console.WriteLine("승리했습니다!");
Console.WriteLine($"남은체력: {player.hp}");
break;
}
// 몬스터 반격
player.hp -= monster.attack;
if (player.hp <= 0)
{
Console.WriteLine("패배하였습니다!");
break;
}
}
}
static void EnterField(ref Player player)
{
while (true)
{
Console.WriteLine("필드에 접속하였습니다!");
Monster monster;
CreateRandomMonster(out monster);
Console.WriteLine("[1] 전투 모드로 돌입");
Console.WriteLine("[2] 일정 확률로 마을로 도망");
string input = Console.ReadLine();
if (input == "1")
{
Fight(ref player, ref monster);
}
else if (input == "2")
{
// 33%
Random rand = new Random();
int randValue = rand.Next(0, 101); // 0~100
if (randValue <= 33) // 33%
{
Console.WriteLine("도망치는데 성공했습니다.");
break;
}
else
{
Fight(ref player, ref monster);
}
}
}
}
static void EnterGame(Player player)
{
while (true)
{
Console.WriteLine("마을에 접속했습니다!");
Console.WriteLine("[1] 필드로 간다");
Console.WriteLine("[2] 로비로 돌아가기");
string input = Console.ReadLine();
if (input == "1")
{
EnterField(ref player);
}
else if (input == "2")
{
break;
}
}
}
static void Main(string[] args)
{
while (true)
{
ClassType choice = ChooseClass();
if (choice == ClassType.None)
continue;
Player player;
CreatePlayer(choice, out player);
EnterGame(player);
}
}
}
}
섹션 4 퀴즈 | [C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part1: C# 기초 프로그래밍 입문
섹션 4 퀴즈
www.inflearn.com
TextRPG 31강 - 34강
'C# > 기본 문법' 카테고리의 다른 글
| C# 메모리 영역 (0) | 2025.11.01 |
|---|---|
| C# 객체 지향 시작 및 복사와 값 참조 (0) | 2025.11.01 |
| C# 간단한 연습문제 (0) | 2025.10.27 |
| C# 기초 문법 정리 - 함수 (0) | 2025.10.27 |
| C# 기초 문법 정리 — 변수, 진법, 형변환, 상수, 열거형, if문, switch (0) | 2025.10.26 |