aboutsummaryrefslogtreecommitdiff
path: root/src/coordinates.c
blob: 284aaaa4f44af5a7e3ccf549da9904305b0a0cd7 (plain)
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
/* blabla */

#include <stdio.h>

#include "utils.h"
#include "coordinates.h"

/* Names of pieces and moves. */
char edge_string_list[12][5] = {
  "UF", "UL", "UB", "UR", "DF", "DL", "DB", "DR", "FR", "FL", "BL", "BR"
};

char corner_string_list[8][5] = {
  "UFR", "UFL", "UBL", "UBR", "DFR", "DFL", "DBL", "DBR"
};

char move_string_list[19][5] = {
  "-",
  "U", "U2", "U\'", "D", "D2", "D\'", "R", "R2", "R\'",
  "L", "L2", "L\'", "F", "F2", "F\'", "B", "B2", "B\'"
};

int inverse_move[19] = {
  -1, U3, U2, U, D3, D2, D, R3, R2, R, L3, L2, L, F3, F2, F, B3, B2, B
};

/* Convert piece representation from integer to array.
 * Come convertions are not "perfect": for example, and epud type of piece
 * is represented by a permutation index in 8! elements, but it as an array
 * it is converted to the first 8 elements of a 12 elements ep array (with
 * meaningless values for the other 4 elements). */

void ep_int_to_array(int ep, int a[12]) {
  index_to_perm(ep, 12, a);
}

void epud_int_to_array(int epud, int a[12]) {
  index_to_perm(epud, 8, a); /* Last 4 elements are left untouched. */
}

void epfb_int_to_array(int epfb, int a[12]) {
  int edges[] = {UF, UB, DF, DB, FR, FL, BL, BR};
  int b[8];
  index_to_perm(epfb, 8, b);
  for (int i = 0; i < 8; i++)
    a[edges[i]] = edges[b[i]];
}

void eprl_int_to_array(int eprl, int a[12]) {
  int edges[] = {UL, UR, DL, DR, FR, FL, BL, BR};
  int b[8];
  index_to_perm(eprl, 8, b); 
  for (int i = 0; i < 8; i++)
    a[edges[i]] = edges[b[i]];
}

void epose_int_to_array(int epos, int a[12]) {
  int edges[] = {FR, FL, BL, BR};
  index_to_subset(epos, 12, 4, a);
  for (int i = 0, j = 0; i < 12; i++)
    a[i] = (a[i] == 1) ? edges[j++] : -1;
}

void eposs_int_to_array(int epos, int a[12]) {
  int edges[] = {UL, UR, DL, DR};
  index_to_subset(epos, 12, 4, a);
  for (int i = 0, j = 0; i < 12; i++)
    a[i] = (a[i] == 1) ? edges[j++] : -1;
  /* Swap with last 4, so 0 is alway solved state */
  for (int i = 0; i < 4; i++)
    swap(&a[edges[i]], &a[i+8]);
}

void eposm_int_to_array(int epos, int a[12]) {
  int edges[] = {UF, UB, DF, DB};
  index_to_subset(epos, 12, 4, a);
  for (int i = 0, j = 0; i < 12; i++)
    a[i] = (a[i] == 1) ? edges[j++] : -1;
  /* Swap with last 4, so 0 is alway solved state */
  for (int i = 0; i < 4; i++)
    swap(&a[edges[i]], &a[i+8]);
}

void epe_int_to_array(int epe, int a[12]) {
  index_to_perm(epe, 4, a+8);
  for (int i = 0; i < 4; i++)
    a[i+8] += 8;
}

void eps_int_to_array(int eps, int a[12]) {
  int edges[] = {UL, UR, DL, DR};
  int b[4];
  index_to_perm(eps, 4, b);
  for (int i = 0; i < 4; i++)
   a[edges[i]] = edges[b[i]];
} 

void epm_int_to_array(int epm, int a[12]) {
  int edges[] = {UF, UB, DF, DB};
  int b[4];
  index_to_perm(epm, 4, b);
  for (int i = 0; i < 4; i++)
    a[edges[i]] = edges[b[i]];
}

void emslices_int_to_array(int emslices, int a[12]) {
  int b[] = {0,0,0,0,0,0,0,0};
  int eslice[] = {FR, FL, BL, BR};
  int mslice[] = {UF, UB, DF, DB};

  index_to_subset(emslices % binom12on4, 12, 4, a);
  index_to_subset(emslices / binom12on4, 8,  4, b);

  if (emslices % binom12on4 == 0) {
    swap(&b[UF], &b[DL]);
    swap(&b[UB], &b[DR]);
    /*for (int i = 0; i < 4; i++)
      swap(&b[mslice[i]], &b[i+4]);*/
  }

  for (int i = 0, j = 0; j < 8; i++, j++) {
    while (a[i])
      i++;
    a[i] = b[j] ? 2 : -1;
  }
  for (int i = 0, j1 = 0, j2 = 0; i < 12; i++) {
    if (a[i] == 1)
      a[i] = eslice[j1++];
    if (a[i] == 2)
      a[i] = mslice[j2++];
  }
}

