Minor changes
[matilda.git] / src / board_io.c
blob0bea522858a9728f8f2d85ad79fea7b86695dd8c
1 /*
2 More board functions related to cleaning and outputing board states.
3 */
5 #include "config.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
11 #include "alloc.h"
12 #include "board.h"
13 #include "pts_file.h"
14 #include "state_changes.h"
15 #include "stringm.h"
17 extern bool is_hoshi[TOTAL_BOARD_SIZ];
22 Clears the contents of a board.
24 void clear_board(
25 board * b
26 ) {
27 memset(b->p, EMPTY, TOTAL_BOARD_SIZ);
28 b->last_played = b->last_eaten = NONE;
33 Clears the contents of an output board.
35 void clear_out_board(
36 out_board * b
37 ) {
38 memset(b->tested, false, TOTAL_BOARD_SIZ);
39 b->pass = 0.0;
44 Format a string with a representation of the contents of an output board.
45 RETURNS string representation
47 void out_board_to_string(
48 char * dst,
49 const out_board * src
50 ) {
51 u16 idx = 0;
52 for (move m = 0; m < TOTAL_BOARD_SIZ; ++m) {
53 if (src->tested[m]) {
54 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, " %4.2f", src->value[m]);
55 } else {
56 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, " -- ");
59 if (((m + 1) % BOARD_SIZ) == 0) {
60 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "\n");
63 snprintf(dst + idx, MAX_PAGE_SIZ - idx, "Pass: %4.2f\n", src->pass);
68 Prints the string representation on an output board.
70 void fprint_out_board(
71 FILE * fp,
72 const out_board * b
73 ) {
74 char * s = alloc();
75 out_board_to_string(s, b);
76 fprintf(fp, "%s", s);
77 release(s);
82 Format a string with a representation of the contents of a board, complete with
83 ko violation indication and subject to the display options of european/japanese
84 styles (defined in board.h).
85 RETURNS string representation
87 void board_to_string(
88 char * dst,
89 const u8 p[static TOTAL_BOARD_SIZ],
90 move last_played,
91 move last_eaten
92 ) {
93 load_hoshi_points();
95 move ko_pos = NONE;
96 if (last_eaten != NONE) {
97 board tmp;
98 memcpy(tmp.p, p, TOTAL_BOARD_SIZ);
99 tmp.last_played = last_played;
100 tmp.last_eaten = last_eaten;
101 u8 own = BLACK_STONE;
103 if (is_board_move(last_played) && p[last_played] == BLACK_STONE) {
104 own = WHITE_STONE;
107 if (test_ko(&tmp, last_eaten, own)) {
108 ko_pos = last_eaten;
112 u16 idx = 0;
115 Column line
117 #if (!EUROPEAN_NOTATION) && (BOARD_SIZ > 9)
118 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, " ");
120 for (u8 i = 0; i < BOARD_SIZ; ++i) {
121 if (i >= 9) {
122 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "%2u", (i + 1) / 10);
123 } else {
124 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, " ");
128 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "\n");
129 #endif
131 if (BOARD_SIZ < 10) {
132 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, " ");
133 } else {
134 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, " ");
137 for (u8 i = 0; i < BOARD_SIZ; ++i) {
138 #if EUROPEAN_NOTATION
139 char c = i + 'A';
141 if (c >= 'I') {
142 ++c;
145 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, " %c", c);
146 #else
147 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, " %u", (i + 1) % 10);
148 #endif
152 Body
154 for (move m = 0; m < TOTAL_BOARD_SIZ; ++m) {
155 if ((m % BOARD_SIZ == 0)) {
156 move n = BOARD_SIZ - (m / BOARD_SIZ);
158 if (BOARD_SIZ < 10) {
159 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "\n%2u", n);
160 } else {
161 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "\n%3u", n);
165 u8 x;
166 u8 y;
167 move_to_coord(m, &x, &y);
169 char last_play_indicator = (m == last_played) ? '(' : ((m == last_played + 1 && x > 0) ? ')' : ' ');
171 switch (p[m]) {
172 case EMPTY:
173 if (m == ko_pos) {
174 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "%c!", last_play_indicator);
175 } else if (is_hoshi[m]) {
176 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "%c+", last_play_indicator);
177 } else {
178 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "%c%c", last_play_indicator, EMPTY_STONE_CHAR);
180 break;
181 case BLACK_STONE:
182 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "%c%c", last_play_indicator, BLACK_STONE_CHAR);
183 break;
184 case WHITE_STONE:
185 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "%c%c", last_play_indicator, WHITE_STONE_CHAR);
186 break;
187 default:
188 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "%c?", last_play_indicator);
191 if (x == BOARD_SIZ - 1) {
192 last_play_indicator = (m == last_played) ? ')' : ' ';
193 move n = BOARD_SIZ - (m / BOARD_SIZ);
195 if (BOARD_SIZ < 10) {
196 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "%c%u", last_play_indicator, n);
197 } else {
198 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "%c%2u", last_play_indicator, n);
205 Column line
207 if (BOARD_SIZ < 10) {
208 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "\n ");
209 } else {
210 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "\n ");
213 for (u8 i = 0; i < BOARD_SIZ; ++i) {
214 #if EUROPEAN_NOTATION
215 char c = i + 'A';
216 if (c >= 'I') {
217 ++c;
220 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, " %c", c);
221 #else
222 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, " %u", i >= 9 ? (i + 1) / 10 : (i + 1) % 10);
223 #endif
225 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "\n");
227 #if (!EUROPEAN_NOTATION) && (BOARD_SIZ > 9)
228 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, " ");
229 for (u8 i = 0; i < BOARD_SIZ; ++i) {
230 if (i >= 9) {
231 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "%2u", (i + 1) % 10);
232 } else {
233 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, " ");
236 idx += snprintf(dst + idx, MAX_PAGE_SIZ - idx, "\n");
237 #endif
239 if (last_played == PASS) {
240 snprintf(dst + idx, MAX_PAGE_SIZ - idx, "\nLast play was a pass\n");
241 } else if (last_played != NONE) {
242 char * mstr = alloc();
243 #if EUROPEAN_NOTATION
244 coord_to_alpha_num(mstr, last_played);
245 #else
246 coord_to_num_num(mstr, last_played);
247 #endif
248 snprintf(dst + idx, MAX_PAGE_SIZ - idx, "\nLast played %s\n", mstr);
249 release(mstr);
255 Print a board string representation.
257 void fprint_board(
258 FILE * fp,
259 const board * b
261 char * s = alloc();
262 board_to_string(s, b->p, b->last_played, b->last_eaten);
263 fprintf(fp, "%s", s);
264 release(s);