Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- Basic
- Programming
- Tutorial
- 오류
- Unity
- DP
- python
- w3school
- dynamic
- github
- guide
- C#
- c++
- 시작해요 언리얼 2022
- loop
- 프로그래밍
- Material
- 재귀
- 파이썬
- Unreal Engine 5
- parameter
- Algorithm
- dfs
- UE5
- W3Schools
- String
- 문제풀이
- 백준
- Class
- 기초
Archives
- Today
- Total
행복한 개구리
C# 21.03.19 ~ 21.과제10 본문
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
class GameLauncher
{
public int[] arrPlate;
//생성자
public GameLauncher()
{
}
public void Init()
{
Console.WriteLine("초기화 합니다.");
}
public void CreatePlate()
{
Console.WriteLine("새로운 판을 생성합니다.");
this.arrPlate = new int[4];
this.arrPlate[1] = 2; //한 칸은 그냥 깔아두고 시작해봄.
}
//빈 자리 중 한 칸에 랜덤하게 2 또는 4가 생성됨
public void CreateBlock()
{
Random rand = new Random();
int num = rand.Next(0, 101);
int i = 0;
while (i < 1)
{
Random rand2 = new Random();
int blockIndex = rand2.Next(0, this.arrPlate.Length);
if (this.arrPlate[blockIndex] == 0)
{
if (num > 50)
{
this.arrPlate[blockIndex] = 4;
break;
}
else
{
this.arrPlate[blockIndex] = 2;
break;
}
}
}
for (int a = 0; a < this.arrPlate.Length; a++)
{
Console.Write("{0, 5}", this.arrPlate[a]);
}
Console.WriteLine();
}
public void LeftAlignment() //왼쪽 정렬
{
for (int i = 0; i < this.arrPlate.Length; i++)
{
for (int j = 0; j < this.arrPlate.Length; j++)
{
if (j < this.arrPlate.Length - 1)
{
if (this.arrPlate[j] == 0 && this.arrPlate[j + 1] != 0)
{
this.arrPlate[j] = this.arrPlate[j + 1];
this.arrPlate[j + 1] = 0;
}
else if (this.arrPlate[j] != 0 && this.arrPlate[j + 1] != 0)
{
if (this.arrPlate[j] == this.arrPlate[j + 1])
{
this.arrPlate[j] += this.arrPlate[j + 1];
this.arrPlate[j + 1] = 0;
}
}
}
else
{
this.arrPlate[i] = this.arrPlate[i];
}
}
}
}
public void RightAlignment() //오른쪽 정렬
{
for (int i = this.arrPlate.Length - 1; i >= 0; i--)
{
for (int j = this.arrPlate.Length - 1; j >= 0; j--)
{
if (j > 0)
{
if (this.arrPlate[j] == 0 && this.arrPlate[j - 1] != 0)
{
this.arrPlate[j] = this.arrPlate[j - 1];
this.arrPlate[j - 1] = 0;
}
else if (this.arrPlate[j] != 0 && this.arrPlate[j - 1] != 0)
{
if (this.arrPlate[j] == this.arrPlate[j - 1])
{
this.arrPlate[j] += this.arrPlate[j - 1];
this.arrPlate[j - 1] = 0;
}
}
}
else
{
this.arrPlate[i] = this.arrPlate[i];
}
}
}
}
//게임오버
public bool isEnd()
{
int ValAmt = 0;
int ZeroAmt = 0;
for (int i = 0; i < this.arrPlate.Length; i++)
{
if (this.arrPlate[i] == 0)
{
ZeroAmt++;
}
}
if (ZeroAmt == 0) //배열에 0이 없고
{
for (int j = 0; j < this.arrPlate.Length - 1; j++)
{
if (this.arrPlate[j] == this.arrPlate[j + 1])
{
ValAmt++;
}
}
if (ValAmt == 0) //배열에 같은수가 나란히 없다면
{
Console.WriteLine("---------GAME__OVER---------");
return true;
}
}
return false;
}
//게임 시작
public void StartGame()
{
while (true)
{
ConsoleKeyInfo keyInfo = Console.ReadKey();
Console.WriteLine(keyInfo.Key);
if (keyInfo.Key == ConsoleKey.LeftArrow)
{
this.LeftAlignment();
this.CreateBlock();
Console.WriteLine("============================");
this.isEnd();
if (this.isEnd() == true)
{
break;
}
}
else if (keyInfo.Key == ConsoleKey.RightArrow)
{
this.RightAlignment();
this.CreateBlock();
Console.WriteLine("============================");
if(this.isEnd() == true)
{
break;
}
}
else if (keyInfo.Key == ConsoleKey.Escape)
{
this.EndGame();
break;
}
}
}
public void EndGame()
{
Console.WriteLine("게임을 종료합니다.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
class App
{
public App()
{
Console.WriteLine("App");
GameLauncher game = new GameLauncher();
game.CreatePlate();
game.CreateBlock();
game.StartGame();
}
}
}

================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class App
{
//생성된 인스턴스에 종족을 부여하는 방법
public App()
{
GameLauncher game = new GameLauncher();
game.CreateBoard();
game.CreateBlock();
game.StartGame();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class GameLauncher
{
public int[,] arrPlate;
public GameLauncher()
{
}
public void Swap(Indexes a, Indexes b)
{
int temp = this.arrPlate[a.rowIndex, a.colIndex];
this.arrPlate[a.rowIndex, a.colIndex] = this.arrPlate[b.rowIndex, b.colIndex];
this.arrPlate[b.rowIndex, b.colIndex] = temp;
}
public void CreateBoard()
{
this.arrPlate = new int[5, 5];
}
public bool IsWin()
{
for(int r = 0; r < this.arrPlate.GetLength(0); r++)
{
for(int c = 0; c < this.arrPlate.GetLength(1); c++)
{
if(this.arrPlate[r,c] == 2048)
{
return true;
}
}
}
return false;
}
public bool IsOver()
{
int Val = 0;
int row = this.arrPlate.GetLength(0);
int col = this.arrPlate.GetLength(1);
if(this.NullAmt() == 0)
{
for(int r = 0; r < row - 1; r++)
{
for(int c = 0; c < col - 1; c++)
{
if(this.arrPlate[r, c] == this.arrPlate[r + 1, c] || this.arrPlate[r,c] == this.arrPlate[r, c + 1])
{
Val++;
}
else if(this.arrPlate[row-1,col-1] == this.arrPlate[row-1,col-2] || this.arrPlate[row-1,col-1] == this.arrPlate[row - 2, col - 1])
{
Val++;
}
}
}
if (Val == 0)
{
return true;
}
}
return false;
}
public int NullAmt()
{
int isNull = 0;
for (int i = 0; i < this.arrPlate.GetLength(0); i++)
{
for (int j = 0; j < this.arrPlate.GetLength(1); j++)
{
if (this.arrPlate[i, j] == 0)
{
isNull++;
}
}
}
return isNull;
}
public void CreateBlock()
{
Random rand = new Random();
int per1 = rand.Next(1, 101);
int per2 = rand.Next(1, 101);
if (this.NullAmt() != 0)
{
int loop1 = 0;
while (loop1 < 1)
{
Random blockArray1 = new Random();
int randRow1 = blockArray1.Next(0, this.arrPlate.GetLength(0));
int randCol1 = blockArray1.Next(0, this.arrPlate.GetLength(1));
if (this.arrPlate[randRow1, randCol1] == 0)
{
int a = per1 > 30 ? 2 : 4;
this.arrPlate[randRow1, randCol1] = a;
loop1++;
int loop2 = 0;
while (loop2 < 1 && this.NullAmt() != 0) //빈칸이 없다면 만들지 못하게
{
Random blockArray2 = new Random();
int randRow2 = blockArray2.Next(0, this.arrPlate.GetLength(0));
int randCol2 = blockArray2.Next(0, this.arrPlate.GetLength(1));
if (this.arrPlate[randRow2, randCol2] == 0)
{
int b = per2 > 30 ? 2 : 4;
this.arrPlate[randRow2, randCol2] = b;
loop2++;
}
}
}
}
}
for (int i = 0; i < this.arrPlate.GetLength(0); i++)
{
for (int j = 0; j < this.arrPlate.GetLength(1); j++)
{
Console.Write("{0, 5}", this.arrPlate[i, j]);
}
Console.WriteLine();
}
}
public void LeftAlign()
{
int row = this.arrPlate.GetLength(0);
int col = this.arrPlate.GetLength(1);
int[,] arrPlate = this.arrPlate;
int loop = 0;
while (loop < col)
{
for (int r = 0; r < col; r++)
{
for (int c = 0; c < col-1; c++)
{
if (this.arrPlate[r, c] == 0 && c < col - 1)
{
Swap(new Indexes(r, c), new Indexes(r, c + 1));
}
else if (this.arrPlate[r,c] == this.arrPlate[r, c + 1])
{
this.arrPlate[r,c] += this.arrPlate[r, c + 1];
this.arrPlate[r, c + 1] = 0;
}
}
}
loop++;
}
}
public void RightAlign()
{
int row = this.arrPlate.GetLength(0);
int col = this.arrPlate.GetLength(1);
int[,] arrPlate = this.arrPlate;
int loop = 0;
while (loop < col)
{
for (int r = 0; r < col; r++)
{
for (int c = col-1; c > 0; c--)
{
if (this.arrPlate[r, c] == 0)
{
Swap(new Indexes(r, c), new Indexes(r, c - 1));
}
else if (this.arrPlate[r, c] == this.arrPlate[r, c - 1])
{
this.arrPlate[r, c] += this.arrPlate[r, c - 1];
this.arrPlate[r, c - 1] = 0;
}
}
}
loop++;
}
}
public void UpAlign()
{
int row = this.arrPlate.GetLength(0);
int col = this.arrPlate.GetLength(1);
int[,] arrPlate = this.arrPlate;
int loop = 0;
while (loop < col)
{
for (int c = 0; c < col; c++)
{
for (int r = 0; r < row - 1; r++)
{
if (this.arrPlate[r, c] == 0 && r < row - 1)
{
Swap(new Indexes(r, c), new Indexes(r + 1, c));
}
else if (this.arrPlate[r, c] == this.arrPlate[r + 1, c])
{
this.arrPlate[r, c] += this.arrPlate[r + 1, c];
this.arrPlate[r + 1, c] = 0;
}
}
}
loop++;
}
}
public void DownAlign()
{
int row = this.arrPlate.GetLength(0);
int col = this.arrPlate.GetLength(1);
int[,] arrPlate = this.arrPlate;
int loop = 0;
while (loop < col)
{
for (int c = 0; c < col; c++)
{
for (int r = row - 1; r >0 ; r--)
{
if (this.arrPlate[r, c] == 0)
{
Swap(new Indexes(r, c), new Indexes(r - 1, c));
}
else if (this.arrPlate[r, c] == this.arrPlate[r - 1, c])
{
this.arrPlate[r, c] += this.arrPlate[r - 1, c];
this.arrPlate[r - 1, c] = 0;
}
}
}
loop++;
}
}
public void StartGame()
{
while (true)
{
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.RightArrow)
{
Console.WriteLine("Right================");
this.RightAlign();
this.CreateBlock();
if (this.IsOver() == true)
{
Console.WriteLine("========GAME_OVER========");
break;
}
else if (this.IsWin() == true)
{
Console.WriteLine("=========YOU_WIN=========");
break;
}
}
else if (keyInfo.Key == ConsoleKey.LeftArrow)
{
Console.WriteLine("Left=================");
this.LeftAlign();
this.CreateBlock();
if (this.IsOver() == true)
{
Console.WriteLine("========GAME_OVER========");
break;
}
else if (this.IsWin() == true)
{
Console.WriteLine("=========YOU_WIN=========");
break;
}
}
else if (keyInfo.Key == ConsoleKey.UpArrow)
{
Console.WriteLine("Up===================");
this.UpAlign();
this.CreateBlock();
if (this.IsOver() == true)
{
Console.WriteLine("========GAME_OVER========");
break;
}
else if (this.IsWin() == true)
{
Console.WriteLine("=========YOU_WIN=========");
break;
}
}
else if (keyInfo.Key == ConsoleKey.DownArrow)
{
Console.WriteLine("Down=================");
this.DownAlign();
this.CreateBlock();
if (this.IsOver() == true)
{
Console.WriteLine("========GAME_OVER========");
break;
}
else if (this.IsWin() == true)
{
Console.WriteLine("=========YOU_WIN=========");
break;
}
}
else if (keyInfo.Key == ConsoleKey.Escape)
{
Console.WriteLine("===============================");
Console.WriteLine("ESC");
Console.WriteLine("========게임을 종료합니다.========");
break;
}
}
}
}
}


'C# > 수업과제' 카테고리의 다른 글
C# 21.03.24.과제12 (0) | 2021.03.24 |
---|---|
C# 21.03.22.과제 11 (0) | 2021.03.22 |
C# 21.03.18.과제 (0) | 2021.03.18 |
C# 21.03.17.과제 8 (0) | 2021.03.17 |
C# 21.03.16.과제 7 (0) | 2021.03.16 |