Skip to content

Instantly share code, notes, and snippets.

@Ardagan
Created January 7, 2022 04:19
Show Gist options
  • Save Ardagan/07d53a251d081e6a0ed3dc3403bf51be to your computer and use it in GitHub Desktop.
Save Ardagan/07d53a251d081e6a0ed3dc3403bf51be to your computer and use it in GitHub Desktop.
Torches puzzle
void Main()
{
int size = 6;
CheckBox[,] buttons = new CheckBox[size,size];
Form myform = new Form();
var layout = new TableLayoutPanel();
myform.Controls.Add(layout);
layout.AutoSize = true;
layout.ColumnCount = size;
layout.RowCount = size;
for(int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
layout.Controls.Add(CreateButton(i, j, buttons, size));
}
}
//Random r = new Random();
//for(int n = 0; n < 50; ++n) {
// buttons[r.Next()%size, r.Next()%size].
//}
myform.ShowDialog();
while (myform.Created)
{
Thread.Sleep(100);
}
}
CheckBox CreateButton(int i, int j, CheckBox[,] buttons, int size)
{
CheckBox newBox = new CheckBox(){
Checked = true
};
newBox.Click += (o, s) =>
{
if (i-1 >= 0) {
buttons[i-1, j].Checked = !buttons[i-1, j].Checked;
}
if (j - 1 >= 0)
{
buttons[i, j - 1].Checked = !buttons[i, j - 1].Checked;
}
if (i + 1 <size)
{
buttons[i+1, j].Checked = !buttons[i+1, j].Checked;
}
if (j + 1 < size)
{
buttons[i, j+1].Checked = !buttons[i, j+1].Checked;
}
};
buttons[i,j] = newBox;
return newBox;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment