Example #1
0
        /*
         * read in the file from stream and instantiate everything
         * attempt to solve the puzzle
         */
        public bool Run(Board board)
        {
            brd          = board;
            InitialBoard = new Cell[brd.n, brd.n];
            brd.DisplayBoard();

            for (int i = 0; i < brd.n; i++)
            {
                for (int j = 0; j < brd.n; j++)
                {
                    InitialBoard[i, j] = brd.board[i, j];
                }
            }

            //if the board is still valid and a change has been made keep trying.
            while (changes && brd.valid)
            {
                //reset changes and update the list of choices for each cell.
                changes = false;
                brd.UpdateChoices();
                //if there are no choices at the current cell skip.


                changes = OneChoice(brd);
                if (changes)
                {
                    continue;
                }
                changes = OnePlace(brd);
                if (changes)
                {
                    continue;
                }
                changes = Guess(brd);
            }

            //check if any empty cells remaining. change to false if not completely solved.
            foreach (Cell c in brd.board)
            {
                if (c.displayedChar == '-' && brd.valid)
                {
                    brd.valid  = false;
                    brd.reason = "Couldn't solve";
                    break;
                }
            }
            brd.DisplayBoard();
            return(brd.valid);
        }