-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
139 lines (119 loc) · 4.58 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LightsOut
{
public partial class MainForm : Form
{
private const int GridOffset = 25; //Distance from upper-left side of window
private const int GridLength = 200; //Size in pixels of grid
private const int NumCells = 3; //Number of cells in grid
private const int CellLength = GridLength / NumCells;
private bool[,] grid; //Store on/off state of cells in grid
private Random rand; //used to generate random numbers
public MainForm()
{
InitializeComponent();
rand = new Random(); //Initializes random number generator
grid = new bool[NumCells, NumCells];
//Turn entire grid on
for (int r = 0; r < NumCells; r++)
for (int c = 0; c < NumCells; c++)
grid[r, c] = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void MainForm_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
for (int r=0;r< NumCells; r++)
{
for (int c=0;c< NumCells; c++)
{
//Get proper pen and brush for on/off
//grid section
Brush brush;
Pen pen;
if (grid[r, c])
{
pen = Pens.Black;
brush = Brushes.White; //On
}
else
{
pen = Pens.White;
brush = Brushes.Black; //off
}
//Determine(x,y) coord of row and col to draw a rectangel
int x = c * CellLength + GridOffset;
int y = r * CellLength + GridOffset;
//Draw outline and inner rectangle
g.DrawRectangle(pen, x, y, CellLength, CellLength);
g.FillRectangle(brush, x + 1, y + 1, CellLength - 1, CellLength - 1);
}
}
}
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
//Make sure click was inside the grid
if(e.X < GridOffset || e.X > CellLength * NumCells + GridOffset ||
e.Y<GridOffset || e.Y > CellLength * NumCells + GridOffset)
return;
//Find row, Col of Mouse Press
int r = (e.Y - GridOffset) / CellLength;
int c = (e.X - GridOffset) / CellLength;
//Invert selected box and all surrounding boxes
for (int i = r - 1; i <= r + 1; i++)
for (int j = c - 1; j <= c + 1; j++)
if (i >= 0 && i < NumCells && j >= 0 && j < NumCells)
grid[i, j] = !grid[i, j];
//Redraw grid
this.Invalidate();
//Check to see if puzzel has been solved
if(PlayerWon())
{
//Display winner dialog box
MessageBox.Show(this, "Congratulations! You've won!", "Lights Out!",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private bool PlayerWon()
{
//if one cell is on, then this function returns false
for (int r = 0; r < NumCells; r++)
for (int c = 0; c < NumCells; c++)
if (grid[r, c] == true)
return false;
return true;
}
private void newGameButton_Click(object sender, EventArgs e)
{
//Fill grid with either white or black
for (int r = 0; r < NumCells; r++)
for (int c = 0; c < NumCells; c++)
grid[r, c] = rand.Next(2) == 1;
//Redraw the grid
this.Invalidate();
}
private void newToolStripMenuItem1_Click(object sender, EventArgs e)
{
newGameButton_Click(sender, e);
}
private void exitButton_Click(object sender, EventArgs e)
{
Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutForm aboutBox = new AboutForm();
aboutBox.ShowDialog(this);
}
}
}