-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEight_Puzzle.cs
150 lines (137 loc) · 4.4 KB
/
Eight_Puzzle.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
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 N_Puzzle_Game
{
public partial class Eight_Puzzle : Form
{
string path = "";
Timer t;
UserControl_Puzzle_Numbers usdg;
UserControl_Puzzle_Pictures uspc;
public bool b_bfs = false;
public Eight_Puzzle()
{
InitializeComponent();
lbl_time.Text = "";
lbl_time.ForeColor = Color.OrangeRed;
}
private void button1_Click(object sender, EventArgs e)
{
lbl_time.Text = "";
if (radioButton1.Checked)
{
panel1.Controls.Clear();
usdg = new UserControl_Puzzle_Numbers(270, 3, 90);
panel1.Controls.Add(usdg);
}
else if (radioButton2.Checked && path != "")
{
panel1.Controls.Clear();
uspc = new UserControl_Puzzle_Pictures(path, 270, 3, 90);
panel1.Controls.Add(uspc);
}
}
private void Eight_Puzzle_Load(object sender, EventArgs e)
{
this.Width = 310;
t = new Timer();
t.Interval = 5;
t.Tick += new EventHandler(t_click);
radioButton1.Checked = true;
radioButton2.Checked = false;
}
private void t_click(object sender, EventArgs e)
{
if (radioButton2.Checked)
{
int x = this.Width + 1;
if (x <= 624)
this.Width = x;
else t.Stop();
}
else if (radioButton1.Checked)
{
int x = this.Width - 1;
if (x >= 310)
this.Width = x;
else t.Stop();
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
t.Start();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
t.Start();
}
private void button3_Click(object sender, EventArgs e)
{
int[,] state;
string s = "";
int start = DateTime.Now.Minute * 60 * 1000 +
DateTime.Now.Second * 1000 + DateTime.Now.Millisecond;
if (usdg != null && radioButton1.Checked)
{
state = get_state(usdg.state);
if (b_bfs && !usdg.is_goal())
{
usdg.obj_bfs = new bfs(usdg.state);
usdg.start();
s = "BFS";
}
}
else if (uspc != null && uspc.state.Length == 9 && radioButton2.Checked)
{
state = get_state(uspc.state);
if (b_bfs && !uspc.is_goal())
{
uspc.obj_bfs = new bfs(uspc.state);
uspc.start();
s = "BFS";
}
}
int end = DateTime.Now.Minute * 60 * 1000 +
DateTime.Now.Second * 1000 + DateTime.Now.Millisecond;
lbl_time.Text = "time required is : " + (end - start).ToString()
+ " ms, " + s;
}
private int[,] get_state(string state)
{
int N = (int)Math.Sqrt(state.Length);
int[,] rs = new int[N, N];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
int idx = i * 3 + j;
rs[i, j] = state[idx] - 48;
}
return rs;
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog choose = new OpenFileDialog();
choose.Filter =
"Image Files (JPEG, GIF, PNG , JPG) |*.jpeg; *.gif; *.png; *.jpg";
if (choose.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(choose.FileName);
path = choose.FileName;
}
}
private void Eight_Puzzle_FormClosed(object sender, FormClosedEventArgs e)
{
if (Start_Window.__main__ != null)
{
Start_Window.__main__.Show();
}
}
}
}