diff --git a/Controller.cs b/Controller.cs index 036a13a..b5c8520 100644 --- a/Controller.cs +++ b/Controller.cs @@ -20,7 +20,7 @@ public void AddModel(Model m) mList.Add(m); } - // virtual keyword allow the method to be overriden + // The `virtual` keyword allows the method to be overridden public virtual void ActionPerformed(int action) { throw new NotImplementedException(); diff --git a/Model.cs b/Model.cs index bf48ef0..729997b 100644 --- a/Model.cs +++ b/Model.cs @@ -27,6 +27,5 @@ public void AttachObserver(View m) { oList.Add(m); } - } } diff --git a/TwoZeroFourEightModel.cs b/TwoZeroFourEightModel.cs index 2c70ab6..4da3b2a 100644 --- a/TwoZeroFourEightModel.cs +++ b/TwoZeroFourEightModel.cs @@ -11,17 +11,13 @@ class TwoZeroFourEightModel : Model protected int boardSize; // default is 4 protected int[,] board; protected Random rand; + public int score = 0; public TwoZeroFourEightModel() : this(4) { // default board size is 4 } - public int[,] GetBoard() - { - return board; - } - public TwoZeroFourEightModel(int size) { boardSize = size; @@ -37,9 +33,28 @@ public TwoZeroFourEightModel(int size) NotifyAll(); } + public int[,] GetBoard() + { + return board; + } + + public int GetScore() + { + score = 0; + for(int i = 0; i < 4; i++) + { + for(int j = 0; j < 4; j++) + { + score += board[i, j]; + } + } + + return score; + } + private int[,] Random(int[,] input) { - while (true) + while (!isFull()) { int x = rand.Next(boardSize); int y = rand.Next(boardSize); @@ -54,211 +69,278 @@ public TwoZeroFourEightModel(int size) public void PerformDown() { - int[] buffer; - int pos; - int[] rangeX = Enumerable.Range(0, boardSize).ToArray(); - int[] rangeY = Enumerable.Range(0, boardSize).ToArray(); - Array.Reverse(rangeY); - foreach (int i in rangeX) + if (!isOver()) { - pos = 0; - buffer = new int[4]; - foreach (int k in rangeX) - { - buffer[k] = 0; - } - //shift left - foreach (int j in rangeY) + int[] buffer; + int pos; + int[] rangeX = Enumerable.Range(0, boardSize).ToArray(); + int[] rangeY = Enumerable.Range(0, boardSize).ToArray(); + Array.Reverse(rangeY); + foreach (int i in rangeX) { - if (board[j, i] != 0) + pos = 0; + buffer = new int[4]; + foreach (int k in rangeX) { - buffer[pos] = board[j, i]; - pos++; + buffer[k] = 0; } - } - // check duplicate - foreach (int j in rangeX) - { - if (j > 0 && buffer[j] != 0 && buffer[j] == buffer[j - 1]) + // shift down + foreach (int j in rangeY) { - buffer[j - 1] *= 2; - buffer[j] = 0; + if (board[j, i] != 0) + { + buffer[pos] = board[j, i]; + pos++; + } } - } - // shift left again - pos = 3; - foreach (int j in rangeX) - { - if (buffer[j] != 0) + // check duplicate + foreach (int j in rangeX) { - board[pos, i] = buffer[j]; - pos--; + if (j > 0 && buffer[j] != 0 && buffer[j] == buffer[j - 1]) + { + buffer[j - 1] *= 2; + buffer[j] = 0; + } + } + // shift down again + pos = 3; + foreach (int j in rangeX) + { + if (buffer[j] != 0) + { + board[pos, i] = buffer[j]; + pos--; + } + } + // copy back + for (int k = pos; k != -1; k--) + { + board[k, i] = 0; } } - // copy back - for (int k = pos; k != -1; k--) - { - board[k, i] = 0; - } + + board = Random(board); + NotifyAll(); + } + else + { + } - board = Random(board); - NotifyAll(); } public void PerformUp() { - int[] buffer; - int pos; - - int[] range = Enumerable.Range(0, boardSize).ToArray(); - foreach (int i in range) + if (!isOver()) { - pos = 0; - buffer = new int[4]; - foreach (int k in range) - { - buffer[k] = 0; - } - //shift left - foreach (int j in range) + int[] buffer; + int pos; + + int[] range = Enumerable.Range(0, boardSize).ToArray(); + foreach (int i in range) { - if (board[j, i] != 0) + pos = 0; + buffer = new int[4]; + foreach (int k in range) { - buffer[pos] = board[j, i]; - pos++; + buffer[k] = 0; } - } - // check duplicate - foreach (int j in range) - { - if (j > 0 && buffer[j] != 0 && buffer[j] == buffer[j - 1]) + // shift up + foreach (int j in range) { - buffer[j - 1] *= 2; - buffer[j] = 0; + if (board[j, i] != 0) + { + buffer[pos] = board[j, i]; + pos++; + } } - } - // shift left again - pos = 0; - foreach (int j in range) - { - if (buffer[j] != 0) + // check duplicate + foreach (int j in range) { - board[pos, i] = buffer[j]; - pos++; + if (j > 0 && buffer[j] != 0 && buffer[j] == buffer[j - 1]) + { + buffer[j - 1] *= 2; + buffer[j] = 0; + } + } + // shift up again + pos = 0; + foreach (int j in range) + { + if (buffer[j] != 0) + { + board[pos, i] = buffer[j]; + pos++; + } + } + // copy back + for (int k = pos; k != boardSize; k++) + { + board[k, i] = 0; } } - // copy back - for (int k = pos; k != boardSize; k++) - { - board[k, i] = 0; - } + board = Random(board); + NotifyAll(); } - board = Random(board); - NotifyAll(); + else + { + + } + } public void PerformRight() { - int[] buffer; - int pos; - - int[] rangeX = Enumerable.Range(0, boardSize).ToArray(); - int[] rangeY = Enumerable.Range(0, boardSize).ToArray(); - Array.Reverse(rangeX); - foreach (int i in rangeY) + if (!isOver()) { - pos = 0; - buffer = new int[4]; - foreach (int k in rangeY) - { - buffer[k] = 0; - } - //shift left - foreach (int j in rangeX) + int[] buffer; + int pos; + + int[] rangeX = Enumerable.Range(0, boardSize).ToArray(); + int[] rangeY = Enumerable.Range(0, boardSize).ToArray(); + Array.Reverse(rangeX); + foreach (int i in rangeY) { - if (board[i, j] != 0) + pos = 0; + buffer = new int[4]; + foreach (int k in rangeY) { - buffer[pos] = board[i, j]; - pos++; + buffer[k] = 0; } - } - // check duplicate - foreach (int j in rangeY) - { - if (j > 0 && buffer[j] != 0 && buffer[j] == buffer[j - 1]) + // shift right + foreach (int j in rangeX) { - buffer[j - 1] *= 2; - buffer[j] = 0; + if (board[i, j] != 0) + { + buffer[pos] = board[i, j]; + pos++; + } } - } - // shift left again - pos = 3; - foreach (int j in rangeY) - { - if (buffer[j] != 0) + // check duplicate + foreach (int j in rangeY) { - board[i, pos] = buffer[j]; - pos--; + if (j > 0 && buffer[j] != 0 && buffer[j] == buffer[j - 1]) + { + buffer[j - 1] *= 2; + buffer[j] = 0; + } + } + // shift right again + pos = 3; + foreach (int j in rangeY) + { + if (buffer[j] != 0) + { + board[i, pos] = buffer[j]; + pos--; + } + } + // copy back + for (int k = pos; k != -1; k--) + { + board[i, k] = 0; } } - // copy back - for (int k = pos; k != -1; k--) - { - board[i, k] = 0; - } + board = Random(board); + NotifyAll(); + } + else + { + } - board = Random(board); - NotifyAll(); } public void PerformLeft() { - int[] buffer; - int pos; - int[] range = Enumerable.Range(0, boardSize).ToArray(); - foreach (int i in range) + if (!isOver()) { - pos = 0; - buffer = new int[boardSize]; - foreach (int k in range) - { - buffer[k] = 0; - } - //shift left - foreach (int j in range) + int[] buffer; + int pos; + int[] range = Enumerable.Range(0, boardSize).ToArray(); + foreach (int i in range) { - if (board[i, j] != 0) + + pos = 0; + buffer = new int[boardSize]; + foreach (int k in range) { - buffer[pos] = board[i, j]; - pos++; + buffer[k] = 0; } - } - // check duplicate - foreach (int j in range) - { - if (j > 0 && buffer[j] != 0 && buffer[j] == buffer[j - 1]) + // shift left + foreach (int j in range) { - buffer[j - 1] *= 2; - buffer[j] = 0; + if (board[i, j] != 0) + { + buffer[pos] = board[i, j]; + pos++; + } + } + // check duplicate + foreach (int j in range) + { + if (j > 0 && buffer[j] != 0 && buffer[j] == buffer[j - 1]) + { + buffer[j - 1] *= 2; + buffer[j] = 0; + } + } + // shift left again + pos = 0; + foreach (int j in range) + { + if (buffer[j] != 0) + { + board[i, pos] = buffer[j]; + pos++; + } + } + for (int k = pos; k != boardSize; k++) + { + board[i, k] = 0; } } - // shift left again - pos = 0; - foreach (int j in range) + + board = Random(board); + NotifyAll(); + } + else + { + + } + } + + public bool isOver() + { + if (isFull()) + { + for (int i = 0; i < 3; i++) { - if (buffer[j] != 0) + for (int j = 0; j < 3; j++) { - board[i, pos] = buffer[j]; - pos++; + if (board[i, j] == board[i+1,j] || board[i, j] == board[i, j + 1]) + { + return false; + } } } - for (int k = pos; k != boardSize; k++) + return true; + } + return false; + } + + public bool isFull() + { + for(int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) { - board[i, k] = 0; + if (board[i, j] == 0) + { + return false; + } } } - board = Random(board); - NotifyAll(); + + return true; } } } diff --git a/TwoZeroFourEightView.Designer.cs b/TwoZeroFourEightView.Designer.cs index 8d079e6..052f0df 100644 --- a/TwoZeroFourEightView.Designer.cs +++ b/TwoZeroFourEightView.Designer.cs @@ -44,237 +44,247 @@ private void InitializeComponent() this.lbl32 = new System.Windows.Forms.Label(); this.lbl31 = new System.Windows.Forms.Label(); this.lbl30 = new System.Windows.Forms.Label(); - this.btnLeft = new System.Windows.Forms.Button(); - this.btnUp = new System.Windows.Forms.Button(); - this.btnRight = new System.Windows.Forms.Button(); - this.btnDown = new System.Windows.Forms.Button(); + this.label1 = new System.Windows.Forms.Label(); + this.lblDisplay = new System.Windows.Forms.Label(); this.SuspendLayout(); // // lbl00 // this.lbl00.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl00.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(19, 11); + this.lbl00.Location = new System.Drawing.Point(31, 13); + this.lbl00.Margin = new System.Windows.Forms.Padding(0); this.lbl00.Name = "lbl00"; - this.lbl00.Size = new System.Drawing.Size(51, 51); + this.lbl00.Size = new System.Drawing.Size(99, 92); this.lbl00.TabIndex = 0; this.lbl00.Text = "0"; + this.lbl00.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbl01 // this.lbl01.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl01.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(76, 9); + this.lbl01.Location = new System.Drawing.Point(131, 13); + this.lbl01.Margin = new System.Windows.Forms.Padding(0); this.lbl01.Name = "lbl01"; - this.lbl01.Size = new System.Drawing.Size(51, 51); + this.lbl01.Size = new System.Drawing.Size(99, 92); this.lbl01.TabIndex = 1; this.lbl01.Text = "0"; + this.lbl01.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbl02 // this.lbl02.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl02.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(140, 11); + this.lbl02.Location = new System.Drawing.Point(231, 13); + this.lbl02.Margin = new System.Windows.Forms.Padding(0); this.lbl02.Name = "lbl02"; - this.lbl02.Size = new System.Drawing.Size(51, 51); + this.lbl02.Size = new System.Drawing.Size(99, 92); this.lbl02.TabIndex = 2; this.lbl02.Text = "0"; + this.lbl02.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbl03 // this.lbl03.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl03.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(209, 9); + this.lbl03.Location = new System.Drawing.Point(331, 13); + this.lbl03.Margin = new System.Windows.Forms.Padding(0); this.lbl03.Name = "lbl03"; - this.lbl03.Size = new System.Drawing.Size(51, 51); + this.lbl03.Size = new System.Drawing.Size(99, 92); this.lbl03.TabIndex = 3; this.lbl03.Text = "0"; + this.lbl03.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbl13 // this.lbl13.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl13.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(209, 74); + this.lbl13.Location = new System.Drawing.Point(331, 105); + this.lbl13.Margin = new System.Windows.Forms.Padding(0); this.lbl13.Name = "lbl13"; - this.lbl13.Size = new System.Drawing.Size(51, 51); + this.lbl13.Size = new System.Drawing.Size(99, 92); this.lbl13.TabIndex = 7; this.lbl13.Text = "0"; + this.lbl13.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbl12 // this.lbl12.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl12.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(142, 74); + this.lbl12.Location = new System.Drawing.Point(231, 105); + this.lbl12.Margin = new System.Windows.Forms.Padding(0); this.lbl12.Name = "lbl12"; - this.lbl12.Size = new System.Drawing.Size(51, 51); + this.lbl12.Size = new System.Drawing.Size(99, 92); this.lbl12.TabIndex = 6; this.lbl12.Text = "0"; + this.lbl12.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbl11 // this.lbl11.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl11.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(76, 74); + this.lbl11.Location = new System.Drawing.Point(131, 105); + this.lbl11.Margin = new System.Windows.Forms.Padding(0); this.lbl11.Name = "lbl11"; - this.lbl11.Size = new System.Drawing.Size(51, 51); + this.lbl11.Size = new System.Drawing.Size(99, 92); this.lbl11.TabIndex = 5; this.lbl11.Text = "0"; + this.lbl11.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbl10 // this.lbl10.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl10.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(19, 74); + this.lbl10.Location = new System.Drawing.Point(31, 105); + this.lbl10.Margin = new System.Windows.Forms.Padding(0); this.lbl10.Name = "lbl10"; - this.lbl10.Size = new System.Drawing.Size(51, 51); + this.lbl10.Size = new System.Drawing.Size(99, 92); this.lbl10.TabIndex = 4; this.lbl10.Text = "0"; + this.lbl10.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbl23 // this.lbl23.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl23.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(209, 138); + this.lbl23.Location = new System.Drawing.Point(331, 198); + this.lbl23.Margin = new System.Windows.Forms.Padding(0); this.lbl23.Name = "lbl23"; - this.lbl23.Size = new System.Drawing.Size(51, 51); + this.lbl23.Size = new System.Drawing.Size(99, 92); this.lbl23.TabIndex = 11; this.lbl23.Text = "0"; + this.lbl23.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbl22 // this.lbl22.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl22.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(142, 138); + this.lbl22.Location = new System.Drawing.Point(231, 198); + this.lbl22.Margin = new System.Windows.Forms.Padding(0); this.lbl22.Name = "lbl22"; - this.lbl22.Size = new System.Drawing.Size(51, 51); + this.lbl22.Size = new System.Drawing.Size(99, 92); this.lbl22.TabIndex = 10; this.lbl22.Text = "0"; + this.lbl22.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbl21 // this.lbl21.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl21.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(82, 138); + this.lbl21.Location = new System.Drawing.Point(131, 198); + this.lbl21.Margin = new System.Windows.Forms.Padding(0); this.lbl21.Name = "lbl21"; - this.lbl21.Size = new System.Drawing.Size(51, 51); + this.lbl21.Size = new System.Drawing.Size(99, 92); this.lbl21.TabIndex = 9; this.lbl21.Text = "0"; + this.lbl21.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbl20 // this.lbl20.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl20.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(19, 138); + this.lbl20.Location = new System.Drawing.Point(31, 198); + this.lbl20.Margin = new System.Windows.Forms.Padding(0); this.lbl20.Name = "lbl20"; - this.lbl20.Size = new System.Drawing.Size(51, 51); + this.lbl20.Size = new System.Drawing.Size(99, 92); this.lbl20.TabIndex = 8; this.lbl20.Text = "0"; + this.lbl20.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbl33 // this.lbl33.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl33.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(209, 201); + this.lbl33.Location = new System.Drawing.Point(331, 290); + this.lbl33.Margin = new System.Windows.Forms.Padding(0); this.lbl33.Name = "lbl33"; - this.lbl33.Size = new System.Drawing.Size(51, 51); + this.lbl33.Size = new System.Drawing.Size(99, 92); this.lbl33.TabIndex = 15; this.lbl33.Text = "0"; + this.lbl33.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbl32 // this.lbl32.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl32.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(142, 201); + this.lbl32.Location = new System.Drawing.Point(231, 290); + this.lbl32.Margin = new System.Windows.Forms.Padding(0); this.lbl32.Name = "lbl32"; - this.lbl32.Size = new System.Drawing.Size(51, 51); + this.lbl32.Size = new System.Drawing.Size(99, 92); this.lbl32.TabIndex = 14; this.lbl32.Text = "0"; + this.lbl32.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbl31 // this.lbl31.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl31.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(82, 201); + this.lbl31.Location = new System.Drawing.Point(131, 290); + this.lbl31.Margin = new System.Windows.Forms.Padding(0); this.lbl31.Name = "lbl31"; - this.lbl31.Size = new System.Drawing.Size(51, 51); + this.lbl31.Size = new System.Drawing.Size(99, 92); this.lbl31.TabIndex = 13; this.lbl31.Text = "0"; + this.lbl31.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbl30 // this.lbl30.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.lbl30.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + 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(19, 201); + this.lbl30.Location = new System.Drawing.Point(31, 290); + this.lbl30.Margin = new System.Windows.Forms.Padding(0); this.lbl30.Name = "lbl30"; - this.lbl30.Size = new System.Drawing.Size(51, 51); + this.lbl30.Size = new System.Drawing.Size(99, 92); this.lbl30.TabIndex = 12; this.lbl30.Text = "0"; + this.lbl30.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // - // btnLeft - // - this.btnLeft.Location = new System.Drawing.Point(85, 336); - 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); - // - // btnUp - // - this.btnUp.Location = new System.Drawing.Point(140, 307); - 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); - // - // btnRight - // - this.btnRight.Location = new System.Drawing.Point(195, 336); - 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); - // - // btnDown - // - this.btnDown.Location = new System.Drawing.Point(140, 371); - this.btnDown.Name = "btnDown"; - this.btnDown.Size = new System.Drawing.Size(53, 45); - this.btnDown.TabIndex = 19; - this.btnDown.Text = "v"; - this.btnDown.UseVisualStyleBackColor = true; - this.btnDown.Click += new System.EventHandler(this.btnDown_Click); - // - // Form1 - // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(209, 423); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(43, 17); + this.label1.TabIndex = 20; + this.label1.Text = "score"; + // + // lblDisplay + // + this.lblDisplay.BackColor = System.Drawing.SystemColors.ControlLightLight; + this.lblDisplay.Font = new System.Drawing.Font("Microsoft Sans Serif", 36F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.lblDisplay.Location = new System.Drawing.Point(31, 450); + this.lblDisplay.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); + this.lblDisplay.MinimumSize = new System.Drawing.Size(200, 0); + this.lblDisplay.Name = "lblDisplay"; + this.lblDisplay.Size = new System.Drawing.Size(415, 64); + this.lblDisplay.TabIndex = 41; + this.lblDisplay.Text = "0"; + this.lblDisplay.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + // + // TwoZeroFourEightView + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(344, 428); - this.Controls.Add(this.btnDown); - this.Controls.Add(this.btnRight); - this.Controls.Add(this.btnUp); - this.Controls.Add(this.btnLeft); + this.ClientSize = new System.Drawing.Size(459, 527); + this.Controls.Add(this.lblDisplay); + this.Controls.Add(this.label1); this.Controls.Add(this.lbl33); this.Controls.Add(this.lbl32); this.Controls.Add(this.lbl31); @@ -291,9 +301,12 @@ private void InitializeComponent() this.Controls.Add(this.lbl02); this.Controls.Add(this.lbl01); this.Controls.Add(this.lbl00); - this.Name = "Form1"; + this.Margin = new System.Windows.Forms.Padding(4); + this.Name = "TwoZeroFourEightView"; this.Text = "Form1"; + this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.btnUp_KeyDown); this.ResumeLayout(false); + this.PerformLayout(); } @@ -315,10 +328,8 @@ private void InitializeComponent() private System.Windows.Forms.Label lbl32; private System.Windows.Forms.Label lbl31; private System.Windows.Forms.Label lbl30; - private System.Windows.Forms.Button btnLeft; - private System.Windows.Forms.Button btnUp; - private System.Windows.Forms.Button btnRight; - private System.Windows.Forms.Button btnDown; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Label lblDisplay; } } diff --git a/TwoZeroFourEightView.cs b/TwoZeroFourEightView.cs index 8201edf..e05e6a2 100644 --- a/TwoZeroFourEightView.cs +++ b/TwoZeroFourEightView.cs @@ -12,9 +12,9 @@ namespace twozerofoureight { public partial class TwoZeroFourEightView : Form, View { - Model model; + TwoZeroFourEightModel model; Controller controller; - + public TwoZeroFourEightView() { InitializeComponent(); @@ -27,7 +27,7 @@ public TwoZeroFourEightView() public void Notify(Model m) { - UpdateBoard(((TwoZeroFourEightModel) m).GetBoard()); + UpdateBoard(((TwoZeroFourEightModel)m).GetBoard()); } private void UpdateTile(Label l, int i) @@ -35,7 +35,9 @@ private void UpdateTile(Label l, int i) if (i != 0) { l.Text = Convert.ToString(i); - } else { + } + else + { l.Text = ""; } switch (i) @@ -59,43 +61,62 @@ 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]); - } - - private void btnLeft_Click(object sender, EventArgs e) - { - controller.ActionPerformed(TwoZeroFourEightController.LEFT); + 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 btnRight_Click(object sender, EventArgs e) + private void btnUp_KeyDown(object sender, KeyEventArgs e) { - controller.ActionPerformed(TwoZeroFourEightController.RIGHT); - } - - private void btnUp_Click(object sender, EventArgs e) - { - controller.ActionPerformed(TwoZeroFourEightController.UP); - } - - private void btnDown_Click(object sender, EventArgs e) - { - controller.ActionPerformed(TwoZeroFourEightController.DOWN); + bool check = model.isOver(); + if (!check) + { + if (e.KeyCode == Keys.Right) + { + controller.ActionPerformed(TwoZeroFourEightController.RIGHT); + int score = model.GetScore(); + lblDisplay.Text = score.ToString(); + } + else if (e.KeyCode == Keys.Left) + { + controller.ActionPerformed(TwoZeroFourEightController.LEFT); + int score = model.GetScore(); + lblDisplay.Text = score.ToString(); + } + else if (e.KeyCode == Keys.Up) + { + controller.ActionPerformed(TwoZeroFourEightController.UP); + int score = model.GetScore(); + lblDisplay.Text = score.ToString(); + } + else if (e.KeyCode == Keys.Down) + { + controller.ActionPerformed(TwoZeroFourEightController.DOWN); + int score = model.GetScore(); + lblDisplay.Text = score.ToString(); + } + } + else + { + lblDisplay.Text = "Game Over!"; + } + + } + } }