summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2018-07-03 20:53:55 +0200
committerTom Smeding <tom.smeding@gmail.com>2018-07-03 21:03:40 +0200
commitdc2c8a680417e395ddbf7c4684ef646d36ffdf1a (patch)
treeeddf6737d959d553d9ae456c401c05da531e1156
parent303aac14a89cb6b6dc312b3bbadfb6c0051694d9 (diff)
Optimise applyCW
-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 {