-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
67 lines (58 loc) · 1.52 KB
/
main.c
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
//
// Copyright 2023 Stephen E. Bensley
//
// This file is licensed under the MIT License. You may obtain a copy of the
// license at https://github.com/stephenbensley/Queah/blob/main/LICENSE.
//
#include "QueahEngine.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char* get_outcome(const QueahGame* game)
{
if (queah_game_repetitions(game) >= 3) {
return "Draw";
}
if (queah_game_is_over(game)) {
return queah_game_player_to_move(game) ? "White wins" : "Black wins";
}
return NULL;
}
int main(int argc, const char * argv[])
{
int result = EXIT_SUCCESS;
QueahGame* game = NULL;
QueahAI* ai = NULL;
char buf[256];
int buflen = sizeof(buf);
QueahMove move;
const char* outcome;
int num_moves;
game = queah_game_create();
if (game == NULL) {
printf("Failed to create game object.");
result = EXIT_FAILURE;
goto cleanup;
}
ai = queah_ai_create("queah.dat");
if (ai == NULL) {
printf("Failed to create AI object.");
result = EXIT_FAILURE;
goto cleanup;
}
queah_game_to_string(game, buf, buflen);
puts(buf);
do {
queah_ai_get_move(ai, game, &move);
queah_game_make_move(game, move, NULL);
queah_game_to_string(game, buf, buflen);
puts(buf);
outcome = get_outcome(game);
} while (!outcome);
num_moves = queah_game_moves_completed(game);
printf("%s in %d moves.\n", outcome, num_moves);
cleanup:
queah_ai_destroy(ai);
queah_game_destroy(game);
return result;
}