Main functions:
1. The program comes with multiple puzzles and random puzzles.
2. You can add puzzles manually.
3. Successful judgment of the game.
4.30 seconds timeout judgment.
using System; using ; using ; using ; using ; using ; using ; using ; using ; namespace Puzzle { public partial class Puzzle : Form { //Picture List PictureBox[] pictureList = null; //Picture Location Dictionary SortedDictionary<string, Bitmap> pictureLocationDict = new SortedDictionary<string, Bitmap>(); //Location List Point[] pointList = null; //Picture Control Dictionary SortedDictionary<string, PictureBox> pictureBoxLocationDict = new SortedDictionary<string, PictureBox>(); //Just Map Time int second = 0; //The dragged picture PictureBox currentPictureBox = null; //Pictures that are forced to move PictureBox haveToPictureBox = null; //Original location Point oldLocation = ; //New Location Point newLocation = ; // Press the coordinates with the mouse (relative coordinates of the control control) Point mouseDownPoint = ; //Rectangle showing drag effect Rectangle rect = ; //Is it dragging or not bool isDrag = false; public Puzzle() { InitializeComponent(); InitGame(); } /// <summary> /// Initialize game resources /// </summary> public void InitGame() { pictureList = new PictureBox[9] { pictureBox1, pictureBox2, pictureBox3, pictureBox4, pictureBox5, pictureBox6, pictureBox7, pictureBox8, pictureBox9 }; pointList = new Point[9] { new Point(0, 0), new Point(100, 0), new Point(200, 0), new Point(0, 100), new Point(100, 100), new Point(200, 100), new Point(0, 200), new Point(100, 200), new Point(200, 200) }; if (!(() + "\\Picture")) { (() + "\\Picture"); .default.Save(() + "\\Picture\\"); ._1.Save(() + "\\Picture\\"); ._2.Save(() + "\\Picture\\"); ._3.Save(() + "\\Picture\\"); ._4.Save(() + "\\Picture\\"); .success.Save(() + "\\Picture\\"); .cheer.Save(() + "\\Picture\\"); } Random r = new Random(); int i = (7); Flow(() + "\\Picture\\"+()+".jpg"); } private void Puzzle_Paint(object sender, PaintEventArgs e) { if (rect != ) { if (isDrag) { (, rect); } else { (new Pen(), rect); } } } /// <summary> /// Not easy to use /// </summary> /// <returns></returns> public PictureBox GetPictureBoxByLocation() { PictureBox pic = null; if (("pictureBox")) { pic = (PictureBox); } return pic; } public PictureBox GetPictureBoxByLocation(MouseEventArgs e) { PictureBox pic = null; foreach (PictureBox item in pictureList) { if ( > && > && + 100 > && + 100 > ) { pic = item; } } return pic; } public PictureBox GetPictureBoxByLocation(int x,int y) { PictureBox pic = null; foreach (PictureBox item in pictureList) { if (x> && y > && + 100 > x && + 100 > y) { pic = item; } } return pic; } /// <summary> /// Get picture through hashcode, use mouseeventargs to get coordinates relative to picture not relative to the form /// </summary> /// <param name="hascode"></param> /// <returns></returns> public PictureBox GetPictureBoxByHashCode(string hascode) { PictureBox pic = null; foreach (PictureBox item in pictureList) { if (hascode == ().ToString()) { pic = item; } } return pic; } private void pictureBox_MouseDown(object sender, MouseEventArgs e) { oldLocation = new Point(, ); currentPictureBox = GetPictureBoxByHashCode(().ToString()); MoseDown(currentPictureBox, sender, e); } public void MoseDown(PictureBox pic, object sender, MouseEventArgs e) { if ( == ) { oldLocation = ; rect = ; } } private void pictureBox_MouseMove(object sender, MouseEventArgs e) { if ( == ) { isDrag = true; = getPointToForm(new Point( - , - )); (); } } private void reset() { mouseDownPoint = ; rect = ; isDrag = false; } private Point getPointToForm(Point p) { return ((p)); } private void pictureBox_MouseUp(object sender, MouseEventArgs e) { oldLocation = new Point(, ); if ( + > 300 || + > 300|| + < 0 || + < 0) { return; } haveToPictureBox = GetPictureBoxByLocation( + , + ); newLocation = new Point(, ); = oldLocation; PictureMouseUp(currentPictureBox, sender, e); if ( Judge()) { lab_result.Text = "success!"; //("Congratulations on the success of the puzzle"); } } public void PictureMouseUp(PictureBox pic, object sender, MouseEventArgs e) { if ( == ) { if (isDrag) { isDrag = false; = newLocation; (); } reset(); } } public void ExchangePictureBox(MouseEventArgs e) { } private void btn_sta_Click(object sender, EventArgs e) { (); } /// <summary> /// Initialization /// </summary> /// <param name="path"></param> public void Flow(string path) { Image bm = (path, 300, 300); = new List<Bitmap>(); for (int y = 0; y < 300; y += 100) { for (int x = 0; x < 300; x += 100) { //string key = x + "-" + y; Bitmap temp = (bm, x, y, 100, 100); //(key, temp); (temp); } } ImportBitMap(); } /// <summary> /// Disrupt data /// </summary> /// <param name="pictureArray"></param> /// <returns></returns> public PictureBox[] DisOrderArray(PictureBox[] pictureArray) { PictureBox[] tempArray = pictureArray; for (int i = - 1; i > 0; i--) { Random rand = new Random(); int p = (i); PictureBox temp = tempArray[p]; tempArray[p] = tempArray[i]; tempArray[i] = temp; } return tempArray; } /// <summary> /// Determine whether the puzzle is successful /// </summary> /// <returns></returns> public bool Judge() { bool result = true; int i = 0; foreach (PictureBox item in pictureList) { if ( != pointList[i]) { result = false; } i++; } return result; } private void btn_import_Click(object sender, EventArgs e) { lab_result.Text = ""; ofd_picture.ShowDialog(); = ofd_picture.FileName; Flow(); CountTime(); } /// <summary> /// Time /// </summary> public void CountTime() { lab_time.Text = "0"; (); } /// <summary> /// Assign value to the pilotbox /// </summary> public void ImportBitMap() { try { int i = 0;// DisOrderArray(pictureList) foreach (PictureBox item in pictureList) { Bitmap temp = [i]; = temp; i++; } ResetPictureLocation(); } catch (Exception exp) { (); } } /// <summary> /// Disrupt the location list /// </summary> /// <returns></returns> public Point[] DisOrderLocation() { Point[] tempArray = (Point[])(); for (int i = - 1; i > 0; i--) { Random rand = new Random(); int p = (i); Point temp = tempArray[p]; tempArray[p] = tempArray[i]; tempArray[i] = temp; } return tempArray; } /// <summary> /// Reset the image position /// </summary> public void ResetPictureLocation() { Point[] temp = DisOrderLocation(); int i = 0; foreach (PictureBox item in pictureList) { = temp[i]; i++; } } /// <summary> /// Time count, stop time counting after 30 seconds /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer1_Tick(object sender, EventArgs e) { second++; lab_time.Text = (); if (second == 30) { (); lab_result.Text = "fail!"; } } private void btn_sta_Click_1(object sender, EventArgs e) { lab_result.Text = ""; (); } } }
using System; using ; using ; using ; using ; using ; using ; namespace Puzzle { class CutPicture { public static string PicturePath = ""; public static List<Bitmap> BitMapList = null; /// <summary> /// Cut the picture /// </summary> /// <param name="b">Picture</param> /// <param name="StartX">X coordinates</param> /// <param name="StartY">Y coordinates</param> /// <param name="iWidth">Width</param> /// <param name="iHeight">High</param> /// <returns></returns> public static Bitmap Cut(Image b, int StartX, int StartY, int iWidth, int iHeight) { if (b == null) { return null; } int w = ; int h = ; if (StartX >= w || StartY >= h) { return null; } if (StartX + iWidth > w) { iWidth = w - StartX; } if (StartY + iHeight > h) { iHeight = h - StartY; } try { Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb); Graphics g = (bmpOut); (b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), ); (); return bmpOut; } catch { return null; } } /// <summary> /// Save the picture to the Pictures folder in the root directory /// </summary> /// <param name="path">File path</param> /// <param name="iWidth">Adjust width</param> /// <param name="iHeignt">adjustment high</param> /// <returns></returns> public static Image Resize(string path, int iWidth, int iHeignt) { Image thumbnail = null; try { var img = (path); thumbnail = (iWidth, iHeignt, null, ); (() + "\\Picture\\"); } catch (Exception exp) { (); } return thumbnail; } } }
mouse_down
private void pictureBox_MouseDown(object sender, MouseEventArgs e) { oldLocation = new Point(, ); currentPictureBox = GetPictureBoxByHashCode(().ToString()); MoseDown(currentPictureBox, sender, e); } public void MoseDown(PictureBox pic, object sender, MouseEventArgs e) { if ( == ) { oldLocation = ; rect = ; } }
mouse_move
private void pictureBox_MouseMove(object sender, MouseEventArgs e) { if ( == ) { isDrag = true; = getPointToForm(new Point( - , - )); (); } }
mouse_up
private void pictureBox_MouseUp(object sender, MouseEventArgs e) { oldLocation = new Point(, ); if ( + > 300 || + > 300|| + < 0 || + < 0) { return; } haveToPictureBox = GetPictureBoxByLocation( + , + ); newLocation = new Point(, ); = oldLocation; PictureMouseUp(currentPictureBox, sender, e); if ( Judge()) { lab_result.Text = "success!"; //("Congratulations on the success of the puzzle"); } } public void PictureMouseUp(PictureBox pic, object sender, MouseEventArgs e) { if ( == ) { if (isDrag) { isDrag = false; = newLocation; (); } reset(); } }
reset
private void reset() { mouseDownPoint = ; rect = ; isDrag = false; }
The above is the entire content of this article, I hope you like it.