| using System; |
| using System.Collections.Generic; |
| using System.ComponentModel; |
| using System.Data; |
| using System.Drawing; |
| using System.Linq; |
| using System.Reflection; |
| using System.Text; |
| using System.Threading.Tasks; |
| using System.Windows.Forms; |
| |
| namespace _20200502 |
| { |
| public partial class form : Form |
| { |
| Random random; |
| |
| int[,] array; |
| bool start = true; |
| int totalScore; |
| |
| int x; |
| int y; |
| int randomNum1; |
| int randomNum2; |
| |
| Control newNumTemp; |
| |
| |
| public form() |
| { |
| InitializeComponent(); |
| |
| this.Init(); |
| this.StartGame(); |
| |
| this.TableInit(); |
| |
| this.IsNumberBackgroundColor(); |
| |
| } |
| |
| public void Init() |
| { |
| this.random = new Random(); |
| this.newNumTemp = new Control(); |
| } |
| |
| |
| public void UpDataSort() |
| { |
| int[,] newArr = new int[4, 4]; |
| |
| for (int i = 0; i < 4; i++) |
| { |
| for (int j = 0; j < 4; j++) |
| { |
| if (this.array[i, j] > 0) |
| { |
| for (int k = 0; k < newArr.GetLength(1); k++) |
| { |
| if (newArr[k, j] == 0) |
| { |
| if (k != 0) |
| { |
| if (newArr[k - 1, j] == this.array[i, j]) |
| { |
| newArr[k - 1, j] = this.array[i, j] * 2; |
| this.totalScore += newArr[k - 1, j]; |
| break; |
| } |
| } |
| newArr[k, j] = this.array[i, j]; |
| break; |
| } |
| } |
| } |
| } |
| } |
| this.array = newArr; |
| } |
| |
| |
| |
| |
| public void DownDataSort() |
| { |
| int[,] newArr = new int[4, 4]; |
| |
| |
| for (int i = 3; i >= 0; i--) |
| { |
| for (int j = 3; j >= 0; j--) |
| { |
| if (this.array[i, j] > 0) |
| { |
| for (int k = 3; k >= 0; k--) |
| { |
| if (newArr[k, j] == 0) |
| { |
| if (k != 3) |
| { |
| if (newArr[k + 1, j] == this.array[i, j]) |
| { |
| newArr[k + 1, j] = this.array[i, j] * 2; |
| this.totalScore += newArr[k + 1, j]; |
| break; |
| } |
| } |
| newArr[k, j] = this.array[i, j]; |
| break; |
| } |
| } |
| } |
| } |
| } |
| this.array = newArr; |
| } |
| |
| |
| |
| |
| public void LeftDataSort() |
| { |
| int[,] newArr = new int[4, 4]; |
| |
| for (int i = 0; i < 4; i++) |
| { |
| for (int j = 0; j < 4; j++) |
| { |
| |
| if (this.array[j, i] > 0) |
| { |
| |
| |
| for (int k = 0; k < newArr.GetLength(1); k++) |
| { |
| |
| if (newArr[j, k] == 0) |
| { |
| if (k != 0) |
| { |
| if (newArr[j, k - 1] == this.array[j, i]) |
| { |
| newArr[j, k - 1] = this.array[j, i] * 2; |
| this.totalScore += newArr[j, k - 1]; |
| break; |
| } |
| } |
| newArr[j, k] = this.array[j, i]; |
| break; |
| } |
| } |
| } |
| } |
| } |
| this.array = newArr; |
| } |
| |
| |
| |
| |
| public void RightDataSort() |
| { |
| int[,] newArr = new int[4, 4]; |
| |
| for (int i = 3; i >= 0; i--) |
| { |
| for (int j = 0; j < 4; j++) |
| { |
| |
| if (this.array[j, i] > 0) |
| { |
| |
| for (int k = 3; k >= 0; k--) |
| { |
| |
| if (newArr[j, k] == 0) |
| { |
| if (k != 3) |
| { |
| if (newArr[j, k + 1] == this.array[j, i]) |
| { |
| newArr[j, k + 1] = this.array[j, i] * 2; |
| this.totalScore += newArr[j, k + 1]; |
| break; |
| } |
| } |
| newArr[j, k] = this.array[j, i]; |
| break; |
| } |
| } |
| } |
| } |
| } |
| this.array = newArr; |
| } |
| |
| |
| |
| |
| public void TableInit() |
| { |
| this.array = new int[4, 4]; |
| |
| foreach (Control ctrl in tableLayoutPanel1.Controls) |
| { |
| if (ctrl is Label) |
| { |
| var row = tableLayoutPanel1.GetRow(ctrl); |
| var col = tableLayoutPanel1.GetColumn(ctrl); |
| |
| this.array[row, col] = 0; |
| |
| |
| if (ctrl.Text.Length > 0) |
| { |
| int myNum = int.Parse(ctrl.Text); |
| this.array[row, col] = myNum; |
| } |
| } |
| } |
| this.IsNumberBackgroundColor(); |
| } |
| |
| |
| |
| public void NewTable() |
| { |
| foreach (Control ctrl in tableLayoutPanel1.Controls) |
| { |
| if (ctrl is Label) |
| { |
| ctrl.Text = ""; |
| } |
| } |
| this.array = new int[4, 4]; |
| } |
| |
| |
| public void StartGame() |
| { |
| this.RandomXY(); |
| this.RandomNum(); |
| tableLayoutPanel1.GetControlFromPosition(y, x).Text = randomNum2.ToString(); |
| |
| this.RandomXY(); |
| this.RandomNum(); |
| tableLayoutPanel1.GetControlFromPosition(y, x).Text = randomNum1.ToString(); |
| } |
| |
| |
| public void RandomXY() |
| { |
| |
| while (true) |
| { |
| int tempX = random.Next(0, 4); |
| int tempY = random.Next(0, 4); |
| |
| if (this.x != tempX && this.y != tempY) |
| { |
| this.x = tempX; |
| this.y = tempY; |
| break; |
| } |
| } |
| } |
| |
| |
| |
| public void RandomNum() |
| { |
| while (true) |
| { |
| int tempNum1 = random.Next(2, 5); |
| int tempNum2 = random.Next(2, 5); |
| |
| bool cheeck1 = false; |
| bool cheeck2 = false; |
| |
| |
| if (this.start) |
| { |
| if (tempNum2 == 2 || tempNum2 == 4) |
| { |
| this.randomNum1 = tempNum2; |
| cheeck1 = true; |
| } |
| } |
| |
| if (tempNum1 == 2 || tempNum1 == 4) |
| { |
| this.randomNum2 = tempNum1; |
| cheeck2 = true; |
| } |
| |
| if (cheeck1 && cheeck2) |
| { |
| break; |
| } |
| } |
| |
| } |
| |
| |
| public void CreateNewNum() |
| { |
| while (true) |
| { |
| this.RandomXY(); |
| if (this.array[this.x, this.y] == 0) |
| { |
| break; |
| } |
| } |
| this.newNumTemp.ForeColor = Color.Black; |
| this.RandomNum(); |
| this.array[this.x, this.y] = this.randomNum2; |
| tableLayoutPanel1.GetControlFromPosition(y, x).Text = this.randomNum2.ToString(); |
| |
| tableLayoutPanel1.GetControlFromPosition(y, x).ForeColor = Color.IndianRed; |
| |
| this.newNumTemp = tableLayoutPanel1.GetControlFromPosition(y, x); |
| |
| logBox.Items.Add($"신규랜덤난수:[{x},{y}]:{this.randomNum2}"); |
| } |
| |
| |
| |
| |
| public void GameReset() |
| { |
| this.array = new int[4, 4]; |
| this.start = true; |
| this.totalScore = 0; |
| |
| |
| this.ViewReset(); |
| this.StartGame(); |
| this.TableInit(); |
| this.SetScoreView(); |
| } |
| |
| |
| public void ViewReset() |
| { |
| foreach (Control ctrl in tableLayoutPanel1.Controls) |
| { |
| if (ctrl is Label) |
| { |
| ctrl.Text = ""; |
| } |
| } |
| logBox.Items.Clear(); |
| } |
| |
| |
| public void UpdateView() |
| { |
| foreach (Control ctrl in tableLayoutPanel1.Controls) |
| { |
| if (ctrl is Label) |
| { |
| var row = tableLayoutPanel1.GetRow(ctrl); |
| var col = tableLayoutPanel1.GetColumn(ctrl); |
| ctrl.Text = ""; |
| |
| |
| if (this.array[row, col] > 0) |
| { |
| ctrl.Text = this.array[row, col].ToString(); |
| } |
| } |
| } |
| } |
| |
| |
| public bool EndGameCheck() |
| { |
| foreach (var item in this.array) |
| { |
| if (item == 0) |
| { |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| |
| public bool ClearGameCheck() |
| { |
| foreach (var item in this.array) |
| { |
| if (item.Equals(2048)) |
| { |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| |
| public void IsNumberBackgroundColor() |
| { |
| int checkNum = 0; |
| foreach (Control ctrl in tableLayoutPanel1.Controls) |
| { |
| if (ctrl is Label) |
| { |
| ctrl.BackColor = Color.Gainsboro; |
| |
| if (ctrl.Text != "") |
| { |
| checkNum = int.Parse(ctrl.Text); |
| |
| if (checkNum == 8) |
| { |
| ctrl.ForeColor = Color.WhiteSmoke; |
| ctrl.BackColor = Color.LightSalmon; |
| } |
| else if (checkNum == 16) |
| { |
| ctrl.ForeColor = Color.WhiteSmoke; |
| ctrl.BackColor = Color.LightCoral; |
| } |
| else if (checkNum == 32) |
| { |
| ctrl.ForeColor = Color.WhiteSmoke; |
| ctrl.BackColor = Color.Coral; |
| } |
| else if (checkNum >= 64) |
| { |
| ctrl.ForeColor = Color.WhiteSmoke; |
| ctrl.BackColor = Color.Tomato; |
| } |
| else |
| { |
| ctrl.BackColor = Color.Silver; |
| ctrl.ForeColor = Color.Black; |
| } |
| } |
| |
| if (ctrl.Text == "") |
| { |
| ctrl.BackColor = Color.Silver; |
| ctrl.ForeColor = Color.Black; |
| } |
| } |
| } |
| } |
| |
| |
| public void SetScoreView() |
| { |
| scoreLabel.Text = this.totalScore.ToString(); |
| } |
| |
| |
| public void ArrayPrint() |
| { |
| for (int i = 0; i < this.array.GetLength(0); i++) |
| { |
| for (int j = 0; j < this.array.GetLength(1); j++) |
| { |
| Console.Write("[{0}, {1}]:{2} ㅣ", i, j, this.array[i, j]); |
| } |
| Console.WriteLine(); |
| } |
| Console.WriteLine(); |
| } |
| |
| |
| private void UpBtn_Click(object sender, EventArgs e) |
| { |
| if (ClearGameCheck()) { MessageBox.Show("게임 클리어!!"); } |
| if (EndGameCheck()) |
| { |
| this.UpDataSort(); |
| this.UpdateView(); |
| this.CreateNewNum(); |
| this.IsNumberBackgroundColor(); |
| this.SetScoreView(); |
| } |
| else |
| { |
| MessageBox.Show("게임 종료"); |
| } |
| } |
| |
| private void DownBtn_Click(object sender, EventArgs e) |
| { |
| if (ClearGameCheck()) { MessageBox.Show("게임 클리어!!"); } |
| if (EndGameCheck()) |
| { |
| this.DownDataSort(); |
| this.UpdateView(); |
| this.CreateNewNum(); |
| this.IsNumberBackgroundColor(); |
| this.SetScoreView(); |
| } |
| else |
| { |
| MessageBox.Show("게임 종료"); |
| } |
| } |
| |
| private void LeftBtn_Click(object sender, EventArgs e) |
| { |
| if (ClearGameCheck()) { MessageBox.Show("게임 클리어!!"); } |
| if (EndGameCheck()) |
| { |
| this.LeftDataSort(); |
| this.UpdateView(); |
| this.CreateNewNum(); |
| this.IsNumberBackgroundColor(); |
| this.SetScoreView(); |
| } |
| else |
| { |
| MessageBox.Show("게임 종료"); |
| } |
| } |
| |
| private void RightBtn_Click(object sender, EventArgs e) |
| { |
| if (ClearGameCheck()) { MessageBox.Show("게임 클리어!!"); } |
| if (EndGameCheck()) |
| { |
| this.RightDataSort(); |
| this.UpdateView(); |
| this.CreateNewNum(); |
| this.IsNumberBackgroundColor(); |
| this.SetScoreView(); |
| } |
| else |
| { |
| MessageBox.Show("게임 종료"); |
| } |
| } |
| |
| |
| |
| |
| private void button1_Click(object sender, EventArgs e) |
| { |
| this.GameReset(); |
| } |
| |
| |
| private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e) |
| { |
| |
| } |
| |
| } |
| } |