2019-10-23 게임 만들기 2048 게임
Console Programming/C# Console 2019. 10. 24. 18:47
Program.cs
namespace Game2048
{
class Program
{
static void Main(string[] args)
{
new App();
}
}
}
App.cs
using System;
namespace Game2048
{
public class App
{
enum EDir { Right, Left, Top, Bottom };
Random rand = new Random();
int num1;
int num2;
int row1;
int col1;
int row2;
int col2;
int[,] arrBase = new int[4, 4];
public App()
{
num1 = rand.Next(0, 2);
num2 = rand.Next(0, 2);
row1 = rand.Next(0, arrBase.GetLength(0));
col1 = rand.Next(0, arrBase.GetLength(1));
row2 = rand.Next(0, arrBase.GetLength(0));
col2 = rand.Next(0, arrBase.GetLength(1));
while (row1 == row2 && col1 == col2)
{
row2 = rand.Next(0, arrBase.GetLength(0));
col2 = rand.Next(0, arrBase.GetLength(1));
}
if (num1 == 0)
{
arrBase[row1, col1] = 2;
}
else
{
arrBase[row1, col1] = 4;
}
if (num2 == 0)
{
arrBase[row2, col2] = 2;
}
else
{
arrBase[row2, col2] = 4;
}
//arrBase = new int[,]
//{
//{0,2,2,2 },
//{0,2,2,2 },
//{2,2,2,0 },
//{2,0,2,0 }
//};
Show();
GameStart();
}
public void GameStart()
{
while (true)
{
var keyInfo = Console.ReadKey();
Console.WriteLine();
switch (keyInfo.Key)
{
case ConsoleKey.RightArrow:
{
Move(0);
break;
}
case ConsoleKey.LeftArrow:
{
Move(1);
break;
}
case ConsoleKey.UpArrow:
{
Move(2);
break;
}
case ConsoleKey.DownArrow:
{
Move(3);
break;
}
}
int row3 = rand.Next(0, arrBase.GetLength(0));
int col3 = rand.Next(0, arrBase.GetLength(1));
//Console.WriteLine($"{row3}, {col3} == {arrBase[row3, col3]}");
while (arrBase[row3, col3] != 0)
{
row3 = rand.Next(0, arrBase.GetLength(0));
col3 = rand.Next(0, arrBase.GetLength(1));
//Console.WriteLine($"{row3}, {col3}");
}
int num3 = rand.Next(0, 2);
if (num3 == 0)
{
arrBase[row3, col3] = 2;
}
else
{
arrBase[row3, col3] = 4;
}
Show();
}
}
public void Move(int dir)
{
switch (dir)
{
case (int)EDir.Right:
{
for (int j = 0; j < 4; j++)//j는 행 0~3까지
{
for (int k = 0; k < 4; k++) //숫자를 오른쪽으로 밀어냄
{
for (int i = 3; i > 0; i--)
{
if (i - 1 > -1 && arrBase[j, i] == 0)
{
arrBase[j, i] = arrBase[j, i - 1];
arrBase[j, i - 1] = 0;
}
}
}
for (int k = 0; k < 4; k++) //인접한 숫자가 같으면 더함
{
for (int i = 3; i > 0; i--)
{
if (arrBase[j, i] == arrBase[j, i - 1])
{
arrBase[j, i] += arrBase[j, i - 1];
arrBase[j, i - 1] = 0;
}
}
}
for (int k = 0; k < 4; k++) //숫자를 오른쪽으로 밀어냄
{
for (int i = 3; i > 0; i--)
{
if (i - 1 > -1 && arrBase[j, i] == 0)
{
arrBase[j, i] = arrBase[j, i - 1];
arrBase[j, i - 1] = 0;
}
}
}
}
break;
}
case (int)EDir.Left:
{
for (int j = 0; j < 4; j++)
{
for (int k = 0; k < 4; k++) //숫자를 왼쪽으로 밀어냄
{
for (int i = 0; i < 3; i++)
{
if (i + 1 < 4 && arrBase[j, i] == 0)
{
arrBase[j, i] = arrBase[j, i + 1];
arrBase[j, i + 1] = 0;
}
}
}
for (int k = 0; k < 4; k++) //인접한 숫자를 더함
{
for (int i = 0; i < 3; i++)
{
if (arrBase[j, i] == arrBase[j, i + 1])
{
arrBase[j, i] += arrBase[j, i + 1];
arrBase[j, i + 1] = 0;
}
}
}
for (int k = 0; k < 4; k++) //숫자를 왼쪽으로 밀어냄
{
for (int i = 0; i < 3; i++)
{
if (i + 1 < 4 && arrBase[j, i] == 0)
{
arrBase[j, i] = arrBase[j, i + 1];
arrBase[j, i + 1] = 0;
}
}
}
}
break;
}
case (int)EDir.Top:
{
for (int j = 0; j < 4; j++) //열 전체를 검사함
{
for (int k = 0; k < 4; k++) //숫자를 위로 밀어냄
{
for (int i = 0; i < 3; i++)
{
if (i + 1 < 4 && arrBase[i, j] == 0)
{
arrBase[i, j] = arrBase[i + 1, j];
arrBase[i + 1, j] = 0;
}
}
}
for (int k = 0; k < 4; k++) //인접한 숫자를 더함
{
for (int i = 0; i < 3; i++)
{
if (i + 1 < 4 && arrBase[i, j] == arrBase[i + 1, j])
{
arrBase[i, j] += arrBase[i + 1, j];
arrBase[i + 1, j] = 0;
}
}
}
for (int k = 0; k < 4; k++) //숫자를 위로 밀어냄
{
for (int i = 0; i < 3; i++)
{
if (i + 1 < 4 && arrBase[i, j] == 0)
{
arrBase[i, j] = arrBase[i + 1, j];
arrBase[i + 1, j] = 0;
}
}
}
}
break;
}
case (int)EDir.Bottom:
{
for (int j = 0; j < 4; j++) //열 전체를 검사함
{
for (int k = 0; k < 4; k++) //숫자를 위로 밀어냄
{
for (int i = 3; i > 0; i--)
{
if (i - 1 > -1 && arrBase[i, j] == 0)
{
arrBase[i, j] = arrBase[i - 1, j];
arrBase[i - 1, j] = 0;
}
}
}
for (int k = 0; k < 4; k++) //인접한 숫자를 더함
{
for (int i = 3; i > 0; i--)
{
if (i - 1 > -1 && arrBase[i, j] == arrBase[i - 1, j])
{
arrBase[i, j] += arrBase[i - 1, j];
arrBase[i - 1, j] = 0;
}
}
}
for (int k = 0; k < 4; k++) //숫자를 위로 밀어냄
{
for (int i = 3; i > 0; i--)
{
if (i - 1 > -1 && arrBase[i, j] == 0)
{
arrBase[i, j] = arrBase[i - 1, j];
arrBase[i - 1, j] = 0;
}
}
}
}
break;
}
}
}
public void Show()
{
for (int i = 0; i < arrBase.GetLength(0); i++)
{
for (int j = 0; j < arrBase.GetLength(1); j++)
{
Console.Write($"{arrBase[i, j]}\t");
}
Console.WriteLine();
}
Console.WriteLine("=============================");
}
}
}
'Console Programming > C# Console' 카테고리의 다른 글
2019-10-24 구조체 테스트 (0) | 2019.10.24 |
---|---|
2019-10-24 24일차 벡터 (0) | 2019.10.24 |
2019-10-23 23일차 Json 역직렬화, 직렬화 (0) | 2019.10.24 |
2019-10-23 23일차 엔트리 라인레인저스 내용을 코드로바꾸면 (0) | 2019.10.24 |
2019-10-23 23일차 재귀함수 4 x 5행렬에서 숫자 움직이기 (0) | 2019.10.24 |