Example #1
0
        /// <summary>
        /// Suspend the visuals of the gamePanel until a board has been generated and is ready to view.
        /// </summary>
        /// <param name="size"></param>
        private void CreateGame(int size, int width, int height)
        {
            // Hide the game panel.
            gamePanel.Visible = false;
            gamePanel.Controls.Clear();

            // Get the flags (settings) for the new game.
            BlockFlag gameMode = GetFlags();

            grid = new SudokuGrid(width, height, size, gameMode);

            ConnectCells();
            // Show the game panel after cells are connected again.
            gamePanel.ResumeLayout();
            gamePanel.Visible = true;
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the SudokuSolver.SudokuGrid class.
        /// </summary>
        public SudokuGrid(int width, int height, int size, BlockFlag gameMode = new BlockFlag())
        {
            this.width  = width;
            this.height = height;
            this.size   = size;

            this.cells = GridOperations.Create(size);
            var movement = new Movement(cells);

            this.mapping = new Dictionary <Direction, Func <SudokuCell, SudokuCell> >
            {
                { Direction.Up, cell => movement.Up(cell) },
                { Direction.Down, cell => movement.Down(cell) },
                { Direction.Left, cell => movement.Left(cell) },
                { Direction.Right, cell => movement.Right(cell) },
                { Direction.JumpForward, cell => movement.JumpForward(cell) },
                { Direction.JumpBackward, cell => movement.JumpBackward(cell) }
            };

            this.neighbors  = Block.FindNeighbors(this, gameMode);
            this.activeCell = cells[0, 0];
        }
Example #3
0
        public static IDictionary <SudokuCell, IEnumerable <SudokuCell> > FindNeighbors(SudokuGrid grid, BlockFlag gameMode)
        {
            Dictionary <SudokuCell, IEnumerable <SudokuCell> > keyValuePairs = new Dictionary <SudokuCell, IEnumerable <SudokuCell> >();

            foreach (var cell in grid.cells)
            {
                // Focus on the cell under inspection.
                grid.activeCell = cell;
                // Always get horizontal and vertical neighbors.
                IEnumerable <SudokuCell> region = GetHorizontal(grid).Union(GetVertical(grid));
                // Usually get nearby neighbors.
                if (gameMode.Boxes)
                {
                    region = region.Union(GetBox(cell, grid));
                }
                if (gameMode.XSudoku)
                {
                    // Only look at true diagonals in XSudoku.
                    if (grid.activeCell.X + grid.activeCell.Y == grid.size - 1)
                    {
                        region = region.Union(BottomLeftToTopRight(grid));
                    }
                    if (grid.activeCell.X == grid.activeCell.Y)
                    {
                        region = region.Union(TopLeftToBottomRight(grid));
                    }
                }
                // Add region to dictionary for easy access later.
                keyValuePairs[cell] = region;
            }
            return(keyValuePairs);
        }