-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhanoi.h
319 lines (255 loc) · 7.13 KB
/
hanoi.h
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#pragma once
#include <stdbool.h>
#include "ch32v003_cvbs_text_32x24.h"
#define HANOI_PIECES 9
#define HANOI_ROWS (HANOI_PIECES+1)
#define HANOI_TOWER_WIDTH 10
#define HANOI_TOWER_GAP 1
#define HANOI_SOLVER_DELAY 1
void hanoi_putc(char ch, unsigned n) {
if (n > 1024)
return;
while (n--)
putchar(ch);
}
uint32_t rand() {
static uint32_t seed = 123456789;
if (seed & 1)
seed = (seed>>1) ^ 0xA000001U;
else
seed = seed>>1;
return seed;
}
typedef struct hanoi_context_s {
cvbs_text_32x24_context_t *cvbs_text;
uint8_t towers[3][HANOI_ROWS];
uint8_t holding, holding_over;
} hanoi_context_t;
unsigned hanoi_bottom_piece(hanoi_context_t *ctx, int pin) {
return ctx->towers[pin][0];
}
unsigned hanoi_top_piece(hanoi_context_t *ctx, int pin) {
unsigned piece = 0;
for (int i=0; ctx->towers[pin][i]; i++)
piece = ctx->towers[pin][i];
return piece;
}
unsigned hanoi_tower_width(hanoi_context_t *ctx, int pin) {
return HANOI_TOWER_WIDTH;
unsigned W = hanoi_bottom_piece(ctx,pin);
if (ctx->holding_over == pin && W < ctx->holding)
W = ctx->holding;
if (W < HANOI_PIECES-1)
W = HANOI_PIECES-1;
return 2*W+1;
}
void hanoi_print_piece(unsigned W, unsigned piece) {
unsigned w = piece+1;
for (int i=0; i<W; i+=2) {
if (i+1==W-w)
hanoi_putc(0x85, 1);
else if (i<W-w)
hanoi_putc(0x00, 1);
else
hanoi_putc(0x80, 1);
}
for (int i=0; i<W; i+=2) {
if (i+1==w)
hanoi_putc(0x05, 1);
else if (i<w)
hanoi_putc(0x80, 1);
else
hanoi_putc(0x00, 1);
}
}
void hanoi_print_pin(unsigned W) {
hanoi_putc(0x00, W/2-1);
hanoi_putc(0x1A, 1);
hanoi_putc(0x15, 1);
hanoi_putc(0x00, W/2-1);
}
void hanoi_print_hand(unsigned W, unsigned piece) {
if (piece) {
hanoi_print_piece(W, piece);
return;
}
hanoi_putc(' ', W/2-3);
hanoi_putc('(', 1);
hanoi_putc(' ', 4);
hanoi_putc(')', 1);
hanoi_putc(' ', W/2-3);
}
void hanoi_print_row_pin(hanoi_context_t *ctx, unsigned row, unsigned pin) {
unsigned piece = ctx->towers[pin][row];
unsigned W = hanoi_tower_width(ctx, pin);
if (piece)
hanoi_print_piece(W, piece);
else
hanoi_print_pin(W);
}
void hanoi_print_row(hanoi_context_t *ctx, unsigned row) {
hanoi_print_row_pin(ctx, row, 0);
hanoi_putc(' ', HANOI_TOWER_GAP);
hanoi_print_row_pin(ctx, row, 1);
hanoi_putc(' ', HANOI_TOWER_GAP);
hanoi_print_row_pin(ctx, row, 2);
// hanoi_putc('\n', 1);
}
void hanoi_print_hand_row(hanoi_context_t *ctx) {
unsigned W;
W = hanoi_tower_width(ctx, 0);
if (ctx->holding_over == 0)
hanoi_print_hand(W, ctx->holding);
else
hanoi_putc(' ', W);
hanoi_putc(' ', HANOI_TOWER_GAP);
W = hanoi_tower_width(ctx, 1);
if (ctx->holding_over == 1)
hanoi_print_hand(W, ctx->holding);
else
hanoi_putc(' ', W);
hanoi_putc(' ', HANOI_TOWER_GAP);
W = hanoi_tower_width(ctx, 2);
if (ctx->holding_over == 2)
hanoi_print_hand(W, ctx->holding);
else
hanoi_putc(' ', W);
hanoi_putc('\n', 1);
}
void hanoi_print_pins(hanoi_context_t *ctx) {
for (int row = HANOI_ROWS; row--;)
hanoi_print_row(ctx, row);
unsigned W = 2*HANOI_TOWER_GAP
+ hanoi_tower_width(ctx, 0)
+ hanoi_tower_width(ctx, 1)
+ hanoi_tower_width(ctx, 2);
hanoi_putc(31, W);
hanoi_putc('\n', 1);
}
void hanoi_print(hanoi_context_t *ctx) {
cvbs_text_32x24_wait_for_vsync(ctx->cvbs_text);
hanoi_putc('\f',1);
hanoi_print_hand_row(ctx);
hanoi_putc('\n',1);
hanoi_print_pins(ctx);
printf("\n\n");
printf("towers of hanoi on ch32v003,\n");
printf("zx80 fonts and cvbs ntsc output.\n\n");
printf("github.com/lhartmann\n");
printf(" /ch32v003_cvbs\n");
}
bool hanoi_place_at(hanoi_context_t *ctx, unsigned pin, unsigned piece) {
for (unsigned pos=0; pos<HANOI_ROWS; pos++) {
uint8_t *p = &ctx->towers[pin][pos];
if (*p) {
// If tower contains a smaller piece. Can not place;
if (*p < piece)
return false;
} else {
// Place the piece
*p = piece;
return true;
}
}
// Tower has not enough space. Should never happen.
return false;
}
unsigned hanoi_take_from(hanoi_context_t *ctx, unsigned pin) {
for (unsigned pos=HANOI_ROWS; pos--;) {
uint8_t *p = &ctx->towers[pin][pos];
if (!*p)
continue;
// Take piece
unsigned piece = *p;
*p = 0;
return piece;
}
// No pieces to take
return 0;
}
bool hanoi_place(hanoi_context_t *ctx) {
if (!ctx->holding)
return false;
if (!hanoi_place_at(ctx, ctx->holding_over, ctx->holding))
return false;
ctx->holding = 0;
return true;
}
bool hanoi_take(hanoi_context_t *ctx) {
if (ctx->holding)
return false;
ctx->holding = hanoi_take_from(ctx, ctx->holding_over);
return !!ctx->holding;
}
void hanoi_clean(hanoi_context_t *ctx) {
memset(ctx->towers, 0, sizeof(ctx->towers));
ctx->holding = 0;
ctx->holding_over = 0;
}
void hanoi_reset(hanoi_context_t *ctx) {
hanoi_clean(ctx);
for (int i=0; i<HANOI_PIECES; i++)
hanoi_place_at(ctx, 0, HANOI_PIECES-i);
}
void hanoi_randomize(hanoi_context_t *ctx) {
hanoi_clean(ctx);
for (int i=0; i<HANOI_PIECES; ++i) {
int pin = rand()%3;
int piece = HANOI_PIECES-i;
hanoi_place_at(ctx, pin, piece);
}
ctx->holding_over = rand() % 3;
if (rand() % 2)
hanoi_take(ctx);
}
void hanoi_solver_move(hanoi_context_t *ctx, unsigned from, unsigned to, unsigned n) {
if (!n)
return;
unsigned tmp = 0 + 1 + 2 - from - to;
hanoi_solver_move(ctx, from, tmp, n-1);
ctx->holding_over = from;
hanoi_print(ctx);
Delay_Ms(HANOI_SOLVER_DELAY);
hanoi_take(ctx);
hanoi_print(ctx);
Delay_Ms(HANOI_SOLVER_DELAY);
ctx->holding_over = to;
hanoi_print(ctx);
Delay_Ms(HANOI_SOLVER_DELAY);
hanoi_place(ctx);
hanoi_print(ctx);
Delay_Ms(HANOI_SOLVER_DELAY);
hanoi_solver_move(ctx, tmp, to, n-1);
}
void hanoi_solver(hanoi_context_t *ctx) {
hanoi_reset(ctx);
hanoi_print(ctx);
Delay_Ms(1000);
hanoi_solver_move(ctx, 0, 2, HANOI_PIECES);
Delay_Ms(1000);
}
void hanoi_main(cvbs_text_32x24_context_t *cvbs_text) {
hanoi_context_t ctx;
ctx.cvbs_text = cvbs_text;
hanoi_solver(&ctx);
return;
// Test the worst case for dynamic width
hanoi_clean(&ctx);
for (int i=0; i<HANOI_PIECES; ++i) {
int pin = i%3;
int piece = HANOI_PIECES-i;
hanoi_place_at(&ctx, pin, piece);
}
hanoi_print(&ctx);
printf("Worst case scneario for dynamic width.\n");
Delay_Ms(5000);
// Test random states
for (int i=0; i<60; i++) {
hanoi_randomize(&ctx);
hanoi_print(&ctx);
for (int i=0; i<32; i++)
hanoi_putc('0'+i%10, 1);
printf("Random scnearios...\n");
Delay_Ms(1000);
}
}