summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--board.cpp30
1 files changed, 27 insertions, 3 deletions
diff --git a/board.cpp b/board.cpp
index 04f0c70..b49b077 100644
--- a/board.cpp
+++ b/board.cpp
@@ -141,9 +141,33 @@ void Board::apply(Move mv) {
}
int Board::applyCW(Move mv) {
- // TODO: make more efficient
- apply(mv);
- return checkWin();
+ int x2 = mv.to % N, y2 = mv.to / N;
+
+ if (cells[mv.from] == KING) {
+ apply(mv);
+
+ if (cells[mv.to] == KING) {
+ if (x2 == 0 || x2 == N-1 || y2 == 0 || y2 == N-1) return 1;
+ }
+ } else {
+ bool haveking1 =
+ (x2 > 0 && cells[mv.to - 1] == KING) ||
+ (y2 > 0 && cells[mv.to - N] == KING) ||
+ (x2 < N-1 && cells[mv.to + 1] == KING) ||
+ (y2 < N-1 && cells[mv.to + N] == KING);
+
+ apply(mv);
+
+ bool haveking2 =
+ (x2 > 0 && cells[mv.to - 1] == KING) ||
+ (y2 > 0 && cells[mv.to - N] == KING) ||
+ (x2 < N-1 && cells[mv.to + 1] == KING) ||
+ (y2 < N-1 && cells[mv.to + N] == KING);
+
+ if (haveking1 && !haveking2) return -1;
+ }
+
+ return 0;
}
int Board::checkWin() const {