Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

StoneBoard Class Reference

Tracks played stone information. More...

#include <StoneBoard.hpp>

List of all members.

Public Member Functions

 StoneBoard ()
 Constructs an unusable board.
 StoneBoard (unsigned size)
 Constructs a square board.
 StoneBoard (unsigned width, unsigned height)
 Constructs a rectangular board.
 StoneBoard (unsigned width, unsigned height, const std::string &str)
 Contructs a rectangular board with given state.
 ~StoneBoard ()
const ConstBoardConst () const
 Returns reference to ConstBoard.
int Width () const
 Same as Const().width().
int Height () const
 Same as Const().height().
hash_t Hash () const
 Returns zobrist hash for the current position, which depends only on the played cells; unplayed cells do not contribute to the hash.
hash_t Hash (HexColor toPlay) const
 Returns the hash of the position taking the color to play into account.
BoardID GetBoardID () const
 Returns BoardID for the current board state, looking only at the played cells.
std::string GetBoardIDString () const
 Returns BoardID as a string.
std::size_t NumStones () const
 Number of played stones on the interior of the board.
HexColor WhoseTurn () const
 Computes whose turn it is on the given board; IsStandardPosition() must be true to use this method.
bool IsStandardPosition () const
 Returns true if position is attainable in a normal game.
std::string Write () const
 Returns a string representation of the board.
std::string Write (const bitset_t &b) const
 Returns a string representation of the board with the cells marked in the given bitset denoted by a '*'.
void SetPosition (const BoardID &id)
 Sets the board to the state encoded by id.
void SetPosition (const StoneBoard &brd)
 Copies state of brd into this board.
void SetPositionOnlyPlayed (const StoneBoard &brd)
 Copies only the played state of brd.
void SetPosition (const std::string &str)
 Sets state from string.
Methods defined on cells and edges.

All methods accept and return edge and interior cells only.

Note that PlayMove() (which is not in this section) can play a move like (BLACK, RESIGN), but getBlack() (which is in this section) will not return a bitset with the RESIGN move set.

Todo:
Why did we agree on this? :)
bitset_t GetBlack () const
 Returns the set of BLACK stones.
bitset_t GetWhite () const
 Returns the set of WHITE stones.
bitset_t GetColor (HexColor color) const
 Returns color's stones.
bitset_t GetEmpty () const
 Returns all empty cells.
bitset_t GetOccupied () const
 Returns all occupied (not empty) cells.
bool IsColor (HexPoint cell, HexColor color) const
 Returns true if cell is of color.
bool IsEmpty (HexPoint cell) const
 Returns true if cell is empty.
bool IsOccupied (HexPoint cell) const
 Returns true if cell is occupied (not empty).
Methods defined on all valid moves.

bool IsBlack (HexPoint cell) const
 Returns true if cell is BLACK.
bool IsWhite (HexPoint cell) const
 Returns true if cell is WHITE.
HexColor GetColor (HexPoint cell) const
 Retruns color of cell.
bitset_t GetPlayed () const
 Returns the set of played cells.
bitset_t GetPlayed (HexColor color) const
bitset_t GetLegal () const
 Returns the set of all legal moves; ie, moves that can be played from this state.
bool IsPlayed (HexPoint cell) const
 Returns true if cell has been played.
bool IsLegal (HexPoint cell) const
 Returns true if cell is a legal move.
Methods not modifying Hash() or BoardID()

void AddColor (HexColor color, const bitset_t &b)
 Adds the cells in b as stones of color.
void RemoveColor (HexColor color, const bitset_t &b)
 Sets cells in b to EMPTY.
void SetColor (HexColor color, HexPoint cell)
 Sets color of cell.
void SetColor (HexColor color, const bitset_t &bs)
 Sets color of cells in bitset.
bool IsSelfRotation () const
 Returns true if rotating the board returns the same board.
Methods modifying Hash() and BoardID()

void StartNewGame ()
 Clears the board and plays the edge stones.
void SetPlayed (const bitset_t &p)
 Sets the played stones.
void PlayMove (HexColor color, HexPoint cell)
 Plays a move of the given color to the board.
void UndoMove (HexPoint cell)
 Removes the move from the board, setting cell to EMPTY.
void RotateBoard ()
 Rotates the board by 180' about the center.
void MirrorBoard ()
 Mirrors the board in the diagonal joining acute corners.
Operators

bool operator== (const StoneBoard &other) const
 Two boards are equal if their dimensions match and their sets of black, white, and played stones are all equal.
bool operator!= (const StoneBoard &other) const
 Returns true if the boards differ.
Iterators

BoardIterator Stones (HexColorSet colorset) const
 Returns iterator over all stones in colorset.
BoardIterator Stones (HexColor color) const
 Returns iterator over all stones of color.

Private Member Functions

void ComputeHash ()
void MarkAsDirty ()
bool IsBlackWhiteDisjoint ()

Private Attributes

ConstBoardm_const
bitset_t m_played
bitset_t m_stones [BLACK_AND_WHITE]
ZobristHash m_hash
bool m_stones_calculated
std::vector< HexPointm_stones_list [NUM_COLOR_SETS]

Detailed Description

Tracks played stone information.

Each cell on the board is assigned a HexColor, and so every cell is either EMPTY, BLACK, or WHITE.

Each cell is also marked as 'played' or 'unplayed'. A cell should be marked as played when it corresponds to an actual move played in a game; that is, not a fill-in move. This means it is possible for a cell to be BLACK or WHITE and not played. Played stones contribute to the board hash and id, unplayed stones do not.

See also:
Hash(), GetBoardID(), setPlayed().

Definition at line 35 of file StoneBoard.hpp.


Constructor & Destructor Documentation

StoneBoard::StoneBoard (  ) 

Constructs an unusable board.

This is to allow StoneBoard to appear in a stl container.

Definition at line 13 of file StoneBoard.cpp.

StoneBoard::StoneBoard ( unsigned  size  )  [explicit]

Constructs a square board.

Definition at line 19 of file StoneBoard.cpp.

References StartNewGame().

StoneBoard::StoneBoard ( unsigned  width,
unsigned  height 
)

Constructs a rectangular board.

Definition at line 26 of file StoneBoard.cpp.

References StartNewGame().

StoneBoard::StoneBoard ( unsigned  width,
unsigned  height,
const std::string &  str 
)

Contructs a rectangular board with given state.

See SetPosition(std::string)

Definition at line 33 of file StoneBoard.cpp.

References SetPosition().

StoneBoard::~StoneBoard (  ) 

Definition at line 40 of file StoneBoard.cpp.


Member Function Documentation

void StoneBoard::AddColor ( HexColor  color,
const bitset_t b 
)
void StoneBoard::ComputeHash (  )  [private]
const ConstBoard & StoneBoard::Const (  )  const [inline]
bitset_t StoneBoard::GetBlack (  )  const [inline]
BoardID StoneBoard::GetBoardID (  )  const

Returns BoardID for the current board state, looking only at the played cells.

Packs each interior cell into 2 bits.

Note:
Assumes all valid HexColors lie between [0,2].
When this code was written, the cells were iterated over in the order (a1, b1, c1, ..., a2, b2, c2, ... , etc). Any changes to the order in Interior() will break all existing databases that use BoardID as a lookup, unless this method is updated to always compute in the above order.

Definition at line 238 of file StoneBoard.cpp.

References BoardID, Const(), EMPTY, GetColor(), GetPlayed(), Height(), benzene_bitset< _Nb >::test(), and Width().

Referenced by GetBoardIDString().

std::string StoneBoard::GetBoardIDString (  )  const

Returns BoardID as a string.

Todo:
Moves this out of here?

Definition at line 266 of file StoneBoard.cpp.

References BoardID, byte, and GetBoardID().

Referenced by HexHtpEngine::CmdBoardID().

HexColor StoneBoard::GetColor ( HexPoint  cell  )  const

Retruns color of cell.

Definition at line 46 of file StoneBoard.cpp.

References BLACK, Const(), EMPTY, HexAssert, IsBlack(), IsWhite(), and WHITE.

bitset_t StoneBoard::GetColor ( HexColor  color  )  const [inline]
bitset_t StoneBoard::GetEmpty (  )  const [inline]
bitset_t StoneBoard::GetLegal (  )  const

Returns the set of all legal moves; ie, moves that can be played from this state.

Definition at line 54 of file StoneBoard.cpp.

References Const(), FIRST_TO_PLAY, ConstBoard::GetCells(), GetColor(), GetPlayed(), HexAssert, IsPlayed(), RESIGN, benzene_bitset< _Nb >::set(), and SWAP_PIECES.

Referenced by HexHtpEngine::CmdAllLegalMoves(), and IsLegal().

bitset_t StoneBoard::GetOccupied (  )  const [inline]

