--------------------------------------------------------------------------------------------------------------------------
Program.cs
--------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Puzzle
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Puzzle_frm());
}
}
}
--------------------------------------------------------------------------------------------------------------------------
Puzzle_frm.cs
--------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Puzzle
{
public partial class Puzzle_frm : Form
{
public Puzzle_frm()
{
InitializeComponent();
ToBeResize = Image.FromFile(Application.StartupPath + "\\game.jpg");
original_pic.BackgroundImage = ToBeResize;
EmptyPoint.X = 210;
EmptyPoint.Y = 210;
AddImagesToButtons(ReturnCroppedList(Resize(ToBeResize), 70, 70));
}
Point EmptyPoint;
Image ToBeResize;
private void Puzzle_frm_Load(object sender, EventArgs e)
{
}
private void Buttons_Click(object sender, EventArgs e)
{
MoveButton((Button)sender);
}
private void MoveButton(Button button)
{
int x = EmptyPoint.X - button.Location.X;
int px = x < 0 ? -x : x;
int y = EmptyPoint.Y - button.Location.Y;
int py = y < 0 ? -y : y;
//moving right &left
if (button.Location.Y.Equals(EmptyPoint.Y) && px.Equals(button.Size.Width))
{
button.Location = new Point(button.Location.X + x, button.Location.Y);
EmptyPoint.X -= x;
}
//moving top &botton
if (button.Location.X.Equals(EmptyPoint.X) && py.Equals(button.Size.Width))
{
button.Location = new Point(button.Location.X, button.Location.Y + y);
EmptyPoint.Y -= y;
}
}
public void AddImagesToButtons(ArrayList pieces)
{
int x = 0;
int[] c = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
Shuffle(c);
foreach (Button b in butns_container.Controls)
{
if (x < c.Length)
{
b.Image = (Image)pieces[c[x]];
x++;
}
}
}
public ArrayList ReturnCroppedList(Bitmap ToBeCropped, int x, int y)
{
ArrayList img_pieces = new ArrayList();
int moveright = 0;
int movedown = 0;
for (int k = 0; k < 15; k++)
{
Bitmap piece = new Bitmap(x, y);
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
piece.SetPixel(i, j, ToBeCropped.GetPixel(i + moveright, j + movedown));
img_pieces.Add(piece);
moveright += 70;
if (moveright == 280) { moveright = 0; movedown += 70; }
if (movedown == 280) { break; }
}
return img_pieces;
}
private void img_brws_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Image Files (JPEG,GIF,BMP,PNG,JPG)|*.jpeg;*.bmp;*.png;*.jpg";
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
ToBeResize = Image.FromFile(openFileDialog1.FileName);
original_pic.Image = Resize(ToBeResize);
AddImagesToButtons(ReturnCroppedList(Resize(ToBeResize), 70, 70));
}
}
private Bitmap Resize(Image img)
{
int w = 280;
int h = 280;
//create a new Bitmap the size of the new image
Bitmap bmp = new Bitmap(w, h);
//create a new graphic from the Bitmap
Graphics graphic = Graphics.FromImage((Image)bmp);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
//draw the newly resized image
graphic.DrawImage(img, 0, 0, w, h);
//dispose and free up the resources
graphic.Dispose();
//return the image
return bmp;
}
private void stretch_frm_Click(object sender, EventArgs e)
{
if (stretch_frm.Text == "<<<")
{
stretch_frm.Text = ">>>";
this.Size = new Size(380, 386);
}
else if (stretch_frm.Text == ">>>")
{
stretch_frm.Text = "<<<";
this.Size = new Size(687, 386);
}
}
private void Randm_btn_Click(object sender, EventArgs e)
{
AddImagesToButtons(ReturnCroppedList(Resize(ToBeResize), 70, 70));
}
private int[] Shuffle(int[] array)
{
Random r = new Random();
int start = r.Next(1, array.Length);
for (int i = 0; i < array.Length; i++)
for (int j = start;j>0; j--)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
--------------------------------------------------------------------------------------------------------------------------
Puzzle_frm.Designer.cs
--------------------------------------------------------------------------------------------------------------------------
namespace Puzzle
{
partial class Puzzle_frm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Puzzle_frm));
this.butns_container = new System.Windows.Forms.Panel();
this.button5 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button15 = new System.Windows.Forms.Button();
this.button14 = new System.Windows.Forms.Button();
this.button13 = new System.Windows.Forms.Button();
this.button12 = new System.Windows.Forms.Button();
this.button11 = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.Randm_btn = new System.Windows.Forms.Button();
this.img_brws = new System.Windows.Forms.Button();
this.original_pic = new System.Windows.Forms.PictureBox();
this.stretch_frm = new System.Windows.Forms.Button();
this.butns_container.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.original_pic)).BeginInit();
this.SuspendLayout();
//
// butns_container
//
this.butns_container.BackColor = System.Drawing.SystemColors.HotTrack;
this.butns_container.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.butns_container.Controls.Add(this.button5);
this.butns_container.Controls.Add(this.button8);
this.butns_container.Controls.Add(this.button7);
this.butns_container.Controls.Add(this.button6);
this.butns_container.Controls.Add(this.button15);
this.butns_container.Controls.Add(this.button14);
this.butns_container.Controls.Add(this.button13);
this.butns_container.Controls.Add(this.button12);
this.butns_container.Controls.Add(this.button11);
this.butns_container.Controls.Add(this.button10);
this.butns_container.Controls.Add(this.button9);
this.butns_container.Controls.Add(this.button1);
this.butns_container.Controls.Add(this.button4);
this.butns_container.Controls.Add(this.button3);
this.butns_container.Controls.Add(this.button2);
this.butns_container.Location = new System.Drawing.Point(12, 12);
this.butns_container.Name = "butns_container";
this.butns_container.Size = new System.Drawing.Size(280, 280);
this.butns_container.TabIndex = 16;
//
// button5
//
this.button5.Location = new System.Drawing.Point(0, 70);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(70, 70);
this.button5.TabIndex = 4;
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.Buttons_Click);
//
// button8
//
this.button8.Location = new System.Drawing.Point(210, 70);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(70, 70);
this.button8.TabIndex = 7;
this.button8.UseVisualStyleBackColor = true;
this.button8.Click += new System.EventHandler(this.Buttons_Click);
//
// button7
//
this.button7.Location = new System.Drawing.Point(140, 70);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(70, 70);
this.button7.TabIndex = 6;
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.Buttons_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(70, 70);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(70, 70);
this.button6.TabIndex = 5;
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.Buttons_Click);
//
// button15
//
this.button15.Location = new System.Drawing.Point(140, 210);
this.button15.Name = "button15";
this.button15.Size = new System.Drawing.Size(70, 70);
this.button15.TabIndex = 14;
this.button15.UseVisualStyleBackColor = true;
this.button15.Click += new System.EventHandler(this.Buttons_Click);
//
// button14
//
this.button14.Location = new System.Drawing.Point(70, 210);
this.button14.Name = "button14";
this.button14.Size = new System.Drawing.Size(70, 70);
this.button14.TabIndex = 13;
this.button14.UseVisualStyleBackColor = true;
this.button14.Click += new System.EventHandler(this.Buttons_Click);
//
// button13
//
this.button13.Location = new System.Drawing.Point(0, 210);
this.button13.Name = "button13";
this.button13.Size = new System.Drawing.Size(70, 70);
this.button13.TabIndex = 12;
this.button13.UseVisualStyleBackColor = true;
this.button13.Click += new System.EventHandler(this.Buttons_Click);
//
// button12
//
this.button12.Location = new System.Drawing.Point(210, 140);
this.button12.Name = "button12";
this.button12.Size = new System.Drawing.Size(70, 70);
this.button12.TabIndex = 11;
this.button12.UseVisualStyleBackColor = true;
this.button12.Click += new System.EventHandler(this.Buttons_Click);
//
// button11
//
this.button11.Location = new System.Drawing.Point(140, 140);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(70, 70);
this.button11.TabIndex = 10;
this.button11.UseVisualStyleBackColor = true;
this.button11.Click += new System.EventHandler(this.Buttons_Click);
//
// button10
//
this.button10.Location = new System.Drawing.Point(70, 140);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(70, 70);
this.button10.TabIndex = 9;
this.button10.UseVisualStyleBackColor = true;
this.button10.Click += new System.EventHandler(this.Buttons_Click);
//
// button9
//
this.button9.Location = new System.Drawing.Point(0, 140);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(70, 70);
this.button9.TabIndex = 8;
this.button9.UseVisualStyleBackColor = true;
this.button9.Click += new System.EventHandler(this.Buttons_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(70, 70);
this.button1.TabIndex = 0;
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.Buttons_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(210, 0);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(70, 70);
this.button4.TabIndex = 3;
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.Buttons_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(140, 0);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(70, 70);
this.button3.TabIndex = 2;
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.Buttons_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(70, 0);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(70, 70);
this.button2.TabIndex = 1;
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.Buttons_Click);
//
// Randm_btn
//
this.Randm_btn.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Randm_btn.Location = new System.Drawing.Point(12, 313);
this.Randm_btn.Name = "Randm_btn";
this.Randm_btn.Size = new System.Drawing.Size(103, 23);
this.Randm_btn.TabIndex = 17;
this.Randm_btn.Text = "Start Puzzle";
this.Randm_btn.UseVisualStyleBackColor = true;
this.Randm_btn.Click += new System.EventHandler(this.Randm_btn_Click);
//
// img_brws
//
this.img_brws.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.img_brws.Location = new System.Drawing.Point(178, 313);
this.img_brws.Name = "img_brws";
this.img_brws.Size = new System.Drawing.Size(115, 23);
this.img_brws.TabIndex = 18;
this.img_brws.Text = "Choose image";
this.img_brws.UseVisualStyleBackColor = true;
this.img_brws.Click += new System.EventHandler(this.img_brws_Click);
//
// original_pic
//
this.original_pic.Location = new System.Drawing.Point(368, 13);
this.original_pic.Name = "original_pic";
this.original_pic.Size = new System.Drawing.Size(280, 280);
this.original_pic.TabIndex = 19;
this.original_pic.TabStop = false;
//
// stretch_frm
//
this.stretch_frm.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.stretch_frm.Location = new System.Drawing.Point(299, 142);
this.stretch_frm.Name = "stretch_frm";
this.stretch_frm.Size = new System.Drawing.Size(54, 23);
this.stretch_frm.TabIndex = 20;
this.stretch_frm.Text = ">>>";
this.stretch_frm.UseVisualStyleBackColor = true;
this.stretch_frm.Click += new System.EventHandler(this.stretch_frm_Click);
//
// Puzzle_frm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.MenuHighlight;
this.ClientSize = new System.Drawing.Size(361, 346);
this.Controls.Add(this.stretch_frm);
this.Controls.Add(this.original_pic);
this.Controls.Add(this.img_brws);
this.Controls.Add(this.Randm_btn);
this.Controls.Add(this.butns_container);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Puzzle_frm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Puzzle Game Created By:-sagar mistry";
this.Load += new System.EventHandler(this.Puzzle_frm_Load);
this.butns_container.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.original_pic)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel butns_container;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button15;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button13;
private System.Windows.Forms.Button button11;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button10;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button12;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button14;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button Randm_btn;
private System.Windows.Forms.Button img_brws;
private System.Windows.Forms.PictureBox original_pic;
private System.Windows.Forms.Button stretch_frm;
}
}
Thank you
subscribe my channel and likes my videos and any doubt you can put in comments or my blogger comments