git.haldean.org grandmaster / c327210
add termination and draws to game state and board json Haldean 3 years ago
3 changed file(s) with 45 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
2828 if (!t) return json_pack("{ss}", "error", "missing field " field);
2929
3030 json_t *
31 game_state(struct game_tree *gt, game_id_t game)
31 game_state(struct game_tree *gt, game_id_t game_id)
3232 {
33 json_t *res;
3334 struct move *move;
34 move = get_game(gt, game)->current->move;
35 return board_to_json(move->post_board);
35 struct game *game;
36
37 game = get_game(gt, game_id);
38 move = game->current->move;
39 res = board_to_json(move->post_board);
40 json_object_set_new(
41 res, "termination",
42 json_string(termination_str(game->termination)));
43 return res;
3644 }
3745
3846 json_t *
214214 char *
215215 move_to_fen(const struct move *);
216216
217 /* String representation for a termination state. */
218 char *
219 termination_str(termination_t term);
220
217221 #endif
5353 return player == WHITE ? "white" : "black";
5454 }
5555
56 char *
57 termination_str(termination_t term)
58 {
59 switch (term) {
60 case AVAILABLE_MOVE:
61 return "available_move";
62 case VICTORY_WHITE:
63 return "victory_white";
64 case VICTORY_BLACK:
65 return "victory_black";
66 case STALEMATE:
67 return "stalemate";
68 case TAKEN_DRAW_WHITE:
69 return "taken_draw_white";
70 case TAKEN_DRAW_BLACK:
71 return "taken_draw_black";
72 case RESIGNATION_WHITE:
73 return "resignation_white";
74 case RESIGNATION_BLACK:
75 return "resignation_black";
76 default:
77 return "bad_termination_value";
78 }
79 }
80
5681 void
5782 print_move(const struct move *move)
5883 {
152177 json_set(board_root, "pgn", json_string(board->pgn));
153178 json_set(board_root, "fen", json_string(board->fen));
154179
180 json_set(board_root, "termination",
181 json_string(termination_str(board->termination)));
182 json_set(board_root, "draws", json_integer(board->draws));
183 json_set(board_root, "in_check", json_boolean(board->in_check));
184
155185 return board_root;
156186 }
157187