This repository was archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSide.cs
More file actions
285 lines (256 loc) · 6.96 KB
/
Side.cs
File metadata and controls
285 lines (256 loc) · 6.96 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
namespace RubiksCubeSolver
{
[DebuggerDisplay("{side}")]
public class Side
{
private const int sideLength = 3;
/// <summary>
/// row is the first index, column the second
/// </summary>
private Brush[,] fields;
public Brush[,] Fields
{
get { return fields; }
set { fields = value; }
}
private Sides side;
public Sides CubeSide
{
get { return side; }
}
private Side left;
public Side Left
{
get { return left; }
set { left = value; }
}
private Side right;
public Side Right
{
get { return right; }
set { right = value; }
}
private Side top;
public Side Top
{
get { return top; }
set { top = value; }
}
private Side bottom;
public Side Bottom
{
get { return bottom; }
set { bottom = value; }
}
private Side opposite;
public Side Opposite
{
get { return opposite; }
set { opposite = value; }
}
private Brush color;
public Brush Color
{
get { return color; }
set { color = value; }
}
public Side(Brush initializationColor, Sides side)
{
color = initializationColor;
fields = new Brush[sideLength, sideLength];
for (int i = 0; i < sideLength; i++)
{
for (int j = 0; j < sideLength; j++)
{
fields[i, j] = initializationColor;
}
}
this.side = side;
}
public Brush[] GetRow(int row)
{
Debug.Assert(row >= 0 && row < sideLength);
var rowColors = new Brush[sideLength];
for (int i = 0; i < sideLength; i++)
{
rowColors[i] = fields[row, i];
}
return rowColors;
}
public Brush[] GetColumn(int column)
{
Debug.Assert(column >= 0 && column < sideLength);
var columnColors = new Brush[sideLength];
for (int i = 0; i < sideLength; i++)
{
columnColors[i] = fields[i, column];
}
return columnColors;
}
public void SetRow(int row, Brush[] rowColors)
{
Debug.Assert(rowColors.Length == sideLength);
Debug.Assert(row >= 0 && row < sideLength);
for (int i = 0; i < sideLength; i++)
{
fields[row, i] = rowColors[i];
}
}
public void SetColumn(int column, Brush[] columnColors)
{
Debug.Assert(columnColors.Length == sideLength);
Debug.Assert(column >= 0 && column < sideLength);
for (int i = 0; i < sideLength; i++)
{
fields[i, column] = columnColors[i];
}
}
public void RotateCW()
{
var newFields = new Brush[sideLength, sideLength];
newFields[0, 0] = fields[2, 0];
newFields[0, 1] = fields[1, 0];
newFields[0, 2] = fields[0, 0];
newFields[1, 0] = fields[2, 1];
newFields[1, 1] = fields[1, 1];
newFields[1, 2] = fields[0, 1];
newFields[2, 0] = fields[2, 2];
newFields[2, 1] = fields[1, 2];
newFields[2, 2] = fields[0, 2];
fields = newFields;
}
public void RotateCCW()
{
var newFields = new Brush[sideLength, sideLength];
newFields[0, 0] = fields[0, 2];
newFields[0, 1] = fields[1, 2];
newFields[0, 2] = fields[2, 2];
newFields[1, 0] = fields[0, 1];
newFields[1, 1] = fields[1, 1];
newFields[1, 2] = fields[2, 1];
newFields[2, 0] = fields[0, 0];
newFields[2, 1] = fields[1, 0];
newFields[2, 2] = fields[2, 0];
fields = newFields;
}
public RelativeEdgePosition GetRelativeEdgePosition(Brush primaryColor, Brush secondaryColor)
{
if (fields[1, 0] == primaryColor && GetSecondaryEdgeColor(RelativeEdgePosition.Left) == secondaryColor)
{
return RelativeEdgePosition.Left;
}
if (fields[0, 1] == primaryColor && GetSecondaryEdgeColor(RelativeEdgePosition.Top) == secondaryColor)
{
return RelativeEdgePosition.Top;
}
if (fields[1, 2] == primaryColor && GetSecondaryEdgeColor(RelativeEdgePosition.Right) == secondaryColor)
{
return RelativeEdgePosition.Right;
}
if (fields[2, 1] == primaryColor && GetSecondaryEdgeColor(RelativeEdgePosition.Bottom) == secondaryColor)
{
return RelativeEdgePosition.Bottom;
}
return RelativeEdgePosition.NotExisting;
}
public Brush GetSecondaryEdgeColor(RelativeEdgePosition edgePosition)
{
//these 2 (top, bottom) sides are special cases, because they change the column/row alignment with other sides
if (CubeSide == Sides.Top)
{
switch (edgePosition)
{
case RelativeEdgePosition.Left:
return left.GetRow(0)[1];
case RelativeEdgePosition.Right:
return right.GetRow(0)[1];
case RelativeEdgePosition.Top:
return top.GetRow(0)[1];
case RelativeEdgePosition.Bottom:
return bottom.GetRow(0)[1];
}
}
if (CubeSide == Sides.Bottom)
{
switch (edgePosition)
{
case RelativeEdgePosition.Left:
return left.GetRow(2)[1];
case RelativeEdgePosition.Right:
return right.GetRow(2)[1];
case RelativeEdgePosition.Top:
return top.GetRow(2)[1];
case RelativeEdgePosition.Bottom:
return bottom.GetRow(2)[1];
}
}
else
{
if (edgePosition == RelativeEdgePosition.Left)
{
return left.GetColumn(2)[1];
}
if (edgePosition == RelativeEdgePosition.Right)
{
return right.GetColumn(0)[1];
}
if (edgePosition == RelativeEdgePosition.Top)
{
//only has to handle the remaining 4 cases (top and bottom are handled before)
switch (CubeSide)
{
case Sides.Front:
return top.GetRow(2)[1];
case Sides.Left:
return top.GetColumn(0)[1];
case Sides.Back:
return top.GetRow(0)[1];
case Sides.Right:
return top.GetColumn(2)[1];
}
}
if (edgePosition == RelativeEdgePosition.Bottom)
{
//only has to handle the remaining 4 cases (top and bottom are handled before)
switch (CubeSide)
{
case Sides.Front:
return bottom.GetRow(0)[1];
case Sides.Left:
return bottom.GetColumn(0)[1];
case Sides.Back:
return bottom.GetRow(2)[1];
case Sides.Right:
return bottom.GetColumn(2)[1];
}
}
}
return null; //default case to satisfy the compiler, should never occur
}
public Side GetRelativeSide(RelativeSidePosition relativeSide)
{
if (relativeSide == RelativeSidePosition.Left) return left;
if (relativeSide == RelativeSidePosition.Right) return right;
if (relativeSide == RelativeSidePosition.Top) return top;
if (relativeSide == RelativeSidePosition.Bottom) return bottom;
if (relativeSide == RelativeSidePosition.Opposite) return opposite;
if (relativeSide == RelativeSidePosition.Self) return this;
return null; //default case to satisfy the compiler, should never occur;
}
public Brush GetCornerField(RelativeCornerPosition corner)
{
if (corner == RelativeCornerPosition.BottomLeft) return fields[2, 0];
if (corner == RelativeCornerPosition.BottomRight) return fields[2, 2];
if (corner == RelativeCornerPosition.TopLeft) return fields[0, 0];
if (corner == RelativeCornerPosition.TopRight) return fields[0, 2];
return Brushes.Transparent; //default to satisfy compiler
}
}
}