Ejemplo n.º 1
0
        /// <summary>
        /// This method creates three new sudoku grids.
        /// </summary>
        private void makeNewGrids()
        {
            easy = gen.Generate(DifficultyLevel.Easy);
            easyPanel.Controls.Clear();
            SudokuGridControl easyGcontrol = new SudokuGridControl(easy);

            easyPanel.Controls.Add(easyGcontrol);
            easyGcontrol.Dock       = DockStyle.Fill;
            easyGcontrol.IsEditable = false;

            med = gen.Generate(DifficultyLevel.Medium);
            medPanel.Controls.Clear();
            SudokuGridControl medGcontrol = new SudokuGridControl(med);

            medPanel.Controls.Add(medGcontrol);
            medGcontrol.Dock       = DockStyle.Fill;
            medGcontrol.IsEditable = false;

            hard = gen.Generate(DifficultyLevel.Hard);
            hardPanel.Controls.Clear();
            SudokuGridControl hardGcontrol = new SudokuGridControl(hard);

            hardPanel.Controls.Add(hardGcontrol);
            hardGcontrol.Dock       = DockStyle.Fill;
            hardGcontrol.IsEditable = false;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method creates three new sudoku grids.
        /// </summary>
        private void makeNewGrids()
        {
            easy = gen.Generate(DifficultyLevel.Easy);
            easyPanel.Controls.Clear();
            SudokuGridControl easyGcontrol = new SudokuGridControl(easy);
            easyPanel.Controls.Add(easyGcontrol);
            easyGcontrol.Dock = DockStyle.Fill;
            easyGcontrol.IsEditable = false;

            med = gen.Generate(DifficultyLevel.Medium);
            medPanel.Controls.Clear();
            SudokuGridControl medGcontrol = new SudokuGridControl(med);
            medPanel.Controls.Add(medGcontrol);
            medGcontrol.Dock = DockStyle.Fill;
            medGcontrol.IsEditable = false;

            hard = gen.Generate(DifficultyLevel.Hard);
            hardPanel.Controls.Clear();
            SudokuGridControl hardGcontrol = new SudokuGridControl(hard);
            hardPanel.Controls.Add(hardGcontrol);
            hardGcontrol.Dock = DockStyle.Fill;
            hardGcontrol.IsEditable = false;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a new form for playing the game
        /// </summary>
        /// <param name="grid">The grid to start with</param>
        /// <param name="playingMode">If true: We're "playing" the game. If false: We're "editing" the game.</param>
        public GameForm(Grid grid, bool playingMode)
        {
            InitializeComponent();
            this.grid      = grid;
            this.isPlaying = playingMode;

            hintBarText.Text = "";
            if (isPlaying)
            {
                solveButton.Text = "&Solve";
            }
            else
            {
                solveButton.Text = "&Enter Puzzle";
            }

            // make the grid control and put it somewhere
            this.gcontrol = new SudokuGridControl(grid);
            this.gridPanel.Controls.Add(gcontrol);
            gcontrol.Dock = DockStyle.Fill;

            // When the cell is cleared, mark/unmark errors and hints
            gcontrol.CellClear += (int row, int col) =>
            {
                nagAboutErrors  = true;
                nagAboutWonGame = true;
                RecalculateErrors();
                RecalculateHints(row, col);
                ShowOrHideHintBar();
            };

            // When the cell's value is changed, mark/unmark errors and hints
            gcontrol.CellChange += (int row, int col) =>
            {
                MaybeTryGameOver();
                RecalculateErrors();
                RecalculateHints(row, col);
                ShowOrHideHintBar();
            };

            // When the user selects a different cell, mark/unmark errors and hints
            gcontrol.CellFocused += (int row, int col) =>
            {
                ShowOrHideHintBar();
                if (isPlaying)
                {
                    RecalculateHints(row, col);
                }
            };

            // Add a drop-down context menu to each textbox
            gcontrol.ForEachTextBox((TextBox tbox, int row, int col) =>
            {
                tbox.ContextMenu = new ContextMenu();
                if (isPlaying)
                {
                    // The context menu should only appear when in "Playing" mode
                    tbox.ContextMenu.MenuItems.Add(new MenuItem("Show &Hints", (s, e) =>
                    {
                        hintBarText.Show();
                        // the hints bar will disappear on next call to ShowOrHideHints()
                    }));
                    tbox.ContextMenu.MenuItems.Add(new MenuItem("&Solve This Square", (s, e) =>
                    {
                        // solve the grid and copy the value
                        if (grid.IsEditable(row, col))
                        {
                            Grid solvedGrid = grid.Copy();
                            solver.Solve(solvedGrid);
                            if (!solvedGrid.IsFull())
                            {
                                MessageBox.Show("This square cannot be solved because there are errors in your puzzle. Please erase some of your work and try again.");
                            }
                            else
                            {
                                grid.Set(solvedGrid.Get(row, col), true, row, col);
                            }
                            gcontrol.UpdateGridView();
                            RecalculateErrors();
                        }
                    }));
                }
            });

            // initial setup: start the timer and optionally show errors.
            gameTimerTick(this, new EventArgs());
            RecalculateErrors();
            ShowOrHideHintBar();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a new form for playing the game
        /// </summary>
        /// <param name="grid">The grid to start with</param>
        /// <param name="playingMode">If true: We're "playing" the game. If false: We're "editing" the game.</param>
        public GameForm(Grid grid, bool playingMode)
        {
            InitializeComponent();
            this.grid = grid;
            this.isPlaying = playingMode;

            hintBarText.Text = "";
            if (isPlaying)
            {
                solveButton.Text = "&Solve";
            }
            else
            {
                solveButton.Text = "&Enter Puzzle";
            }

            // make the grid control and put it somewhere
            this.gcontrol = new SudokuGridControl(grid);
            this.gridPanel.Controls.Add(gcontrol);
            gcontrol.Dock = DockStyle.Fill;

            // When the cell is cleared, mark/unmark errors and hints
            gcontrol.CellClear += (int row, int col) =>
            {
                nagAboutErrors = true;
                nagAboutWonGame = true;
                RecalculateErrors();
                RecalculateHints(row, col);
                ShowOrHideHintBar();
            };

            // When the cell's value is changed, mark/unmark errors and hints
            gcontrol.CellChange += (int row, int col) =>
            {
                MaybeTryGameOver();
                RecalculateErrors();
                RecalculateHints(row, col);
                ShowOrHideHintBar();
            };

            // When the user selects a different cell, mark/unmark errors and hints
            gcontrol.CellFocused += (int row, int col) =>
            {
                ShowOrHideHintBar();
                if (isPlaying)
                {
                    RecalculateHints(row, col);
                }
            };

            // Add a drop-down context menu to each textbox
            gcontrol.ForEachTextBox((TextBox tbox, int row, int col) =>
            {
                tbox.ContextMenu = new ContextMenu();
                if (isPlaying)
                {
                    // The context menu should only appear when in "Playing" mode
                    tbox.ContextMenu.MenuItems.Add(new MenuItem("Show &Hints", (s, e) =>
                        {
                            hintBarText.Show();
                            // the hints bar will disappear on next call to ShowOrHideHints()
                        }));
                    tbox.ContextMenu.MenuItems.Add(new MenuItem("&Solve This Square", (s, e) =>
                        {
                            // solve the grid and copy the value
                            if (grid.IsEditable(row, col))
                            {
                                Grid solvedGrid = grid.Copy();
                                solver.Solve(solvedGrid);
                                if (!solvedGrid.IsFull())
                                {
                                    MessageBox.Show("This square cannot be solved because there are errors in your puzzle. Please erase some of your work and try again.");
                                }
                                else
                                {
                                    grid.Set(solvedGrid.Get(row, col), true, row, col);
                                }
                                gcontrol.UpdateGridView();
                                RecalculateErrors();
                            }
                        }));
                }
            });

            // initial setup: start the timer and optionally show errors.
            gameTimerTick(this, new EventArgs());
            RecalculateErrors();
            ShowOrHideHintBar();
        }