You are on page 1of 6

`using `using `using `using `using

System;` System.Drawing;` System.Windows.Forms;` System.Collections;` System.Threading;`

namespace 16-Shuffle Puzzle { public partial class 16-Shuffle Puzzle : Form { private int ROW_COUNT = 4; private int COLUMN_COUNT = 4; private Thread timerThread = null; Random r; private Label[] label = new Label[16]; private int[,] gridVal, checkVal; ArrayList genArr; int Num, num; static int x, y; // for 0 val static int z; // for label[i]. static int min = 0;// static int keyCount = 0; string gameStr; // 0 //0 //1 //2 //3 1 // // // // 2 0 4 8 12 3 1 5 9 13 2 6 10 14 3 7 11 15

public 16-Shuffle Puzzle() { genArr = new ArrayList(16); r = new Random(); InitializeComponent(); Intialize(); }k private void 16-Shuffle Puzzle_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; createlabels(); timerThread = new Thread(new ThreadStart(timerFunc)); timerThread.IsBackground = true; timerThread.Start(); } private void Key_Down(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Left: { moveLeft(); keyCount++; } break; case Keys.Right:

{ moveRight(); keyCount++; } break; case Keys.Up: { moveUp(); keyCount++; } break; case Keys.Down: { moveDown(); keyCount++; } break; default: System.Console.Beep(); break; } if (check()) { timerThread.Abort(); gameStr = string.Format("You took {0} moves to arrange numbers", keyCount); MessageBox.Show(gameStr, "Game Over!"); Application.Exit(); } } private void swap(int a, int b) { int tmp; tmp = gridVal[x, y]; gridVal[x, y] = gridVal[a, b]; gridVal[a, b] = tmp; } private void moveDown() { if ((x - 1) < 0) return; this.label[z].BackColor = Color.SkyBlue; this.label[z].BorderStyle = BorderStyle.FixedSingle; this.label[z].Visible = true; this.label[z].Text = gridVal[x - 1, y].ToString(); swap(x - 1, y); x = x - 1; z = z - 4; this.label[z].BackColor = Color.SkyBlue; this.label[z].BorderStyle = BorderStyle.None; this.label[z].Visible = false; this.label[z].Text = gridVal[x, y].ToString();

} private void moveUp() { if ((x + 1) > 3) return; this.label[z].BackColor = Color.SkyBlue; this.label[z].BorderStyle = BorderStyle.FixedSingle; this.label[z].Visible = true; this.label[z].Text = gridVal[x + 1, y].ToString(); swap(x + 1, y); x = x + 1; z = z + 4; this.label[z].BackColor = Color.Transparent; this.label[z].BorderStyle = BorderStyle.None; this.label[z].Visible = false; this.label[z].Text = gridVal[x, y].ToString(); } private void moveRight() { if ((y - 1) < 0) return; this.label[z].BackColor = Color.SkyBlue; this.label[z].BorderStyle = BorderStyle.FixedSingle; this.label[z].Visible = true; this.label[z].Text = gridVal[x, y - 1].ToString(); swap(x, y - 1); y = y - 1; z = z - 1; this.label[z].BackColor = Color.Transparent; this.label[z].BorderStyle = BorderStyle.None; this.label[z].Visible = false; this.label[z].Text = gridVal[x, y].ToString(); } private void moveLeft() { if ((y + 1) > 3) return; this.label[z].BackColor = Color.SkyBlue; this.label[z].BorderStyle = BorderStyle.FixedSingle; this.label[z].Visible = true; this.label[z].Text = gridVal[x, y + 1].ToString(); swap(x, y + 1); y = y + 1; z = z + 1; this.label[z].BackColor = Color.Transparent; this.label[z].BorderStyle = BorderStyle.None; this.label[z].Visible = false; this.label[z].Text = gridVal[x, y].ToString(); } public void Intialize() { gridVal = new int[4, 4] { {0, 0, 0, 0}, {0, 0, 0, 0},

{0, 0, 0, 0}, {0, 0, 0, 0}, }; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if ((i == 3) && (j == 3)) { x = i; y = j; } else { gridVal[i, j] = genRandomVal(); } } } } public int Random() { Num = r.Next(1, 16); for (int k = 0; k < genArr.Count; k++) { if (genArr[k].Equals(Num)) { Random(); } } return Num; } public int genRandomVal() { num = Random(); genArr.Add(num); return num; } public void createlabels() { int i = 0; int val; for (int j = 0; j < ROW_COUNT; j++) for (int k = 0; k < COLUMN_COUNT; k++) { val = gridVal[j, k]; label[i] = new Label(); label[i].Font = new Font("Microsoft Sans Serif", 15F, FontSt yle.Bold, GraphicsUnit.Point, ((byte)(0))); label[i].Width = 60; label[i].Height = 60; label[i].Left = k * 60;

label[i].Top = j * 63; label[i].Tag = 0; label[i].TextAlign = System.Drawing.ContentAlignment.MiddleC enter; if (i != 15) { label[i].BackColor = Color.SkyBlue; label[i].BorderStyle = BorderStyle.FixedSingle; label[i].Text = val.ToString(); } else { label[i].BackColor = Color.Transparent; label[i].BorderStyle = BorderStyle.None; label[i].Text = ""; label[i].Visible = false; z = i; } this.Controls.Add(label[i]); i++; } } private void timerFunc() { string str; for (min = 0; min < 20; min++) { for (int sec = 0; sec < 60; sec++) { str = string.Format("Mind Puzzle min, sec); this.Text = str; Thread.Sleep(1000); } }

00:0{0}:{1} ",

if (min == 20) { this.Enabled = false; MessageBox.Show(" Better Luck next time ", "You Lose the Game!") ; Application.Exit(); } } private bool check() { checkVal = new int[4, 4] { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}, }; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) {

if (gridVal[i, j] != checkVal[i, j]) return false; } } return true; } } }

You might also like