Returns all occupied (not empty) cells.

Definition at line 364 of file StoneBoard.hpp.

References Const(), GetBlack(), ConstBoard::GetLocations(), and GetWhite().

Referenced by GetEmpty(), NumStones(), and VCUtils::ValidEdgeBridge().

bitset_t StoneBoard::GetPlayed ( HexColor  color  )  const [inline]

Definition at line 405 of file StoneBoard.hpp.

References GetColor(), HexAssert, HexColorUtil::isBlackWhite(), and m_played.

bitset_t StoneBoard::GetPlayed (  )  const [inline]
bitset_t StoneBoard::GetWhite (  )  const [inline]
hash_t StoneBoard::Hash ( HexColor  toPlay  )  const [inline]

Returns the hash of the position taking the color to play into account.

This will be different than the hash returned by Hash().

Definition at line 337 of file StoneBoard.hpp.

References ZobristHash::Hash(), and m_hash.

hash_t StoneBoard::Hash (  )  const [inline]

Returns zobrist hash for the current position, which depends only on the played cells; unplayed cells do not contribute to the hash.

Changing the color of an unplayed cell does not change the hash for the position. Methods that change the color of played cells internally will always compute a new hash automatically.

Definition at line 332 of file StoneBoard.hpp.

References EMPTY, ZobristHash::Hash(), and m_hash.

Referenced by ICEngine::ComputeInferiorCells(), HexState::Hash(), HexAbSearch::SearchState(), ProofUtil::StoreTranspositions(), and Write().

int StoneBoard::Height (  )  const [inline]
bool StoneBoard::IsBlack ( HexPoint  cell  )  const [inline]

Returns true if cell is BLACK.

Definition at line 369 of file StoneBoard.hpp.

References BLACK, Const(), HexAssert, m_stones, and benzene_bitset< _Nb >::test().

Referenced by GetColor(), IsOccupied(), HandicapPlayer::threeToOne(), and Write().

bool StoneBoard::IsBlackWhiteDisjoint (  )  [private]

Definition at line 418 of file StoneBoard.cpp.

References BLACK, LogWarning(), m_stones, HexPointUtil::ToString(), and WHITE.

Referenced by AddColor(), RemoveColor(), and SetColor().

bool StoneBoard::IsColor ( HexPoint  cell,
HexColor  color 
) const [inline]

Returns true if cell is of color.

Definition at line 381 of file StoneBoard.hpp.

References Const(), HexAssert, HexColorUtil::isBlackWhite(), m_stones, and benzene_bitset< _Nb >::test().

bool StoneBoard::IsEmpty ( HexPoint  cell  )  const [inline]

Returns true if cell is empty.

Definition at line 388 of file StoneBoard.hpp.

References Const(), HexAssert, and IsOccupied().

Referenced by HexUctPolicy::GenerateMove(), HexUctPolicy::GenerateRandomMove(), and HexUctPolicy::GenerateResponseMove().

bool StoneBoard::IsLegal ( HexPoint  cell  )  const

Returns true if cell is a legal move.

Definition at line 77 of file StoneBoard.cpp.

References Const(), GetLegal(), HexAssert, and benzene_bitset< _Nb >::test().

Referenced by BookUtil::Value().

bool StoneBoard::IsOccupied ( HexPoint  cell  )  const [inline]
bool StoneBoard::IsPlayed ( HexPoint  cell  )  const [inline]
bool StoneBoard::IsSelfRotation (  )  const

Returns true if rotating the board returns the same board.

Definition at line 218 of file StoneBoard.cpp.

References BLACK, Const(), m_stones, BoardUtils::Rotate(), and WHITE.

bool StoneBoard::IsStandardPosition (  )  const [inline]

Returns true if position is attainable in a normal game.

That is, FIRST_TO_PLAY went first, and the colors alternated afterwards.

Definition at line 444 of file StoneBoard.hpp.

References BLACK, benzene_bitset< _Nb >::count(), GetPlayed(), and WHITE.

Referenced by WhoseTurn().

bool StoneBoard::IsWhite ( HexPoint  cell  )  const [inline]

Returns true if cell is WHITE.

Definition at line 375 of file StoneBoard.hpp.

References Const(), HexAssert, m_stones, benzene_bitset< _Nb >::test(), and WHITE.

Referenced by GetColor(), IsOccupied(), HandicapPlayer::threeToOne(), and Write().

void StoneBoard::MarkAsDirty (  )  [private]
void StoneBoard::MirrorBoard (  ) 

Mirrors the board in the diagonal joining acute corners.

Note that this method requires the board to be square. Hash is updated.

Definition at line 227 of file StoneBoard.cpp.

References ComputeHash(), Const(), m_played, m_stones, MarkAsDirty(), and BoardUtils::Mirror().

std::size_t StoneBoard::NumStones (  )  const [inline]

Number of played stones on the interior of the board.

Similar to:

            num bits = (GetOccupied() & GetPlayed() & GetCells()).count();

Definition at line 417 of file StoneBoard.hpp.

References Const(), GetOccupied(), and GetPlayed().

Referenced by SolverDB< HASH, DB, DATA >::Get(), SwapCheck::PlaySwap(), and SolverDB< HASH, DB, DATA >::Put().

bool StoneBoard::operator!= ( const StoneBoard other  )  const [inline]

Returns true if the boards differ.

See operator==().

Definition at line 430 of file StoneBoard.hpp.

References operator==().

bool StoneBoard::operator== ( const StoneBoard other  )  const [inline]

Two boards are equal if their dimensions match and their sets of black, white, and played stones are all equal.

Definition at line 422 of file StoneBoard.hpp.

References BLACK, m_const, m_played, m_stones, and WHITE.

Referenced by operator!=().

void StoneBoard::PlayMove ( HexColor  color,
HexPoint  cell 
)

Plays a move of the given color to the board.

Adds cell to the set of played stones. Updates the board hash.

Parameters:
color must BLACK or WHITE
cell Any valid move, include RESIGN and SWAP_PIECES.

Definition at line 180 of file StoneBoard.cpp.

References Const(), HexAssert, HexColorUtil::isBlackWhite(), m_hash, m_played, MarkAsDirty(), benzene_bitset< _Nb >::set(), SetColor(), and ZobristHash::Update().

Referenced by ICEngine::BackupOpponentDead(), DfsSolver::OrderMoves(), HexState::PlayMove(), HexBoard::PlayMove(), Game::PlayMove(), GameUtil::SequenceFromPosition(), SetPosition(), HexSgUtil::SetPositionInBoard(), StartNewGame(), and ProofUtil::StoreTranspositions().

void StoneBoard::RemoveColor ( HexColor  color,
const bitset_t b 
)

Sets cells in b to EMPTY.

Does not modify hash.

Definition at line 115 of file StoneBoard.cpp.

References HexAssert, HexColorUtil::isBlackWhite(), IsBlackWhiteDisjoint(), m_stones, and MarkAsDirty().

void StoneBoard::RotateBoard (  ) 

Rotates the board by 180' about the center.

Hash is updated.

Definition at line 209 of file StoneBoard.cpp.

References ComputeHash(), Const(), m_played, m_stones, MarkAsDirty(), and BoardUtils::Rotate().

Referenced by _BEGIN_BENZENE_NAMESPACE_::GetHash().

void StoneBoard::SetColor ( HexColor  color,
const bitset_t bs 
)

Sets color of cells in bitset.

Does not modify hash.

Todo:
Should we make this support EMPTY color too?

Definition at line 139 of file StoneBoard.cpp.

References Const(), HexAssert, HexColorUtil::isBlackWhite(), IsBlackWhiteDisjoint(), m_stones, and MarkAsDirty().

void StoneBoard::SetColor ( HexColor  color,
HexPoint  cell 
)
void StoneBoard::SetPlayed ( const bitset_t p  ) 

Sets the played stones.

These stones, and only these stones, will contribute to the board hash and board id. Hash is recomputed.

See also:
Hash().

Definition at line 151 of file StoneBoard.cpp.

References ComputeHash(), m_played, and MarkAsDirty().

Referenced by SetPosition(), SetPositionOnlyPlayed(), and MoHexPlayer::TryReuseSubtree().

void StoneBoard::SetPosition ( const std::string &  str  ) 

Sets state from string.

String must contain wxh non space characters. Any spacing is allowed betwen such characters. A '.' is an empty cell, 'B' and 'W' are played black and white stones, 'b' and 'w' are filled-in black and white stones.

Note:
This depends on the order defined by Interior().

Definition at line 326 of file StoneBoard.cpp.

References BLACK, ComputeHash(), HexPointUtil::coordsToPoint(), Height(), MarkAsDirty(), PlayMove(), SetColor(), StartNewGame(), WHITE, and Width().

void StoneBoard::SetPosition ( const StoneBoard brd  ) 

Copies state of brd into this board.

Definition at line 280 of file StoneBoard.cpp.

References BLACK, GetBlack(), GetPlayed(), GetWhite(), SetColor(), SetPlayed(), StartNewGame(), and WHITE.

void StoneBoard::SetPosition ( const BoardID id  ) 

Sets the board to the state encoded by id.

Note this state will have no unplayed stones, so the code:

            brd.SetPosition(brd.GetBoardID());

will remove all unplayed stones.

Note:
This depends on the order defined by Interior(). See note in implementation of GetBoardID().

Definition at line 296 of file StoneBoard.cpp.

References BLACK, byte, ComputeHash(), Const(), Height(), HexAssert, MarkAsDirty(), PlayMove(), StartNewGame(), WHITE, and Width().

Referenced by BookBuilder< PLAYER >::GenerateMoves(), PlayAndSolve::GenMove(), BenzenePlayer::InitSearch(), DfpnSolver::MID(), PlayAndSolve::SolverThread::operator()(), PlayAndSolve::PlayerThread::operator()(), BookBuilder< PLAYER >::Worker::operator()(), HexBoard::PopHistory(), PerfectPlayer::Search(), and StoneBoard().

void StoneBoard::SetPositionOnlyPlayed ( const StoneBoard brd  ) 

Copies only the played state of brd.

Definition at line 288 of file StoneBoard.cpp.

References BLACK, GetBlack(), GetPlayed(), GetWhite(), SetColor(), SetPlayed(), StartNewGame(), and WHITE.

void StoneBoard::StartNewGame (  ) 
BoardIterator StoneBoard::Stones ( HexColor  color  )  const [inline]

Returns iterator over all stones of color.

Definition at line 450 of file StoneBoard.hpp.

References HexColorSetUtil::Only(), and Stones().

BoardIterator StoneBoard::Stones ( HexColorSet  colorset  )  const

Returns iterator over all stones in colorset.

Definition at line 83 of file StoneBoard.cpp.

References Const(), GetColor(), HexColorSetUtil::InSet(), INVALID_POINT, m_stones_calculated, m_stones_list, and NUM_COLOR_SETS.

Referenced by VCSetUtil::ConnectedTo(), VCBuilder::MergeAndShrink(), HexUctUtil::SaveTree(), and Stones().

void StoneBoard::UndoMove ( HexPoint  cell  ) 

Removes the move from the board, setting cell to EMPTY.

Hash is updated.

Definition at line 193 of file StoneBoard.cpp.

References Const(), EMPTY, GetColor(), HexAssert, m_hash, m_played, MarkAsDirty(), benzene_bitset< _Nb >::reset(), SetColor(), and ZobristHash::Update().

Referenced by DfsSolver::OrderMoves(), HexState::UndoMove(), and Game::UndoMove().

HexColor StoneBoard::WhoseTurn (  )  const [inline]
int StoneBoard::Width (  )  const [inline]
std::string StoneBoard::Write ( const bitset_t b  )  const

Returns a string representation of the board with the cells marked in the given bitset denoted by a '*'.

Definition at line 372 of file StoneBoard.cpp.

References HexPointUtil::coordsToPoint(), Hash(), Height(), IsBlack(), IsPlayed(), IsWhite(), benzene_bitset< _Nb >::test(), HexColorUtil::toString(), and Width().

std::string StoneBoard::Write (  )  const

Returns a string representation of the board.

Definition at line 367 of file StoneBoard.cpp.

References EMPTY_BITSET.

Referenced by DfsCommands::CmdSolverFindWinning(), HexBoard::HandleVCDecomposition(), operator<<(), and HexBoard::Write().


Member Data Documentation

Definition at line 297 of file StoneBoard.hpp.

Referenced by Const(), Height(), operator==(), and Width().

See also:
Hash()

Definition at line 304 of file StoneBoard.hpp.

Referenced by ComputeHash(), Hash(), PlayMove(), and UndoMove().

bool StoneBoard::m_stones_calculated [mutable, private]

Definition at line 306 of file StoneBoard.hpp.

Referenced by MarkAsDirty(), and Stones().

std::vector<HexPoint> StoneBoard::m_stones_list[NUM_COLOR_SETS] [mutable, private]

Definition at line 308 of file StoneBoard.hpp.

Referenced by Stones().


The documentation for this class was generated from the following files:


6 Jan 2011 Doxygen 1.6.3