-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnightWay.cs
295 lines (260 loc) · 10 KB
/
KnightWay.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// L1ttl3S1st3r
// Program finds the shortest knight way from one pole to another
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lab5extra
{
// cordinates for Knight Explorer
// x - [A, H], A is 1, y - [1, 8]
class ChessCordinates
{
private int _x;
private int _y;
// converts (x, y) cords to A-H 1-8 cords
override public string ToString()
{
string resultString;
switch(this._x)
{
case 1:
resultString = 'A' + this._y.ToString();
break;
case 2:
resultString = 'B' + this._y.ToString();
break;
case 3:
resultString = 'C' + this._y.ToString();
break;
case 4:
resultString = 'D' + this._y.ToString();
break;
case 5:
resultString = 'E' + this._y.ToString();
break;
case 6:
resultString = 'F' + this._y.ToString();
break;
case 7:
resultString = 'G' + this._y.ToString();
break;
case 8:
resultString = 'H' + this._y.ToString();
break;
// handle if x is invalid
default:
throw new Exception('(' + this._x.ToString() + ',' +
this._y.ToString() + ") is invalid cordinates");
}
return resultString;
}
// new x must be > 0 and < 9
public void SetX(int newX)
{
if (newX > 0 && newX < 9)
this._x = newX;
}
// new y must be > 0 and < 9
public void SetY(int newY)
{
if (newY > 0 && newY < 9)
this._y = newY;
}
// set cordinates by string (example "A4")
public void SetCordinatesByString(string newCordinates)
{
// check new cordinates
newCordinates = newCordinates.ToUpper();
if (newCordinates.Length < 2)
throw new Exception(newCordinates + "are invalid cordinates");
// convert x
switch (newCordinates[0])
{
case 'A':
this._x = 1;
break;
case 'B':
this._x = 2;
break;
case 'C':
this._x = 3;
break;
case 'D':
this._x = 4;
break;
case 'E':
this._x = 5;
break;
case 'F':
this._x = 6;
break;
case 'G':
this._x = 7;
break;
case 'H':
this._x = 8;
break;
// handle if cords are invalid
default:
throw new Exception(newCordinates + "are invalid cordinates");
}
// convert y
int newY = int.Parse(newCordinates[1].ToString());
if (newY > 8 || newY < 1)
throw new Exception(newCordinates + "are invalid cordinates");
else
_y = newY;
}
public int GetX()
{
return this._x;
}
public int GetY()
{
return this._y;
}
// check if cords is valid
public bool IsValid()
{
if (this._x < 9 && this._x > 0 && this._y < 9 && this._y > 0)
return true;
else
return false;
}
public static ChessCordinates operator +(ChessCordinates firstCordinates,
ChessCordinates secondCordinates)
{
return new ChessCordinates(firstCordinates.GetX() + secondCordinates.GetX(),
firstCordinates.GetY() + secondCordinates.GetY());
}
public static bool operator ==(ChessCordinates firstCordinates,
ChessCordinates secondCordinates)
{
if (firstCordinates.GetX() == secondCordinates.GetX() &&
firstCordinates.GetY() == secondCordinates.GetY())
return true;
else
return false;
}
public static bool operator !=(ChessCordinates firstCordinates,
ChessCordinates secondCordinates)
{
if (firstCordinates.GetX() != secondCordinates.GetX() ||
firstCordinates.GetY() != secondCordinates.GetY())
return true;
else
return false;
}
public ChessCordinates()
{
this._x = 1;
this._y = 1;
}
// contsructor don't check x and y!
public ChessCordinates(int x, int y)
{
this._x = x;
this._y = y;
}
public ChessCordinates(string cordinates)
{
this.SetCordinatesByString(cordinates);
}
}
class Program
{
static void Main(string[] args)
{
// set start and end cordinates
System.Console.WriteLine("Input start cordinates:");
string startCordinatesString = System.Console.ReadLine();
System.Console.WriteLine("Input end cordinates:");
string endCordinatesString = System.Console.ReadLine();
// set KnightExplorer, which will find the way
KnightExplorer explorer = new KnightExplorer(new ChessCordinates(startCordinatesString),
new ChessCordinates(endCordinatesString));
// find the way
ArrayList way = explorer.FindWay();
// print it
way.Reverse();
System.Console.WriteLine("Result way:");
foreach(ChessCordinates step in way)
{
System.Console.Write(step.ToString() + ", ");
}
System.Console.ReadKey();
}
}
// finds the way of Knight from one pole to another
class KnightExplorer
{
// variables:
private bool[,] _chessMatrix;
private Queue _ways;
private ChessCordinates _endCordinates;
private ChessCordinates[] _shifts; // posible shifts to calculate the pole
public KnightExplorer(ChessCordinates startCordinates, ChessCordinates endCordinates)
{
this._chessMatrix = new bool[8, 8];
this._ways = new Queue();
this._endCordinates = endCordinates;
this._shifts = new ChessCordinates[8] { new ChessCordinates(-2, 1), new ChessCordinates(-1, 2),
new ChessCordinates(1, 2), new ChessCordinates(2, 1),
new ChessCordinates(2, -1), new ChessCordinates(1, -2),
new ChessCordinates(-1, -2), new ChessCordinates(-2, -1)};
// add start cordinates to ways queue
ArrayList tempList = new ArrayList();
tempList.Add(startCordinates);
this._ways.Enqueue(tempList);
}
// set pole as visited
private void _SetPoint(ChessCordinates poleCordinates)
{
if (poleCordinates.GetX() < 9 && poleCordinates.GetX() > 0
&& poleCordinates.GetY() < 9 && poleCordinates.GetY() > 0)
this._chessMatrix[poleCordinates.GetX() - 1, poleCordinates.GetY() - 1] = true;
}
// helps to check if pole haven't been visited
// return true if pole have been visited
// return false if not
private bool _CheckPoint(ChessCordinates poleCordinates)
{
if (poleCordinates.IsValid())
return this._chessMatrix[poleCordinates.GetX() - 1, poleCordinates.GetY() - 1];
else
return false;
}
// finds way from start cordinates to end cordinates
// (breadth-first seacrh)
// return array with way from end cords to start cords
public ArrayList FindWay()
{
while (true) {
// eject head way and set point on chess board
ArrayList headWay = (ArrayList)this._ways.Dequeue();
_SetPoint((ChessCordinates)headWay[0]);
// check all reachable poles, add them to ways queue if they are not equal end cords
foreach (ChessCordinates shift in _shifts)
{
// calculate new pole
ChessCordinates newPole = shift + (ChessCordinates)headWay[0];
// if new is possible and pole haven't been visited, add way with it to ways queue
if(newPole.IsValid() && (_CheckPoint(newPole) == false))
{
// set new way
ArrayList newWay = new ArrayList() { newPole };
newWay.AddRange(headWay);
// if exploring reachs end cords, return result
if ((ChessCordinates)newWay[0] == _endCordinates)
return newWay;
// else add new way to ways queues
else
_ways.Enqueue(newWay);
}
}
}
}
}
}