public void OnPuzzleClick(object sender, EventArgs e) { if (firstBox == null) { firstBox = (MysteryBox)sender; firstBox.BorderStyle = BorderStyle.FixedSingle; } else if (secondBox == null) { secondBox = (MysteryBox)sender; firstBox.BorderStyle = BorderStyle.Fixed3D; secondBox.BorderStyle = BorderStyle.FixedSingle; SwitchImage(firstBox, secondBox); firstBox = null; secondBox = null; } }
// Method for switching two puzzles private void SwitchImage(MysteryBox box1, MysteryBox box2) { int tmp = box2.ImageIndex; box2.Image = images[box1.ImageIndex]; box2.ImageIndex = box1.ImageIndex; box1.Image = images[tmp]; box1.ImageIndex = tmp; if (IsSuccessful()) { buttonCheck.PerformClick(); MessageBox.Show("Success"); numericUpDownRows.Enabled = true; numericUpDownColumns.Enabled = true; } }
private void ButtonShuffle_Click(object sender, EventArgs e) { numberOfRows = Convert.ToInt32(numericUpDownRows.Value); numberOfColumns = Convert.ToInt32(numericUpDownColumns.Value); countOfFragments = numberOfRows * numberOfColumns; if (pictureboxPuzzle != null) { groupBoxPuzzle.Controls.Remove(pictureboxPuzzle); pictureboxPuzzle.Dispose(); pictureboxPuzzle = null; } if (mysteryBoxes == null) { images = new Image[countOfFragments]; mysteryBoxes = new MysteryBox[countOfFragments]; } else { for (int j = 0; j < mysteryBoxes.Length; j++) { //images[j].Dispose(); images[j] = null; //mysteryBoxes[j].Dispose(); mysteryBoxes[j] = null; } images = new Image[countOfFragments]; mysteryBoxes = new MysteryBox[countOfFragments]; } int unitX = groupBoxPuzzle.Width / numberOfColumns; int unitY = groupBoxPuzzle.Height / numberOfRows; int[] indice = new int[countOfFragments]; for (int i = 0; i < countOfFragments; i++) { indice[i] = i; if (mysteryBoxes[i] != null) { mysteryBoxes[i].BorderStyle = BorderStyle.Fixed3D; groupBoxPuzzle.Controls.Remove(mysteryBoxes[i]); mysteryBoxes[i] = null; } if (mysteryBoxes[i] == null) { mysteryBoxes[i] = new MysteryBox(); mysteryBoxes[i].Click += new EventHandler(OnPuzzleClick); mysteryBoxes[i].BorderStyle = BorderStyle.Fixed3D; mysteryBoxes[i].SizeMode = PictureBoxSizeMode.CenterImage; mysteryBoxes[i].Width = unitX; mysteryBoxes[i].Height = unitY; } mysteryBoxes[i].Index = i; BitmapHelper.CreateBitmapImage(image, images, i, numberOfColumns, unitX, unitY); mysteryBoxes[i].Location = new Point(unitX * (i % numberOfColumns), unitY * (i / numberOfColumns)); if (!groupBoxPuzzle.Controls.Contains(mysteryBoxes[i])) { groupBoxPuzzle.Controls.Add(mysteryBoxes[i]); } } Shuffle(ref indice); listOfShuffledImages = new List <Bitmap>(countOfFragments); for (int i = 0; i < countOfFragments; i++) { listOfShuffledImages.Add((Bitmap)images[indice[i]]); mysteryBoxes[i].ImageIndex = indice[i]; mysteryBoxes[i].Image = images[indice[i]]; } numericUpDownRows.Enabled = false; numericUpDownColumns.Enabled = false; buttonCheck.Enabled = true; buttonAutomaticAssemblyPuzzle.Enabled = true; }