summaryrefslogtreecommitdiff
path: root/main.cpp
blob: 1419b08de3d9c066377cd7c08f295ac63eb52e64 (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 <string>
#include <stdexcept>
#include <time.h>
#include "board.h"
#include "minimax.h"


const int AB_MAXDEPTH = 6;

int main() {
	Board B;
	cout << B << endl;

	// clock_t start = clock();
	// for (int i = 0; i < 1000000; i++) {
	// 	B.subsequents();
	// }
	// clock_t diff = clock() - start;
	// cout << (double)diff / CLOCKS_PER_SEC << endl;

	// for (const Board &B2 : B.subsequents()) {
	// 	cout << B2 << endl;
	// }

	while (true) {
		string line;
		getline(cin, line);
		if (!cin) break;
		if (line == "s") {
			cout << "----------------------" << endl;
			for (const Board &B2 : B.subsequents()) {
				cout << B2 << endl;
			}
			cout << "----------------------" << endl << B << endl;
			continue;
		}
		if (line == "q") break;
		if (line == "r") {
			vector<Board> subs = B.subsequents();
			if (subs.size() == 0) {
				cout << "No possible moves" << endl;
			} else {
				B = subs[rand() % subs.size()];
				cout << B << endl;
			}
			continue;
		}
		if (line == "a") {
			vector<Board> subs = B.subsequents();
			if (subs.size() == 0) {
				cout << "No possible moves" << endl;
			} else {
				int maxval = INT_MIN, maxat = -1;
				const int multfactor = B.onTurn == WHITE ? 1 : -1;
				cout << "[";
				for (size_t i = 0; i < subs.size(); i++) {
					int v = alphabeta<evaluate_pieceScore>(subs[i], AB_MAXDEPTH);
					if (i != 0) cout << ", ";
					cout << v << endl;
					v *= multfactor;
					if (v > maxval) {
						maxval = v;
						maxat = i;
					}
				}
				cout << "]" << endl;
				B = subs[maxat];
				cout << B << endl;
			}
			continue;
		}
		try {
			Move mv(line.data());
			if (B.isValid(mv)) {
				B.apply(mv);
				cout << B << endl;
			} else {
				cout << "Invalid move." << endl;
			}
		} catch (const runtime_error &e) {
			cout << "Unrecognised input, expected move." << endl;
		}
	}
}