방명록
- C# #17 TextRPG - 턴제 전투2023년 08월 25일 19시 49분 18초에 업로드 된 글입니다.작성자: noun06
개요
4주차 과제인 턴 기반 콘솔 RPG 게임이다. 인터페이스 개념을 활용하여 캐릭터와 아이템을 체계적으로 관리하는 것을 중심으로 개발을 진행하였다. 스테이지가 시작되면 플레이어와 몬스터가 교대로 턴을 진행하며 스테이지 클리어 시 더 강한 몬스터가 나오는 구조이다. 플레이어나 몬스터 중 하나가 죽으면 스테이지가 종료된다.
설명
- ICharacter 인터페이스: 캐릭터의 기본 속성을 정의하며 모든 캐릭터 클래스는 이 인터페이스를 구현한다.
- Warriror 클래스: 플레이어의 캐릭터를 나타낸다. StagesCleared라는 속성을 통해 클리어한 스테이지 수를 확인한다. TakeDamage 메서드를 통해 피해를 받는다.
- Monster 클래스: Warrior 클래스와 같이 ICharacter 인터페이스를 구현하며 몬스터를 나타낸다.
- Goblin, Dragon 클래스: Monster 클래스를 상속받아 사용한다.
- IItem 인터페이스: 아이템의 기본 속성을 정의하며 Use 메서드를 통해 아이템 사용을 나타낸다.
- HealthPotion, StrengthPotion: IItem 인터페이스를 구현하며 각각 체력 물약과 힘 물약을 나타낸다.
- Stage 클래스: 게임의 스테이지를 관리하는 클래스이다. Start 메서드는 게임 플레이의 전체적인 흐름을 나타낸다. PlayerTurn과 MonsterTurn 메서드는 상대에게 데미지를 입히는 각 캐릭터의 공격 턴을 처리한다. UseReward 메서드는 플레이어 승리 시 보상을 선택하고 보상 아이템을 사용하는 것을 처리한다. StartNextStage 메서드는 다음 스테이지를 시작하는 것을 나타내며 새로운 몬스터와 보상 아이템으로 다음 스테이지를 세팅하고 시작한다.
코드
namespace TextRPG2 { internal class Program { static void Main(string[] args) { Warrior player = new Warrior("김전사", 100, 20); Monster monster = new Monster("고블린", 50, 10); IItem[] rewards = { new HealthPotion("체력 물약"), new StrengthPotion("힘 물약") }; Stage stage = new Stage(player, monster, rewards); stage.Start(); } public interface ICharacter { string Name { get; } int Health { get; } int Attack { get; } bool IsDead { get; } //캐릭터 피해 public void TakeDamage(int damage); } public class Warrior : ICharacter { public string Name { get; set; } public int Health { get; set; } public int Attack { get; set; } public bool IsDead => Health <= 0; public int StagesCleared { get; set; } //클리어한 스테이지 수 public Warrior(string name, int health, int attack) { Name = name; Health = health; Attack = attack; StagesCleared = 0; } public void TakeDamage(int damage) { Health -= damage; Console.WriteLine($"{Name}가 {damage}의 피해를 입었다."); } } public class Monster : ICharacter { public string Name { get; set; } public int Health { get; set; } public int Attack { get; set; } public bool IsDead => Health <= 0; public Monster(string name, int health, int attack) { Name = name; Health = health; Attack = attack; } public void TakeDamage(int damage) { Health -= damage; Console.WriteLine($"{Name}이 {damage}의 피해를 입었다."); } } public class Goblin : Monster { public Goblin(string name, int health, int attack) : base(name, health, attack) { } } public class Dragon : Monster { public Dragon(string name, int health, int attack) : base(name, health, attack) { } } public interface IItem { string Name { get; } //플레이어 아이템 사용 public void Use(Warrior warrior); } public class HealthPotion : IItem { public string Name { get; set; } public HealthPotion(string name) { Name = name; } public void Use(Warrior warrior) { int bonusHealth = 20; Console.WriteLine(); Console.WriteLine($"{warrior.Name}의 체력이 {bonusHealth} 회복되었다."); warrior.Health += bonusHealth; } } public class StrengthPotion : IItem { public string Name { get; set; } public StrengthPotion(string name) { Name = name; } public void Use(Warrior warrior) { int bonusDamage = 20; Console.WriteLine(); Console.WriteLine($"{warrior.Name}의 공격력이 {bonusDamage} 증가하였다."); warrior.Attack += bonusDamage; } } public class Stage { private Warrior player; private Monster monster; private IItem[] rewards; public Stage(Warrior player, Monster monster, IItem[] rewards) { this.player = player; this.monster = monster; this.rewards = rewards; } //전투 시작 public void Start() { Console.WriteLine(); Console.WriteLine("------------------------------------------"); Console.WriteLine($"플레이어: {player.Name} | 체력: {player.Health} | 공격력: {player.Attack}"); Console.WriteLine($"몬스터: {monster.Name} | 체력: {monster.Health} | 공격력: {monster.Attack}"); Console.WriteLine("------------------------------------------"); //캐릭터들이 죽을 때까지 턴 돌리기 반복 while (!player.IsDead && !monster.IsDead) { PlayerTurn(); if (!monster.IsDead) { Thread.Sleep(1000); MonsterTurn(); } } if(player.IsDead) { Console.WriteLine(); Console.WriteLine("플레이어가 사망하였습니다."); Console.WriteLine("게임을 종료합니다."); } else { Console.WriteLine(); Console.WriteLine("몬스터가 사망하였습니다."); UseReward(); Console.WriteLine("다음 스테이지로 가시겠습니까? (y/n)"); Console.Write(">> "); string input = Console.ReadLine(); if (input.ToLower() == "y") { StartNextStage(); } else { Console.WriteLine("게임을 종료합니다."); } } } //플레이어 공격 private void PlayerTurn() { Console.WriteLine(); Console.WriteLine($"[{player.Name}의 차례]"); monster.TakeDamage(player.Attack); } //몬스터 공격 private void MonsterTurn() { Console.WriteLine(); Console.WriteLine($"[{monster.Name}의 차례]"); player.TakeDamage(monster.Attack); } //보상 선택 및 사용 private void UseReward() { Console.WriteLine(); Console.WriteLine("보상을 선택하세요: "); for (int i = 0; i < rewards.Length; i++) { Console.WriteLine($"{i + 1}.{rewards[i].Name}"); } Console.Write(">> "); int choice = int.Parse(Console.ReadLine()) - 1; rewards[choice].Use(player); } //다음 스테이지 진행 private void StartNextStage() { player.StagesCleared++; // 클리어한 스테이지 수 증가 Monster dragon = new Dragon("드래곤", 100, 0 + (player.StagesCleared * 10)); // 클리어 스테이지 수에 비례하여 적 공격력 증가 IItem[] nextStageRewards = { new HealthPotion("체력 물약"), new StrengthPotion("힘 물약") }; Stage nextStage = new Stage(player, dragon, nextStageRewards); nextStage.Start(); } } } }
결과물
'C#' 카테고리의 다른 글
C# #20 알고리즘 코드카타 1 (0) 2023.08.27 C# #19 정렬 알고리즘 (0) 2023.08.26 C# #16 델리게이트, 람다, LINQ, 고급 자료형 및 기능 (0) 2023.08.24 C# #15 인터페이스, 열거형, 예외 처리, 값형과 참조형 (0) 2023.08.24 C# #14 블랙잭 (0) 2023.08.23 다음글이 없습니다.이전글이 없습니다.댓글