유니티3D 프로그래밍
C# 2주차 5일 수업 내용 및 과제 : 2차원 배열 및 2048 프로그램(21.03.19) 본문
2차원 배열 연습
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
public struct Indexes
{
public int rowIndex;
public int colIndex;
public Indexes(int rowIndex, int colIndex)
{
this.rowIndex = rowIndex;
this.colIndex = colIndex;
}
}
class App
{
private int[,] arr;
private int row;
private int col;
//생성자
public App()
{
Console.WriteLine("App");
//int형 2차원 배열 초기화
this.arr = new int[4, 3];//행(row), 열(col)
//차원의 길이 출력
//지정한 요소수를 나타낸다
this.row = arr.GetLength(0); //1차원 (row) : 4
this.col = arr.GetLength(1); //2차원 (col) : 3
//배열 요소의 값 할당.
arr[1, 2] = 100;
PrintArray();
arr[2, 0] = 300;
PrintArray();
Indexes IndexesA = new Indexes(1, 2);
Indexes IndexesB = new Indexes(2, 0);
Swap(IndexesA, IndexesB);
PrintArray();
}
private void Swap(Indexes a, Indexes b)
{
int temp = arr[b.rowIndex, b.colIndex];
arr[b.rowIndex, b.colIndex] = arr[a.rowIndex, a.colIndex];
arr[a.rowIndex, a.colIndex] = temp;
}
private void PrintArray()
{
Console.WriteLine(arr[1, 2]);
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
Console.Write("arr[{0},{1}] ", i, j);
}
Console.WriteLine();
}
Console.WriteLine("**************************");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
Console.Write("{0, 3} ", arr[i, j]);
}
Console.WriteLine();
}
}
}
}
2048 게임 1차원 배열 구현
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
public class App
{
//생성자
public App()
{
Console.WriteLine("App");
GameLauncher gameLauncher = new GameLauncher();
gameLauncher.StactGame();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
class GameLauncher
{
private int[] arrPlate;
public GameLauncher()
{
}
public void Init()
{
Console.WriteLine("초기화 합니다");
this.CreatePlate();
}
public void CreatePlate()
{
Console.WriteLine("새로운 판을 생성합니다.");
this.arrPlate = new int[4];
ArrayAddRandom();
PrintPlate(arrPlate);
}
//게임 시작
public void StactGame()
{
bool gameEnd = true;
Init();
while(gameEnd)
{
ConsoleKeyInfo keyInfo;
keyInfo = GameControll();
if(keyInfo.Key == ConsoleKey.LeftArrow)
{
LeftShift();
}
if (keyInfo.Key == ConsoleKey.RightArrow)
{
RightShift();
}
PrintPlate(arrPlate);
gameEnd = EndGame();
}
}
//게임 종료
public bool EndGame()
{
bool isZero = false;
for(int i = 0; i < arrPlate.Length; i++)
{
if(arrPlate[i] != 0)
{
isZero = false;
}
else
{
return true;
}
}
Console.WriteLine("게임이 종료되었습니다.");
return isZero;
}
public ConsoleKeyInfo GameControll()
{
Console.Write("방향키를 입력합니다 : ");
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.LeftArrow)
{
Console.WriteLine("<-");
}
else if (keyInfo.Key == ConsoleKey.RightArrow)
{
Console.WriteLine("->");
}
Console.WriteLine(keyInfo.Key);
return keyInfo;
}
public void PrintPlate(int[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
Console.Write("[{0}] ", arr[i]);
}
Console.WriteLine();
}
//왼쪽으로 이동시키기
public void LeftShift()
{
int[] checkArr = new int[4];
for (int i = 0; i < arrPlate.Length; i++)
{
checkArr[i] = arrPlate[i];
}
for (int i = 0; i < checkArr.Length; i++)
{
for(int j = 0; j < checkArr.Length; j++)
{
if(checkArr[i] == 0 && checkArr[j] != 0 && j >= i)
{
checkArr[i] = checkArr[j];
checkArr[j] = 0;
}
if (i + 1 < checkArr.Length)
{
if (checkArr[i] != 0 && checkArr[i + 1] != 0)
{
if (checkArr[i] == checkArr[i + 1])
{
checkArr[i] += checkArr[i + 1];
checkArr[i + 1] = 0;
}
}
else if(checkArr[i] != 0 && checkArr[i + 1] == 0)
{
if( j > i && checkArr[j] != 0)
{
if(checkArr[i] == checkArr[j])
{
checkArr[i] += checkArr[j];
checkArr[j] = 0;
}
else
{
break;
}
}
}
}
}
}
if (isMove(checkArr))
{
arrPlate = checkArr;
ArrayAddRandom();
}
}
//오른쪽으로 이동시키기
public void RightShift()
{
int[] checkArr = new int[8];
for (int i = 0; i < arrPlate.Length; i++)
{
checkArr[i] = arrPlate[i];
}
for (int i = checkArr.Length-1; i >= 0; i--)
{
for (int j = checkArr.Length - 1; j >= 0; j--)
{
if (checkArr[i] == 0 && checkArr[j] != 0 && j <= i)
{
checkArr[i] = checkArr[j];
checkArr[j] = 0;
}
if (i - 1 > 0)
{
if (checkArr[i] != 0 && checkArr[i - 1] != 0)
{
if (checkArr[i] == checkArr[i - 1])
{
checkArr[i] += checkArr[i - 1];
checkArr[i - 1] = 0;
}
}
else if (checkArr[i] != 0 && checkArr[i - 1] == 0)
{
if (j < i && checkArr[j] != 0)
{
if (checkArr[i] == checkArr[j])
{
checkArr[i] += checkArr[j];
checkArr[j] = 0;
}
else
{
break;
}
}
}
}
}
}
if (isMove(checkArr))
{
arrPlate = checkArr;
ArrayAddRandom();
}
}
//배열에 랜덤한 위치에 2 또는 4의 값을 추가.
public void ArrayAddRandom()
{
while(true)
{
Random rand = new Random();
int num = rand.Next(0, arrPlate.Length);
int num2 = rand.Next(1, 3);
if(arrPlate[num] == 0)
{
arrPlate[num] = (num2 / 2 == 1) ? 2 : 4;
break;
}
else
{
continue;
}
}
}
public bool isMove(int[] arr)
{
bool move = false;
for(int i = 0; i < arrPlate.Length; i++)
{
if (arrPlate[i] != arr[i])
move = true;
}
return move;
}
}
}
2048 2차원 배열 구현
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
public class App
{
//생성자
public App()
{
Console.WriteLine("App");
GameLauncher2 gameLauncher2 = new GameLauncher2();
gameLauncher2.StactGame();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
class GameLauncher2
{
private int[,] arrPlate;
public GameLauncher2()
{
}
public void Init()
{
Console.WriteLine("초기화 합니다");
this.CreatePlate();
}
public void CreatePlate()
{
Console.WriteLine("새로운 판을 생성합니다.");
this.arrPlate = new int[4, 4];
ArrayAddRandom();
PrintPlate(arrPlate);
}
//게임 시작
public void StactGame()
{
bool gameEnd = true;
Init();
while (gameEnd)
{
ConsoleKeyInfo keyInfo;
keyInfo = GameControll();
if (keyInfo.Key == ConsoleKey.LeftArrow || keyInfo.Key == ConsoleKey.RightArrow
|| keyInfo.Key == ConsoleKey.UpArrow || keyInfo.Key == ConsoleKey.DownArrow)
{
SetLineShift(keyInfo.Key);
} else if(keyInfo.Key == ConsoleKey.Escape)
{
Console.WriteLine("ESC를 눌러 게임을 종료합니다.");
break;
}
PrintPlate(arrPlate);
gameEnd = EndGame();
}
Console.WriteLine("게임이 종료되었습니다.");
}
//게임 종료
public bool EndGame()
{
bool isZeroOrWin = true;
int arrCheck = 0;
if (AllScanZero())
{
for (int i = 0; i < arrPlate.GetLength(0); i++)
{
for (int j = 0; j < arrPlate.GetLength(1); j++)
{
arrCheck = arrPlate[i, j];
if (arrCheck == 2048)
{
Console.WriteLine("게임을 승리하셨습니다!.");
isZeroOrWin = false;
}
if (i + 1 < arrPlate.GetLength(1))
{
if (j + 1 < arrPlate.GetLength(0))
{
if (arrCheck == arrPlate[i, j + 1] || arrCheck == arrPlate[i + 1, j])
{
isZeroOrWin = true;
return isZeroOrWin;
}
}
if (j == 3 && arrCheck == arrPlate[i + 1, j])
{
isZeroOrWin = true;
return isZeroOrWin;
}
if (i == 3 && arrCheck == arrPlate[i, j + 1])
{
isZeroOrWin = true;
return isZeroOrWin;
}
}
isZeroOrWin = false;
}
}
}
return isZeroOrWin;
}
public bool AllScanZero()
{
bool isFull = true;
for (int i = 0; i < arrPlate.GetLength(0); i++)
{
for (int j = 0; j < arrPlate.GetLength(1); j++)
{
if(arrPlate[i, j] == 0)
{
isFull = false;
break;
}
}
}
return isFull;
}
public ConsoleKeyInfo GameControll()
{
Console.WriteLine("방향키를 입력합니다 : ");
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.LeftArrow)
{
Console.WriteLine("<-");
}
else if (keyInfo.Key == ConsoleKey.RightArrow)
{
Console.WriteLine("->");
}
else if (keyInfo.Key == ConsoleKey.UpArrow)
{
Console.WriteLine("A");
Console.WriteLine(" I");
}
else if (keyInfo.Key == ConsoleKey.DownArrow)
{
Console.WriteLine("I");
Console.WriteLine(" V");
}
Console.WriteLine(keyInfo.Key);
return keyInfo;
}
public void PrintPlate(int[,] arr)
{
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write("[ {0} ] ", arr[i, j]);
}
Console.WriteLine();
}
}
//입력한 방향에 따라 이동
public void SetLineShift(ConsoleKey key)
{
int[,] checkArr = new int[arrPlate.GetLength(0), arrPlate.GetLength(1)];
for (int i = 0; i < arrPlate.GetLength(0); i++)
{
//2차원 배열의 한 줄을 1차원 배열로 받아서 정렬 시킨 후
//2차원 배열에 저장
int[] saveArr = SingleDirectionShift(i, key);
if(key == ConsoleKey.DownArrow || key == ConsoleKey.UpArrow)
{
for (int j = 0; j < arrPlate.GetLength(1); j++)
{
checkArr[j, i] = saveArr[j];
}
}
if (key == ConsoleKey.RightArrow || key == ConsoleKey.LeftArrow)
{
for (int j = 0; j < arrPlate.GetLength(1); j++)
{
checkArr[i, j] = saveArr[j];
}
}
}
//움직임이 없으면 랜덤한 위치에 값을 추가하지 않음
isMove(checkArr);
}
//배열에 랜덤한 위치에 2 또는 4의 값을 추가.
public void ArrayAddRandom()
{
while (true)
{
Random rand = new Random();
int row = rand.Next(0, arrPlate.GetLength(0));
int col = rand.Next(0, arrPlate.GetLength(1));
int num2 = rand.Next(1, 3);
if (arrPlate[row, col] == 0)
{
arrPlate[row, col] = (num2 / 2 == 1) ? 2 : 4;
break;
}
else
{
continue;
}
}
}
//배열의 움직임이 있는가 없는가
public void isMove(int[,] arr)
{
bool move = false;
for (int i = 0; i < arrPlate.GetLength(0); i++)
{
for (int j = 0; j < arrPlate.GetLength(1); j++)
{
if (arrPlate[i, j] != arr[i, j])
{
move = true;
}
}
}
if(move == true)
{
arrPlate = arr;
ArrayAddRandom();
}
}
//2차원 배열의 한줄을 1차원 배열로 만들어서 정렬 후 반환
public int[] SingleDirectionShift(int k, ConsoleKey key)
{
int[] checkArr = SetArr(k, key);
if(key == ConsoleKey.LeftArrow || key == ConsoleKey.UpArrow)
{
checkArr = SetLeftAndUp(checkArr);
}
if (key == ConsoleKey.RightArrow || key == ConsoleKey.DownArrow)
{
checkArr = SetRightAndDown(checkArr);
}
return checkArr;
}
//초기화할 배열을 선언하는 메소드
public int[] SetArr(int k, ConsoleKey key)
{
int[] setArr = new int[arrPlate.GetLength(0)];
if (key == ConsoleKey.RightArrow || key == ConsoleKey.LeftArrow)
{
for (int i = 0; i < arrPlate.GetLength(0); i++)
{
setArr[i] = arrPlate[k, i];
}
}
if (key == ConsoleKey.UpArrow || key == ConsoleKey.DownArrow)
{
for (int i = 0; i < arrPlate.GetLength(0); i++)
{
setArr[i] = arrPlate[i, k];
}
}
return setArr;
}
//오른쪽과 아래쪽으로 하는 배치는 구조적으로 같아서 통일시킨다
public int[] SetRightAndDown(int[] checkArr)
{
for (int i = checkArr.Length - 1; i >= 0; i--)
{
for (int j = checkArr.Length - 1; j >= 0; j--)
{
if (checkArr[i] == 0 && checkArr[j] != 0 && j < i)
{
checkArr[i] = checkArr[j];
checkArr[j] = 0;
}
if (checkArr[i] != 0 && i - 1 >= 0)
{
if (checkArr[i - 1] != 0 && checkArr[i] == checkArr[i - 1])
{
checkArr[i] += checkArr[i - 1];
checkArr[i - 1] = 0;
}
if (checkArr[i - 1] == 0 && j < i && checkArr[j] != 0)
{
if (checkArr[i] == checkArr[j])
{
checkArr[i] += checkArr[j];
checkArr[j] = 0;
}
else
{
break;
}
}
}
}
}
return checkArr;
}
//왼쪽과 위쪽으로 하는 배치는 구조적으로 같아서 통일시킨다.
public int[] SetLeftAndUp(int[] checkArr)
{
for (int i = 0; i < checkArr.Length; i++)
{
for (int j = 0; j < checkArr.Length; j++)
{
if (checkArr[i] == 0 && checkArr[j] != 0 && j > i)
{
checkArr[i] = checkArr[j];
checkArr[j] = 0;
}
if (checkArr[i] != 0 && i + 1 < checkArr.Length)
{
if (checkArr[i + 1] != 0 && checkArr[i] == checkArr[i + 1])
{
checkArr[i] += checkArr[i + 1];
checkArr[i + 1] = 0;
}
if (checkArr[i + 1] == 0 && j > i && checkArr[j] != 0)
{
if (checkArr[i] == checkArr[j])
{
checkArr[i] += checkArr[j];
checkArr[j] = 0;
}
else
{
break;
}
}
}
}
}
return checkArr;
}
}
}
'C# > 수업과제' 카테고리의 다른 글
C# 3주차 3일 수업 과제 : File (21.03.24) (0) | 2021.03.24 |
---|---|
C# 3주차 1일 수업 과제 : Delegate, Action, Callback (21.03.22) (0) | 2021.03.22 |
C# 2주차 3일 수업 과제 : Stack (21.03.17) (0) | 2021.03.17 |
C# 2주차 2일 수업 과제 (21.03.16) (0) | 2021.03.16 |
C# 2주차 1일 수업 과제 (21.03.15) (0) | 2021.03.15 |