summaryrefslogtreecommitdiff
path: root/board.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'board.cpp')
-rw-r--r--board.cpp53
1 files changed, 26 insertions, 27 deletions
diff --git a/board.cpp b/board.cpp
index 822fddd..b3820a0 100644
--- a/board.cpp
+++ b/board.cpp
@@ -1,5 +1,4 @@
#include <cstdlib>
-#include <cstring>
#include <cassert>
#include "board.h"
#include "util.h"
@@ -65,7 +64,9 @@ Board Board::makeEmpty() {
Board bd;
bd.bag = Bag::makeFull();
- memset(bd.bd, 0, BSZ * BSZ);
+ bd.bd.fill(0);
+ bd.edgeCells.reserve(BSZ * BSZ);
+ bd.inEdgeCells.fill(-1);
return bd;
}
@@ -84,15 +85,29 @@ int Board::countStones(uint8_t clr, int idx, int delta) const {
void Board::newEdgeCand(int idx) {
// cerr << "(newEdgeCand(" << Idx(idx) << "))" << endl;
- if (!inEdgeCands.test(idx)) {
- edgeCands.push_back(idx);
- inEdgeCands.set(idx);
+ if (inEdgeCells[idx] == -1 && bd[idx] == 0 && checkEdge(idx)) {
+ inEdgeCells[idx] = edgeCells.size();
+ edgeCells.push_back(idx);
+ }
+}
+
+void Board::removeEdgeCell(int idx) {
+ // cerr << "(removeEdgeCell(" << Idx(idx) << "))" << endl;
+ int pos = inEdgeCells[idx];
+ if (pos != -1) {
+ inEdgeCells[idx] = -1;
+ if ((unsigned)pos < edgeCells.size() - 1) {
+ int val = edgeCells[pos] = edgeCells.back();
+ inEdgeCells[val] = pos;
+ }
+ edgeCells.pop_back();
}
}
void Board::put(int idx, uint8_t clr) {
bd[idx] = clr;
+ removeEdgeCell(idx);
newEdgeCand(idx - 1);
newEdgeCand(idx + 1);
newEdgeCand(idx - BSZ);
@@ -103,6 +118,10 @@ void Board::undo(int idx) {
bd[idx] = 0;
newEdgeCand(idx);
+ removeEdgeCell(idx - 1);
+ removeEdgeCell(idx + 1);
+ removeEdgeCell(idx - BSZ);
+ removeEdgeCell(idx + BSZ);
}
uint8_t Board::putCW(int idx, uint8_t clr) {
@@ -134,28 +153,8 @@ bool Board::checkEdge(int idx) const {
return bd[idx - 1] || bd[idx + 1] || bd[idx - BSZ] || bd[idx + BSZ];
}
-void Board::forEachMove(const function<void(int idx)> &f) {
- vector<int> eC = edgeCands;
- bitset<BSZ * BSZ> iEC = inEdgeCands;
-
- size_t j = 0;
- for (size_t i = 0; i < eC.size(); i++) {
- int idx = eC[i];
- // cerr << "(fEM: candidate " << Idx(idx) << "; ";
- if (bd[idx] == 0 && checkEdge(idx)) {
- // cerr << "edge)" << endl;
- f(idx);
- eC[j++] = idx;
- } else {
- // cerr << "-)" << endl;
- iEC.reset(idx);
- }
- }
-
- eC.resize(j);
-
- swap(edgeCands, eC);
- swap(inEdgeCands, iEC);
+const vector<int>& Board::getEdgeCells() const {
+ return edgeCells;
}
Bounds Board::computeBounds() const {