C# 공부를 좀 재밌게 할 수 있는 방법 없나 찾아보다가 문득 머드게임이 떠올랐다.
머드게임을 즐겨 하던 세대는 아니지만, 머드게임을 구현하면서 C#을 공부하면 참 재밌을 것 같았다.
이름은 전사의 모험 RPG로 간단하게 정해봤다.
그냥 무난하게 턴제 형식의 전투를 만들 생각이다.
using C_Study;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
class MainClass
{
public static void Main()
{
GameCore gameCore = new GameCore();
gameCore.Start();
gameCore.Update();
gameCore.End();
}
}
GameCore라는 클래스 내부에서 게임을 전체적으로 관리할 것이다.
Main함수에선 인스턴스의 Start, Update, End를 호출하도록 하였다.
Start에선 게임이 시작되기 전에 설정해야 할 것들을 세팅하는 함수이고 Update는 게임이 진행되는 함수이다.
End는 게임이 종료될 때 해야 할 일을 하는 함수이다.
아래는 GameCore 클래스 내부 코드이다.
using C_Study;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace C_Study
{
public enum LevelType
{
Start,
Play,
}
public class GameCore
{
public void Start()
{
_player = new Player();
_levelType = LevelType.Start;
}
public void Update()
{
while(_player != null)
{
switch (_levelType)
{
case LevelType.Start:
StartLevelUpdate();
break;
case LevelType.Play:
PlayLevelUpdate();
break;
}
}
}
public void End()
{
}
private void StartLevelUpdate()
{
while (_levelType == LevelType.Start)
{
Console.Clear();
Console.WriteLine("*************************************************");
Console.WriteLine("*************************************************");
Console.Write("*****************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("전사의 모험 RPG");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("*****************");
Console.Write("******************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("제작자 오의현");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("******************");
Console.WriteLine("*************************************************");
Console.WriteLine("*************************************************");
Console.WriteLine();
Console.WriteLine("모험을 시작하시겠습니까?");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("1 : YES");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("2 : NO");
Console.ForegroundColor = ConsoleColor.White;
string input = Console.ReadLine();
if (DevFunctions.IsNumeric(input) == false)
{
continue;
}
if(input.Length > 1)
{
continue;
}
int toInt = int.Parse(input);
switch (toInt)
{
case 1:
_levelType = LevelType.Play;
break;
case 2:
Environment.Exit(0);
break;
}
}
}
private void PlayLevelUpdate()
{
while (_levelType == LevelType.Play)
{
Console.Clear();
Console.WriteLine("**************************************************");
Console.WriteLine("*****시작*****");
Console.WriteLine("**************************************************");
Console.WriteLine();
string input = Console.ReadLine();
if (DevFunctions.IsNumeric(input) == false)
{
continue;
}
int toInt = int.Parse(input);
}
}
private Player _player;
private LevelType _levelType;
}
}
Start에선 일단 플레이어 생성과 최초 레벨 설정만 해주었다.
Update에선 Level에 따라 다른 함수가 실행되도록 하였다.
중간에 DevFunctions.IsNumeric()이라는 함수가 보이는데 이는 입력받은 문자열이 정수로만 이루어져있는지 판단하는 함수이다.
내부 코드는 아래와 같다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_Study
{
public class DevFunctions
{
public static bool IsNumeric(string str)
{
if (string.IsNullOrEmpty(str) == true)
{
return false;
}
foreach (char curChar in str)
{
if (char.IsDigit(curChar) == false)
{
return false;
}
}
return true;
}
}
}
StartLevel은 게임시작 화면이다.
이런 식으로 화면에 출력된다. 시작 창을 화려하게 꾸며보고 싶었는데, 그런 쪽에는 재능이 없는지라 심플하게 갔다.
1번을 누르면 게임이 시작되고 2번을 누르면 콘솔이 종료된다.
PlayLevel을 구현하기 전에 무기 클래스를 먼저 구현해놓았다. 무기 클래스 코드는 아래와 같다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
namespace C_Study
{
public enum WeaponType
{
None,
Sword,
Spear,
Hammer,
}
public class Weapon
{
protected Weapon()
{
}
protected WeaponType WPType
{
get { return _wpType; }
set { _wpType = value; }
}
protected string WPName
{
get { return _wpName; }
set { _wpName = value; }
}
protected int AttPower
{
get { return _attPower; }
set { _attPower = value; }
}
protected float BonusAttackProb
{
get { return _bonusAttackprob; }
set { _bonusAttackprob = value; }
}
private int _attPower;
private float _bonusAttackprob;
private string _wpName;
private WeaponType _wpType;
}
public class Sword : Weapon
{
protected Sword()
{
WPType = WeaponType.Sword;
BonusAttackProb = 25.0f;
}
}
public class Spear : Weapon
{
protected Spear()
{
WPType = WeaponType.Spear;
BonusAttackProb = 15.0f;
}
}
public class Hammer : Weapon
{
protected Hammer()
{
WPType = WeaponType.Hammer;
BonusAttackProb = 5.0f;
}
}
public class OldSword : Sword
{
public OldSword()
{
WPName = "오래된 검";
AttPower = 10;
}
}
public class OldSpear : Spear
{
public OldSpear()
{
WPType = WeaponType.Spear;
WPName = "오래된 창";
AttPower = 15;
}
}
public class OldHammer : Hammer
{
public OldHammer()
{
WPType = WeaponType.Hammer;
WPName = "오래된 망치";
AttPower = 30;
}
}
}
Weapon 클래스를 Spear, Sword, Hammer 클래스가 상속받도록 하였고 실제로 플레이어가 장착할 무기들은 종류에 맞는 클래스를 상속받도록 하였다.
코드의 아래쪽에 보면 OldSword, OldSpear, OldHammer가 있는데 이게 플레이어가 실제 장착할 아이템이다.
처음에 장착할 기본템이며 일단은 이 3개만 만들어두었다.
무기는 2개의 스탯을 가지고 있다. 공격력과 추가공격확률이다.
공격력은 말 그대로 공격력이고, 추가공격확률은 공격시에 추가적으로 공격이 발생할 확률이다.
검은 공격력이 낮은 대신 추가공격확률이 높고, 망치는 공격력이 높은 대신 추가공격확률이 낮다.
창은 그 중간쯤이다.
일단은 이정도만 구현해보았다.
다음엔 게임 플레이 내용을 본격적으로 구현할 것이다.
'프로젝트 > C# 머드게임' 카테고리의 다른 글
C# 머드 게임 프로젝트 (4) (0) | 2024.08.01 |
---|---|
C# 머드 게임 프로젝트 (3) (0) | 2024.07.30 |
C# 머드 게임 프로젝트 (2) - 구조 변경, 기능 추가 (0) | 2024.07.28 |