summaryrefslogtreecommitdiff
path: root/main.cpp
blob: 993bfc3f701e7d552e301b0dc4a9b277d9d413db (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <cassert>
#include <sys/time.h>
#include "board.h"
#include "ai_mc.h"
#include "ai_mm.h"
#include "ai_mcts.h"
#include "ai_rand.h"

using namespace std;

#ifndef AI_CHOICE
#define AI_CHOICE MCTS
#endif

#define STR_(x) #x
#define STR(x) STR_(x)
#define AI_CHOICE_STR STR(AI_CHOICE)


static void skipLine(istream &stream) {
	while (stream.get() != '\n');
}

static Move findMove(const Board &bd, int player) {
	clock_t start = clock();
	Move mv = AI:: AI_CHOICE ::findMove(bd, player);
	clock_t diff = clock() - start;
	cerr << "Time taken: " << (double)diff / CLOCKS_PER_SEC << " seconds" << endl;
	return mv;
}

int main() {
	struct timeval tv;
	gettimeofday(&tv, nullptr);
	srand(tv.tv_sec * 1000000UL + tv.tv_usec);

	cerr << "Using AI: " << AI_CHOICE_STR << endl;

	Board bd = Board::makeInitial();
	// cerr << bd << endl;

	int onturn = -1;

	char c = cin.peek();
	if (c == 's' || c == 'S') {
		skipLine(cin);
		Move mv = findMove(bd, onturn);
		assert(bd.isValid(mv, onturn));
		cout << mv << endl;
		bd.apply(mv);
		onturn = -onturn;
	}

	int win = 0;

	string line;
	while (true) {
		cerr << endl << bd << endl;

		getline(cin, line);
		if (!cin || line[0] == 'q' || line[0] == 'Q') break;

		Move mv;
		if (auto omv = Move::parse(line)) {
			mv = *omv;
		} else {
			cerr << "Unreadable move" << endl;
			break;
		}

		if (!bd.isValid(mv, onturn)) {
			cerr << "Invalid move read" << endl;
			break;
		}

		win = bd.applyCW(mv);
		onturn = -onturn;
		if (win != 0) break;

		cerr << endl << bd << endl;

		mv = findMove(bd, onturn);
		assert(bd.isValid(mv, onturn));

		cout << mv << endl;

		win = bd.applyCW(mv);
		onturn = -onturn;
		if (win != 0) break;
	}

	cerr << bd << endl;
	if (win == 1) cerr << "White won" << endl;
	else if (win == -1) cerr << "Black won" << endl;
}