2048
-2,4,8,16,...2^으로 숫자가 커진다
-4x4꼴에서 빈칸 중 랜덤한 위치에 2가 2칸 생긴다.
-방향키로 움직인다: (상,하,좌,우) 키입력받기
-칸 움직이기 ->
1. 크기가 4인 배열을 만든 후 좌, 우 방향으로 움직이게 하였다.
-방향키 명과 움직인 후를 출력한다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2048
{
internal class Game
{
//멤버 변수
ConsoleKeyInfo info;
int[] arr = { 0,0,0,0};
int num;
//생성자
public Game()
{
}
public void GameStart()
{
Console.WriteLine("게임시작\n");
PrintBoard();
while (true)
{
ReadKey();
}
}
void ReadKey()
{
this.info = Console.ReadKey();
Console.WriteLine("{0}", this.info.Key);
if(info.Key == ConsoleKey.RightArrow)
{
MoveRight(info);
}
if (info.Key == ConsoleKey.LeftArrow)
{
MoveLeft(info);
}
}
void PrintBoard()
{
//보드 만들기 연습
for (int i = 0; i < arr.Length; i++)
{
Console.Write("{0}", arr[i]);
}
Console.WriteLine("\n");
}
void MoveLeft(ConsoleKeyInfo infoKey)
{//왼쪽 방향키 입력시 왼쪽으로 이동
if (info.Key == ConsoleKey.LeftArrow)
{
if (arr[num] != 0)
{
arr[num-1] = arr[num];
arr[num] = 0;
num--;
PrintBoard();
}
else
{
arr[0] = 1;
PrintBoard();
}
}
}
void MoveRight(ConsoleKeyInfo infoKey)
{//오른쪽 방향키 입력시 오른쪽으로 이동
// if (info.Key == ConsoleKey.RightArrow)
{
if (arr[num] != 0)
{
arr[num +1] = arr[num];
arr[num] = 0;
num++;
PrintBoard();
}
else
{
arr[0] = 1;
PrintBoard();
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2048
{
internal class App
{
public App()
{
Game game = new Game();
game.GameStart();
}
}
}
'C# 기초 Homework' 카테고리의 다른 글
복소수와 사원수, 벡터 (1) | 2023.08.13 |
---|---|
Unity-Homework-몬스터 이동&공격(수정중) (2) | 2023.08.06 |
미션-보상 만들기 (2) | 2023.07.31 |
디자인 패턴& 싱글톤 패턴,Dictionary 검색 (0) | 2023.07.26 |
20230719_Homework-디아블로_아이템사전 1~10 (0) | 2023.07.20 |