SIGN UP MEMBER LOGIN:    
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
share this article :
post comment
 

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

how to get score in puzzle game if it finish it, and when the moves is lesser it have a more score but if the moves is greater the score is low... can you help me

Posted by oneweek reign Jul 15, 2011

Thanks

Posted by foffa beroo Jun 03, 2011

Many Thanks

Posted by Anis Mujkanovic Apr 10, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor