13,513,296 members
alternative version

#### Stats

52.7K views
17 bookmarked
Posted 29 Jun 2011

# Game Puzzle

, 29 Jun 2011
This is an easy game puzzle for beginners.

## Introduction

This is an easy game puzzle. In this game, the user must order pieces of picture near others to complete a desired picture. He/she can help numbers which are displayed on the surface of each picture. When user completes the picture, the game will show him the number of his moves to win. This game could be very complex, for example, it is very good that the user can choose the image or game save the highest score for each picture or in each point, game becomes harder than before or if it has a timer and score of user calculated according to the time of the game. All these are true, but I was going to create a game puzzle which is very easy.

## Using the Code

First, I say that there are a lot of ways to do each work for example 'Puzzle Game'. I know some parts of this game are good, some parts could be better, and perhaps some parts are wrong. Perhaps, you can do it better. When I made this game, I was a beginner in programming, it could be better if I did it now. Perhaps you can change some part of this code to work better, it is OK :)

• `MyButton `is a class which inherits from class "`Button`" and has an extension filled "`Number`". This field determines the location which button must be located on the surface of the form.

```public class MyButton : Button
{
private int number;
public int Number
{
get
{
return this.number;
}
set
{
this.Text = value.ToString();
this.number = value;
}
}
public MyButton()
{
}
} ```

The main form of this project has these members:

```private int CrrentPictureNumber;
private Bitmap CurrentBitmapImage;
private Point EmptyLocation;
private MyButton[] ButtonArray;
private bool YouWin;
private int NumberOfMoves;
private FrmShowCurrentImage MyFrmShowCurrentImage;
public event EventHandler CurrentImageChanged;
```
• `CurentPictureNumber `keeps a number between 1 and 7 which determines the number of current picture which is loaded and located on the surface of main form.
• `CurrentBitmapImage `is a bitmap class which keeps the current image which is located on the form.
• `Emptylocation `is a point which determines the location of empty square on the surface of the form. It is better to say that we have 25 squares on the surface of the form, surface of 24 squares are painted by selected picture and one square is empty to move the other squares.
• `ButtonArray `is a two-dimensional of class `MyButton`, this array is used when the game is going to calculate the location of each square to win the user or continue game.
• `NumberOfMoves `keeps move number which user has done to complete the Image and win the game. Each time user moves a square, this field increases 1.
• This game has 7 `static `images in resources which are always fixed and user cannot change them. When a new game starts, a picture is loaded from resources of project and it splits in 25 pieces. The piece which is located in the right bottom order of picture is removed and this location is knows as '`EmptyLocation`', then these pieces are located randomly in the surface of the game. All these and some others are done in these parts of code.

```private void LoadNewGame()
{
this.myButton1.Location = new System.Drawing.Point(20, 40);
this.myButton2.Location = new System.Drawing.Point(95, 40);
this.myButton3.Location = new System.Drawing.Point(170, 40);
this.myButton4.Location = new System.Drawing.Point(245, 40);
this.myButton5.Location = new System.Drawing.Point(320, 40);
this.myButton6.Location = new System.Drawing.Point(20, 115);
this.myButton7.Location = new System.Drawing.Point(95, 115);
this.myButton8.Location = new System.Drawing.Point(170, 115);
this.myButton9.Location = new System.Drawing.Point(245, 115);
this.myButton10.Location = new System.Drawing.Point(320, 115);
this.myButton11.Location = new System.Drawing.Point(20, 190);
this.myButton12.Location = new System.Drawing.Point(95, 190);
this.myButton13.Location = new System.Drawing.Point(170, 190);
this.myButton14.Location = new System.Drawing.Point(245, 190);
this.myButton15.Location = new System.Drawing.Point(320, 190);
this.myButton16.Location = new System.Drawing.Point(20, 265);
this.myButton17.Location = new System.Drawing.Point(95, 265);
this.myButton18.Location = new System.Drawing.Point(170, 265);
this.myButton19.Location = new System.Drawing.Point(245, 265);
this.myButton20.Location = new System.Drawing.Point(320, 265);
this.myButton21.Location = new System.Drawing.Point(20, 340);
this.myButton22.Location = new System.Drawing.Point(95, 340);
this.myButton23.Location = new System.Drawing.Point(170, 340);
this.myButton24.Location = new System.Drawing.Point(245, 340);

if (this.CrrentPictureNumber == 7)
this.CrrentPictureNumber = 1;
else
this.CrrentPictureNumber++;
switch (this.CrrentPictureNumber)
{
case 1: CurrentBitmapImage = new Bitmap
(GamePuzzle.Properties.Resources._1, new Size(375, 375));
break;
case 2: CurrentBitmapImage = new Bitmap
(GamePuzzle.Properties.Resources._2, new Size(375, 375));
break;
case 3: CurrentBitmapImage = new Bitmap
(GamePuzzle.Properties.Resources._3, new Size(375, 375));
break;
case 4: CurrentBitmapImage = new Bitmap
(GamePuzzle.Properties.Resources._4, new Size(375, 375));
break;
case 5: CurrentBitmapImage = new Bitmap
(GamePuzzle.Properties.Resources._5, new Size(375, 375));
break;
case 6: CurrentBitmapImage = new Bitmap
(GamePuzzle.Properties.Resources._6, new Size(375, 375));
break;
case 7: CurrentBitmapImage = new Bitmap
(GamePuzzle.Properties.Resources._7, new Size(375, 375));
break;
}
this.EmptyLocation = new Point(320, 340);
this.NumberOfMoves = 0;
this.ButtonArray = new MyButton[24];

this.ButtonArray[0] = this.myButton1;
this.ButtonArray[1] = this.myButton2;
this.ButtonArray[2] = this.myButton3;
this.ButtonArray[3] = this.myButton4;
this.ButtonArray[4] = this.myButton5;
this.ButtonArray[5] = this.myButton6;
this.ButtonArray[6] = this.myButton7;
this.ButtonArray[7] = this.myButton8;
this.ButtonArray[8] = this.myButton9;
this.ButtonArray[9] = this.myButton10;
this.ButtonArray[10] = this.myButton11;
this.ButtonArray[11] = this.myButton12;
this.ButtonArray[12] = this.myButton13;
this.ButtonArray[13] = this.myButton14;
this.ButtonArray[14] = this.myButton15;
this.ButtonArray[15] = this.myButton16;
this.ButtonArray[16] = this.myButton17;
this.ButtonArray[17] = this.myButton18;
this.ButtonArray[18] = this.myButton19;
this.ButtonArray[19] = this.myButton20;
this.ButtonArray[20] = this.myButton21;
this.ButtonArray[21] = this.myButton22;
this.ButtonArray[22] = this.myButton23;
this.ButtonArray[23] = this.myButton24;

Random r = new Random();
int[] a = new int[24];
int i = 0;
int b;
bool exist;
while (i != a.Length)
{
exist = false;
b = (r.Next(24) + 1);
for (int j = 0; j < a.Length; j++)
if (a[j] == b) exist = true;
if (!exist) a[i++] = b;
}
for (int j = 0; j < a.Length; j++)
ButtonArray[j].Number = a[j];
// set picture pieces as the background image
int Number;
int Row, Column;
for (int k = 0; k < 5; k++)
for (int j = 0; j < 5; j++)
{
if (k == 4)
if (j == 4) break;
Number = ButtonArray[k * 5 + j].Number; //Get The Number Of Button
Row = (Number - 1) / 5;
Column = (Number - 1) - (Row * 5);
ButtonArray[k * 5 + j].Image = CurrentBitmapImage.Clone
(new Rectangle(new Point(Column * 75, Row * 75),
new Size(75, 75)), System.Drawing.Imaging.PixelFormat.DontCare);
}
if (this.CurrentImageChanged != null)
this.CurrentImageChanged(this, EventArgs.Empty);
} ```
• When user clicks a button (a piece of picture), the game program must check this: "is empty location beside the clicked button?" If yes, the current button and "`EmptyLocation`" must change locations with one another. All these are done in this part of code.

