/// <summary> /// Triggers when a custom configuration is added /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnCustomConfigSolved(object sender, CustomConfigEventArgs e) { // Set move and moves variables move = 1; labelCurrentMove.Text = (move - 1).ToString(); moves = e.Moves; labelMovesNeeded.Text = (moves - 1).ToString(); // Set Solution and Positions solution = e.Solution; positions = e.Positions; #region Loop thru all puzzle pieces to populate and set values to them for (int i = 0; i < Puzzle.NUM_PIECES; i++) { // Assign the value to the relevent puzzle piece pieces[i].Text = positions[i].ToString(); #region Check for the "Empty Tile" if (positions[i] == 0) { // This is the "Empty Tile" pieces[i].Text = null; pieces[i].BackColor = emptyPieceColor; } else { // Not the "Empty Tile" pieces[i].BackColor = Properties.Settings.Default.BackColor; } #endregion } #endregion groupBoxPlayModes.Enabled = true; labelStatus.Text = AI_MODE; isShuffled = true; }
/// <summary> /// Tries to find a soluton within given max moves /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnOK_Click(object sender, EventArgs e) { #region Add values of all NumericUpDown controls to a list List <int> values = new List <int> { (int)numFirstBox.Value, (int)numSecondBox.Value, (int)numThirdBox.Value, (int)numFourthBox.Value, (int)numFifthBox.Value, (int)numSixthBox.Value, (int)numSeventhBox.Value, (int)numEightBox.Value, (int)numNinthBox.Value }; #endregion // Try to solve and get a solution for the configuration moves = Solve(values); // Check if a solution found within the max moves if (moves == Puzzle.MAX_MOVES) { // No solution found DialogResult result = MessageBox.Show("Sorry, could not find a solution for this configuration", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information); if (result == DialogResult.Retry) { // Retry moves = Solve(values); if (moves == Puzzle.MAX_MOVES) { // No solution found MessageBox.Show("Sorry, could not find a solution for this configuration," + "please try another one", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } else { // Solution found // Clear errors errorProvider.Clear(); #region Get position of each puzzle piece for the first move int[] positions = new int[Puzzle.NUM_PIECES]; for (int i = 0; i < Puzzle.NUM_PIECES; i++) { positions[i] = solution[0, i]; } #endregion // Close parent form ParentForm.Close(); #region Set values for custom config CustomConfigEventArgs conf = new CustomConfigEventArgs() { Moves = moves, Positions = positions, Solution = solution }; #endregion // Invoke the custom event OnCustomConfigSolved.Invoke(this, conf); } }