diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2018-07-03 20:53:55 +0200 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2018-07-03 21:03:40 +0200 |
commit | dc2c8a680417e395ddbf7c4684ef646d36ffdf1a (patch) | |
tree | eddf6737d959d553d9ae456c401c05da531e1156 | |
parent | 303aac14a89cb6b6dc312b3bbadfb6c0051694d9 (diff) |
Optimise applyCW
-rw-r--r-- | board.cpp | 30 |
1 files changed, 27 insertions, 3 deletions
@@ -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 { |