-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle.cs
557 lines (513 loc) · 22.5 KB
/
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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
using Raylib_cs;
using static Raylib_cs.Raylib;
using static rbx.Keybinds;
using static rbx.RbxWindow;
using static rbx.GfxConstants;
using rbx.Colors;
using System;
using System.Numerics;
using System.Collections.Generic;
namespace rbx.Puzzle {
public enum Mvmt {
X, Xp, Y, Yp, Z, Zp,
F, Fp, R, Rp, U, Up, D, Dp, B, Bp, L, Lp,
M, Mp, E, Ep, S, Sp
}
public enum Side {
X, Y, Z,
}
public class MoveObject {
public MoveObject(Mvmt mvmt){
this.mvmt = mvmt;
}
public static MoveObject operator!(MoveObject lhs) {
return (Mvmt)(((int)lhs.mvmt % 2 != 0) ? lhs.mvmt - 1 : lhs.mvmt + 1);
}
public static bool IsOpposite(MoveObject lhs, MoveObject rhs) {
return (lhs == !rhs);
}
public static implicit operator Mvmt(MoveObject m) => m.mvmt;
public static implicit operator MoveObject(Mvmt m) => new MoveObject(m);
private Mvmt mvmt;
}
public class RubixCube {
public RubixCube(uint size = 3) {
Size = size;
for(uint i = 0; i < 6; i++)
Faces.Add(new Face(i, size, this));
ConnectFaces();
}
public void Shuffle(uint moves = 1000) {
for(uint i = 0; i < moves; i++) {
MoveObject move = new MoveObject(RbxWindow.rng.GenMvmt());
if(Moves.Count > 0 && MoveObject.IsOpposite(Moves[Moves.Count-1], move)) {
i--;
continue;
}
Move(move);
}
}
private void ConnectFaces() {
Faces[0].Connect(new Face[]
{Faces[1], Faces[3], Faces[2], Faces[4], Faces[5]});
Faces[1].Connect(new Face[]
{Faces[5], Faces[0], Faces[2], Faces[4], Faces[3]});
Faces[2].Connect(new Face[]
{Faces[1], Faces[3], Faces[5], Faces[0], Faces[4]});
Faces[3].Connect(new Face[]
{Faces[0], Faces[5], Faces[2], Faces[4], Faces[1]});
Faces[4].Connect(new Face[]
{Faces[1], Faces[3], Faces[0], Faces[5], Faces[2]});
Faces[5].Connect(new Face[]
{Faces[1], Faces[3], Faces[4], Faces[2], Faces[0]});
this.X = Faces[0];
this.Y = Faces[2];
this.Z = Faces[1];
}
public void Draw3D() {
foreach(var Face in Faces)
Face.Draw3D();
}
public void DrawMiniMap() {
// TODO figure out anti aliasing
float size = RbxWindow.MiniMapUnitSize(Size);
float BezlSize = (float)(size * GfxConstants.BEZEL_RATIO_MM);
float TileSize = BezlSize + size;
float FaceSize = (float)((size * Size) + (BezlSize * (Size + 1)));
float TLxcoord = (float)RbxWindow.MiniMapCenter().X - (FaceSize);
float TLycoord = (float)RbxWindow.MiniMapCenter().Y - (FaceSize/2);
float TMxcoord = TLxcoord;
float TMycoord = TLycoord;
Rectangle MiniMapBkdrop = RbxWindow.MiniMapRect();
Rectangle bkdrop = new Rectangle(TMxcoord, TMycoord, FaceSize, FaceSize);
Raylib.DrawRectangleRec(MiniMapBkdrop, SystemPalette.MiniMapBg());
Raylib.DrawRectangleRec(bkdrop, SystemPalette.cubeBg);
//Raylib.DrawText($"X {this.X.Id}", (int)TMxcoord, (int)TMycoord, 10, SystemPalette.fg);
for(int row = 0; row < Size; row++) {
for(int column = 0; column < Size; column++) {
Rectangle tileRect = new Rectangle(
TMxcoord+BezlSize+(column * TileSize),
TMycoord+BezlSize+(row * TileSize),
size,size);
Raylib.DrawRectangleRec(tileRect, SystemPalette.SideColor[this.X.Tiles[row,column]]);
}
}
TMxcoord = TLxcoord - FaceSize;
TMycoord = TLycoord;
bkdrop = new Rectangle(TMxcoord, TMycoord, FaceSize, FaceSize);
Raylib.DrawRectangleRec(bkdrop, SystemPalette.cubeBg);
//Raylib.DrawText($"Y {this.Y.Id}", (int)TMxcoord, (int)TMycoord, 10, SystemPalette.fg);
for(int row = 0; row < Size; row++) {
for(int column = 0; column < Size; column++) {
Rectangle tileRect = new Rectangle(
TMxcoord+BezlSize+(column * TileSize),
TMycoord+BezlSize+(row * TileSize),
size,size);
Raylib.DrawRectangleRec(tileRect, SystemPalette.SideColor[this.Y.Tiles[row,column]]);
}
}
TMxcoord = TLxcoord;
TMycoord = TLycoord - FaceSize;
bkdrop = new Rectangle(TMxcoord, TMycoord, FaceSize, FaceSize);
Raylib.DrawRectangleRec(bkdrop, SystemPalette.cubeBg);
//Raylib.DrawText($"Z {this.Z.Id}", (int)TMxcoord, (int)TMycoord, 10, SystemPalette.fg);
for(int row = 0; row < Size; row++) {
for(int column = 0; column < Size; column++) {
Rectangle tileRect = new Rectangle(
TMxcoord+BezlSize+(column * TileSize),
TMycoord+BezlSize+(row * TileSize),
size,size);
Raylib.DrawRectangleRec(tileRect, SystemPalette.SideColor[this.Z.Tiles[row,column]]);
}
}
TMxcoord = TLxcoord;
TMycoord = TLycoord + FaceSize;
bkdrop = new Rectangle(TMxcoord, TMycoord, FaceSize, FaceSize);
Raylib.DrawRectangleRec(bkdrop, SystemPalette.cubeBg);
//Raylib.DrawText($"ZOPP {this.ZOpp.Id}", (int)TMxcoord, (int)TMycoord, 10, SystemPalette.fg);
for(int row = 0; row < Size; row++) {
for(int column = 0; column < Size; column++) {
Rectangle tileRect = new Rectangle(
TMxcoord+BezlSize+(column * TileSize),
TMycoord+BezlSize+(row * TileSize),
size,size);
Raylib.DrawRectangleRec(tileRect, SystemPalette.SideColor[this.ZOpp.Tiles[row,column]]);
}
}
TMxcoord = TLxcoord + FaceSize;
TMycoord = TLycoord;
bkdrop = new Rectangle(TMxcoord, TMycoord, FaceSize, FaceSize);
Raylib.DrawRectangleRec(bkdrop, SystemPalette.cubeBg);
//Raylib.DrawText($"YOPP {this.YOpp.Id}", (int)TMxcoord, (int)TMycoord, 10, SystemPalette.fg);
for(int row = 0; row < Size; row++) {
for(int column = 0; column < Size; column++) {
Rectangle tileRect = new Rectangle(
TMxcoord+BezlSize+(column * TileSize),
TMycoord+BezlSize+(row * TileSize),
size,size);
Raylib.DrawRectangleRec(tileRect, SystemPalette.SideColor[this.YOpp.Tiles[row,column]]);
}
}
TMxcoord = TLxcoord + (2*FaceSize);
TMycoord = TLycoord;
//Raylib.DrawText($"XOPP {this.XOpp.Id}", (int)TMxcoord, (int)TMycoord, 10, SystemPalette.fg);
bkdrop = new Rectangle(TMxcoord, TMycoord, FaceSize, FaceSize);
Raylib.DrawRectangleRec(bkdrop, SystemPalette.cubeBg);
for(int row = 0; row < Size; row++) {
for(int column = 0; column < Size; column++) {
Rectangle tileRect = new Rectangle(
TMxcoord+BezlSize+(column * TileSize),
TMycoord+BezlSize+(row * TileSize),
size,size);
Raylib.DrawRectangleRec(tileRect, SystemPalette.SideColor[this.XOpp.Tiles[row,column]]);
}
}
Raylib.DrawText($"Viewing face {this.X.Id}", 20, 20, 50, SystemPalette.fg);
if(Solved())
Raylib.DrawText("SOLVED", 20, 80, 50, Color.GREEN);
else
Raylib.DrawText("NOT SOLVED", 20, 80, 50, Color.RED);
}
public bool Solved() {
bool solved = true;
foreach(Face f in Faces)
solved &= f.Solved();
return solved;
}
public void Move(Mvmt? mvmt) {
if(mvmt == null) return;
Mvmt m = (Mvmt)mvmt;
Moves.Add(m);
InternalMove(m);
}
public void Undo() {
if(Moves.Count > 0) {
Mvmt m = Moves[Moves.Count-1];
InternalMove(!new MoveObject(m));
Moves.RemoveAt(Moves.Count-1);
}
}
private void InternalMove(Mvmt mvmt, bool prime = false) {
switch(mvmt) {
case Mvmt.X:
{
Face tmp = this.Y;
this.Y = this.ZOpp;
this.Z = tmp;
} break;
case Mvmt.Xp:
{
Face tmp = this.Z;
this.Z = this.YOpp;
this.Y = tmp;
} break;
case Mvmt.Y:
{
Face tmp = this.Z;
this.Z = this.XOpp;
this.X = tmp;
} break;
case Mvmt.Yp:
{
Face tmp = this.X;
this.X = this.ZOpp;
this.Z = tmp;
} break;
case Mvmt.Z:
{
Face tmp = this.X;
this.X = this.YOpp;
this.Y = tmp;
} break;
case Mvmt.Zp:
{
Face tmp = this.Y;
this.Y = this.XOpp;
this.X = tmp;
} break;
case Mvmt.F: this.X.FMove(prime); break;
case Mvmt.R: this.X.Rgt.FMove(prime); break;
case Mvmt.U: this.X.Top.FMove(prime); break;
case Mvmt.D: this.X.Bot.FMove(prime); break;
case Mvmt.B: this.X.Opp.FMove(prime); break;
case Mvmt.L: this.X.Lft.FMove(prime); break;
case Mvmt.M: this.X.MMove(prime); break;
case Mvmt.E: this.X.EMove(prime); break;
case Mvmt.S: this.X.SMove(prime); break;
default: // Prime Moves
InternalMove(mvmt - 1, true);
return;
}
Console.WriteLine($"Made move {mvmt}{(prime ? 'p' : ' ')}");
if(mvmt <= Mvmt.Zp)
foreach(var Face in Faces)
Face.Orient();
}
public class Face {
public Face(uint id, uint size, RubixCube cube) {
Id = id;
Size = size;
Cube = cube;
Tiles = new uint[size, size];
for(uint i = 0; i < size; i++)
for(uint j = 0; j < size; j++)
Tiles[i, j] = Id;
}
public static bool operator==(Face lhs, Face rhs) {
return (lhs.Id == rhs.Id);
}
public static bool operator!=(Face lhs, Face rhs) {
return (lhs.Id != rhs.Id);
}
public bool Solved(){
bool solved = true;
uint id = Tiles[0,0];
foreach(uint tile in Tiles)
solved &= (tile == id);
return solved;
}
public void Connect(Face[] faces) {
this.Top = faces[0];
this.Bot = faces[1];
this.Lft = faces[2];
this.Rgt = faces[3];
this.Opp = faces[4];
}
public void Orient() {
uint wasTop = this.Top.Id;
if(this == Cube.Z) {
this.Top = Cube.XOpp;
this.Bot = Cube.X;
this.Lft = Cube.Y;
this.Rgt = Cube.YOpp;
} else if(this == Cube.ZOpp) {
this.Top = Cube.X;
this.Bot = Cube.XOpp;
this.Lft = Cube.Y;
this.Rgt = Cube.YOpp;
} else {
this.Top = Cube.Z;
this.Bot = Cube.ZOpp;
if(this == Cube.X) {
this.Lft = Cube.Y;
this.Rgt = Cube.YOpp;
} else if(this == Cube.Y) {
this.Lft = Cube.XOpp;
this.Rgt = Cube.X;
} else if(this == Cube.XOpp) {
this.Lft = Cube.YOpp;
this.Rgt = Cube.Y;
} else {
this.Lft = Cube.X;
this.Rgt = Cube.XOpp;
}
}
if(wasTop == this.Top.Id) { return; }
if(wasTop == this.Rgt.Id) { SilentFMove(); return; }
if(wasTop == this.Bot.Id) { SilentFMove(); SilentFMove(); return; }
SilentFMove(true);
}
protected void SilentFMove(bool prime = false) {
uint[,] RtoL = new uint[Size, Size];
uint[,] UtoD = new uint[Size, Size];
uint[,] Clockwise = new uint[Size, Size];
uint[,] AntiClockwise = new uint[Size, Size];
for(uint i = 0; i < Size; i++) {
for(uint j = 0; j < Size; j++) {
RtoL[i, j] = Tiles[i, Size-j-1];
UtoD[i, j] = Tiles[Size-i-1, j];
}
}
for(uint i = 0; i < Size; i++) {
for(uint j = 0; j < Size; j++) {
Clockwise[i,j] = UtoD[j, i];
AntiClockwise[i,j] = RtoL[j,i];
}
}
Tiles = prime ? AntiClockwise : Clockwise;
}
public void FMove(bool prime = false) {
SilentFMove(prime);
if(prime)
Top.FSide(Id,
Rgt.FSide(Id,
Bot.FSide(Id,
Lft.FSide(Id,
Top.FSide(Id)))));
else
Top.FSide(Id,
Lft.FSide(Id,
Bot.FSide(Id,
Rgt.FSide(Id,
Top.FSide(Id)))));
}
public void MMove(bool prime = false) {
if(Size < 3) return;
if(prime)
this.MSide(
Bot.MSide(
Opp.MSide(
Top.MSide(
MSide(), true),
true)));
else
this.MSide(
Top.MSide(
Opp.MSide(
Bot.MSide(MSide(), true), true)));
}
public void EMove(bool prime = false) {
if(Size < 3) return;
if(prime)
this.ESide(
Rgt.ESide(
Opp.ESide(
Lft.ESide(
this.ESide()))));
else this.ESide(
Lft.ESide(
Opp.ESide(
Rgt.ESide(
this.ESide()))));
}
public void SMove(bool prime = false) {
if(Size < 3) return;
if(prime)
Top.ESide(
Rgt.MSide(
Bot.ESide(
Lft.MSide(
Top.ESide(null, true)
),
true)
),
true);
else
Top.ESide(
Lft.MSide(
Bot.ESide(
Rgt.MSide(
Top.ESide(),
true)
),
true)
);
}
protected uint[] FSide(uint id, uint[] assign = null) {
uint[] row = new uint[Size];
uint? i = null;
uint? j = null;
if(Top.Id == id) i = 0;
if(Bot.Id == id) i = Size - 1;
if(Lft.Id == id) j = 0;
if(Rgt.Id == id) j = Size - 1;
if((i == null && j == null) || (i != null && j != null))
throw new Exception("Face not connected to cube properly.");
if(i == null) {
for(i = 0; i < Size; i++) {
uint k = (j == 0) ? Size - (uint)i - 1 : (uint)i;
row[(uint)i] = Tiles[k, (uint)j];
if(assign != null)
Tiles[k, (uint)j] = assign[(uint)i];
}
} else {
for(j = 0; j < Size; j++) {
uint k = (i > 0) ? Size - (uint)j - 1 : (uint)j;
row[(uint)j] = Tiles[(uint)i, k];
if(assign != null)
Tiles[(uint)i, k] = (uint)assign[(uint)j];
}
}
return row;
}
protected uint[] MSide(uint[] assign=null, bool prime=false) {
uint[] row = new uint[Size];
uint pos = (Size-1)/2; // TODO fix for other sizes
for(uint i = 0; i < Size; i++) {
uint k = (prime) ? (Size - i) - 1 : i;
row[k] = Tiles[i,pos];
if(assign != null)
Tiles[i,pos] = (uint)assign[i];
}
return row;
}
protected uint[] ESide(uint[] assign=null, bool prime=false) {
uint[] row = new uint[Size];
uint pos = (Size-1)/2; // TODO fix for other sizes
for(uint i = 0; i < Size; i++) {
uint k = (prime) ? (Size - i) - 1 : i;
row[k] = Tiles[pos,i];
if(assign != null)
Tiles[pos,i] = (uint)assign[i];
}
return row;
}
public void Draw3D() {
//Raylib.DrawLine3D(new Vector3(0f, 0f, 0f), TopLftVec(), (HexColor)SystemPalette.SideColor[Id]);
//Raylib.DrawLine3D(new Vector3(0f, 0f, 0f), BotRgtVec(), SystemPalette.SideColor[Id]);
Raylib.DrawCubeV(CenterVec()*GfxConstants.FACE_SIZE_3D / 2, (CenterVec() - TopLftVec()) + (CenterVec() * 0.0001f) * GfxConstants.FACE_SIZE_3D, SystemPalette.cubeBg);
for(uint row = 0; row < Size; row++) {
for(uint col = 0; col < Size; col++) {
Vector3 pos = TopLftVec() * GfxConstants.FACE_SIZE_3D / 2;
Vector3 BezlOff = ((row+1) * Bot.CenterVec() * TileSize3D() * GfxConstants.BEZEL_RATIO_3D)
+ ((col+1) * Rgt.CenterVec() * TileSize3D() * GfxConstants.BEZEL_RATIO_3D);
Vector3 FaceOff = ((row + 0.5f) * Bot.CenterVec() * TileSize3D())
+ ((col + 0.5f) * Rgt.CenterVec() * TileSize3D());
//Raylib.DrawLine3D(new Vector3(0f, 0f, 0f), pos, !(HexColor)SystemPalette.SideColor[Tiles[row,col]]);
Raylib.DrawCubeV(pos + BezlOff + FaceOff, TileSizeVec(), SystemPalette.SideColor[Tiles[row,col]]);
//Raylib.DrawCubeWiresV(pos + BezlOff + FaceOff, TileSizeVec(), Miamineon.Lime);
}
}
}
public Vector3 CenterVec() {
if(this == Cube.X)
return new Vector3(0f, 0f, 1f);
if(this == Cube.Z)
return new Vector3(0f, 1f, 0f);
if(this == Cube.Y)
return new Vector3(-1f, 0f, 0f);
if(this == Cube.ZOpp)
return new Vector3(0f, -1f, 0f);
if(this == Cube.YOpp)
return new Vector3(1f, 0f, 0f);
if(this == Cube.XOpp)
return new Vector3(0f, 0f, -1f);
throw new Exception("Invalid face id.");
}
public Vector3 TopLftVec() {
return CenterVec() + Top.CenterVec() + Lft.CenterVec();
}
public Vector3 BotRgtVec() {
return CenterVec() + Bot.CenterVec() + Rgt.CenterVec();
}
public Vector3 TileSizeVec() {
Vector3 svec = (TopLftVec() - CenterVec()) * TileSize3D()
+ (CenterVec() * 0.01f);
return (this == Cube.X || this == Cube.XOpp) ? -1 * svec : svec;
}
private float TileSize3D() {
return ((float)GfxConstants.FACE_SIZE_3D / (Size + ((Size + 1) * GfxConstants.BEZEL_RATIO_3D)));
}
public RubixCube Cube;
public uint[,] Tiles { get; private set; }
public uint Size;
public Face? Top;
public Face? Bot;
public Face? Lft;
public Face? Rgt;
public Face? Opp;
public uint Id { get; private set; }
public Vector3 Normal { get { return Vector3.Normalize(CenterVec()); } }
}
private List<Face> Faces = new List<Face>();
private List<MoveObject> Moves = new List<MoveObject>();
public Face X { get; private set; }
public Face Y { get; private set; }
public Face Z { get; private set; }
public Face XOpp { get { return this.X.Opp; } }
public Face YOpp { get { return this.Y.Opp; } }
public Face ZOpp { get { return this.Z.Opp; } }
private Face MainFace () => this.X;
private uint Size;
}
}