ARTICLE

Mind Puzzle Game in C#

Posted by Taj Articles | Current Affairs 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.
Reader Level:
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;
}

Login to add your contents and source code to this article
post comment
     

nice one

Posted by Sazid Mauhammad Feb 26, 2013

Thnks dude, this is very helpful

Posted by Renaldho Hanoch Dec 13, 2012

is this made in c# 2008 or c#2010?

Posted by sz steph Nov 03, 2012

I need explaining the code in detail(the job of each each function) pleaze

Posted by Omar Mabrouk Feb 09, 2012

Do you work for Valtech?

Posted by Ebenezer Pramod Kumar Dec 20, 2011
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts