Skip to content

Commit d258521

Browse files
can now solve a floppy cube!
1 parent d60223c commit d258521

File tree

9 files changed

+145
-4
lines changed

9 files changed

+145
-4
lines changed

floppysolver

448 Bytes
Binary file not shown.

makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test:
1010
./floppysolver
1111

1212
compile: $(MAIN)
13-
clang -framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL lib/libraylib.a src/main.c src/floppy.c src/helpers.c -I raylib -o floppysolver
13+
clang -framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL lib/libraylib.a src/main.c src/floppy.c src/helpers.c src/moves.c -I raylib -o floppysolver
1414

1515
clean:
1616
rm -f floppysolver

src/floppy.c

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ int rRotating = 0;
1717
int lRotating = 0;
1818
int bRotating = 0;
1919

20-
int animSpeed = 4;
20+
int animSpeed = 14;
21+
22+
int turnStack[10];
2123

2224
FloppyCube DrawFloppyCube(FloppyCube floppyCube){
2325
if(fRotating || rRotating || lRotating || bRotating){
@@ -41,6 +43,14 @@ FloppyCube DrawFloppyCube(FloppyCube floppyCube){
4143
floppyCube = ExecuteTurn(floppyCube, B);
4244
bRotating = 0;
4345
}
46+
47+
if(turnStack[0] != 0){
48+
DoTurn(floppyCube, turnStack[0] - 1);
49+
50+
for(int i = 1; i < 10; i++){
51+
turnStack[i-1] = turnStack[i];
52+
}
53+
}
4454
}
4555
Vector2 planeSize = {1.85, 1.85};
4656

@@ -278,6 +288,18 @@ FloppyCube ExecuteTurn(FloppyCube cube, int turn){
278288
}
279289

280290
void DoTurn(FloppyCube cube, int turn){
291+
if(fRotating || bRotating || rRotating || lRotating){
292+
int stackLength = 0;
293+
294+
for(int i = 0; i < 10; i++){
295+
if(turnStack[i] != 0){
296+
stackLength = i + 1;
297+
}
298+
}
299+
300+
turnStack[stackLength] = turn+1;
301+
return;
302+
}
281303
if(turn == F){
282304
fRotating = 1;
283305
}
@@ -302,10 +324,37 @@ FloppyCube RandomScramble(FloppyCube cube){
302324
num = RandomRange(3);
303325
}
304326

305-
cube = ExecuteTurn(cube, num);
327+
DoTurn(cube, num);
306328

307329
previous = num;
308330
}
309331

310332
return cube;
333+
}
334+
335+
FloppyCube CloneFloppyCube(FloppyCube cube, FloppyCube clone){
336+
CloneNumberArray(cube.topFace, 8, clone.topFace);
337+
CloneNumberArray(cube.bottomFace, 8, clone.bottomFace);
338+
CloneNumberArray(cube.rightFace, 3, clone.rightFace);
339+
CloneNumberArray(cube.frontFace, 3, clone.frontFace);
340+
CloneNumberArray(cube.leftFace, 3, clone.leftFace);
341+
CloneNumberArray(cube.backFace, 3, clone.backFace);
342+
343+
return clone;
344+
}
345+
346+
bool IsCubeSolved(FloppyCube cube){
347+
bool isSolved = true;
348+
349+
for(int i = 0; i < 8; i++){
350+
if(cube.topFace[i] != CWHITE){
351+
isSolved = false;
352+
}
353+
}
354+
355+
if(cube.rightFace[0] != cube.rightFace[1]){
356+
isSolved = false;
357+
}
358+
359+
return isSolved;
311360
}

src/floppy.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ typedef enum _Turns Turns;
4242

4343
void DoTurn(FloppyCube cube, int turn);
4444

45+
bool IsCubeSolved(FloppyCube cube);
46+
4547
FloppyCube RandomScramble(FloppyCube cube);
4648
FloppyCube DrawFloppyCube(FloppyCube floppyCube);
4749
FloppyCube InitFloppyCube(FloppyCube floppyCube);
4850
FloppyCube ExecuteTurn(FloppyCube cube, int turn);
51+
FloppyCube CloneFloppyCube(FloppyCube cube, FloppyCube clone);
4952

