TIL을 올바르게 작성하는 법
TIL에서는 문제해결이나 적용 결과에 대해서
기존과 개선된거에 대한 차이등을 적는것이 좋음
이유 : 단순 실수를 반복하지 않기위해
(주니어 개발자에게는 지식보다는 문제해결력이 중요하다!!!!)
메인
namespace Snake
{
class Program
{
public static void Main()
{
Map map = new Map();
SnakeBody snake = new SnakeBody();
Control control = new Control();
Food food = new Food();
ConsoleKeyInfo command;
Console.SetWindowSize(110, 60);
bool loop = true;
do
{
while (Console.KeyAvailable == false)
{
Console.Clear();
if (control.CookTime()) food.MakeFood(snake,map);
control.PrintMap(snake,map);
if (control.MoveSnake(snake, map)) {
loop = false;
break;
}
Thread.Sleep(150);
}
// Loop until input is entered.
if (!loop) break;
command = Console.ReadKey(true);
control.WhereGo(command);
} while (command.Key != ConsoleKey.Escape);
Console.Clear();
Console.Write("주금");
Console.ReadKey();
}
}
}
몸통
namespace Snake
{
public class SnakeBody
{
int[] head = new int[3] { 3, 3, 7 };
int tailNum = 6;
public int[] last = new int[2];
public ArrayList bodyList = new ArrayList();
public SnakeBody()
{
bodyList.Add(head[0]);
bodyList.Add(head[1]);
bodyList.Add(head[2]);
last[0] = 3;
last[1] = 3;
}
public void SetBody()
{
bodyList.Add(last[0]);
bodyList.Add(last[1]);
bodyList.Add(tailNum);
}
}
}
이동
namespace Snake
{
class Control
{
public string whereGo = "+x";
int cook = 0;
public static void ColoredConsoleWrite(ConsoleColor color, string text)
{
ConsoleColor originalColor = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.Write(text);
Console.ForegroundColor = originalColor;
}
public Control() { }
public bool CookTime()
{
bool Makeit = false;
cook++;
if(cook == 10)
{
cook = 0;
Makeit = true;
}
return Makeit;
}
public void PrintMap(SnakeBody Snake,Map map)
{
for(int i = 0; i < map.mapInfo01.GetLength(0);i++)
{
for(int j = 0; j< map.mapInfo01.GetLength(1); j++)
{
if (map.mapInfo01[i, j] == 1) ColoredConsoleWrite(ConsoleColor.DarkGray, map.map01[i, j]);
else if (map.mapInfo01[i, j] == 4) ColoredConsoleWrite(ConsoleColor.Yellow,"♨");
else if (CheckHead(i, j, Snake)) ColoredConsoleWrite(ConsoleColor.Cyan, "●");
else if (CheckTail(i, j, Snake)) ColoredConsoleWrite(ConsoleColor.Cyan, "▣");
else ColoredConsoleWrite(ConsoleColor.White, " ");
}
Console.WriteLine();
}
}
public bool MoveSnake(SnakeBody snake,Map map)
{
bool dieCheck = false;
int x=0;
int y=0;
int info = 0;
for(int roop =0; roop < (snake.bodyList.Count / 3); roop++)
{
y = int.Parse( snake.bodyList[roop * 3].ToString());
x = int.Parse(snake.bodyList[roop * 3+1].ToString());
info = int.Parse(snake.bodyList[roop * 3+2].ToString());
}
if (whereGo == "+x")
{
snake.last[0] = int.Parse(snake.bodyList[0].ToString());
snake.last[1] = int.Parse(snake.bodyList[1].ToString());
y = int.Parse(snake.bodyList[0].ToString());
x = int.Parse(snake.bodyList[1].ToString()) + 1;
if (map.mapInfo01.GetLength(1) < x) x--;
if (map.mapInfo01[y, x] == 1) dieCheck = true;
else if (map.mapInfo01[y, x] == 4) MakeTail(y, x, snake, map);
snake.bodyList[0] = y;
snake.bodyList[1] = x;
MoveTail(snake);
if (EatTail(y, x, snake)) dieCheck = true;
}
else if (whereGo == "-x")
{
snake.last[0] = int.Parse(snake.bodyList[0].ToString());
snake.last[1] = int.Parse(snake.bodyList[1].ToString());
y = int.Parse(snake.bodyList[0].ToString());
x = int.Parse(snake.bodyList[1].ToString()) - 1;
if (0 > x) x++;
if (map.mapInfo01[y, x] == 1) dieCheck = true;
else if (map.mapInfo01[y, x] == 4) MakeTail(y, x, snake, map);
snake.bodyList[0] = y;
snake.bodyList[1] = x;
MoveTail(snake);
if (EatTail(y, x, snake)) dieCheck = true;
}
else if (whereGo == "+y")
{
snake.last[0] = int.Parse(snake.bodyList[0].ToString());
snake.last[1] = int.Parse(snake.bodyList[1].ToString());
y = int.Parse(snake.bodyList[0].ToString()) + 1;
x = int.Parse(snake.bodyList[1].ToString());
if (map.mapInfo01.GetLength(0) < y) y--;
if (map.mapInfo01[y, x] == 1) dieCheck = true;
else if (map.mapInfo01[y, x] == 4) MakeTail(y, x, snake, map);
snake.bodyList[0] = y;
snake.bodyList[1] = x;
MoveTail(snake);
if (EatTail(y, x, snake)) dieCheck = true;
}
else if (whereGo == "-y")
{
snake.last[0] = int.Parse(snake.bodyList[0].ToString());
snake.last[1] = int.Parse(snake.bodyList[1].ToString());
y = int.Parse(snake.bodyList[0].ToString()) - 1;
x = int.Parse(snake.bodyList[1].ToString());
if (0> y) y++;
if (map.mapInfo01[y, x] == 1) dieCheck = true;
else if (map.mapInfo01[y, x] == 4) MakeTail(y,x,snake,map);
snake.bodyList[0] = y;
snake.bodyList[1] = x;
MoveTail(snake);
if (EatTail(y, x, snake)) dieCheck = true;
}
return dieCheck;
}
bool EatTail(int i,int j, SnakeBody snake)
{
bool eat = false;
for(int roop =1; roop < (snake.bodyList.Count)/3; roop++)
{
if ((i == int.Parse(snake.bodyList[roop * 3].ToString()) && j == int.Parse(snake.bodyList[roop * 3 + 1].ToString()))) eat = true;
}
return eat;
}
bool CheckHead(int i,int j,SnakeBody snake)
{
int x = 0;
int y = 0;
int.TryParse(snake.bodyList[0].ToString(), out y);
int.TryParse(snake.bodyList[1].ToString(), out x);
bool checkHead = false;
if (i == y && j == x) checkHead = true;
return checkHead;
}
void MoveTail(SnakeBody snake)
{
for(int roop =1; roop < (snake.bodyList.Count / 3); roop++)
{
int stayY =int.Parse( snake.bodyList[roop * 3].ToString());
int stayX = int.Parse(snake.bodyList[roop * 3 + 1].ToString());
snake.bodyList[roop * 3] = snake.last[0];
snake.bodyList[roop * 3 + 1] = snake.last[1];
snake.last[0] = stayY;
snake.last[1] = stayX;
}
}
void MakeTail(int y,int x,SnakeBody snake,Map map)
{
int yPut = int.Parse(snake.bodyList[snake.bodyList.Count - 3].ToString());
int xPut = int.Parse(snake.bodyList[snake.bodyList.Count - 2].ToString());
if (whereGo == "-x") xPut++;
if (whereGo == "+x") xPut--;
if (whereGo == "-y") yPut++;
if (whereGo == "+y") yPut--;
snake.bodyList.Add(yPut);
snake.bodyList.Add(xPut);
snake.bodyList.Add(6);
map.mapInfo01[y, x] = 0;
}
bool CheckTail(int i, int j, SnakeBody snake)
{
int x = 0;
int y = 0;
bool checkTail = false;
for(int roop=0; roop < (snake.bodyList.Count / 3); roop++)
{
int.TryParse(snake.bodyList[roop*3].ToString(), out y);
int.TryParse(snake.bodyList[roop*3+1].ToString(), out x);
if (y == i && x == j) checkTail = true;
}
return checkTail;
}
public void WhereGo(ConsoleKeyInfo command)
{
if(command.Key == ConsoleKey.RightArrow) { this.whereGo = "+x"; }
else if (command.Key == ConsoleKey.LeftArrow) { this.whereGo = "-x"; }
else if (command.Key == ConsoleKey.DownArrow) { this.whereGo = "+y"; }
else if (command.Key == ConsoleKey.UpArrow) { this.whereGo = "-y"; }
}
}
}