C# TextRPG #3

정제로·2023년 8월 22일
0

C#

목록 보기
24/30
post-thumbnail

오늘 만든 기능

인벤토리 글자 정렬

중간에 오류를 수정해서 앞으로 나올 그림은 지금거와 다를수 있다.
하지만 이렇게 정렬을 완료했다는 사실만 기억하자

상점 구매 및 판매 기능

판매




판매가 잘 완료되었고, 보유중인 골드에서 골드가 추가되었다!
인벤토리에서도 판매가 된 결과를 확인가능

구매

  1. 수련자 갑옷과 낡은검을 구매했다
  2. 보유중인 골드가 빠져있는걸 확인가능하고
  3. 구매한 상품은 구매완료가 되어있다.
  4. 인벤토리에도 추가되었다

어떻게 만들었나

글자 정렬

인벤토리 글자 정렬의 핵심은, 한글과 영어의 차이이다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TextRPG;

namespace Utility
{
    class KoreanCount
    {
        public static int CountKoreanCharacters(string input) //한글 카운트 하기
        {
            int koreanCount = 0;
            foreach (char c in input)
            {
                // 한글 범위: 0xAC00 - 0xD7A3
                if (c >= 0xAC00 && c <= 0xD7A3)
                {
                    koreanCount++;
                }
            }
            return koreanCount;
        }
    }
}

위와같은 새로운 유틸리티를 담당하는 네임스페이스를 만들어주고,

한글 범위, 즉 '가~힣"까지가 0xAC00 - 0xD7A3이다.
그래서 한글개수가 인자값으로 들어온 문자열에서 몇개인지 카운트해주는 함수를 만들었다

이후 해당 기능을

int itNameKoreanCount = KoreanCount.CountKoreanCharacters(equipItemList[i].itName);
int attKoreanCount = KoreanCount.CountKoreanCharacters(attackBonusText);
int defKoreanCount = KoreanCount.CountKoreanCharacters(defenseBonusText);
int itInfoKoreanCount = KoreanCount.CountKoreanCharacters(equipItemList[i].itInfo);

Console.Write($" - {i + 1}. ");

Console.Write(string.Format("{0}{1}", equipItemList[i].equiped, equipItemList[i].itName).PadRight(25 - itNameKoreanCount - 3) + "|");

Console.Write(string.Format(" {0}", attackBonusText).PadRight(15 - attKoreanCount) + "|");

Console.Write(string.Format(" {0}", defenseBonusText).PadRight(15 - defKoreanCount) + "|");

Console.Write(string.Format(" {0}", equipItemList[i].itInfo).PadRight(60 - itInfoKoreanCount) + "|");

인벤토리 클래스내에서 위의 식을 사용하여, 한글의 갯수만큼 padding값에서 빼줘서 해결해였다

상점 골드표시과 이미 구매한 아이템 구매완료 표시하기

아이템 클래스에서 새로운 오버로딩을 만들어주었다.

class EquipItemsInformation // 장비목록
{
    public string equiped { get; set; }
    public string itName { get; }
    public int itAtt { get; }
    public int itDef { get; }
    public string itInfo { get; }
    public string sellPrice { get; }
    public string buyPrice { get; }
    public string soldOut { get; }

    public EquipItemsInformation(string _Equiped, string _ItName, int _ItAtt, int _ItDef, string _ItInfo, string _SellPrice)
    {
        equiped = _Equiped;
        itName = _ItName;
        itAtt = _ItAtt;
        itDef = _ItDef;
        itInfo = _ItInfo;
        sellPrice = _SellPrice;
    }

    public EquipItemsInformation(string _Equiped, string _ItName, int _ItAtt, int _ItDef, string _ItInfo, string _BuyPrice, string _SoldOut)
    {
        equiped = _Equiped;
        itName = _ItName;
        itAtt = _ItAtt;
        itDef = _ItDef;
        itInfo = _ItInfo;
        buyPrice = _BuyPrice;
        soldOut = _SoldOut;
    }
}

soldout과 sell, buyPrice를 만들어주었다.
이후 인벤토리로 들어갈 인자값과 상점에 보여줄 인자값을 다르게 설정하여, 객체를 만들었다

storeItem3 = new EquipItemsInformation("", "스파르타의 값옷", 0, 15, "스파르타의 전사들이 사용했다는 갑옷입니다", "3500", "구매완료");

이후, store클래스를 만들고, 리스트로 객체를 만들어준 후에, list에 storeItem들을 추가해주었다.

이후 구매완료된 아이템과 구매전 아이템을 구분하기 위해
bool값

bool itemAlreadyInInventory = false;

를 선언해주고,
for문 첫번째 for문으로 storeItem리스트를 돌고
다음 for문으로 equipItem리스트를 돌며, 같은 이름이 있으면,
itemAlreadyInInventory를 true로 바꿔주고,
soldOut변수를 출력하고 for문이 끝나게 해주었고,

만약
itemAlreadyInInventory가 true로 바뀌지 않았으면
sellPrice변수를 출력하게 해주었다.

for(int i = 0; i < storItemList.Count; i++)
{
	for (int j = 0; j < Inven.equipItemList.Count; j++)
        {
            if (storeItemList[i].itName == Inven.equipItemList[j].itName)
            {
                itemAlreadyInInventory = true;
                Console.BackgroundColor = ConsoleColor.DarkGray;
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(string.Format(" {0}", storeItemList[i].soldOut).PadRight(10 - itSoldOutKoreanCount));
                Console.ResetColor();
                Console.Write("|");

                break;
            }
        }

        f (!itemAlreadyInInventory)
        {
            Console.Write(string.Format(" {0} G{1}", storeItemList[i].buyPrice, "").PadRight(10 - itBuyPriceKoreanCount) + "|");
        }
        Console.WriteLine();
        Console.WriteLine();
}

이제 잘 된다!

profile
초보자입니다.. 잘못된 정보, 달게 받겠습니다..

0개의 댓글

관련 채용 정보