diff --git a/TwoZeroFourEightModel.cs b/TwoZeroFourEightModel.cs index 29afceb..c4a26f8 100644 --- a/TwoZeroFourEightModel.cs +++ b/TwoZeroFourEightModel.cs @@ -31,8 +31,8 @@ public TwoZeroFourEightModel(int size) } } rand = new Random(); - board = Random(board); - NotifyAll(); + // initialize board + HandleChanges(); } public int[,] GetBoard() @@ -40,19 +40,32 @@ public TwoZeroFourEightModel(int size) return board; } - private int[,] Random(int[,] input) + public int Getscore() + { + int score1 = 0; + foreach(int num in board) + { + if (num != 0) + { + score1 += num; + } + } + return score1; + } + + private void AddRandomSlot() { while (true) { int x = rand.Next(boardSize); int y = rand.Next(boardSize); - if (input[x, y] == 0) + if (board[x, y] == 0) { - input[x, y] = 2; - break; + board[x, y] = 2; + return; } + } - return input; } // Perform shift and merge to the left of the given array. @@ -91,13 +104,13 @@ protected bool ShiftAndMerge(int[] buffer) return changed; } - protected void HandleChanges(bool changed) + protected void HandleChanges(bool changed = true) { // if the board has changed, add a new number // and notify all views if (changed) { - board = Random(board); + AddRandomSlot(); NotifyAll(); } } @@ -193,5 +206,62 @@ public void PerformLeft() } HandleChanges(changed); } + + public bool gameoverBox() + { + for(int i=0;i< boardSize; i++) + { + for(int j = 0; j < boardSize; j++) + { + if (board[i, j] == 0) + { + return false; + } + } + } + + int tmp; + for(int i = 0; i < boardSize; i++) + { + for(int j = 0; j < boardSize; j++) + { + tmp = board[i, j]; + if (i - 1 >= 0) + { + if(tmp==board[i-1,j]) + { + return false; + } + } + if (i + 1 <= 3) + { + if (tmp == board[i+1, j]) + { + return false; + } + } + if (j - 1 >= 0) + { + if (tmp == board[i,j-1]) + { + return false; + } + } + if (j + 1 >= 0) + { + if (tmp == board[i, j+1]) + { + return false; + } + } + + + + } + + } + return true; + + } } } diff --git a/TwoZeroFourEightView.Designer.cs b/TwoZeroFourEightView.Designer.cs index af4b1f9..b5d59e3 100644 --- a/TwoZeroFourEightView.Designer.cs +++ b/TwoZeroFourEightView.Designer.cs @@ -48,6 +48,8 @@ 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.scorebox = new System.Windows.Forms.Label(); + this.gameoverBox = new System.Windows.Forms.Label(); this.SuspendLayout(); // // lbl00 @@ -298,11 +300,35 @@ private void InitializeComponent() this.btnDown.UseVisualStyleBackColor = true; this.btnDown.Click += new System.EventHandler(this.btnDown_Click); // + // scorebox + // + this.scorebox.AutoSize = true; + this.scorebox.Location = new System.Drawing.Point(265, 323); + this.scorebox.Name = "scorebox"; + this.scorebox.Size = new System.Drawing.Size(35, 13); + this.scorebox.TabIndex = 20; + this.scorebox.Text = "label1"; + // + // gameoverBox + // + this.gameoverBox.AutoSize = true; + this.gameoverBox.BackColor = System.Drawing.SystemColors.ActiveCaption; + this.gameoverBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.gameoverBox.ForeColor = System.Drawing.Color.Red; + this.gameoverBox.Location = new System.Drawing.Point(12, 307); + this.gameoverBox.Name = "gameoverBox"; + this.gameoverBox.Size = new System.Drawing.Size(111, 25); + this.gameoverBox.TabIndex = 21; + this.gameoverBox.Text = "Gameover"; + this.gameoverBox.Visible = false; + // // 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.Controls.Add(this.gameoverBox); + this.Controls.Add(this.scorebox); this.Controls.Add(this.btnDown); this.Controls.Add(this.btnRight); this.Controls.Add(this.btnUp); @@ -326,6 +352,7 @@ private void InitializeComponent() this.Name = "TwoZeroFourEightView"; this.Text = "Form1"; this.ResumeLayout(false); + this.PerformLayout(); } @@ -351,6 +378,8 @@ 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 scorebox; + private System.Windows.Forms.Label gameoverBox; } } diff --git a/TwoZeroFourEightView.cs b/TwoZeroFourEightView.cs index 8201edf..e9442b6 100644 --- a/TwoZeroFourEightView.cs +++ b/TwoZeroFourEightView.cs @@ -14,7 +14,7 @@ public partial class TwoZeroFourEightView : Form, View { Model model; Controller controller; - + public TwoZeroFourEightView() { InitializeComponent(); @@ -27,7 +27,9 @@ public TwoZeroFourEightView() public void Notify(Model m) { - UpdateBoard(((TwoZeroFourEightModel) m).GetBoard()); + UpdateBoard(((TwoZeroFourEightModel)m).GetBoard()); + scorebox.Text = ((TwoZeroFourEightModel)m).Getscore().ToString(); + } private void UpdateTile(Label l, int i) @@ -35,7 +37,9 @@ private void UpdateTile(Label l, int i) if (i != 0) { l.Text = Convert.ToString(i); - } else { + } + else + { l.Text = ""; } switch (i) @@ -59,22 +63,22 @@ private void UpdateTile(Label l, int i) } private void UpdateBoard(int[,] board) { - UpdateTile(lbl00,board[0, 0]); - UpdateTile(lbl01,board[0, 1]); - UpdateTile(lbl02,board[0, 2]); - UpdateTile(lbl03,board[0, 3]); - UpdateTile(lbl10,board[1, 0]); - UpdateTile(lbl11,board[1, 1]); - UpdateTile(lbl12,board[1, 2]); - UpdateTile(lbl13,board[1, 3]); - UpdateTile(lbl20,board[2, 0]); - UpdateTile(lbl21,board[2, 1]); - UpdateTile(lbl22,board[2, 2]); - UpdateTile(lbl23,board[2, 3]); - UpdateTile(lbl30,board[3, 0]); - UpdateTile(lbl31,board[3, 1]); - UpdateTile(lbl32,board[3, 2]); - UpdateTile(lbl33,board[3, 3]); + UpdateTile(lbl00, board[0, 0]); + UpdateTile(lbl01, board[0, 1]); + UpdateTile(lbl02, board[0, 2]); + UpdateTile(lbl03, board[0, 3]); + UpdateTile(lbl10, board[1, 0]); + UpdateTile(lbl11, board[1, 1]); + UpdateTile(lbl12, board[1, 2]); + UpdateTile(lbl13, board[1, 3]); + UpdateTile(lbl20, board[2, 0]); + UpdateTile(lbl21, board[2, 1]); + UpdateTile(lbl22, board[2, 2]); + UpdateTile(lbl23, board[2, 3]); + UpdateTile(lbl30, board[3, 0]); + UpdateTile(lbl31, board[3, 1]); + UpdateTile(lbl32, board[3, 2]); + UpdateTile(lbl33, board[3, 3]); } private void btnLeft_Click(object sender, EventArgs e) @@ -97,5 +101,35 @@ private void btnDown_Click(object sender, EventArgs e) controller.ActionPerformed(TwoZeroFourEightController.DOWN); } + protected override bool ProcessCmdKey(ref Message msg, Keys keyData) + { + // keydata equals Keys.Up (if up arrow is pressed) + if(keyData==Keys.Up||keyData==Keys.Down||keyData==Keys.Left||keyData==Keys.Right){ + + switch(keyData){ + case Keys.Up : + controller.ActionPerformed(TwoZeroFourEightController.UP); + break; + case Keys.Down : + controller.ActionPerformed(TwoZeroFourEightController.DOWN); + break; + case Keys.Left : + controller.ActionPerformed(TwoZeroFourEightController.LEFT); + break; + case Keys.Right : + controller.ActionPerformed(TwoZeroFourEightController.RIGHT); + break; + } + return true; + } + else{ + return false; + } + + } + + + // return base.ProcessCmdKey(ref msg, keyData); + } } -} +