From 3e480751083691d9d9ba417f7641a7c6b47658b0 Mon Sep 17 00:00:00 2001
From: Paolo Capriotti
Date: Tue, 24 Jul 2007 23:51:50 +0200
Subject: [PATCH] Fixed pool equality.
---
src/variants/crazyhouse.cpp | 49 +++++++++++++++++++++++---
src/variants/crazyhouse_p.h | 18 +++++-----
src/variants/shogi.cpp | 2 +-
src/variants/xchess/pool.h | 84 ++++++++++++++++++++++++++++++++++++++++++++-
4 files changed, 137 insertions(+), 16 deletions(-)
diff --git a/src/variants/crazyhouse.cpp b/src/variants/crazyhouse.cpp
index 57a0caf..ad5a1c9 100644
--- a/src/variants/crazyhouse.cpp
+++ b/src/variants/crazyhouse.cpp
@@ -130,7 +130,7 @@ CrazyhousePosition::PoolReference CrazyhousePosition::pool(int index) {
CrazyhousePosition::PoolConstReference CrazyhousePosition::pool(int index) const {
Color c = static_cast(index);
- return PoolConstReference(&m_pool.find(c)->second, c);
+ return PoolConstReference(&m_pool[c], c);
}
CrazyhousePosition::PlayerPool& CrazyhousePosition::rawPool(Piece::Color color) {
@@ -138,7 +138,7 @@ CrazyhousePosition::PlayerPool& CrazyhousePosition::rawPool(Piece::Color color)
}
const CrazyhousePosition::PlayerPool& CrazyhousePosition::rawPool(Piece::Color color) const {
- return const_cast(m_pool)[color];
+ return m_pool[color];
}
bool CrazyhousePosition::pseudolegal(Move& move) const {
@@ -209,7 +209,7 @@ CrazyhouseMove CrazyhousePosition::getMove(const AlgebraicNotation& san, bool& o
if (san.drop) {
Piece piece(m_turn, (PieceType)san.type);
- if ( !m_pool.count(piece.color()) || !m_pool.find(piece.color())->second.count(piece.type()) ) {
+ if (!m_pool[piece.color()].count(piece.type())) {
ok = false;
return CrazyhouseMove::invalid();
}
@@ -231,7 +231,48 @@ void CrazyhousePosition::dump() const {
}
bool CrazyhousePosition::operator==(const CrazyhousePosition& pos) const {
- return m_pool == pos.m_pool && Base::operator==(pos);
+ bool base = Base::operator==(pos);
+ std::cout << "crazyhouse chessboards equals: " << base << std::endl;
+ bool pools = m_pool == pos.m_pool;
+ std::cout << "crazyhouse pools equals: " << pools << std::endl;
+ if (!pools) {
+ // dump pools
+ {
+ const PlayerPool& pp = rawPool(WHITE);
+ for (PlayerPool::const_iterator it = pp.begin();
+ it != pp.end(); ++it) {
+ std::cout << ChessPiece::typeSymbol(it->first) << ":" << it->second << " ";
+ }
+ }
+ std::cout << " | ";
+ {
+ const PlayerPool& pp = rawPool(BLACK);
+ for (PlayerPool::const_iterator it = pp.begin();
+ it != pp.end(); ++it) {
+ std::cout << ChessPiece::typeSymbol(it->first) << ":" << it->second << " ";
+ }
+ }
+ std::cout << std::endl;
+
+ // dump pools
+ {
+ const PlayerPool& pp = pos.rawPool(WHITE);
+ for (PlayerPool::const_iterator it = pp.begin();
+ it != pp.end(); ++it) {
+ std::cout << ChessPiece::typeSymbol(it->first) << ":" << it->second << " ";
+ }
+ }
+ std::cout << " | ";
+ {
+ const PlayerPool& pp = pos.rawPool(BLACK);
+ for (PlayerPool::const_iterator it = pp.begin();
+ it != pp.end(); ++it) {
+ std::cout << ChessPiece::typeSymbol(it->first) << ":" << it->second << " ";
+ }
+ }
+ std::cout << std::endl;
+ }
+ return pools && base;
}
//END CrazyhousePosition
diff --git a/src/variants/crazyhouse_p.h b/src/variants/crazyhouse_p.h
index 89fa594..5ced66d 100644
--- a/src/variants/crazyhouse_p.h
+++ b/src/variants/crazyhouse_p.h
@@ -130,16 +130,14 @@ public:
private:
void generateDrops() {
- if(m_pos.rawPool().count(m_pos.turn())) {
- const CrazyhousePosition::PlayerPool& pp = m_pos.rawPool().find(m_pos.turn())->second;
- for (CrazyhousePosition::PlayerPool::const_iterator it = pp.begin();
- it != pp.end(); ++it) {
- for (Point to = m_pos.first();
- to <= m_pos.last();
- to = m_pos.next(to)) {
- CrazyhouseMove move(CrazyhousePiece(m_pos.turn(),it->first), to);
- if (m_test(move)) m_moves.push_back(move);
- }
+ const CrazyhousePosition::PlayerPool& pp = m_pos.rawPool()[m_pos.turn()];
+ for (CrazyhousePosition::PlayerPool::const_iterator it = pp.begin();
+ it != pp.end(); ++it) {
+ for (Point to = m_pos.first();
+ to <= m_pos.last();
+ to = m_pos.next(to)) {
+ CrazyhouseMove move(CrazyhousePiece(m_pos.turn(),it->first), to);
+ if (m_test(move)) m_moves.push_back(move);
}
}
}
diff --git a/src/variants/shogi.cpp b/src/variants/shogi.cpp
index 788bc2b..11ccd46 100644
--- a/src/variants/shogi.cpp
+++ b/src/variants/shogi.cpp
@@ -446,7 +446,7 @@ ShogiPosition::PoolReference ShogiPosition::pool(int index) {
ShogiPosition::PoolConstReference ShogiPosition::pool(int index) const {
ShogiPiece::Color color = static_cast(index);
- return PoolConstReference(&m_pool.find(color)->second, color);
+ return PoolConstReference(&m_pool[color], color);
}
bool ShogiPosition::operator==(const ShogiPosition& p) const {
diff --git a/src/variants/xchess/pool.h b/src/variants/xchess/pool.h
index cd07870..0d831e5 100644
--- a/src/variants/xchess/pool.h
+++ b/src/variants/xchess/pool.h
@@ -8,6 +8,69 @@ struct PlayerPoolType {
typedef std::map type;
};
+namespace MapUtilities {
+
+template
+struct MapEqual {
+ static bool apply(const T& a, const T& b) {
+ return a == b;
+ }
+};
+
+template
+struct MapEqual > {
+ typedef std::map Map;
+ static bool apply(const Map& a, const Map& b) {
+ typename Map::const_iterator i = a.begin();
+ typename Map::const_iterator j = b.begin();
+
+ while (i != a.end() && j != b.end()) {
+ if (i->first < j->first) {
+ if (!MapEqual::apply(i->second, U()))
+ return false;
+ else
+ ++i;
+ }
+ else if (i->first > j->first) {
+ if (!MapEqual::apply(j->second, U()))
+ return false;
+ else
+ ++j;
+ }
+ else {
+ // same key, compare values
+ if (!MapEqual::apply(i->second, j->second))
+ return false;
+ else {
+ ++i;
+ ++j;
+ }
+ }
+ }
+
+ // check tail
+ while (i != a.end()) {
+ if (!MapEqual::apply(i->second, U()))
+ return false;
+ ++i;
+ }
+ while (j != b.end()) {
+ if (!MapEqual::apply(j->second, U()))
+ return false;
+ ++j;
+ }
+
+ return true;
+ }
+};
+
+template
+bool equal(const T& a, const T& b) {
+ return MapEqual::apply(a, b);
+}
+
+}
+
template
class PoolReferenceBase {
protected:
@@ -15,7 +78,26 @@ protected:
typedef typename Piece::Color Color;
typedef typename Piece::Type Type;
public:
- typedef std::map Pool;
+ class Pool {
+ typedef std::map Map;
+ Map m_map;
+ public:
+ Pool() { }
+
+ PP& operator[](Color color) { return m_map[color]; }
+ const PP& operator[](Color color) const { return const_cast