오늘 학습 키워드
텍스트 포맷팅
오늘 학습 한 내용을 나만의 언어로 정리하기
테두리를 만들고싶었어요..
- 이리 결정되었기 때문에, 이중 테두리를 주변에 두르는 걸로 만들고자 했음.
public static class UI
{
/// <summary>
/// 타이틀에 씬 이름 넣고,
/// contents에다가는 원하는 문자열 리스트 넣으시면 알아서 정렬됨
/// </summary>
/// <param name="title"></param>
/// <param name="contents"></param>
public static void DrawTitledBox(string title, List<string>? contents)
{
List<string> fullStrings = new List<string>();
fullStrings.Add(title);
if(contents != null)
{
foreach (var str in contents)
{
fullStrings.Add(str);
}
}
int width = GetMaxWidth(fullStrings);
string topBorder = "╔" + new string('═', width) + "╗";
string titleLine = $"║{PadCenter(title, width)}║";
string divider = "╠" + new string('═', width) + "╣";
string bottomBorder = "╚" + new string('═', width) + "╝";
Console.WriteLine(topBorder);
Console.WriteLine(titleLine);
if (contents != null && contents.Count > 0)
{
Console.WriteLine(divider);
Console.WriteLine($"║{new string(' ', width)}║");
foreach (string line in contents)
{
Console.WriteLine($"║{PadCenter(line, width)}║");
}
Console.WriteLine($"║{new string(' ', width)}║");
Console.WriteLine(bottomBorder);
}
else
{
Console.WriteLine(bottomBorder);
}
}
public static void DrawBox(List<string> contents)
{
int width = GetMaxWidth(contents);
string topBorder = "╔" + new string('═', width) + "╗";
string bottomBorder = "╚" + new string('═', width) + "╝";
Console.WriteLine(topBorder);
Console.WriteLine($"║{new string(' ', width)}║");
foreach (string line in contents)
{
Console.WriteLine($"║{PadCenter(line, width)}║");
}
Console.WriteLine($"║{new string(' ', width)}║");
Console.WriteLine(bottomBorder);
}
public static void DrawLeftAlignedBox(List<string> contents)
{
int width = GetMaxWidth(contents);
string topBorder = "╔" + new string('═', width) + "╗";
string bottomBorder = "╚" + new string('═', width) + "╝";
Console.WriteLine(topBorder);
Console.WriteLine($"║{new string(' ', width)}║");
foreach (string line in contents)
{
Console.WriteLine($"║{PadLeftAlign(line, width)}║");
}
Console.WriteLine($"║{new string(' ', width)}║");
Console.WriteLine(bottomBorder);
}
private static int GetMaxWidth(List<string> contents)
{
int maxWidth = 0;
foreach (string line in contents)
{
int displayWidth = GetDisplayWidth(line);
if (displayWidth > maxWidth)
maxWidth = displayWidth;
}
return maxWidth + 4; // 좌우 여유 여백
}
private static string PadCenter(string text, int width)
{
int textWidth = GetDisplayWidth(text);
int padding = Math.Max(0, width - textWidth); // 남는 부분 (패딩)
int padLeft = padding / 2;
int padRight = padding - padLeft;
return new string(' ', padLeft) + text + new string(' ', padRight);
}
private static string PadLeftAlign(string text, int width)
{
int displayWidth = GetDisplayWidth(text);
int padding = Math.Max(0, width - displayWidth);
return text + new string(' ', padding);
}
private static int GetDisplayWidth(string text)
{
int width = 0;
foreach (char c in text)
{
width += IsFullWidth(c) ? 2 : 1; // 한글은 두 글자로 취급
}
return width;
}
/// <summary>
/// 입력받은 글자가 한글인지 확인함
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
private static bool IsFullWidth(char c)
{
return c >= 0x1100 && (
c <= 0x115F || // 한글 자모
c == 0x2329 || c == 0x232A ||
(c >= 0x2E80 && c <= 0xA4CF) ||
(c >= 0xAC00 && c <= 0xD7A3) || // 한글 완성형
(c >= 0xF900 && c <= 0xFAFF) ||
(c >= 0xFE10 && c <= 0xFE19) ||
(c >= 0xFF01 && c <= 0xFF60) ||
(c >= 0xFFE0 && c <= 0xFFE6)
);
}
}
- 타이틀 있는 박스의 경우에는 컨텐츠 + 타이틀 중에 긴거 찾아서 그거 기준으로 너비 잡음
팀플이 좋아요
확실히 다른 사람들이랑 팀플하니까 다양한 형식의 코드를 볼 수 있다는 것이 참 좋은 듯…
boss.RewardItems?.ForEach(item => player.Inventory.Add(item));
특히 이 코드 되게 신박했음.. foreach를 저렇게 List 내부로 돌릴 수 있구나!
- boss.RewardItems가 null이 아니라면
- RewardItems 에 있는 item 하나 하나마다
- player.Inventory에 Add해라
학습하며 겪었던 문제점 & 에러
문제 1
- 문제&에러에 대한 정의
테두리가 깨져서 나옴
- 해결 방법
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.InputEncoding = System.Text.Encoding.UTF8;
인코딩을 UTF-8 로 설정함
문제 2
- 문제&에러에 대한 정의
이렇게 밀려서 나옴
- 해결 방법
한글은 콘솔에서 두 글자를 차지함. 그러니까 한 글자가 아니고 두 글자로 처리해야 함
// 한글인지 확인하는 코드
private static bool IsFullWidth(char c)
{
return c >= 0x1100 && (
c <= 0x115F || // 한글 자모
c == 0x2329 || c == 0x232A ||
(c >= 0x2E80 && c <= 0xA4CF) ||
(c >= 0xAC00 && c <= 0xD7A3) || // 한글 완성형
(c >= 0xF900 && c <= 0xFAFF) ||
(c >= 0xFE10 && c <= 0xFE19) ||
(c >= 0xFF01 && c <= 0xFF60) ||
(c >= 0xFFE0 && c <= 0xFFE6)
);
}
내일 학습 할 것은 무엇인지
- 스킬 저장 만들기
- 배틀 전반적으로 손보기
- 상점 테두리 적용
- Enemy class TryDodge 만들기
- 보스 Enemy 상속해서 만들기