aboutsummaryrefslogtreecommitdiff
path: root/src/io.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/io.c')
-rw-r--r--src/io.c232
1 files changed, 232 insertions, 0 deletions
diff --git a/src/io.c b/src/io.c
new file mode 100644
index 0000000..ab0f658
--- /dev/null
+++ b/src/io.c
@@ -0,0 +1,232 @@
1#include <stdio.h>
2#include <stdint.h>
3#include <string.h>
4
5#include "coordinates.h"
6#include "moves.h"
7#include "utils.h"
8
9/* Functions for nice output */
10char *edge_string(int i) {
11 return (i > -1 && i < 12) ? edge_string_list[i] : "-";
12}
13
14char *corner_string(int i) {
15 return (i > -1 && i < 8) ? corner_string_list[i] : "-";
16}
17
18char *move_string(int i) {
19 return (i > -1 && i < 19) ? move_string_list[i] : "err";
20}
21
22void print_ep_array(int ep[12]) {
23 for (int i = 0; i < 12; i++)
24 printf(" %s ", edge_string(ep[i]));
25}
26
27void print_ep_int(int ep) {
28 int aux[12];
29 ep_int_to_array(ep, aux);
30 print_ep_array(aux);
31}
32
33void print_cp_array(int cp[8]) {
34 for (int i = 0; i < 8; i++)
35 printf(" %s ", corner_string(cp[i]));
36}
37
38void print_cp_int(int cp) {
39 int aux[8];
40 cp_int_to_array(cp, aux);
41 print_cp_array(aux);
42}
43
44void print_eo_array(int eo[12]) {
45 for (int i = 0; i < 12; i++) {
46 if (eo[i])
47 printf(" x ");
48 else
49 printf(" ");
50 }
51}
52
53void print_eo_int(int eo) {
54 int aux[12];
55 eo_11bits_to_array(eo, aux);
56 print_eo_array(aux);
57}
58
59void print_co_array(int co[8]) {
60 for (int i = 0; i < 8; i++) {
61 if (co[i] == 0)
62 printf(" ");
63 if (co[i] == 1)
64 printf(" cw ");
65 if (co[i] == 2)
66 printf(" ccw ");
67 }
68}
69
70void print_co_int(int co) {
71 int aux[8];
72 co_7trits_to_array(co, aux);
73 print_co_array(aux);
74}
75
76void print_cube_scram(int *scram) {
77 int ep = 0, cp = 0, eofb = 0, coud = 0;
78 for (int i = 0; scram[i]; i++) {
79 ep = apply_move_ep_int(scram[i], ep);
80 cp = cp_transition_table[cp][scram[i]];
81 eofb = eofb_transition_table[eofb][scram[i]];
82 coud = coud_transition_table[coud][scram[i]];
83 }
84 printf("\t\t"); print_ep_int(0); printf("\n");
85 printf("EP:\t\t"); print_ep_int(ep); printf("\n");
86 printf("EO(F/B):\t"); print_eo_int(eofb); printf("\n");
87 printf("\n");
88 printf("\t\t"); print_cp_int(0); printf("\n");
89 printf("CP:\t\t"); print_cp_int(cp); printf("\n");
90 printf("CO(U/D):\t"); print_co_int(coud); printf("\n");
91}
92
93
94void copy_moves(int *src, int *dst) {
95 for (int i = 0; (dst[i] = src[i]); i++);
96}
97
98void append_moves(int *src, int *dst) {
99 int n = 0;
100 for (; dst[n]; n++);
101 copy_moves(src, dst+n);
102}
103
104/* Parse a string and saves the move in a. Supports NISS notation.
105 * Returns the number of moves, or -1 in case of error. */
106int read_moves(char *str, int *a) {
107 int count = 0;
108 int niss = 0;
109 for (int i = 0; str[i] && str[i] != '\n'; i++) {
110 while (str[i] == ' ' || str[i] == '\t') i++;
111 switch (str[i]) {
112 case 'U':
113 a[count++] = niss ? -U : U;
114 break;
115 case 'D':
116 a[count++] = niss ? -D : D;
117 break;
118 case 'R':
119 a[count++] = niss ? -R : R;
120 break;
121 case 'L':
122 a[count++] = niss ? -L : L;
123 break;
124 case 'F':
125 a[count++] = niss ? -F : F;
126 break;
127 case 'B':
128 a[count++] = niss ? -B : B;
129 break;
130 case '(':
131 if (niss)
132 return -1;
133 else
134 niss = 1;
135 break;
136 case ')':
137 if (!niss)
138 return -1;
139 else
140 niss = 0;
141 break;
142 default:
143 return -1;
144 }
145 switch (str[++i]) {
146 case '2':
147 a[count-1] += niss ? -1 : 1;
148 break;
149 case '\'':
150 case '3':
151 a[count-1] += niss ? -2 : 2;
152 break;
153 case '1':
154 default:
155 --i;
156 }
157 }
158 a[count] = 0;
159 return count;
160}
161
162/* Read moves from standard input, after a prompt. */
163int read_moves_from_prompt(int *a) {
164 char str[1000];
165 printf("Enter moves: ");
166 if (fgets(str, 1000, stdin) == NULL)
167 return -1;
168 return read_moves(str, a);
169}
170
171/* Read moves from a list of token, each containing one or more moves. */
172int read_moves_from_tok(int n, char tok[][100], int *a) {
173 char str[1000] = "";
174 for (int i = 0; i < n; i++)
175 strcat(str, tok[i]);
176 return read_moves(str, a);
177}
178
179/* Checks if a sequence of moves uses NISS */
180int uses_niss(int *str) {
181 for (int i = 0; str[i]; i++)
182 if (str[i] < 0)
183 return 1;
184 return 0;
185}
186
187/* A (B) -> B' A */
188int unniss(int *src, int *dst) {
189 int n = 0;
190 for (int i = 0; src[i]; i++)
191 if (src[i] < 0)
192 n++;
193
194 int norm_count = n, inv_count = n-1;
195 for (int i = 0; src[i]; i++)
196 if (src[i] > 0)
197 dst[norm_count++] = src[i];
198 else
199 dst[inv_count--] = inverse_move[-src[i]];
200
201 dst[norm_count] = 0;
202
203 return n;
204}
205
206int invert(int *src, int *dst) {
207 int aux[255];
208 for (int i = 0; (aux[i] = -src[i]); i++);
209 return unniss(aux, dst);
210}
211
212int len(int *scram) {
213 int m;
214 for (m = 0; scram[m]; m++);
215 return m;
216}
217
218void print_moves(int moves_list[]) {
219 int niss = 0;
220 for (int i = 0; moves_list[i]; i++) {
221 if (!niss && moves_list[i] < 0) {
222 printf("(");
223 niss = 1;
224 }
225 printf("%s", move_string_list[abs(moves_list[i])]);
226 if (niss && moves_list[i+1] >= 0) {
227 niss = 0;
228 printf(")");
229 }
230 printf(" ");
231 }
232}

Generated with cgit - Back to sebastiano.tronto.net