]> git.draconx.ca Git - rrace.git/blob - t/boardmove.c
Mix in PID with initial seed.
[rrace.git] / t / boardmove.c
1 /*
2  * Helper to test game_do_move function.
3  * Copyright © 2022-2023 Nick Bowler
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "game-notime.h"
24
25 static const char *progname;
26
27 void show_board(uint_fast32_t shape)
28 {
29         unsigned i, j;
30
31         for (i = 0; i < 5; i++) {
32                 unsigned row = (shape >> 5*i) & 0x1f;
33
34                 for (j = 0; j < 5; j++) {
35                         printf("%c", row & 1 ? '@' : '.');
36                         row >>= 1;
37                 }
38                 putchar('\n');
39         }
40 }
41
42 static void print_usage(FILE *f)
43 {
44         fprintf(f, "Usage: %s sequence\n", progname);
45 }
46
47 static int get_seq(char c)
48 {
49         if (c >= '0' && c <= '4') {
50                 return c - '0';
51         }
52
53         if (c == 0) {
54                 fprintf(stderr, "%s: unexpected end of sequence\n", progname);
55         } else {
56                 fprintf(stderr, "%s: invalid character %c\n", progname, c);
57         }
58         exit(EXIT_FAILURE);
59 }
60
61 int main(int argc, char **argv)
62 {
63         struct board board;
64         const char *seq;
65         int i = 0;
66
67         if (argc > 0)
68                 progname = argv[0];
69
70         if (argc != 2) {
71                 print_usage(stderr);
72                 return EXIT_FAILURE;
73         }
74
75         seq = argv[1];
76         board.x = get_seq(seq[i++]);
77         board.y = get_seq(seq[i++]);
78
79         board.game[0] = GAME_MASK ^ board_position(board.x, board.y);
80         board.game[1] = board.game[2] = board.game[3] = board.game[0];
81
82         while (1) {
83                 int j, x, y;
84
85                 show_board(board.game[0]);
86                 for (j = 1; j < 4; j++) {
87                         if (board.game[j] != board.game[0]) {
88                                 fprintf(stderr, "%s: plane %d mismatch\n",
89                                                 progname, j);
90                                 board.game[j] = board.game[0];
91                         }
92                 }
93
94                 if (seq[i]) {
95                         x = get_seq(seq[i++]);
96                         y = get_seq(seq[i++]);
97                         game_do_move(&board, x, y);
98                         putchar('\n');
99                 } else {
100                         break;
101                 }
102         }
103
104         return 0;
105 }