public PuzzleGrid InitGrid()
        {                                                                  //Randomly fill in the first row and column of puzzlegrid
            PuzzleGrid tempGrid = new PuzzleGrid {
            };                                                             //temporary grid to assign values into
            int        row      = 0;                                       //variable for navigating 'rows'
            int        col      = 0;                                       //variable for navigating 'columns'
            int        newVal;                                             //value to place into grid
            bool       solved;
            List <int> valueSet = new List <int>(Enumerable.Range(-9, 9)); //range
            //of numbers that can be added to the grid
            List <int> valueSet2 = new List <int>();                       //placeholder values in column 0
            Random     rnd       = new Random();                           //random variable for choosing random number
            int        randIndex = 0;                                      //index in valueSet/valueSet2 that is accessed

            randIndex = rnd.Next(0, 8);                                    //get a random number and place in grid(0,0)
            newVal    = valueSet[randIndex];
            tempGrid.InitSetCell(row, col, newVal);
            valueSet.Remove(newVal); //remove paced value from options
            for (row = 1; row < 9; row++)
            {                        //fills in column 0 with remaining possible values, storing in place-
                      //holder as it goes so as to preserve when placing in row 0 later
                randIndex = rnd.Next(0, valueSet.Count);
                newVal    = valueSet[randIndex];
                valueSet2.Add(newVal);
                valueSet.Remove(newVal);
                tempGrid.InitSetCell(row, col, newVal);
            }
            row = 0;         //reset row to 0
            for (col = 1; col < 3; col++)
            {                //fills in col 1,2 of row 0, checking that don't duplicate the
                //values in rows 1,2 of col 0
                randIndex = rnd.Next(0, valueSet2.Count);
                newVal    = valueSet2[randIndex];
                while ((newVal == tempGrid.Grid[1, 0] || (newVal == tempGrid.Grid[2, 0])))
                {
                    randIndex = rnd.Next(0, valueSet2.Count);
                    newVal    = valueSet2[randIndex];
                }
                valueSet2.Remove(newVal);
                tempGrid.InitSetCell(row, col, newVal);
            }
            for (col = 3; col < 9; col++)
            {                   //fill in remainder of row 0 with remaining possible values
                randIndex = rnd.Next(0, valueSet2.Count);
                newVal    = valueSet2[randIndex];
                valueSet2.Remove(newVal);
                tempGrid.InitSetCell(row, col, newVal);
            }
            do
            {
                puzzleSolver = new PuzzleSolver();
                puzzleSolver.SolveGrid((PuzzleGrid)tempGrid.Clone(), false);     //Slv to fill remainder of grid
                SolutionGrid = puzzleSolver.SolutionGrid;
            } while (SolutionGrid == null || SolutionGrid.IsBlank());
            PermaGrid = Blanker(SolutionGrid); //call Blanker to carry out the
            return(PermaGrid);                 //blanking of fileds,then return the grid to user to solve
        }
        public PuzzleGrid RandomlyBlank(PuzzleGrid tempGrid, int sym, ref int blankCount)
        {
            //blank one or two squares(depending on if on center line) randomly
            Random rnd    = new Random();           //allow random number generation
            int    row    = rnd.Next(0, 8);         //choose randomly the row
            int    column = rnd.Next(0, 8);         //and column of cell to blank

            while (tempGrid.Grid[row, column] == 0) //don't blank a blank cell
            {
                row    = rnd.Next(0, 8);
                column = rnd.Next(0, 8);
            }
            tempGrid.InitSetCell(row, column, 0); //clear chosen cell
            blankCount++;                         //increment the count of blanks
            switch (sym)
            {
            //based on symmetry, blank a second cell
            case 0:                                       //vertical symmetry
                if (tempGrid.Grid[row, 8 - column] != 0)  //if not already blanked
                {
                    blankCount++;                         //increment blank counter
                }
                tempGrid.InitSetCell(row, 8 - column, 0); //blank opposite cell
                break;

            case 1:         //horizontal symmetry
                if (tempGrid.Grid[8 - row, column] != 0)
                {
                    blankCount++;
                }
                tempGrid.InitSetCell(8 - row, column, 0);
                break;

            case 2:         //diagonal symmetry
                if (tempGrid.Grid[column, row] != 0)
                {
                    blankCount++;
                }
                tempGrid.InitSetCell(column, row, 0);
                break;

            default:         //diagonal symmetry
                if (tempGrid.Grid[row, 8 - column] != 0)
                {
                    blankCount++;
                }
                tempGrid.InitSetCell(column, row, 0);
                break;
            }
            return(tempGrid);
        }
