Reader Level:
ARTICLE

Mind Puzzle Game in C#

Posted by Mohammad Tajuddin Articles | C# Language April 12, 2010
This is a simple game written in C# in which user presses Up, Down, Left, Right arrow keys to play this game.
  • 3
  • 0
  • 44080
Download Files:
 

Description

This is a simple game written in C# in which user presses  Up, Down, Left, Right arrow keys to play this game.
 
The objective of this game is to arrange 1 to 15 numbers in ascending order where the numbers in grid are in random.
 
User will have 10 minutes of time to complete this game.
 
If user wins the game he gets displayed in how many moves he completed the game.
 
1.gif
Step By Step

Step 1: Generating random values & Creating Labels on the form.

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, FontStyle.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.MiddleCenter;
            if (i != 15)
            {
                label[i].BackColor = Color.SeaGreen;
                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++;
        }
}

Step 2: This is to handle KeyDown event handlers.

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();
    }
}

Step 3: Creatng the thread to display time.

timerThread = new Thread(new ThreadStart(timerFunc));
timerThread.IsBackground = true;
timerThread.Start();
private void timerFunc()
{
    string str;
    for (min = 0; min < 10; min++)
    {
        for (int sec = 0; sec < 60; sec++)
        {
            str = string.Format("Mind Puzzle       00:0{0}:{1} ", min, sec);
            this.Text = str;
            Thread.Sleep(1000);
        }
    }
    if (min == 10)
    {
        this.Enabled = false;
        MessageBox.Show("Better Luck next time ", "You Lose the Game!");
        Application.Exit();
    }
}

Step 4: Checking the grid values whether they arranged in ascending order or not.

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;
}

COMMENT USING

Trending up