-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
143 lines (117 loc) · 4.3 KB
/
Form1.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
140
141
142
143
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System;
namespace NonogramSolver
{
public partial class Form1 : Form
{
Problem problem;
Board board;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
problem = new Problem("President");
board = new Board(problem.ColumnHint.Length, problem.RowHint.Length);
board.Location = new Point(110, 110);
this.Controls.Add(board);
for (int x = 0; x < problem.ColumnHint.Length; x++)
{
Label l = new Label();
l.Text = string.Join("\r\n", problem.ColumnHint[x]);
l.Size = new Size(Board.GridSize, 120);
l.Font = new Font("Times New Roman", 8);
l.TextAlign = ContentAlignment.BottomCenter;
l.Location = new Point(board.Location.X + x * Board.GridSize, board.Location.Y - 120);
this.Controls.Add(l);
}
for (int y = 0; y < problem.RowHint.Length; y++)
{
Label l = new Label();
l.Text = string.Join(" ", problem.RowHint[y]);
l.Size = new Size(120, Board.GridSize);
l.Font = new Font("Times New Roman", 8);
l.TextAlign = ContentAlignment.MiddleRight;
l.Location = new Point(board.Location.X - 120, board.Location.Y + y * Board.GridSize);
this.Controls.Add(l);
}
}
private void btnSolve_Click(object sender, EventArgs e)
{
btnSolve.Enabled = false;
this.bgWorker.RunWorkerAsync();
}
private void bgWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
this.Solve((BackgroundWorker)sender, e);
}
private void Solve(BackgroundWorker worker, DoWorkEventArgs e)
{
problem.Solve();
worker.ReportProgress(0);
}
private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
for (int x = 0; x < problem.ColumnHint.Length; x++)
for (int y = 0; y < problem.RowHint.Length; y++)
{
switch (problem.Blocks[x, y])
{
case null:
break;
case true:
board[x, y].Text = "";
board[x, y].BackColor = Color.Black;
break;
case false:
board[x, y].Text = "X";
board[x, y].BackColor = Color.White;
break;
}
}
this.Refresh();
}
private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (problem.IsSolved)
for (int x = 0; x < problem.ColumnHint.Length; x++)
for (int y = 0; y < problem.RowHint.Length; y++)
if (problem.Blocks[x, y] == false) board[x, y].Text = "";
btnSolve.Enabled = true;
}
}
public class Board : Panel
{
public const int GridSize = 20;
private Button[,] Grids { get; set; }
public Board(int width, int height)
{
this.Size = new Size(width * GridSize, height * GridSize);
this.Grids = new Button[width, height];
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
{
this[x, y] = new Button();
this[x, y].Enabled = false;
this[x, y].BackColor = Color.White;
this[x, y].Size = new Size(GridSize, GridSize);
this[x, y].Location = new Point(x * GridSize, y * GridSize);
this.Controls.Add(this[x, y]);
}
}
public Button this[int x, int y]
{
get
{
return this.Grids[x, y];
}
set
{
this.Grids[x, y] = value;
}
}
}
}