```private void myButton_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
if ((b.Location.X - 75 == EmptyLocation.X) &&
(b.Location.Y == EmptyLocation.Y))
{
b.Location = EmptyLocation;
EmptyLocation.X += 75;
this.Focus();
}
else if (b.Location.X + 75 == EmptyLocation.X &&
(b.Location.Y == EmptyLocation.Y))
{
b.Location = EmptyLocation;
EmptyLocation.X -= 75;
this.Focus();
}
else if (b.Location.Y - 75 == EmptyLocation.Y &&
(b.Location.X == EmptyLocation.X))
{
b.Location = EmptyLocation;
EmptyLocation.Y += 75;
this.Focus();
}

else if (b.Location.Y + 75 == EmptyLocation.Y &&
(b.Location.X == EmptyLocation.X))
{
b.Location = EmptyLocation;
EmptyLocation.Y -= 75;
this.Focus();
}
}```
• When a square is moved by the user, the game program must check this: "current picture is complete or not?" or "user has won the game or must continue to try? All these jobs must be done in event "`LocationChanged`" of each button:

```private void myButton_LocationChanged(object sender, EventArgs e)
{
MyButton A = sender as MyButton;
YouWin = true;
int ButtonNumber;
this.NumberOfMoves++;
for (int i = 0; i < 5; i++)
{
if (YouWin == false)
break;
else for (int j = 0; j < 5; j++)
{
ButtonNumber = i * 5 + j;
if (i == 4 && j == 4)
break;
else if (GetNumber(ButtonArray[ButtonNumber].Location.X,
ButtonArray[ButtonNumber].Location.Y) ==
ButtonArray[ButtonNumber].Number)
continue;
else
{
YouWin = false;
break;
}
}
}
if (YouWin)
{
if (MessageBox.Show("You Win This Game in " +
this.NumberOfMoves.ToString() + "
Moves\n\rDo You Want To Play Another Game ?",
"Congratulation", MessageBoxButtons.YesNo) == DialogResult.Yes)
else
this.Close();
}
}```
```private int GetNumber(int x, int y)
{
int a, b;
if (y == 40) // y-->Row
a = 0;
else
a = (y - 40) / 75;

if (x == 20) // x-->Column
b = 0;
else
b = (x - 20) / 75;
int Number = a * 5 + b + 1;
return Number;
}```

But how does the Game puzzle program realize that each square is located in its true location or not?

This game has 24 squares, and each square has a number from 1 to 24, and numbering of these squares is like this picture:

These square are like a two-dimensional array, and each square has one X-dimensional and one Y-dimensional, for example square which is marked 14, its X-dimensional is 4 and its Y-dimensional is 3. Therefore we can say the number of squares which is located in (4,3) must be 14, if its number is 14, its location is `true `otherwise its location is `false`.

## History

• 29th June, 2011: Initial post

## Share

 Software Developer Iran (Islamic Republic of)
No Biography provided

## You may also be interested in...

 View All Threads First Prev Next
 My vote of 3 lewax0030-Jun-11 5:24 lewax00 30-Jun-11 5:24
 Last Visit: 31-Dec-99 18:00     Last Update: 26-Apr-18 5:31 Refresh 1