2048
Unity3D/C# 2021. 3. 19. 14:14반응형
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
public class GameLauncher
{
private int[] arrPlate; //판
//생성자
public GameLauncher()
{
}
//초기화
public void Init()
{
Console.WriteLine("초기화 합니다.");
this.CreatePlate();
}
//새로운 판을 생성
public void CreatePlate()
{
Console.WriteLine("새로운 판을 생성합니다.");
this.arrPlate = new int[4];
this.CreateBlock();
}
//빈 자리 중 한 칸에 랜덤하게 2 또는 4가 나온다.
public void CreateBlock()
{
// Console.WriteLine("CreateBlock");
PrintPlate();
List<int> list = new List<int>();
int idx = 0;
for (int i = 0; i < this.arrPlate.Length; i++)
{
if (this.arrPlate[i] == 0)
{
list.Add(i);
}
}
foreach (int a in list)
{
Console.Write("{0} ", a);
}
Console.WriteLine("list.Count: {0}", list.Count);
if (list.Count == 0)
{
Console.WriteLine("판이 꽉참");
return;
}
System.Random random = new System.Random();
int index = list[random.Next(list.Count)];
Console.WriteLine("index: {0}", index);
var num = random.Next(1, 3) * 2;
this.arrPlate[index] = num;
PrintPlate();
}
private void PrintPlate()
{
for (int i = 0; i < this.arrPlate.Length; i++)
{
Console.Write(this.arrPlate[i] + " ");
}
Console.WriteLine();
}
//게임 시작
public void StartGame()
{
while (true)
{
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.LeftArrow)
{
Console.WriteLine("<-");
CreateBlock();
}
else if (keyInfo.Key == ConsoleKey.RightArrow)
{
Console.WriteLine("->");
CreateBlock();
}
else if (keyInfo.Key == ConsoleKey.Escape)
{
this.EndGame();
break;
}
}
}
//게임 종료
public void EndGame()
{
Console.WriteLine("게임 종료");
}
}
}
반응형
'Unity3D > C#' 카테고리의 다른 글
InvertedSphere (0) | 2021.07.30 |
---|---|
싱글톤(Singletone) vs C#정적클래스(static class) 차이점 (0) | 2021.03.25 |
IComparable.CompareTo(Object) 메서드 (0) | 2021.03.16 |
c# async await (0) | 2021.03.10 |
.NET의 \0(U + 0000) 과 String (0) | 2020.12.02 |