From 325816fa607062551df1808c93ac0594afb66dd5 Mon Sep 17 00:00:00 2001 From: holysnipz <9103583+holysnipz@users.noreply.github.com> Date: Mon, 17 Dec 2018 12:20:49 +0700 Subject: [PATCH 1/4] Added keyboard support for Arrow and WASD keys but are there anyone playing 2048 by using WASD ? --- TwoZeroFourEightView.Designer.cs | 1 + TwoZeroFourEightView.cs | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/TwoZeroFourEightView.Designer.cs b/TwoZeroFourEightView.Designer.cs index af4b1f9..6be8f36 100644 --- a/TwoZeroFourEightView.Designer.cs +++ b/TwoZeroFourEightView.Designer.cs @@ -325,6 +325,7 @@ private void InitializeComponent() this.Controls.Add(this.lbl00); this.Name = "TwoZeroFourEightView"; this.Text = "Form1"; + this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TwoZeroFourEightView_KeyDown); this.ResumeLayout(false); } diff --git a/TwoZeroFourEightView.cs b/TwoZeroFourEightView.cs index 912b9bc..9cf0a76 100644 --- a/TwoZeroFourEightView.cs +++ b/TwoZeroFourEightView.cs @@ -99,5 +99,27 @@ private void btnDown_Click(object sender, EventArgs e) controller.ActionPerformed(TwoZeroFourEightController.DOWN); } + private void TwoZeroFourEightView_KeyDown(object sender, KeyEventArgs e) + { + switch (e.KeyCode) + { + case Keys.Up: + case Keys.W: + controller.ActionPerformed(TwoZeroFourEightController.UP); + break; + case Keys.Down: + case Keys.S: + controller.ActionPerformed(TwoZeroFourEightController.DOWN); + break; + case Keys.Left: + case Keys.A: + controller.ActionPerformed(TwoZeroFourEightController.LEFT); + break; + case Keys.Right: + case Keys.D: + controller.ActionPerformed(TwoZeroFourEightController.RIGHT); + break; + } + } } } From 86b34356ff0e03bb7fa8ad9721407076c1f3cbb2 Mon Sep 17 00:00:00 2001 From: holysnipz <9103583+holysnipz@users.noreply.github.com> Date: Mon, 17 Dec 2018 13:13:39 +0700 Subject: [PATCH 2/4] Fix arrow key event, Added Score system --- TwoZeroFourEightModel.cs | 14 ++++++++ TwoZeroFourEightView.Designer.cs | 62 ++++++++++++++++++++------------ TwoZeroFourEightView.cs | 28 ++++++++++----- 3 files changed, 74 insertions(+), 30 deletions(-) diff --git a/TwoZeroFourEightModel.cs b/TwoZeroFourEightModel.cs index be3a993..10f6df0 100644 --- a/TwoZeroFourEightModel.cs +++ b/TwoZeroFourEightModel.cs @@ -54,6 +54,20 @@ private void AddRandomSlot() } } + public int GetScore() + { + int _s = 0; + + for (int i = 0; i < boardSize; i++) + { + for (int j=0; j < boardSize; j++) + { + _s += board[i, j]; + } + } + return _s; + } + // Perform shift and merge to the left of the given array. protected bool ShiftAndMerge(int[] buffer) { diff --git a/TwoZeroFourEightView.Designer.cs b/TwoZeroFourEightView.Designer.cs index 6be8f36..5537731 100644 --- a/TwoZeroFourEightView.Designer.cs +++ b/TwoZeroFourEightView.Designer.cs @@ -48,6 +48,7 @@ private void InitializeComponent() this.btnUp = new System.Windows.Forms.Button(); this.btnRight = new System.Windows.Forms.Button(); this.btnDown = new System.Windows.Forms.Button(); + this.lbScore = new System.Windows.Forms.Label(); this.SuspendLayout(); // // lbl00 @@ -55,7 +56,7 @@ private void InitializeComponent() this.lbl00.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl00.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl00.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl00.Location = new System.Drawing.Point(0, 0); + this.lbl00.Location = new System.Drawing.Point(20, 40); this.lbl00.Margin = new System.Windows.Forms.Padding(0); this.lbl00.Name = "lbl00"; this.lbl00.Size = new System.Drawing.Size(75, 75); @@ -68,7 +69,7 @@ private void InitializeComponent() this.lbl01.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl01.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl01.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl01.Location = new System.Drawing.Point(75, 0); + this.lbl01.Location = new System.Drawing.Point(95, 40); this.lbl01.Margin = new System.Windows.Forms.Padding(0); this.lbl01.Name = "lbl01"; this.lbl01.Size = new System.Drawing.Size(75, 75); @@ -81,7 +82,7 @@ private void InitializeComponent() this.lbl02.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl02.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl02.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl02.Location = new System.Drawing.Point(150, 0); + this.lbl02.Location = new System.Drawing.Point(170, 40); this.lbl02.Margin = new System.Windows.Forms.Padding(0); this.lbl02.Name = "lbl02"; this.lbl02.Size = new System.Drawing.Size(75, 75); @@ -94,7 +95,7 @@ private void InitializeComponent() this.lbl03.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl03.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl03.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl03.Location = new System.Drawing.Point(225, 0); + this.lbl03.Location = new System.Drawing.Point(245, 40); this.lbl03.Margin = new System.Windows.Forms.Padding(0); this.lbl03.Name = "lbl03"; this.lbl03.Size = new System.Drawing.Size(75, 75); @@ -107,7 +108,7 @@ private void InitializeComponent() this.lbl13.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl13.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl13.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl13.Location = new System.Drawing.Point(225, 75); + this.lbl13.Location = new System.Drawing.Point(245, 115); this.lbl13.Margin = new System.Windows.Forms.Padding(0); this.lbl13.Name = "lbl13"; this.lbl13.Size = new System.Drawing.Size(75, 75); @@ -120,7 +121,7 @@ private void InitializeComponent() this.lbl12.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl12.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl12.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl12.Location = new System.Drawing.Point(150, 75); + this.lbl12.Location = new System.Drawing.Point(170, 115); this.lbl12.Margin = new System.Windows.Forms.Padding(0); this.lbl12.Name = "lbl12"; this.lbl12.Size = new System.Drawing.Size(75, 75); @@ -133,7 +134,7 @@ private void InitializeComponent() this.lbl11.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl11.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl11.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl11.Location = new System.Drawing.Point(75, 75); + this.lbl11.Location = new System.Drawing.Point(95, 115); this.lbl11.Margin = new System.Windows.Forms.Padding(0); this.lbl11.Name = "lbl11"; this.lbl11.Size = new System.Drawing.Size(75, 75); @@ -146,7 +147,7 @@ private void InitializeComponent() this.lbl10.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl10.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl10.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl10.Location = new System.Drawing.Point(0, 75); + this.lbl10.Location = new System.Drawing.Point(20, 115); this.lbl10.Margin = new System.Windows.Forms.Padding(0); this.lbl10.Name = "lbl10"; this.lbl10.Size = new System.Drawing.Size(75, 75); @@ -159,7 +160,7 @@ private void InitializeComponent() this.lbl23.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl23.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl23.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl23.Location = new System.Drawing.Point(225, 150); + this.lbl23.Location = new System.Drawing.Point(245, 190); this.lbl23.Margin = new System.Windows.Forms.Padding(0); this.lbl23.Name = "lbl23"; this.lbl23.Size = new System.Drawing.Size(75, 75); @@ -172,7 +173,7 @@ private void InitializeComponent() this.lbl22.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl22.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl22.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl22.Location = new System.Drawing.Point(150, 150); + this.lbl22.Location = new System.Drawing.Point(170, 190); this.lbl22.Margin = new System.Windows.Forms.Padding(0); this.lbl22.Name = "lbl22"; this.lbl22.Size = new System.Drawing.Size(75, 75); @@ -185,7 +186,7 @@ private void InitializeComponent() this.lbl21.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl21.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl21.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl21.Location = new System.Drawing.Point(75, 150); + this.lbl21.Location = new System.Drawing.Point(95, 190); this.lbl21.Margin = new System.Windows.Forms.Padding(0); this.lbl21.Name = "lbl21"; this.lbl21.Size = new System.Drawing.Size(75, 75); @@ -198,7 +199,7 @@ private void InitializeComponent() this.lbl20.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl20.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl20.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl20.Location = new System.Drawing.Point(0, 150); + this.lbl20.Location = new System.Drawing.Point(20, 190); this.lbl20.Margin = new System.Windows.Forms.Padding(0); this.lbl20.Name = "lbl20"; this.lbl20.Size = new System.Drawing.Size(75, 75); @@ -211,7 +212,7 @@ private void InitializeComponent() this.lbl33.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl33.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl33.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl33.Location = new System.Drawing.Point(225, 225); + this.lbl33.Location = new System.Drawing.Point(245, 265); this.lbl33.Margin = new System.Windows.Forms.Padding(0); this.lbl33.Name = "lbl33"; this.lbl33.Size = new System.Drawing.Size(75, 75); @@ -224,7 +225,7 @@ private void InitializeComponent() this.lbl32.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl32.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl32.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl32.Location = new System.Drawing.Point(150, 225); + this.lbl32.Location = new System.Drawing.Point(170, 265); this.lbl32.Margin = new System.Windows.Forms.Padding(0); this.lbl32.Name = "lbl32"; this.lbl32.Size = new System.Drawing.Size(75, 75); @@ -237,7 +238,7 @@ private void InitializeComponent() this.lbl31.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl31.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl31.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl31.Location = new System.Drawing.Point(75, 225); + this.lbl31.Location = new System.Drawing.Point(95, 265); this.lbl31.Margin = new System.Windows.Forms.Padding(0); this.lbl31.Name = "lbl31"; this.lbl31.Size = new System.Drawing.Size(75, 75); @@ -250,7 +251,7 @@ private void InitializeComponent() this.lbl30.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbl30.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222))); this.lbl30.ForeColor = System.Drawing.SystemColors.ControlLightLight; - this.lbl30.Location = new System.Drawing.Point(0, 225); + this.lbl30.Location = new System.Drawing.Point(20, 265); this.lbl30.Margin = new System.Windows.Forms.Padding(0); this.lbl30.Name = "lbl30"; this.lbl30.Size = new System.Drawing.Size(75, 75); @@ -260,49 +261,64 @@ private void InitializeComponent() // // btnLeft // - this.btnLeft.Location = new System.Drawing.Point(85, 336); + this.btnLeft.Location = new System.Drawing.Point(86, 404); this.btnLeft.Name = "btnLeft"; this.btnLeft.Size = new System.Drawing.Size(53, 45); this.btnLeft.TabIndex = 16; this.btnLeft.Text = "←"; this.btnLeft.UseVisualStyleBackColor = true; this.btnLeft.Click += new System.EventHandler(this.btnLeft_Click); + this.btnLeft.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.TwoZeroFourEightView_PreviewKeyDown); // // btnUp // - this.btnUp.Location = new System.Drawing.Point(140, 307); + this.btnUp.Location = new System.Drawing.Point(145, 353); this.btnUp.Name = "btnUp"; this.btnUp.Size = new System.Drawing.Size(53, 45); this.btnUp.TabIndex = 17; this.btnUp.Text = "↑"; this.btnUp.UseVisualStyleBackColor = true; this.btnUp.Click += new System.EventHandler(this.btnUp_Click); + this.btnUp.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.TwoZeroFourEightView_PreviewKeyDown); // // btnRight // - this.btnRight.Location = new System.Drawing.Point(195, 336); + this.btnRight.Location = new System.Drawing.Point(204, 404); this.btnRight.Name = "btnRight"; this.btnRight.Size = new System.Drawing.Size(53, 45); this.btnRight.TabIndex = 18; this.btnRight.Text = "→"; this.btnRight.UseVisualStyleBackColor = true; this.btnRight.Click += new System.EventHandler(this.btnRight_Click); + this.btnRight.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.TwoZeroFourEightView_PreviewKeyDown); // // btnDown // - this.btnDown.Location = new System.Drawing.Point(140, 371); + this.btnDown.Location = new System.Drawing.Point(145, 404); this.btnDown.Name = "btnDown"; this.btnDown.Size = new System.Drawing.Size(53, 45); this.btnDown.TabIndex = 19; this.btnDown.Text = "↓"; this.btnDown.UseVisualStyleBackColor = true; this.btnDown.Click += new System.EventHandler(this.btnDown_Click); + this.btnDown.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.TwoZeroFourEightView_PreviewKeyDown); + // + // lbScore + // + this.lbScore.AutoSize = true; + this.lbScore.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(222))); + this.lbScore.Location = new System.Drawing.Point(12, 9); + this.lbScore.Name = "lbScore"; + this.lbScore.Size = new System.Drawing.Size(76, 20); + this.lbScore.TabIndex = 20; + this.lbScore.Text = "Score: 0"; // // TwoZeroFourEightView // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(344, 428); + this.ClientSize = new System.Drawing.Size(344, 461); + this.Controls.Add(this.lbScore); this.Controls.Add(this.btnDown); this.Controls.Add(this.btnRight); this.Controls.Add(this.btnUp); @@ -325,8 +341,9 @@ private void InitializeComponent() this.Controls.Add(this.lbl00); this.Name = "TwoZeroFourEightView"; this.Text = "Form1"; - this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TwoZeroFourEightView_KeyDown); + this.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.TwoZeroFourEightView_PreviewKeyDown); this.ResumeLayout(false); + this.PerformLayout(); } @@ -352,6 +369,7 @@ private void InitializeComponent() private System.Windows.Forms.Button btnUp; private System.Windows.Forms.Button btnRight; private System.Windows.Forms.Button btnDown; + private System.Windows.Forms.Label lbScore; } } diff --git a/TwoZeroFourEightView.cs b/TwoZeroFourEightView.cs index 9cf0a76..9a5aae0 100644 --- a/TwoZeroFourEightView.cs +++ b/TwoZeroFourEightView.cs @@ -1,11 +1,5 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows.Forms; namespace twozerofoureight @@ -15,6 +9,8 @@ public partial class TwoZeroFourEightView : Form, View Model model; Controller controller; + private bool isOver; + public TwoZeroFourEightView() { InitializeComponent(); @@ -27,7 +23,9 @@ public TwoZeroFourEightView() public void Notify(Model m) { - UpdateBoard(((TwoZeroFourEightModel)m).GetBoard()); + var _m = (TwoZeroFourEightModel)m; + UpdateBoard(_m.GetBoard()); + UpdateScore(_m.GetScore()); } private void UpdateTile(Label l, int i) @@ -79,6 +77,11 @@ private void UpdateBoard(int[,] board) UpdateTile(lbl33, board[3, 3]); } + private void UpdateScore(int score) + { + lbScore.Text = string.Format("Score: {0}", score.ToString()); + } + private void btnLeft_Click(object sender, EventArgs e) { controller.ActionPerformed(TwoZeroFourEightController.LEFT); @@ -99,27 +102,36 @@ private void btnDown_Click(object sender, EventArgs e) controller.ActionPerformed(TwoZeroFourEightController.DOWN); } - private void TwoZeroFourEightView_KeyDown(object sender, KeyEventArgs e) + private void TwoZeroFourEightView_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { switch (e.KeyCode) { case Keys.Up: case Keys.W: + e.IsInputKey = true; controller.ActionPerformed(TwoZeroFourEightController.UP); break; + case Keys.Down: case Keys.S: + e.IsInputKey = true; controller.ActionPerformed(TwoZeroFourEightController.DOWN); break; + case Keys.Left: case Keys.A: + e.IsInputKey = true; controller.ActionPerformed(TwoZeroFourEightController.LEFT); break; + case Keys.Right: case Keys.D: + e.IsInputKey = true; controller.ActionPerformed(TwoZeroFourEightController.RIGHT); break; } + } + } } From d5e5bdf6860c517998149ea9e673d4b5f0db3042 Mon Sep 17 00:00:00 2001 From: holysnipz <9103583+holysnipz@users.noreply.github.com> Date: Mon, 17 Dec 2018 14:54:48 +0700 Subject: [PATCH 3/4] Refactor stuff; Added game over event, with simple msgbox --- TwoZeroFourEightModel.cs | 48 +++++++++++++++++++++++++++++++++++----- TwoZeroFourEightView.cs | 36 +++++++++++++++++++++++------- 2 files changed, 71 insertions(+), 13 deletions(-) diff --git a/TwoZeroFourEightModel.cs b/TwoZeroFourEightModel.cs index 10f6df0..c8ef973 100644 --- a/TwoZeroFourEightModel.cs +++ b/TwoZeroFourEightModel.cs @@ -1,8 +1,5 @@ using System; -using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace twozerofoureight { @@ -58,9 +55,9 @@ public int GetScore() { int _s = 0; - for (int i = 0; i < boardSize; i++) + foreach(int i in range) { - for (int j=0; j < boardSize; j++) + foreach(int j in range) { _s += board[i, j]; } @@ -68,6 +65,47 @@ public int GetScore() return _s; } + public string GetStatus() + { + int filledCount = 0; + bool isWon = true; + + foreach (int i in range) + { + foreach (int j in range) + { + if (board[i, j] > 0) + filledCount++; + + isWon = (board[i, j] == 2048) ? true : false; + } + } + + if (filledCount < (boardSize * boardSize)) + { + return "Alive"; + } + else + { + foreach (int i in range) + { + foreach (int j in range) + { + if (j + 1 < 4 && i >= 0 && board[i, j + 1] == board[i, j]) + { + return "Alive"; + } + if (i + 1 < 4 && j >= 0 && board[i + 1, j] == board[i, j]) + { + return "Alive"; + } + } + } + + return isWon ? "Won" : "Over"; + } + } + // Perform shift and merge to the left of the given array. protected bool ShiftAndMerge(int[] buffer) { diff --git a/TwoZeroFourEightView.cs b/TwoZeroFourEightView.cs index 9a5aae0..927a527 100644 --- a/TwoZeroFourEightView.cs +++ b/TwoZeroFourEightView.cs @@ -9,8 +9,6 @@ public partial class TwoZeroFourEightView : Form, View Model model; Controller controller; - private bool isOver; - public TwoZeroFourEightView() { InitializeComponent(); @@ -26,6 +24,7 @@ public void Notify(Model m) var _m = (TwoZeroFourEightModel)m; UpdateBoard(_m.GetBoard()); UpdateScore(_m.GetScore()); + UpdateStatus(_m.GetStatus()); } private void UpdateTile(Label l, int i) @@ -77,9 +76,30 @@ private void UpdateBoard(int[,] board) UpdateTile(lbl33, board[3, 3]); } - private void UpdateScore(int score) + private void UpdateScore(int s) + { + lbScore.Text = string.Format("Score: {0}", s.ToString()); + } + + private void UpdateStatus(string s) { - lbScore.Text = string.Format("Score: {0}", score.ToString()); + //Skip + if (s.Equals("Alive")) + return; + + //If Over + + btnDown.Enabled = false; + btnLeft.Enabled = false; + btnRight.Enabled = false; + btnUp.Enabled = false; + + //Simple Notice + MessageBox.Show( + string.Format("You {0}!", s.Equals("Over") ? "Lose" : "Won"), + s.Equals("Over") ? "Game Over" : "Congratulations", + MessageBoxButtons.OK + ); } private void btnLeft_Click(object sender, EventArgs e) @@ -108,25 +128,25 @@ private void TwoZeroFourEightView_PreviewKeyDown(object sender, PreviewKeyDownEv { case Keys.Up: case Keys.W: - e.IsInputKey = true; + e.IsInputKey = true; this.btnUp.Focus(); //Force focus controller.ActionPerformed(TwoZeroFourEightController.UP); break; case Keys.Down: case Keys.S: - e.IsInputKey = true; + e.IsInputKey = true; this.btnDown.Focus(); //Force focus controller.ActionPerformed(TwoZeroFourEightController.DOWN); break; case Keys.Left: case Keys.A: - e.IsInputKey = true; + e.IsInputKey = true; this.btnLeft.Focus(); //Force focus controller.ActionPerformed(TwoZeroFourEightController.LEFT); break; case Keys.Right: case Keys.D: - e.IsInputKey = true; + e.IsInputKey = true; this.btnRight.Focus(); //Force focus controller.ActionPerformed(TwoZeroFourEightController.RIGHT); break; } From d531e08d8790b426edda2849f43185a686209ca6 Mon Sep 17 00:00:00 2001 From: thrwKappu Date: Mon, 17 Dec 2018 15:52:18 +0700 Subject: [PATCH 4/4] Some tweaks --- TwoZeroFourEightModel.cs | 24 +++++++++++------------- TwoZeroFourEightView.cs | 17 ++++++++--------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/TwoZeroFourEightModel.cs b/TwoZeroFourEightModel.cs index c8ef973..e68f873 100644 --- a/TwoZeroFourEightModel.cs +++ b/TwoZeroFourEightModel.cs @@ -85,25 +85,23 @@ public string GetStatus() { return "Alive"; } - else + + foreach (int i in range) { - foreach (int i in range) + foreach (int j in range) { - foreach (int j in range) + if (j + 1 < 4 && i >= 0 && board[i, j + 1] == board[i, j]) { - if (j + 1 < 4 && i >= 0 && board[i, j + 1] == board[i, j]) - { - return "Alive"; - } - if (i + 1 < 4 && j >= 0 && board[i + 1, j] == board[i, j]) - { - return "Alive"; - } + return "Alive"; + } + if (i + 1 < 4 && j >= 0 && board[i + 1, j] == board[i, j]) + { + return "Alive"; } } - - return isWon ? "Won" : "Over"; } + + return isWon ? "Won" : "Over"; } // Perform shift and merge to the left of the given array. diff --git a/TwoZeroFourEightView.cs b/TwoZeroFourEightView.cs index 927a527..128efe0 100644 --- a/TwoZeroFourEightView.cs +++ b/TwoZeroFourEightView.cs @@ -21,10 +21,9 @@ public TwoZeroFourEightView() public void Notify(Model m) { - var _m = (TwoZeroFourEightModel)m; - UpdateBoard(_m.GetBoard()); - UpdateScore(_m.GetScore()); - UpdateStatus(_m.GetStatus()); + UpdateBoard(((TwoZeroFourEightModel)m).GetBoard()); + UpdateScore(((TwoZeroFourEightModel)m).GetScore()); + UpdateStatus(((TwoZeroFourEightModel)m).GetStatus()); } private void UpdateTile(Label l, int i) @@ -78,7 +77,7 @@ private void UpdateBoard(int[,] board) private void UpdateScore(int s) { - lbScore.Text = string.Format("Score: {0}", s.ToString()); + lbScore.Text = $@"Score: {s.ToString()}"; } private void UpdateStatus(string s) @@ -95,10 +94,10 @@ private void UpdateStatus(string s) btnUp.Enabled = false; //Simple Notice - MessageBox.Show( - string.Format("You {0}!", s.Equals("Over") ? "Lose" : "Won"), - s.Equals("Over") ? "Game Over" : "Congratulations", - MessageBoxButtons.OK + MessageBox.Show( + text: $@"You {(s.Equals("Over") ? "Lose" : "Won")}!", + caption: s.Equals("Over") ? "Game Over" : "Congratulations", + buttons: MessageBoxButtons.OK ); }