diff options
author | tomsmeding <tom.smeding@gmail.com> | 2019-02-16 18:22:58 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2019-02-16 18:22:58 +0100 |
commit | 39b942ad50b1ebe942afd350d8038929a26d7ac6 (patch) | |
tree | c02b94ef498abecc6398dcfb713857cc457f7cd4 | |
parent | 12cf78567dfde9ebd37548ba13311651d36a55c2 (diff) |
Competition protocol in AI
-rw-r--r-- | main.cpp | 56 |
1 files changed, 55 insertions, 1 deletions
@@ -1,11 +1,12 @@ #include <iostream> +#include <sstream> #include <cstdlib> +#include <unistd.h> #include <sys/time.h> #include "board.h" #include "ai_mc.h" #include "ai_rand.h" #include "ui.h" -#include "util.h" using namespace std; @@ -17,6 +18,54 @@ using namespace std; #endif +static uint8_t readPlace(Board &bd) { + string line; + getline(cin, line); + if (line.size() >= 1 && tolower(line[0]) == 'q') { + exit(0); + } else if (line.size() >= 6 && memcmp(line.data(), "place ", 6) == 0) { + istringstream ss(line.substr(6)); + int x, y, clr; + ss >> x >> y >> clr; + return bd.putCW(BSZ * (y + BMID) + x + BMID, clr); + } else { + cerr << "Expected place line, got '" << line << "'" << endl; + exit(1); + } +} + +static void protocolIO() { + Board bd = Board::makeEmpty(); + readPlace(bd); + + string line; + + char c = cin.peek(); + uint8_t myclr; + if (c == 's' || c == 'S') { + myclr = 1; + getline(cin, line); + } else if (c == 'q' || c == 'Q') { + return; + } else { + myclr = 2; + } + + uint8_t onturn = 1; + while (true) { + if (onturn == myclr) { + int idx = AI::calcMove(bd, onturn); + int x = idx % BSZ - BMID, y = idx / BSZ - BMID; + cout << x << ' ' << y << endl; + readPlace(bd); + } else { + readPlace(bd); + } + + onturn = NEXTTURN(onturn); + } +} + int main() { struct timeval tv; gettimeofday(&tv, nullptr); @@ -24,6 +73,11 @@ int main() { cerr << "Using AI " << STR(AI) << endl; + if (!isatty(STDOUT_FILENO)) { + protocolIO(); + return 0; + } + Board bd = Board::makeEmpty(); // cerr << "Initial stone at " << Idx(BSZ * BMID + BMID) << endl; bd.put(BSZ * BMID + BMID, bd.bag.drawRandom()); |