-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBody.cs
138 lines (124 loc) · 5.04 KB
/
Body.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using MagicSquare15.Properties;
using System.Drawing;
namespace Fifteen
{
class Body
{
Image[] lstImg;
//Color none = Color.FromArgb(0, Color.White);
public Body(TableLayoutPanel tableLayoutPanel1, Timer timer1)
{
//put Resources' images to a List
#region put Resources' images to a List
lstImg = new Image[16];
lstImg[0]=(Resources._01);
lstImg[1]=(Resources._02);
lstImg[2]=(Resources._03);
lstImg[3]=(Resources._04);
lstImg[4]=(Resources._05);
lstImg[5]=(Resources._06);
lstImg[6]=(Resources._07);
lstImg[7]=(Resources._08);
lstImg[8]=(Resources._09);
lstImg[9]=(Resources._10);
lstImg[10]=(Resources._11);
lstImg[11]=(Resources._12);
lstImg[12]=(Resources._13);
lstImg[13]=(Resources._14);
lstImg[14]=(Resources._15);
lstImg[15]=(Resources._16);
#endregion
Field(tableLayoutPanel1, timer1);
}
private void Field(TableLayoutPanel tableLayoutPanel1, Timer timer1)
{
#region Randomizing
//Randomizing of the array
Random rnd = new Random(System.DateTime.Now.Millisecond);
Image[] rndArray = lstImg;
rndArray = rndArray.OrderBy(x => rnd.Next()).ToArray();
//rndArray[14] = lstImg[15]; rndArray[15] = lstImg[14];
#endregion
#region Filling
//Filling the field with the array of numbers
for (int i = 0; i < 16; i++)
{
tableLayoutPanel1.Controls[i].BackgroundImage = rndArray[i];
}
#endregion
timer1.Start();
}
public void Change(TableLayoutPanel tableLayoutPanel1, int num, Timer timer1, Label labelTime)
{
//Check if it is the space cell near by
#region Check space near
Image ofLeft, ofRight, ofUp, ofDown;
try { ofLeft = tableLayoutPanel1.Controls[num - 1].BackgroundImage; } catch { ofLeft = null; }
try { ofRight = tableLayoutPanel1.Controls[num + 1].BackgroundImage; } catch { ofRight = null; }
try { ofUp = tableLayoutPanel1.Controls[num - 4].BackgroundImage; } catch { ofUp = null; }
try { ofDown = tableLayoutPanel1.Controls[num + 4].BackgroundImage; } catch { ofDown = null; }
#endregion
//Changing the chosen cell to the space cell
#region Changing
if (ofLeft == lstImg[15])
{
tableLayoutPanel1.Controls[num - 1].BackgroundImage = tableLayoutPanel1.Controls[num].BackgroundImage;
tableLayoutPanel1.Controls[num].BackgroundImage = lstImg[15];
}
else if (ofRight == lstImg[15])
{
tableLayoutPanel1.Controls[num + 1].BackgroundImage = tableLayoutPanel1.Controls[num].BackgroundImage;
tableLayoutPanel1.Controls[num].BackgroundImage = lstImg[15];
}
else if (ofUp == lstImg[15])
{
tableLayoutPanel1.Controls[num - 4].BackgroundImage = tableLayoutPanel1.Controls[num].BackgroundImage;
tableLayoutPanel1.Controls[num].BackgroundImage = lstImg[15];
}
else if (ofDown == lstImg[15])
{
tableLayoutPanel1.Controls[num + 4].BackgroundImage = tableLayoutPanel1.Controls[num].BackgroundImage;
tableLayoutPanel1.Controls[num].BackgroundImage = lstImg[15];
}
#endregion
Check(tableLayoutPanel1, timer1, labelTime); //Check to complete
}
public void Check(TableLayoutPanel tableLayoutPanel1, Timer timer1, Label labelTime)
{
//Check if all cells are positioned correctly
#region Checking
int finished = 0;
for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
{
if (tableLayoutPanel1.Controls[i].BackgroundImage == lstImg[i])
finished += 1;
}
#endregion
//Finishing if conditions are completed
#region Finishing
if (finished == 16)
{
int tempTime = Convert.ToInt16(labelTime.Text);
timer1.Stop();
labelTime.Text = tempTime.ToString();
MessageBox.Show("You won! Your time is " + tempTime + " seconds.");
HighScores hs = new HighScores();
hs.Register(tempTime);
FormFifteen.time = 0;
Field(tableLayoutPanel1, timer1);
}
#endregion
}
public void MenuNewGame(TableLayoutPanel tableLayoutPanel1, Timer timer1)
{
Field(tableLayoutPanel1, timer1);
}
}
}