-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUserControl_Puzzle_Pictures.cs
158 lines (145 loc) · 5.06 KB
/
UserControl_Puzzle_Pictures.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace N_Puzzle_Game
{
public partial class UserControl_Puzzle_Pictures : UserControl_Need
{
Bitmap btmp;
List<Bitmap> list = new List<Bitmap>();
Dictionary<Image, int> map = new Dictionary<Image, int>();
public bfs obj_bfs;
public UserControl_Puzzle_Pictures(string path, int s, int n, int l)
{
InitializeComponent();
set(s, n, l);
if (N == 3)
while (!is_solvable(state = suffle(get_state(N * N)))) ;
else if (N == 4)
{
load();
state = random_state();
}
resize_image(path);
this.Controls.Clear();
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{
if (state[i * N + j] == '0')
{
x = j * btn_length;
y = i * btn_length;
continue;
}
Button btn = create_button(j * btn_length, i * btn_length, btn_length);
btn.Image = list[state[i * N + j] - 49];
btn.Click += new System.EventHandler(click);
this.Controls.Add(btn);
}
}
private void set(int s, int n, int l)
{
N = n;
btn_length = l;
w_length = s;
btmp = new Bitmap(w_length, w_length);
this.Size = new Size(w_length, w_length);
}
Button create_button(int x, int y, int w)
{
Button btn = new Button();
Point p = new Point(x, y);
btn.Location = p;
btn.Size = new Size(w, w);
return btn;
}
private void click(object sender, EventArgs e)
{
Button btn = (Button)sender;
int btn_x = btn.Location.X, btn_y = btn.Location.Y;
if ((x == btn_x && (y + btn_length == btn_y || y - btn_length == btn_y)) ||
(y == btn_y && (x + btn_length == btn_x || x - btn_length == btn_x)))
{
char[] lst = state.ToCharArray();
lst[y / btn_length * N + x / btn_length] = (char)(map[btn.Image] + 49);
lst[btn_y / btn_length * N + btn_x / btn_length] = (char)48;
state = new string(lst);
Point p = new Point(x, y);
x = btn_x; y = btn_y;
btn.Location = p;
if (x == (N - 1) * btn_length && y == (N - 1) * btn_length) is_goal();
}
}
public bool is_goal()
{
foreach (Button btn in Controls)
{
if ((btn.Location.X / btn_length) + (btn.Location.Y / btn_length) * N
!= map[btn.Image]) return false;
}
MessageBox.Show("Good Play");
return true;
}
private void resize_image(string path)
{
Graphics graphic = Graphics.FromImage(btmp);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.DrawImage(Image.FromFile(path), 0, 0, w_length, w_length);
graphic.Dispose();
int rw = 0, cl = 0;
for (int x = 0; x < N * N - 1; x++)
{
Bitmap mp = new Bitmap(btn_length, btn_length);
for (int i = 0; i < btn_length; i++)
for (int j = 0; j < btn_length; j++)
{
mp.SetPixel(j, i,
btmp.GetPixel(cl * btn_length + j, rw * btn_length + i));
}
list.Add(mp);
map.Add(mp, list.Count - 1);
cl += 1;
if (cl == N)
{
rw += 1;
cl = 0;
}
}
}
private void UserControl_Eigth_Puzzle_Pictures_Load(object sender, EventArgs e)
{
t = new Timer();
t.Interval = 200;
t.Tick += new EventHandler(start_solution);
}
private void Draw(string state)
{
foreach (Button btn in Controls)
{
int idx = btn.Location.X / btn_length + btn.Location.Y / btn_length * N;
if (state[idx] == (char)48)
{
int tmp_x = btn.Location.X, tmp_y = btn.Location.Y;
btn.Location = new Point(x, y);
x = tmp_x; y = tmp_y;
}
}
}
void start_solution(object sender, EventArgs e)
{
if (obj_bfs != null && obj_bfs.solution.Count > 0)
{
state = obj_bfs.solution.Pop();
Draw(state);
}
else t.Stop();
}
}
}