#pragma once #include #include #include #include #include using namespace std; const int N = 9; const int BOARDMID = N * (N/2) + N/2; const uint8_t EMPTY = 0, WHITE = 1, KING = 2, BLACK = 4; struct Move { int from, to; Move() = default; Move(int from, int to); static optional parse(string_view str); }; struct Board { uint8_t cells[N * N]; static Board makeInitial(); void apply(Move mv); int applyCW(Move mv); int checkWin() const; // 1=win white, -1=win black, 0=none yet bool isValid(Move mv, int player) const; // Calls the callback for each valid move of the specified player. // F should have a signature compatible with bool F(Move mv). // If the callback returns true, iteration is stopped. // Callback should not modify this Board instance. template ::value>> void forEachMove(int player, F callback) const { static const int deltas[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; for (int y = 0; y < N; y++) for (int x = 0; x < N; x++) { uint8_t v = cells[N * y + x]; if (player == 1 ? v != WHITE && v != KING : v != BLACK) continue; for (int i = 0; i < 4; i++) { int x2 = x + deltas[i][0], y2 = y + deltas[i][1]; while (x2 >= 0 && x2 < N && y2 >= 0 && y2 < N) { if (cells[N * y2 + x2] != EMPTY) break; if (N * y2 + x2 != BOARDMID) { bool ret = callback(Move(N * y + x, N * y2 + x2)); if (ret) goto stop_iteration; } x2 += deltas[i][0]; y2 += deltas[i][1]; } } } stop_iteration: ; } private: Board(); bool isValid(Move mv) const; bool stoneFlankedH(int pos, uint8_t by) const; bool stoneFlankedV(int pos, uint8_t by) const; bool kingEncircled(int pos) const; }; ostream& operator<<(ostream &os, const Move &mv); ostream& operator<<(ostream &os, const Board &bd);