summaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2018-06-30 00:30:14 +0200
committerTom Smeding <tom.smeding@gmail.com>2018-06-30 00:30:14 +0200
commit44421af15c2f361764b8741bb93f9fddda3f8a8b (patch)
tree15e541b25d145595279de6067cb839ebbb16b982 /main.cpp
Initial
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/main.cpp b/main.cpp
new file mode 100644
index 0000000..c699715
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,76 @@
+#include <iostream>
+#include <cassert>
+#include <sys/time.h>
+#include "board.h"
+#include "ai_mm.h"
+#include "ai_rand.h"
+
+using namespace std;
+
+
+static void skipLine(istream &stream) {
+ while (stream.get() != '\n');
+}
+
+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 = AI::MM::findMove(bd, onturn);
+ assert(bd.isValid(mv, onturn));
+ cout << mv << endl;
+ bd.apply(mv);
+ onturn = -onturn;
+ }
+
+ int win;
+
+ string line;
+ while (true) {
+ cerr << 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 << bd << endl;
+
+ mv = AI::MM::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;
+}