/// <summary> /// When the Solve button is clicked /// </summary> private void SolveClick(object sender, EventArgs e) { if (isPlaying) { if (MessageBox.Show("Are you sure you want the computer to solve the puzzle?", "Solve now?", MessageBoxButtons.YesNo) == DialogResult.Yes) { bool canSolve = solver.Solve(grid); if (!grid.IsFull()) { MessageBox.Show("This puzzle has errors. Erase some of your work and try again."); } else { nagAboutWonGame = false; } gcontrol.UpdateGridView(); RecalculateErrors(); } } else { GameManager.PlayThisPuzzle(grid, this); } }
/// <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(); }
/// <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(); }