aboutsummaryrefslogtreecommitdiff
path: root/src/coordinates.c
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano.tronto@gmail.com>2020-06-21 23:01:57 +0200
committerSebastiano Tronto <sebastiano.tronto@gmail.com>2020-06-21 23:01:57 +0200
commit0e8d73bb3edcc8bdff6e3ded442b66f68265059a (patch)
tree4f92deb9ace97e79332c0e7ce390b76b81aeaae9 /src/coordinates.c
parent4e359b44ce111b04cc4d2b28033fba4ab4e6e989 (diff)
downloadnissy-0e8d73bb3edcc8bdff6e3ded442b66f68265059a.tar.gz
nissy-0e8d73bb3edcc8bdff6e3ded442b66f68265059a.zip
First push
Diffstat (limited to '')
-rw-r--r--src/coordinates.c288
1 files changed, 288 insertions, 0 deletions
diff --git a/src/coordinates.c b/src/coordinates.c
new file mode 100644
index 0000000..284aaaa
--- /dev/null
+++ b/src/coordinates.c
@@ -0,0 +1,288 @@
1/* blabla */
2
3#include <stdio.h>
4
5#include "utils.h"
6#include "coordinates.h"
7
8/* Names of pieces and moves. */
9char edge_string_list[12][5] = {
10 "UF", "UL", "UB", "UR", "DF", "DL", "DB", "DR", "FR", "FL", "BL", "BR"
11};
12
13char corner_string_list[8][5] = {
14 "UFR", "UFL", "UBL", "UBR", "DFR", "DFL", "DBL", "DBR"
15};
16
17char move_string_list[19][5] = {
18 "-",
19 "U", "U2", "U\'", "D", "D2", "D\'", "R", "R2", "R\'",
20 "L", "L2", "L\'", "F", "F2", "F\'", "B", "B2", "B\'"
21};
22
23int inverse_move[19] = {
24 -1, U3, U2, U, D3, D2, D, R3, R2, R, L3, L2, L, F3, F2, F, B3, B2, B
25};
26
27/* Convert piece representation from integer to array.
28 * Come convertions are not "perfect": for example, and epud type of piece
29 * is represented by a permutation index in 8! elements, but it as an array
30 * it is converted to the first 8 elements of a 12 elements ep array (with
31 * meaningless values for the other 4 elements). */
32
33void ep_int_to_array(int ep, int a[12]) {
34 index_to_perm(ep, 12, a);
35}
36
37void epud_int_to_array(int epud, int a[12]) {
38 index_to_perm(epud, 8, a); /* Last 4 elements are left untouched. */
39}
40
41void epfb_int_to_array(int epfb, int a[12]) {
42 int edges[] = {UF, UB, DF, DB, FR, FL, BL, BR};
43 int b[8];
44 index_to_perm(epfb, 8, b);
45 for (int i = 0; i < 8; i++)
46 a[edges[i]] = edges[b[i]];
47}
48
49void eprl_int_to_array(int eprl, int a[12]) {
50 int edges[] = {UL, UR, DL, DR, FR, FL, BL, BR};
51 int b[8];
52 index_to_perm(eprl, 8, b);
53 for (int i = 0; i < 8; i++)
54 a[edges[i]] = edges[b[i]];
55}
56
57void epose_int_to_array(int epos, int a[12]) {
58 int edges[] = {FR, FL, BL, BR};
59 index_to_subset(epos, 12, 4, a);
60 for (int i = 0, j = 0; i < 12; i++)
61 a[i] = (a[i] == 1) ? edges[j++] : -1;
62}
63
64void eposs_int_to_array(int epos, int a[12]) {
65 int edges[] = {UL, UR, DL, DR};
66 index_to_subset(epos, 12, 4, a);
67 for (int i = 0, j = 0; i < 12; i++)
68 a[i] = (a[i] == 1) ? edges[j++] : -1;
69 /* Swap with last 4, so 0 is alway solved state */
70 for (int i = 0; i < 4; i++)
71 swap(&a[edges[i]], &a[i+8]);
72}
73
74void eposm_int_to_array(int epos, int a[12]) {
75 int edges[] = {UF, UB, DF, DB};
76 index_to_subset(epos, 12, 4, a);
77 for (int i = 0, j = 0; i < 12; i++)
78 a[i] = (a[i] == 1) ? edges[j++] : -1;
79 /* Swap with last 4, so 0 is alway solved state */
80 for (int i = 0; i < 4; i++)
81 swap(&a[edges[i]], &a[i+8]);
82}
83
84void epe_int_to_array(int epe, int a[12]) {
85 index_to_perm(epe, 4, a+8);
86 for (int i = 0; i < 4; i++)
87 a[i+8] += 8;
88}
89
90void eps_int_to_array(int eps, int a[12]) {
91 int edges[] = {UL, UR, DL, DR};
92 int b[4];
93 index_to_perm(eps, 4, b);
94 for (int i = 0; i < 4; i++)
95 a[edges[i]] = edges[b[i]];
96}
97
98void epm_int_to_array(int epm, int a[12]) {
99 int edges[] = {UF, UB, DF, DB};
100 int b[4];
101 index_to_perm(epm, 4, b);
102 for (int i = 0; i < 4; i++)
103 a[edges[i]] = edges[b[i]];
104}
105
106void emslices_int_to_array(int emslices, int a[12]) {
107 int b[] = {0,0,0,0,0,0,0,0};
108 int eslice[] = {FR, FL, BL, BR};
109 int mslice[] = {UF, UB, DF, DB};
110
111 index_to_subset(emslices % binom12on4, 12, 4, a);
112 index_to_subset(emslices / binom12on4, 8, 4, b);
113
114 if (emslices % binom12on4 == 0) {
115 swap(&b[UF], &b[DL]);
116 swap(&b[UB], &b[DR]);
117 /*for (int i = 0; i < 4; i++)
118 swap(&b[mslice[i]], &b[i+4]);*/
119 }
120
121 for (int i = 0, j = 0; j < 8; i++, j++) {
122 while (a[i])
123 i++;
124 a[i] = b[j] ? 2 : -1;
125 }
126 for (int i = 0, j1 = 0, j2 = 0; i < 12; i++) {
127 if (a[i] == 1)
128 a[i] = eslice[j1++];
129 if (a[i] == 2)
130 a[i] = mslice[j2++];
131 }
132}
133
134void cp_int_to_array(int cp, int a[8]) {
135 index_to_perm(cp, 8, a);
136}
137
138void eo_11bits_to_array(int eo, int a[12]) {
139 int_to_sum_zero_array(eo, 2, 12, a);
140}
141
142void co_7trits_to_array(int co, int a[8]) {
143 int_to_sum_zero_array(co, 3, 8, a);
144}
145
146
147
148
149
150int ep_array_to_int(int ep[12]) {
151 return perm_to_index(ep, 12);
152}
153
154int epud_array_to_int(int ep[12]) {
155 return perm_to_index(ep, 8); /* Last 4 elements are ignored */
156}
157
158int epfb_array_to_int(int ep[12]) {
159 int index[] = {0, -1, 1, -1, 2, -1, 3, -1, 4, 5, 6, 7};
160 int b[8];
161 for (int i = 0; i < 12; i++)
162 if (index[i] != -1)
163 b[index[i]] = index[ep[i]];
164 return perm_to_index(b, 8);
165}
166
167int eprl_array_to_int(int ep[12]) {
168 int index[] = {-1, 0, -1, 1, -1, 2, -1, 3, 4, 5, 6, 7};
169 int b[8];
170 for (int i = 0; i < 12; i++)
171 if (index[i] != -1)
172 b[index[i]] = index[ep[i]];
173 return perm_to_index(b, 8);
174}
175
176int epose_array_to_int(int ep[12]) {
177 int a[12];
178 for (int i = 0; i < 12; i++)
179 a[i] = (ep[i] >= FR);
180 return subset_to_index(a, 12, 4);
181}
182
183int eposs_array_to_int(int ep[12]) {
184 int a[12];
185 int edges[] = {UL, UR, DL, DR};
186 for (int i = 0; i < 12; i++)
187 a[i] = (ep[i] == UL || ep[i] == UR || ep[i] == DL || ep[i] == DR);
188 /* Swap with last 4, so 0 is alway solved state */
189 for (int i = 0; i < 4; i++)
190 swap(&a[edges[i]], &a[i+8]);
191 return subset_to_index(a, 12, 4);
192}
193
194int eposm_array_to_int(int ep[12]) {
195 int a[12];
196 int edges[] = {UF, UB, DF, DB};
197 for (int i = 0; i < 12; i++)
198 a[i] = (ep[i] == UF || ep[i] == UB || ep[i] == DF || ep[i] == DB);
199 /* Swap with last 4, so 0 is alway solved state */
200 for (int i = 0; i < 4; i++)
201 swap(&a[edges[i]], &a[i+8]);
202 return subset_to_index(a, 12, 4);
203}
204
205int epe_array_to_int(int ep[12]) {
206 int b[4];
207 for (int i = 0; i < 4; i++)
208 b[i] = ep[i+8] - 8;
209 return perm_to_index(b, 4);
210}
211
212int eps_array_to_int(int ep[12]) {
213 int index[] = {-1, 0, -1, 1, -1, 2, -1, 3, -1, -1, -1, -1};
214 int b[4];
215 for (int i = 0; i < 12; i++)
216 if (index[i] != -1)
217 b[index[i]] = index[ep[i]];
218 return perm_to_index(b, 4);
219}
220
221int epm_array_to_int(int ep[12]) {
222 int index[] = {0, -1, 1, -1, 2, -1, 3, -1, -1, -1, -1, -1};
223 int b[4];
224 for (int i = 0; i < 12; i++)
225 if (index[i] != -1)
226 b[index[i]] = index[ep[i]];
227 return perm_to_index(b, 4);
228}
229
230int emslices_array_to_int(int ep[12]) {
231 int a[12], b[12], c[8] = {0, 0, 0, 0, 0, 0, 0, 0};
232 /*int edges[] = {UF, UB, DF, DB};*/
233 for (int i = 0; i < 12; i++) {
234 a[i] = (ep[i] >= FR) ? 1 : 0;
235 b[i] = (ep[i] == UF || ep[i] == UB || ep[i] == DF || ep[i] == DB) ? 1 : 0;
236 }
237
238 /*for ( int i = 0; i < 12; i++)
239 printf("%d ", ep[i]);
240 printf("\n");*/
241
242 for (int i = 0, j = 0; i < 12; i++, j++) {
243 if (a[i])
244 j--;
245 if (b[i])
246 c[j] = 1;
247 }
248
249 int epose = subset_to_index(a, 12, 4);
250
251 /*if (epose == 0) {
252 printf("Before: ");
253 for (int i = 0; i < 8; i++)
254 printf("%d ", c[i]);
255 printf("\n");
256 for (int i = 0; i < 4; i++)
257 swap(&c[edges[i]], &c[i+4]);
258 printf("After: ");
259 for (int i = 0; i < 8; i++)
260 printf("%d ", c[i]);
261 printf("\n");
262 }*/
263 if (epose == 0) {
264 swap(&c[UF], &c[DL]);
265 swap(&c[UB], &c[DR]);
266 }
267
268 /*for ( int i = 0; i < 8; i++)
269 printf("%d ", c[i]);
270 printf("\n");*/
271 int eposm = subset_to_index(c, 8, 4);
272
273 return epose + 495*eposm;
274}
275
276
277int cp_array_to_int(int cp[8]) {
278 return perm_to_index(cp, 8);
279}
280
281int eo_array_to_11bits(int a[12]) {
282 return digit_array_to_int(a, 11, 2);
283}
284
285int co_array_to_7trits(int a[8]) {
286 return digit_array_to_int(a, 7, 3);
287}
288

Generated with cgit - Back to sebastiano.tronto.net