예제 #1
0
        private void btnShowUnsolvedBoard_Click(object sender, RoutedEventArgs e)
        {
            int sz = Convert.ToInt32(txtSz.Text);

            int[] bd = new int[sz];
            for (int i = 0; i < sz; i++)
            {
                bd[i] = i;
            }
            QueensGUI theWindow = new QueensGUI(bd);

            theWindow.Owner = this;
            theWindow.Show();
            theWindow.RefreshQueenPositions();
        }
예제 #2
0
        /// <summary>
        /// Find and return one random solution to the NxN queens problem.
        /// </summary>
        /// <param name="N"></param>
        /// <returns></returns>
        private int[] findQueensSolution(int N)
        {
            // set up the initial board of the correct size
            int[] bd = new int[N];
            for (int i = 0; i < N; i++)
            {
                bd[i] = i;
            }

            bool wantGUI       = cbShowBoard.IsChecked.Value == true;
            bool wantAnimation = wantGUI && cbAnimated.IsChecked.Value == true;

            QueensGUI theWindow = null;

            if (wantGUI)
            {
                theWindow       = new QueensGUI(bd);
                theWindow.Owner = this;
                theWindow.Show();
            }

            int tries = 1;

            while (boardHasDiagonalClashes(bd))
            {
                shuffle(bd);
                if (wantAnimation)
                {
                    theWindow.RefreshQueenPositions();
                }
                tries++;
            }

            if (wantGUI)
            {
                theWindow.RefreshQueenPositions();
            }
            tot += tries;

            txtResults.AppendText(string.Format("Solution {0} found in {1} tries.\n", stringify(bd), tries));
            txtResults.ScrollToEnd();

            return(bd);
        }