신나는 금요일 사실 안 신남.
다 제출한 과제를 싹 다 갈아엎어야 되는 사람의 심정을 아십니까
아ㅋㅋ 누가 일을 두 번씩 해요ㅋㅋ
안녕하세요 누 입니다.
매니저님한테 슬랙 디엠 드렸는데 답이 없으시길래 오늘 게더에서 다른 매니저님을 찾아갔다
여쭤봤는데 내 방식대로 짠 코드를 제출했다고 문제가 생기진 않을 거라고 하셨다.. 휴... 좋은 경험하셨다고..... 네..그래요..............
그래도 요구사항이 어쨌든 있는 과제니까 다시 풀어는 봐야지..힝
과제 다시 풀다가 12시가 되어서 개인 과제 발제 시간이 왔다 두둥 !
주제는 텍스트 RPG 게임을 만드는 것!!! 와!
4주차 때 내가 만들었던 게임이랑 방식이 매우 비슷해서 운 좋게도 설계할 시간이 줄었다!
그래서 바로 구현 시작하고, 중간중간 설계 부분 수정하면서 진행했당
일단 과제 얘기는 기니까 7시에 진행한 코드 컨벤션 특강 내용부터 정리~
static void Main(string[] args)
{
InitPlayerInfo();
InitStore();
while (true)
{
// 1: 상태 보기, 2: 인벤토리, 3: 상점
if (startState == 0) DisplayStartState();
else if (startState == 1) DisplayPlayerInfo();
else if (startState == 2) DisplayInventoryInfo();
else DisplayStore();
}
}
// 플레이어 닉네임을 받아서 플레이어 객체 생성
// 초기값을 설정해줌
static void InitPlayerInfo()
{
Console.Title = "[ 스파르타 던전 ]";
Console.WriteLine("[ 스파르타 던전 ]");
Console.WriteLine();
Console.Write("Player 닉네임을 입력해주세요 : ");
string playerName = Console.ReadLine();
player = new Player(playerName, PLAYER_HP, PLAYER_SHIELD, PLAYER_POWER, PLAYER_MONEY, new List<Item>());
player.InitItemList(GetItemFromDB(0));
player.InitItemList(GetItemFromDB(1));
player.InitItemList(GetItemFromDB(3));
}
static void DisplayStartState()
{
Console.Clear();
Console.WriteLine("스파르타 마을에 오신 여러분 환영합니다.");
Console.WriteLine("이곳에서 던전으로 들어가기 전 활동을 할 수 있습니다.");
Console.WriteLine();
("1").PrintWithColor(ConsoleColor.Magenta, false);
Console.WriteLine(". 상태 보기");
("2").PrintWithColor(ConsoleColor.Magenta, false);
Console.WriteLine(". 인벤토리");
("3").PrintWithColor(ConsoleColor.Magenta, false);
Console.WriteLine(". 상점");
Console.WriteLine();
startState = GetPlayerSelect(1, 3);
}
// 플레이어의 선택이 필요할 때 값이 유효한지 확인하는 함수
// 플레이어가 start부터 end 사이의 값을 선택했다면 그 값을 리턴
static int GetPlayerSelect(int start, int end)
{
Console.WriteLine("원하시는 행동을 입력해주세요.");
(">> ").PrintWithColor(ConsoleColor.Yellow, false);
int select = 0;
bool isNum = false;
while (true)
{
isNum = int.TryParse(Console.ReadLine(), out select);
if (!isNum || (select < start || select > end))
{
("잘못된 입력입니다. 다시 고르세요").PrintWithColor(ConsoleColor.Red, true);
}
else break;
}
return select;
}
// 시작 화면에서 상태 보기 선택시 실행
// 화면에 플레이어의 정보 표시
// 레벨, 이름, 직업, 공격력, 방어력, 체력, Gold
static void DisplayPlayerInfo()
{
Console.Clear();
("상태 보기").PrintWithColor(ConsoleColor.Yellow, true);
Console.WriteLine("캐릭터의 정보가 표시됩니다.");
Console.WriteLine();
player.DisplayPlayerInfo();
Console.WriteLine(); Console.WriteLine();
("0").PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(". 나가기");
Console.WriteLine();
int select = GetPlayerSelect(0, 0);
if (select == 0) startState = select;
}
// 플레이어의 정보를 보여주는 함수
public void DisplayPlayerInfo()
{
Console.Write("Lv. "); ("0" + level).PrintWithColor(ConsoleColor.Magenta, true);
Console.WriteLine(name + " ( " + Job + " )");
// 착용한 아이템 수치 계산
int addAttack = 0, addShield = 0;
foreach (Item item in itemList)
{
if (item.IsEquipped)
{
if (item.Type == 0) addShield += item.Value;
else addAttack += item.Value;
}
}
Console.Write("공격력 : "); (power.ToString()).PrintWithColor(ConsoleColor.Magenta, false);
if (addAttack > 0) // 공격 무기 착용시
{
// 공격력 : 12 (+2)
Console.Write(" ("); ("+").PrintWithColor(ConsoleColor.Yellow, false);
(addAttack.ToString()).PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(")");
}
else Console.WriteLine(); // 공격력 : 12
Console.Write("방어력 : "); (shield.ToString()).PrintWithColor(ConsoleColor.Magenta, false);
if (addShield > 0) // 방어구 착용시
{
// 방어력 : 10 (+5)
Console.Write(" ("); ("+").PrintWithColor(ConsoleColor.Yellow, false);
(addShield.ToString()).PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(")");
}
else Console.WriteLine(); // 방어력 : 10
Console.Write("체력 : "); (hp.ToString()).PrintWithColor(ConsoleColor.Magenta, true);
Console.Write("Gold : "); (money.ToString()).PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(" G");
}
// 플레이어의 인벤토리 상태를 보여줌
static void DisplayInventoryInfo()
{
Console.Clear();
("인벤토리").PrintWithColor(ConsoleColor.Yellow, true);
Console.WriteLine("보유 중인 아이템을 관리할 수 있습니다.");
Console.WriteLine();
if (player.GetItemCount() == 0)
{
Console.WriteLine();
Console.WriteLine("보유하고 있는 아이템이 없습니다!");
} else
{
player.DisplayItemInventory(0);
Console.WriteLine();
("1").PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(". 장착 관리");
("2").PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(". 아이템 정렬");
("0").PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(". 나가기");
Console.WriteLine();
int select = GetPlayerSelect(0, 2);
if (select == 0) startState = select;
else if (select == 1) ManagementItemInventory();
else ArrangeItemInventory();
}
}
// 플레이어의 현재 아이템 인벤토리 상태를 보여주는 함수
// type이 0이면 인벤토리, type이 1이면 인벤토리 - 장착 관리 상태
public void DisplayItemInventory(int type)
{
Console.WriteLine("[ 아이템 목록 ]");
int idx = 1;
foreach (Item item in itemList)
{
// 아이템 이름
("-").PrintWithColor(ConsoleColor.Yellow, false);
// 장착 관리 상태일 경우 번호 표시
if (type == 1) (" " + idx.ToString()).PrintWithColor(ConsoleColor.Magenta, false);
if (item.IsEquipped) Console.Write(" [E]");
Console.Write(" " + item.Name);
Extension.MakeDivider();
// 아이템 효과
if (item.Type == 0) Console.Write("방어력 "); else Console.Write("공격력 ");
("+").PrintWithColor(ConsoleColor.Yellow, false);
(item.Value.ToString()).PrintWithColor(ConsoleColor.Magenta, false);
Extension.MakeDivider();
// 아이템 설명
Console.WriteLine(item.Description);
idx++;
}
}
// 인벤토리 - 장착 관리
static void ManagementItemInventory()
{
bool isExit = false;
while (!isExit)
{
Console.Clear();
("인벤토리 - 장착 관리").PrintWithColor(ConsoleColor.Yellow, true);
Console.WriteLine("보유 중인 아이템을 장착하거나 해제할 수 있습니다.");
Console.WriteLine();
player.DisplayItemInventory(1);
Console.WriteLine();
("0").PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(". 나가기");
Console.WriteLine();
int select = GetPlayerSelect(0, player.GetItemCount());
if (select == 0)
{
isExit = true;
DisplayInventoryInfo();
}
else player.EquipItem(select - 1);
}
}
// 아이템 장착 함수
// itemIdx를 받아서 현재 플레이어의 아이템 리스트 중 itemIdx번째 아이템을 장착 또는 해제
public void EquipItem(int itemIdx)
{
Item curItem = itemList[itemIdx];
curItem.IsEquipped = !curItem.IsEquipped ;
}
여기까지가 필수 요구사항 구현이었고, 이제 추가 요구사항 구현 시작 ~
// Item Database
static List<string[]> itemDB = new List<string[]>
{
new string[] { "무쇠갑옷", "0", "5", "500", "무쇠로 만들어져 튼튼한 갑옷입니다." } ,
new string[]{ "낡은 검", "1", "2", "600", "쉽게 볼 수 있는 낡은 검입니다." },
new string[]{ "수련자 갑옷", "0", "9", "1000", "수련에 도움을 주는 갑옷입니다." },
new string[]{ "스파르타의 갑옷", "0", "30", "3500", "스파르타의 전사들이 사용했다는 전설의 갑옷입니다." },
new string[]{ "청동 도끼", "1", "8", "1200", "어디선가 사용된 것 같은 도끼입니다." },
new string[]{ "스파르타의 창", "1", "15", "2500", "스파르타의 전사들이 사용했다는 전설의 창입니다." }
};
static Item GetItemFromDB(int itemIdx)
{
string[] itemStr = itemDB[itemIdx];
int idx = 0;
return new Item(
itemStr[idx++],
int.Parse(itemStr[idx++]),
int.Parse(itemStr[idx++]),
int.Parse(itemStr[idx++]),
itemStr[idx++]
);
}
public static class ExtensionString
{
// string 확장 함수
// 텍스트 색상을 변경하여 출력해줌
// (콘솔에 출력할 텍스트, 색상, WriteLine or Write)
public static void PrintWithColor(this string text, ConsoleColor color, bool isEnter)
{
Console.ForegroundColor = color;
if (isEnter) Console.WriteLine(text); else Console.Write(text);
Console.ResetColor();
}
}
static void ArrangeItemInventory()
{
bool isExit = false;
while (!isExit)
{
Console.Clear();
("인벤토리 - 아이템 정렬").PrintWithColor(ConsoleColor.Yellow, true);
Console.WriteLine("보유 중인 아이템을 정렬할 수 있습니다.");
Console.WriteLine();
player.DisplayItemInventory(1);
Console.WriteLine();
("1").PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(". 이름");
("2").PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(". 장착순");
("3").PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(". 공격력");
("4").PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(". 방어력");
("0").PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(". 나가기");
Console.WriteLine();
int select = GetPlayerSelect(0, 4);
if (select == 0)
{
isExit = true;
DisplayInventoryInfo();
}
else player.ArrangeItemInventory(select);
}
}
// 플레이어의 아이템 인벤토리를 정렬하는 함수
// pivot 값에 따라 이름, 장착, 공격력, 방어력 순으로 정렬됨
public void ArrangeItemInventory(int pivot)
{
switch (pivot)
{
// 이름 정렬 (긴 순서대로)
case 1:
itemList = itemList.OrderByDescending(item => item.Name.Length).ToList();
break;
// 장착순 정렬 -> 이름순 정렬
case 2:
itemList = itemList.OrderByDescending(item => item.IsEquipped).ThenByDescending(item => item.Name.Length).ToList();
break;
// 타입 정렬(공격은 1이므로 1부터 나오도록) -> 공격력 정렬 -> 이름순 정렬
case 3:
itemList = itemList.OrderByDescending(item => item.Type == 1).ThenByDescending(item => item.Value)
.ThenByDescending(item => item.Name.Length).ToList();
break;
// 타입 정렬(방어는 0이므로 0부터 나오도록) -> 방어력 정렬 -> 이름순 정렬
case 4:
itemList = itemList.OrderByDescending(item => item.Type == 0).ThenByDescending(item => item.Value)
.ThenByDescending(item => item.Name.Length).ToList();
break;
}
}
static void DisplayStore()
{
Console.Clear();
("상점").PrintWithColor(ConsoleColor.Yellow, true);
Console.WriteLine("필요한 아이템을 얻을 수 있는 상점입니다.");
Console.WriteLine();
player.DisplayMoney();
Console.WriteLine();
store.DisplayStore(0);
Console.WriteLine();
("1").PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(". 아이템 구매");
("0").PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(". 나가기");
int select = GetPlayerSelect(0, 1);
if (select == 0) startState = select;
else if (select == 1) BuyItem();
else ArrangeItemInventory();
}
Player.DisplayMoney는 별거 없어서 넘어감 그냥 돈 출력하는게 끝
private List<Item> itemList;
private Dictionary<string, bool> soldState;
// 상점의 아이템 목록을 보여주는 함수
// type이 0이면 상점, type이 1이면 상점 - 아이템 구매 상태
public void DisplayStore(int type)
{
Console.WriteLine("[ 아이템 목록 ]");
int idx = 1;
foreach (Item item in itemList)
{
// 아이템 이름
("-").PrintWithColor(ConsoleColor.Yellow, false);
// 아이템 구매 상태일 경우 번호 표시
if (type == 1) (" " + idx.ToString()).PrintWithColor(ConsoleColor.Magenta, false);
Console.Write(" " + item.Name);
Extension.MakeDivider();
// 아이템 효과
if (item.Type == 0) Console.Write("방어력 "); else Console.Write("공격력 ");
("+").PrintWithColor(ConsoleColor.Yellow, false);
(item.Value.ToString()).PrintWithColor(ConsoleColor.Magenta, false);
Extension.MakeDivider();
// 아이템 설명
Console.Write(item.Description);
Extension.MakeDivider();
// 아이템 가격
if (soldState.GetValueOrDefault(item.Name)) Console.WriteLine("구매 완료");
else
{
(item.Price.ToString()).PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(" G");
}
idx++;
}
}
static void BuyItem()
{
bool isExit = false;
while (!isExit)
{
Console.Clear();
("인벤토리 - 아이템 구매").PrintWithColor(ConsoleColor.Yellow, true);
Console.WriteLine("필요한 아이템을 구매할 수 있습니다.");
Console.WriteLine();
player.DisplayMoney();
Console.WriteLine();
store.DisplayStore(1);
Console.WriteLine();
("0").PrintWithColor(ConsoleColor.Magenta, false); Console.WriteLine(". 나가기");
int select = GetPlayerSelect(0, store.GetStoreItemCount());
if (select == 0)
{
isExit = true;
DisplayStore();
}
else
{
if (store.IsAbleToBuy(select - 1))
{
Item selectedItem = store.GetStoreItem(select - 1);
if (player.IsAbleToBuy(selectedItem.Price))
{
store.BuyItem(select - 1);
player.BuyItem(selectedItem);
("구매를 완료했습니다.").PrintWithColor(ConsoleColor.Blue, true);
Thread.Sleep(1000);
}
else
{
("Gold가 부족합니다.").PrintWithColor(ConsoleColor.Red, true);
Thread.Sleep(1000);
}
} else
{
("이미 구매한 아이템입니다.").PrintWithColor(ConsoleColor.Blue, true);
Thread.Sleep(1000);
}
}
}
}
// Store itemList의 itemIdx번째 아이템이 구매할 수 있는지 확인하는 함수
public bool IsAbleToBuy(int itemIdx)
{
string key = itemList[itemIdx].Name;
return !soldState.GetValueOrDefault(key);
}
// 플레이어가 Store itemList의 itemIdx번째 아이템을 구매할 때 호출되는 함수
// 해당 아이템의 판매 여부를 true로 바꾸고 아이템을 리턴해줌
public void BuyItem(int itemIdx)
{
Item selectedItem = itemList[itemIdx];
string key = selectedItem.Name;
soldState[key] = true;
}
public bool IsAbleToBuy(int itemPrice)
{
return itemPrice <= money;
}
public void BuyItem(Item item)
{
itemList.Add(item);
money -= item.Price;
}
public static class Extension
{
public static void MakeDivider()
{
Console.Write(" ");
("|").PrintWithColor(ConsoleColor.Yellow, false);
Console.Write(" ");
}
}
오늘은 여기까지~~~ 마참내 주말! 얏호
~끗~