예제 #3
0
        /// <summary>
        /// This function opens a game.
        /// </summary>
        public PuzzleGrid OpenFile(String fileName)
        {
            PuzzleGrid openedPuzzle = new PuzzleGrid();
            int        i            = 0; //mnemonic: row
            int        j            = 0; //mnemonic: column
            int        cellVal      = 0;
            bool       eOF          = false;

            using (StreamReader reader = new StreamReader(fileName))
            {
                for (i = 0; i < 9; i++)
                {
                    string currentLine;
                    if ((currentLine = reader.ReadLine()) != null)
                    {
                        for (j = 0; j < 9; j++)
                        {
                            cellVal = (int)currentLine[2 * j] - '0';
                            if (currentLine[(2 * j) + 1] == '+')
                            {
                                openedPuzzle.InitSetCell(i, j, cellVal);
                            }
                            else if (currentLine[(2 * j) + 1] == '-')
                            {
                                openedPuzzle.InitSetCell(i, j, -(cellVal));
                            }
                        }
                    }
                    else
                    {
                        eOF = true;
                    }
                }
            }
            if (eOF)
            {
                return(null);
            }
            else
            {
                return(openedPuzzle);
            }
        }
예제 #4
0
        /// <summary>
        /// This clones the object.
        /// </summary>
        /// <returns>A clone of itself.</returns>
        public object Clone()
        {
            //enable cloning for safe copying of the object
            PuzzleGrid p = new PuzzleGrid();

            for (int i = 0; i < Max; i++)
            {
                for (int j = 0; j < Max; j++)
                {
                    p.InitSetCell(i, j, Grid[i, j]);
                }
            }
            return(p);
        }
예제 #5
0
        /// <summary>
        /// This function is called when a grid item is selected and a key is pressed. It enforces
        /// all the rules about how keys can manipulate the game grid.
        /// </summary>
        private void GridElementKeyDown(object sender, KeyEventArgs e)
        {
            var name = ((TextBox)sender).Name;

            // get the row/column of the text box
            var rowCol = GetRowColumnFromTextboxName(name);

            switch (e.Key)
            {
            // When we're setting the boxes we also keep the game grid up to date.
            case Key.D1:
            case Key.NumPad1:
                ((TextBox)sender).Text = "1";
                puzzleGrid.InitSetCell(rowCol.Item1, rowCol.Item2, 1);
                break;

            case Key.D2:
            case Key.NumPad2:
                ((TextBox)sender).Text = "2";
                puzzleGrid.InitSetCell(rowCol.Item1, rowCol.Item2, 2);
                break;

            case Key.D3:
            case Key.NumPad3:
                ((TextBox)sender).Text = "3";
                puzzleGrid.InitSetCell(rowCol.Item1, rowCol.Item2, 3);
                break;

            case Key.D4:
            case Key.NumPad4:
                ((TextBox)sender).Text = "4";
                puzzleGrid.InitSetCell(rowCol.Item1, rowCol.Item2, 4);
                break;

            case Key.D5:
            case Key.NumPad5:
                ((TextBox)sender).Text = "5";
                puzzleGrid.InitSetCell(rowCol.Item1, rowCol.Item2, 5);
                break;

            case Key.D6:
            case Key.NumPad6:
                ((TextBox)sender).Text = "6";
                puzzleGrid.InitSetCell(rowCol.Item1, rowCol.Item2, 6);
                break;

            case Key.D7:
            case Key.NumPad7:
                ((TextBox)sender).Text = "7";
                puzzleGrid.InitSetCell(rowCol.Item1, rowCol.Item2, 7);
                break;

            case Key.D8:
            case Key.NumPad8:
                ((TextBox)sender).Text = "8";
                puzzleGrid.InitSetCell(rowCol.Item1, rowCol.Item2, 8);
                break;

            case Key.D9:
            case Key.NumPad9:
                ((TextBox)sender).Text = "9";
                puzzleGrid.InitSetCell(rowCol.Item1, rowCol.Item2, 9);
                break;

            case Key.Delete:
            case Key.Back:
            case Key.D0:
            case Key.NumPad0:
            case Key.Space:
                ((TextBox)sender).Text = "";
                puzzleGrid.InitSetCell(rowCol.Item1, rowCol.Item2, 0);
                break;

            case Key.Left:
                // Calc left item and set it to be focused
                var left = (TextBox)FindName(GetMovedGridItem(name, Direction.Left));
                if (left != null)
                {
                    left.Focus();
                }
                break;

            case Key.Right:
                var right = (TextBox)FindName(GetMovedGridItem(name, Direction.Right));
                if (right != null)
                {
                    right.Focus();
                }
                break;

            case Key.Up:
                var above = (TextBox)FindName(GetMovedGridItem(name, Direction.Up));
                if (above != null)
                {
                    above.Focus();
                }
                break;

            case Key.Down:
                var below = (TextBox)FindName(GetMovedGridItem(name, Direction.Down));
                if (below != null)
                {
                    below.Focus();
                }
                break;
            }
        }