]> git.draconx.ca Git - rrace.git/blob - t/boardmove.c
7ed1e667a37a60f9760634d69967961c0ccd9f9a
[rrace.git] / t / boardmove.c
1 #include <config.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "game.h"
6
7 static const char *progname;
8
9 void show_board(uint_fast32_t shape)
10 {
11         unsigned i, j;
12
13         for (i = 0; i < 5; i++) {
14                 unsigned row = (shape >> 5*i) & 0x1f;
15
16                 for (j = 0; j < 5; j++) {
17                         printf("%c", row & 1 ? '@' : '.');
18                         row >>= 1;
19                 }
20                 putchar('\n');
21         }
22 }
23
24 static void print_usage(FILE *f)
25 {
26         fprintf(f, "Usage: %s sequence\n", progname);
27 }
28
29 static int get_seq(char c)
30 {
31         if (c >= '0' && c <= '4') {
32                 return c - '0';
33         }
34
35         if (c == 0) {
36                 fprintf(stderr, "%s: unexpected end of sequence\n", progname);
37         } else {
38                 fprintf(stderr, "%s: invalid character %c\n", progname, c);
39         }
40         exit(EXIT_FAILURE);
41 }
42
43 int main(int argc, char **argv)
44 {
45         struct board board;
46         const char *seq;
47         int i = 0;
48
49         if (argc > 0)
50                 progname = argv[0];
51
52         if (argc != 2) {
53                 print_usage(stderr);
54                 return EXIT_FAILURE;
55         }
56
57         seq = argv[1];
58         board.x = get_seq(seq[i++]);
59         board.y = get_seq(seq[i++]);
60
61         board.game[0] = GAME_MASK ^ board_position(board.x, board.y);
62         board.game[1] = board.game[2] = board.game[3] = board.game[0];
63
64         while (1) {
65                 int j, x, y;
66
67                 show_board(board.game[0]);
68                 for (j = 1; j < 4; j++) {
69                         if (board.game[j] != board.game[0]) {
70                                 fprintf(stderr, "%s: plane %d mismatch\n",
71                                                 progname, j);
72                                 board.game[j] = board.game[0];
73                         }
74                 }
75
76                 if (seq[i]) {
77                         x = get_seq(seq[i++]);
78                         y = get_seq(seq[i++]);
79                         game_do_move(&board, x, y);
80                         putchar('\n');
81                 } else {
82                         break;
83                 }
84         }
85
86         return 0;
87 }