summaryrefslogtreecommitdiff
path: root/main.cpp
blob: 4fb66a92e329ac3185a8404a653a2bad70affaba (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
#include <iostream>
#include <cassert>
#include <sys/time.h>
#include "board.h"
#include "ai_mc.h"
#include "ai_mm.h"
#include "ai_rand.h"

using namespace std;


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

static Move findMove(const Board &bd, int player) {
	clock_t start = clock();
	Move mv = AI::MC::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);

	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;

	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;
}