0% found this document useful (0 votes)
61 views6 pages

Number Puzzle Game (C# Program)

The document describes the steps to create a number puzzle game using C#. It involves opening a new project in Visual Studio, deleting the code in Form1.cs, and copying in code to initialize a 4x4 button grid for the puzzle. The code handles button clicks to swap adjacent numbers and track the number of moves. It also includes functions for shuffling the puzzle and checking if it is solved.

Uploaded by

mercy vara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views6 pages

Number Puzzle Game (C# Program)

The document describes the steps to create a number puzzle game using C#. It involves opening a new project in Visual Studio, deleting the code in Form1.cs, and copying in code to initialize a 4x4 button grid for the puzzle. The code handles button clicks to swap adjacent numbers and track the number of moves. It also includes functions for shuffling the puzzle and checking if it is solved.

Uploaded by

mercy vara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Number Puzzle Game

(C# program)

Step 1 :

Open Visual Studio... Dot net....Windows....Desktop (Name your Project as Number_Puzzle).

David vara Page 1 of 6


Step 2 :
Form1.cs will look like this....

Step3 : Delete all the code on Form1.cs and Copy Paste the following code into Form1.cs

Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Number_Puzzle_Game
{
public partial class Form1 : Form
{
private Button[,] buttons;
private int emptyRow, emptyCol;
private int moves;

public Form1()
{
InitializeComponent();
InitializePuzzle();
this.Width = 700;
this.Height = 500;
this.CenterToScreen();
}

private void InitializePuzzle()


{
buttons = new Button[4, 4];
moves = 0;
David vara Page 2 of 6
// Create and configure buttons
int buttonSize = 100;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
buttons[i, j] = new Button();
buttons[i, j].Width = buttonSize;
buttons[i, j].Height = buttonSize;
buttons[i, j].Font = new System.Drawing.Font("Microsoft Sans Serif", 18, FontStyle.Bold);
buttons[i, j].Click += Button_Click;
buttons[i, j].Tag = new Tuple<int, int>(i, j);

if (i == 3 && j == 3)
{
buttons[i, j].Text = "";
emptyRow = i;
emptyCol = j;
}
else
{
buttons[i, j].Text = (i * 4 + j + 1).ToString();
}

Controls.Add(buttons[i, j]);
buttons[i, j].Location = new System.Drawing.Point(j * buttonSize, i * buttonSize);
}
}

// Create shuffle button


Button shuffleButton = new Button();
shuffleButton.Text = "Shuffle";
shuffleButton.Click += ShuffleButton_Click;
shuffleButton.ForeColor = Color.Red;
shuffleButton.BackColor = Color.Yellow;
shuffleButton.AutoSize = true;
shuffleButton.Font = new Font("Microsoft Sans Serif", 18, FontStyle.Bold);
Controls.Add(shuffleButton);
shuffleButton.Location = new System.Drawing.Point(450, 50);

// Create moves label


Label movesLabel = new Label();
movesLabel.Text = "Moves:";
movesLabel.ForeColor = Color.Red;
movesLabel.BackColor = Color.Yellow;
movesLabel.AutoSize = true;
movesLabel.Font = new Font("Microsoft Sans Serif", 18, FontStyle.Bold);
Controls.Add(movesLabel);
movesLabel.Location = new System.Drawing.Point(450, 150);

// Create moves counter


Label movesCounter = new Label();
movesCounter.Text = "0";
movesCounter.Name = "MovesCounter";
movesCounter.ForeColor = Color.Red;
movesCounter.BackColor = Color.Yellow;
movesCounter.AutoSize = true;
movesCounter.Font = new Font("Microsoft Sans Serif", 18, FontStyle.Bold);
David vara Page 3 of 6
movesCounter.Location = new System.Drawing.Point(550, 150);
Controls.Add(movesCounter);
}

private void Button_Click(object sender, EventArgs e)


{
Button clickedButton = (Button)sender;
Tuple<int, int> clickedPosition = (Tuple<int, int>)clickedButton.Tag;

if (IsAdjacentToEmpty(clickedPosition))
{
SwapButtons(emptyRow, emptyCol, clickedPosition.Item1, clickedPosition.Item2);
emptyRow = clickedPosition.Item1;
emptyCol = clickedPosition.Item2;
moves++;
UpdateMovesCounter(); // Update moves counter after each move
CheckForWin();
}
}

private void ShuffleButton_Click(object sender, EventArgs e)


{
ShufflePuzzle();
moves = 0;
UpdateMovesCounter();
}

private void SwapButtons(int row1, int col1, int row2, int col2)
{
string tempText = buttons[row1, col1].Text;
buttons[row1, col1].Text = buttons[row2, col2].Text;
buttons[row2, col2].Text = tempText;
}

private bool IsAdjacentToEmpty(Tuple<int, int> position)


{
int row = position.Item1;
int col = position.Item2;

return (Math.Abs(row - emptyRow) == 1 && col == emptyCol) || (Math.Abs(col - emptyCol) == 1 && row ==
emptyRow);
}

private void ShufflePuzzle()


{
Random random = new Random();

// Perform a series of random valid moves to shuffle the puzzle


for (int i = 0; i < 1000; i++)
{
List<Tuple<int, int>> validMoves = GetValidMoves(emptyRow, emptyCol);
Tuple<int, int> randomMove = validMoves[random.Next(validMoves.Count)];

SwapButtons(emptyRow, emptyCol, randomMove.Item1, randomMove.Item2);


emptyRow = randomMove.Item1;
emptyCol = randomMove.Item2;
}
}

David vara Page 4 of 6


private List<Tuple<int, int>> GetValidMoves(int row, int col)
{
List<Tuple<int, int>> validMoves = new List<Tuple<int, int>>();

if (row > 0) validMoves.Add(new Tuple<int, int>(row - 1, col));


if (row < 3) validMoves.Add(new Tuple<int, int>(row + 1, col));
if (col > 0) validMoves.Add(new Tuple<int, int>(row, col - 1));
if (col < 3) validMoves.Add(new Tuple<int, int>(row, col + 1));

return validMoves;
}

private void UpdateMovesCounter()


{
// Find the control with the specified name
Control[] controls = Controls.Find("MovesCounter", true);

if (controls.Length > 0 && controls[0] is Label movesCounter)


{
// Update the moves counter text
movesCounter.Text = moves.ToString();
}
}

private void CheckForWin()


{
bool win = true;

for (int i = 0; i < 4; i++)


{
for (int j = 0; j < 4; j++)
{
if (!(i == 3 && j == 3) && buttons[i, j].Text != (i * 4 + j + 1).ToString())
{
win = false;
break;
}
}
}

if (win)
{
MessageBox.Show($"Congratulations! You solved the puzzle in {moves} moves!");
}
}
}
}

David vara Page 5 of 6


Form1.cs looks like this....

Now, Build the Project, Run and Enjoy....

Youtube Video link : https://www.youtube.com/watch?v=og4fG-76f_o&feature=youtu.be

Thank you...
David vara

David vara Page 6 of 6

You might also like