-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRubbik.cpp
More file actions
157 lines (118 loc) · 3.71 KB
/
Rubbik.cpp
File metadata and controls
157 lines (118 loc) · 3.71 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
#include "Kube3.h"
#include <iostream> // for debug
#include <string> // for debug
#include <vector>
#define USE_SIMPLE_STATE
#undef USE_SIMPLE_STATE
#ifdef USE_SIMPLE_STATE
// naive representation
// 2x2x2 = 16 byte on x86
// 3x3x3 = 40 byte on x86
#include "SimpleKubeState2.h"
#include "SimpleKubeState3.h"
typedef rubik::Kube2<rubik::state::SimpleKubeState2> Rubik2;
typedef rubik::Kube3<rubik::state::SimpleKubeState3, rubik::state::SimpleKubeState2> Rubik3;
#else
// min-bit representation
// 2x2x2 = 28 bit ( 4 byte on x86)
// 3x3x3 = 69 bit (12 byte on x86)
#include "KubeState2.h"
#include "KubeState3.h"
typedef rubik::Kube2<rubik::state::KubeState2> Rubik2;
typedef rubik::Kube3<rubik::state::KubeState3, rubik::state::KubeState2> Rubik3;
#endif
template<typename Cube = Rubik3>
auto orbit(const std::vector<rubik::KubeMove> moves) {
Cube cube;
int i = 0;
do {
++i;
for (const auto x : moves)
cube.command(x);
} while (!cube.isSolved());
return i;
}
auto out_orbit(const std::vector<rubik::KubeMove> moves) {
std::cout << "Orbit " << toString(moves) << " is: ";
const auto ret = orbit(moves);
std::cout << ret << std::endl;
return ret;
}
template<typename Cube = Rubik3>
void out_result(rubik::KubeMove move, bool outinit = false) {
Cube cube;
if (outinit) cube.out();
std::cout << toString(move) << std::endl;
cube.command(move);
cube.out();
std::cout << std::endl;
}
template<typename Cube = Rubik3>
void out_result(const std::vector<rubik::KubeMove> moves, bool outinit = false) {
Cube cube;
if (outinit) cube.out();
std::cout << toString(moves) << std::endl;
for (const auto x : moves)
cube.command(x);
cube.out();
std::cout << std::endl;
}
template<typename Cube = Rubik3>
void trace_result(const std::vector<rubik::KubeMove> moves, bool outinit = true) {
std::cout << toString(moves) << std::endl;
Cube cube;
if (outinit) cube.out();
for (const auto x : moves) {
std::cout << toString(x) << std::endl;
cube.command(x);
cube.out();
}
std::cout << std::endl;
}
template<typename Cube = Rubik3>
void play(Cube cube = Cube()) {
do {
cube.out();
std::string input;
std::cin >> input;
if (input[0] == 'q') break;
const auto move = rubik::atoMove(input);
if (move == rubik::KubeMove::INVALID)
std::cout << "Invalid move" << std::endl;
else
cube.command(move);
if (cube.isSolved())
std::cout << "Solved" << std::endl;
} while (true);
}
#include <random>
template<typename Cube = Rubik3>
auto scramble(size_t n = 24, Cube cube = Cube()) {
std::mt19937 rng;
rng.seed(std::random_device()());
const std::uniform_int_distribution<std::mt19937::result_type> dist(0, 11);
for (size_t i = 0; i < n; i++) {
const rubik::KubeMove x = static_cast<rubik::KubeMove>(dist(rng));
cube.command(x);
}
return cube;
}
int main()
{
//std::cout << "sizeof(Rubik2) = " << sizeof(Rubik2) << std::endl;
//std::cout << "sizeof(Rubik3) = " << sizeof(Rubik3) << std::endl;
//auto orbit6 = rubik::atoMoves("R,U,Ri,Ui");
//auto rearrange_yellows = rubik::atoMoves("R,U,Ri,U,R,U,U,Ri");
//auto rearrange_corners = rubik::atoMoves("Ri,F,Ri,B,B,R,Fi,Ri,B,B,R,R");
//auto pll_rotate_cw = rubik::atoMoves("R,Ui,R,U,R,U,R,Ui,Ri,Ui,R,R");
//auto pll_rotate_ccw = rubik::atoMoves("R,R,U,R,U,Ri,Ui,Ri,Ui,Ri,U,Ri");
//out_orbit(pll_rotate_cw);
//out_orbit(pll_rotate_ccw);
//out_orbit(rearrange_corners);
//out_orbit(rearrange_yellows);
//out_orbit(orbit6);
//out_orbit({ rubik::KubeMove::Li, rubik::KubeMove::Ui, rubik::KubeMove::L });
//out_orbit(rubik::atoMoves("R,R,L,L,U,U,D,D,F,F,B,B"));
play(scramble());
return 0;
}