From dc2c8a680417e395ddbf7c4684ef646d36ffdf1a Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Tue, 3 Jul 2018 20:53:55 +0200 Subject: Optimise applyCW --- board.cpp | 30 +++++++++++++++++++++++++++--- 1 file 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 { -- cgit v1.2.3-70-g09d2