void cp_int_to_array(int cp, int a[8]) {
  index_to_perm(cp, 8, a);
}

void eo_11bits_to_array(int eo, int a[12]) {
  int_to_sum_zero_array(eo, 2, 12, a);
}

void co_7trits_to_array(int co, int a[8]) {
  int_to_sum_zero_array(co, 3, 8, a);
}





int ep_array_to_int(int ep[12]) {
  return perm_to_index(ep, 12);
}

int epud_array_to_int(int ep[12]) {
  return perm_to_index(ep, 8); /* Last 4 elements are ignored */
}

int epfb_array_to_int(int ep[12]) {
  int index[] = {0, -1, 1, -1, 2, -1, 3, -1, 4, 5, 6, 7};
  int b[8];
  for (int i = 0; i < 12; i++)
    if (index[i] != -1)
      b[index[i]] = index[ep[i]];
  return perm_to_index(b, 8);
}

int eprl_array_to_int(int ep[12]) {
  int index[] = {-1, 0, -1, 1, -1, 2, -1, 3, 4, 5, 6, 7};
  int b[8];
  for (int i = 0; i < 12; i++)
    if (index[i] != -1)
      b[index[i]] = index[ep[i]];
  return perm_to_index(b, 8);
}

int epose_array_to_int(int ep[12]) {
  int a[12];
  for (int i = 0; i < 12; i++)
    a[i] = (ep[i] >= FR);
  return subset_to_index(a, 12, 4);
}

int eposs_array_to_int(int ep[12]) {
  int a[12];
  int edges[] = {UL, UR, DL, DR};
  for (int i = 0; i < 12; i++)
    a[i] = (ep[i] == UL || ep[i] == UR || ep[i] == DL || ep[i] == DR);
  /* Swap with last 4, so 0 is alway solved state */
  for (int i = 0; i < 4; i++)
    swap(&a[edges[i]], &a[i+8]);
  return subset_to_index(a, 12, 4);
}

int eposm_array_to_int(int ep[12]) {
  int a[12];
  int edges[] = {UF, UB, DF, DB};
  for (int i = 0; i < 12; i++)
    a[i] = (ep[i] == UF || ep[i] == UB || ep[i] == DF || ep[i] == DB);
  /* Swap with last 4, so 0 is alway solved state */
  for (int i = 0; i < 4; i++)
    swap(&a[edges[i]], &a[i+8]);
  return subset_to_index(a, 12, 4);
}

int epe_array_to_int(int ep[12]) {
  int b[4];
  for (int i = 0; i < 4; i++)
    b[i] = ep[i+8] - 8;
  return perm_to_index(b, 4);
}

int eps_array_to_int(int ep[12]) {
  int index[] = {-1, 0, -1, 1, -1, 2, -1, 3, -1, -1, -1, -1};
  int b[4];
  for (int i = 0; i < 12; i++)
    if (index[i] != -1)
      b[index[i]] = index[ep[i]];
  return perm_to_index(b, 4);
}

int epm_array_to_int(int ep[12]) {
  int index[] = {0, -1, 1, -1, 2, -1, 3, -1, -1, -1, -1, -1};
  int b[4];
  for (int i = 0; i < 12; i++)
    if (index[i] != -1)
      b[index[i]] = index[ep[i]];
  return perm_to_index(b, 4);
}

int emslices_array_to_int(int ep[12]) {
  int a[12], b[12], c[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  /*int edges[] = {UF, UB, DF, DB};*/
  for (int i = 0; i < 12; i++) {
    a[i] = (ep[i] >= FR) ? 1 : 0;
    b[i] = (ep[i] == UF || ep[i] == UB || ep[i] == DF || ep[i] == DB) ? 1 : 0;
  }

  /*for ( int i = 0; i < 12; i++)
    printf("%d ", ep[i]);
  printf("\n");*/
 
  for (int i = 0, j = 0; i < 12; i++, j++) {
    if (a[i])
      j--;
    if (b[i])
      c[j] = 1;
  }

  int epose = subset_to_index(a, 12, 4);

  /*if (epose == 0) {
    printf("Before: ");
    for (int i = 0; i < 8; i++)
      printf("%d ", c[i]);
    printf("\n");
    for (int i = 0; i < 4; i++)
      swap(&c[edges[i]], &c[i+4]); 
    printf("After: ");
    for (int i = 0; i < 8; i++)
      printf("%d ", c[i]);
    printf("\n");
  }*/
  if (epose == 0) {
    swap(&c[UF], &c[DL]);
    swap(&c[UB], &c[DR]);
  }

  /*for ( int i = 0; i < 8; i++)
    printf("%d ", c[i]);
  printf("\n");*/
  int eposm = subset_to_index(c, 8, 4);

  return epose + 495*eposm;
}


int cp_array_to_int(int cp[8]) {
  return perm_to_index(cp, 8);
}

int eo_array_to_11bits(int a[12]) {
  return digit_array_to_int(a, 11, 2);
}

int co_array_to_7trits(int a[8]) {
  return digit_array_to_int(a, 7, 3);
}

Generated with cgit - Back to sebastiano.tronto.net