5053
#endif

src/helpers.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,10 @@ int RandomRange(int max){
6363
}while (num > max);
6464

6565
return num;
66+
}
67+
68+
void CloneNumberArray(int* array, int size, int* clone){
69+
for(int i = 0; i < size; i++){
70+
clone[i] = array[i];
71+
}
6672
}

src/helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Color GetColorFromIndex(int index);
1010
int RandomRange(int max);
1111

1212
int* FillNumberArray(int* array, const int length, int value);
13+
void CloneNumberArray(int* array, int size, int* clone);
1314

1415
void ValueSwap(int* array1, int index1, int* array2, int index2);
1516

src/main.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33

44
#include "raylib.h"
55
#include "floppy.h"
6+
#include "moves.h"
67

78
const int screenWidth = 800;
89
const int screenHeight = 450;
910

1011
FloppyCube floppy;
1112

1213
void setup(){
13-
srand(time(NULL));
14+
srand(time(NULL));
1415

1516
floppy = InitFloppyCube(floppy);
17+
18+
// moveList = CreateMoveList(10);
1619
}
1720

1821
int main(){
@@ -89,6 +92,9 @@ int main(){
8992
if(IsKeyReleased(KEY_S)){
9093
floppy = RandomScramble(floppy);
9194
}
95+
if(IsKeyReleased(KEY_A)){
96+
floppy = SolveCube(floppy);
97+
}
9298
}
9399

94100
CloseWindow();

src/moves.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "moves.h"
2+
3+
int* CreateMoveList(const int length){
4+
int moveList[length];
5+
return moveList;
6+
}
7+
8+
int* AddToMoveList(int* movelist, int move){
9+
int moveListIndex = 0;
10+
11+
for(int i = 0; i < sizeof(&movelist)/sizeof(int); i++){
12+
if(movelist[i] != 0){
13+
moveListIndex = i+1;
14+
}
15+
}
16+
17+
movelist[moveListIndex] = move+1;
18+
19+
return movelist;
20+
}
21+
22+
int* ShiftMoveList(int* movelist){
23+
for(int i = 1; i < sizeof(&movelist)/sizeof(int); i++){
24+
movelist[i-1] = movelist[i];
25+
}
26+
27+
return movelist;
28+
}
29+
30+
FloppyCube SolveCube(FloppyCube cube){
31+
int* turns;
32+
FillNumberArray(turns, 7, -1);
33+
34+
FloppyCube clone;
35+
36+
for(int i = 0; i < 21844; i++){
37+
turns[0]++;
38+
for(int j = 0; j < 6; j++){
39+
if(turns[j] == 4){
40+
turns[j+1]++;
41+
turns[j] = 0;
42+
}
43+
}
44+
45+
clone = CloneFloppyCube(cube, clone);
46+
47+
for(int j = 0; j < 7; j++){
48+
clone = ExecuteTurn(clone, turns[j]);
49+
}
50+
if(IsCubeSolved(clone)){
51+
DoTurn(cube, turns[0]);
52+
DoTurn(cube, turns[1]);
53+
DoTurn(cube, turns[2]);
54+
DoTurn(cube, turns[3]);
55+
DoTurn(cube, turns[4]);
56+
DoTurn(cube, turns[5]);
57+
DoTurn(cube, turns[6]);
58+
return cube;
59+
}
60+
}
61+
62+
return cube;
63+
}

src/moves.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef FLOPPYSOLVER_MOVES
2+
#define FLOPPYSOLVER_MOVES
3+
4+
#include <stdio.h>
5+
#include "helpers.h"
6+
#include "floppy.h"
7+
8+
int* CreateMoveList(const int length);
9+
int* AddToMoveList(int* movelist, int move);
10+
int* ShiftMoveList(int* movelist);
11+
FloppyCube SolveCube(FloppyCube cube);
12+
13+
#endif

0 commit comments

Comments
 (0)