C#/수업내용
03/19 1차원 2048게임 구현
박준희
2021. 3. 19. 17:06
728x90

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study06
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main");
new App();
}
}
}
App.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study06
{
public class App
{
public App()
{
Console.WriteLine("App");
GameLauncher gameLauncher = new GameLauncher();
gameLauncher.Init();
gameLauncher.StartGame();
}
}
}
GameLauncher.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study06
{
public class GameLauncher
{
public enum eDirection
{
Left,
Right
}
public int[] arrPlate;
public List<int> listTmp;
//생성자
public GameLauncher()
{
}
//초기화
public void Init()
{
Console.WriteLine("초기화 합니다.");
this.CreatePlate();
}
//새로운 판을 생성
public void CreatePlate()
{
Console.WriteLine("새로운 판을 생성합니다.");
this.arrPlate = new int[8];
}
//빈 자리 중 한 칸에 랜점하게 2 또는 4가 나온다.
public void CreateBlock()
{
Console.WriteLine();
Console.WriteLine("블럭을 생성합니다.");
listTmp = new List<int>();
for(int i = 0; i <arrPlate.Length; i++)
{
if(arrPlate[i] == 0)
{
listTmp.Add(i);
}
}
Random random = new Random();
int tmp = random.Next(0, listTmp.Count);
arrPlate[listTmp[tmp]] = random.Next() % 2 == 0 ? 2 : 4;
PrintPlate();
listTmp.Clear();
}
//게임 시작
public void StartGame()
{
CreateBlock();
while (true)
{
if(!BlockMovementCheck())
{
Console.WriteLine("Game Over");
break;
}
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.RightArrow)
{
Console.WriteLine("->");
CalculateBlocks(eDirection.Right);
CreateBlock();
}
else if (keyInfo.Key == ConsoleKey.LeftArrow)
{
Console.WriteLine("<-");
CalculateBlocks(eDirection.Left);
CreateBlock();
}
else if (keyInfo.Key == ConsoleKey.Escape)
{
this.EndGame();
break;
}
}
}
//게임 종료
public void EndGame()
{
Console.WriteLine("게임이 종료되었습니다.");
}
//판 출력
public void PrintPlate()
{
Console.Clear();
Console.WriteLine("조작 오른쪽 : ->, 왼쪽 : <-, 종료 : ESC");
Console.WriteLine("\n -------------------------------");
foreach (int i in arrPlate)
{
Console.Write("|{0, 2} ", i);
}
Console.WriteLine("|\n -------------------------------");
}
//합칠 수 있는 블럭이 있거나, 빈 공간이 있는가
public bool BlockMovementCheck()
{
bool plug = false;
for(int i = 0; i<arrPlate.Length; i++)
{
if(arrPlate[i] == 0)
{
plug = true;
}
}
for(int i = 0; i<arrPlate.Length-1; i++)
{
if(arrPlate[i] == arrPlate[i+1])
{
plug = true;
}
}
return plug;
}
//블럭 계산
public void CalculateBlocks(eDirection direction)
{
switch (direction)
{
case eDirection.Left:
{
int[] arrTmp = new int[arrPlate.Length];
int tmp = 0;
for (int i = 0; i < arrPlate.Length; i++)
{
if (arrPlate[i] != 0)
{
arrTmp[tmp] = arrPlate[i];
tmp++;
}
}
for(int i = 0; i< arrPlate.Length-1; i++)
{
if(arrTmp[i]== arrTmp[i+1])
{
arrTmp[i] += arrTmp[i + 1];
for(int j = i + 1; j< arrPlate.Length - 1; j++)
{
arrTmp[j] = arrTmp[j + 1];
}
}
}
arrTmp[arrTmp.Length-1] = 0;
arrPlate = arrTmp;
break;
}
case eDirection.Right:
{
int[] arrTmp = new int[arrPlate.Length];
int tmp = arrPlate.Length-1;
for (int i = arrPlate.Length-1; i >= 0; i--)
{
if (arrPlate[i] != 0)
{
arrTmp[tmp] = arrPlate[i];
tmp--;
}
}
for (int i = arrPlate.Length - 1; i > 0; i--)
{
if (arrTmp[i] == arrTmp[i - 1])
{
arrTmp[i] += arrTmp[i - 1];
for (int j = i - 1; j > 0; j--)
{
arrTmp[j] = arrTmp[j - 1];
}
}
}
arrTmp[0] = 0;
arrPlate = arrTmp;
break;
}
default:
break;
}
}
}
}
728x90