private void MainForm_MouseDown(object sender, MouseEventArgs e) { int cellLength = gridLength / game.NumCells; // Make sure click was inside the grid if (e.X < GridOffset || e.X > cellLength * game.NumCells + GridOffset || e.Y < GridOffset || e.Y > cellLength * game.NumCells + GridOffset) { return; } // Find row, col of mouse press int r = (e.Y - GridOffset) / cellLength; int c = (e.X - GridOffset) / cellLength; game.Move(r, c); // Redraw grid this.Invalidate(); // Check to see if puzzle has been solved if (game.IsWinner()) { // Display winner dialog box just inside window MessageBox.Show(this, "Congratulations! You've won!", "Lights Out!", MessageBoxButtons.OK, MessageBoxIcon.Information); } }