Game state and move history including variations. More...
#include <GoGame.h>
Public Member Functions | |
GoGame (int boardSize=GO_DEFAULT_SIZE) | |
Create a game record for replaying games on the given board. | |
~GoGame () | |
void | Init (SgNode *root) |
Init from an existing game tree. | |
void | Init (int size, const GoRules &rules) |
Delete the old game record and start with a fresh one. | |
const GoBoard & | Board () const |
Get the board associated with this game record. | |
const SgNode & | Root () const |
Return the root of this tree. | |
const GoTimeSettings & | TimeSettings () const |
void | PlaceHandicap (const SgVector< SgPoint > &stones) |
Set handicap stones at explicitely given points. | |
void | SetupPosition (const SgBWArray< SgPointSet > &stones) |
Set up a position on the board. | |
void | AddMove (SgMove move, SgBlackWhite player, const SgSearchStatistics *stat=0, bool makeMainVariation=true) |
Add move to the game record and make it the current node. | |
void | AddComment (const std::string &comment) |
Add a comment to the current node. | |
void | AddComment (const SgNode &node, const std::string &comment) |
Add a comment to any node. | |
const SgNode & | AddResignNode (SgBlackWhite player) |
Add a node with a comment that a player resigned. | |
void | AppendChild (SgNode *child) |
Append a node as a new child to the current node. | |
void | GoToNode (const SgNode *dest) |
Play to the given node. | |
void | GoInDirection (SgNode::Direction dir) |
Play to the next node in the given direction. | |
bool | CanGoInDirection (SgNode::Direction dir) const |
Return whether there is a next node in the given direction. | |
void | SetToPlay (SgBlackWhite toPlay) |
Set the current player. | |
bool | EndOfGame () const |
Return whether the game is finished. | |
SgTimeRecord & | Time () |
Deprecated. | |
const SgTimeRecord & | Time () const |
The time left in the game at the current position. | |
const SgNode * | CurrentNode () const |
Return the current position in the tree. | |
SgMove | CurrentMove () const |
Return the move of the current node. | |
int | CurrentMoveNumber () const |
Get the number of moves since root or last node with setup properties. | |
Query or change game info properties | |
void | SetKomiGlobal (GoKomi komi) |
Set komi property in the root node and delete all komi properties in the tree. | |
void | SetTimeSettingsGlobal (const GoTimeSettings &timeSettings, double overhead=0) |
Set time settings properties in the root node and delete all such properties in the rest of the tree. | |
std::string | GetPlayerName (SgBlackWhite player) const |
Get the player name. | |
void | UpdatePlayerName (SgBlackWhite player, const std::string &name) |
Set the player name at root node or most recent node with this property. | |
void | UpdateDate (const std::string &date) |
Set the date at root node or most recent node with this property. | |
std::string | GetResult () const |
Get the game result. | |
void | UpdateResult (const std::string &result) |
Set the result at root node or most recent node with this property. | |
std::string | GetGameName () const |
Get the game name. | |
void | UpdateGameName (const std::string &name) |
Set the game name at root node or most recent node with this property. | |
void | SetRulesGlobal (const GoRules &rules) |
Private Member Functions | |
GoGame (const GoGame &) | |
Not implemented. | |
GoGame & | operator= (const GoGame &) |
Not implemented. | |
std::string | GetGameInfoStringProp (SgPropID id) const |
Find the game info node with a game info property that determines this property for the current node. | |
void | InitHandicap (const GoRules &rules, SgNode *root) |
SgNode * | NonConstNodePtr (const SgNode *node) const |
Convert a const reference of a node received by the user of this class to a non-const reference. | |
SgNode & | NonConstNodeRef (const SgNode &node) const |
See NonConstNodePtr(). | |
void | UpdateGameInfoStringProp (SgPropID id, const std::string &value) |
Private Attributes | |
GoBoard | m_board |
SgNode * | m_root |
The root node of the current tree. | |
SgNode * | m_current |
The position in the current tree. | |
GoBoardUpdater | m_updater |
GoTimeSettings | m_timeSettings |
SgTimeRecord | m_time |
A record of the clock settings and time left. | |
int | m_numMovesToInsert |
Moves inserted into a line of play instead of added at the end. |
Game state and move history including variations.
Contains a game tree, a pointer to a current node, a board and information of the time left. The current node is always a valid node of the tree and the board and time records reflect the game state at the current node.
Remove non-const function Time() and decouple time measurement from tracking the time left at a node. This class should not have to deal with time measurement, instead add a time parameter to AddMove() that informs the game about the time that needs to be subtracted from the time left.
Also ensure in GoGame that the time settings and time left records always reflect the state at the current node. This should be implemented in GoBoardUpdater::Update(), which is used by GoGame.
Definition at line 33 of file GoGame.h.
GoGame::GoGame | ( | int | boardSize = GO_DEFAULT_SIZE |
) | [explicit] |
Create a game record for replaying games on the given board.
Definition at line 57 of file GoGame.cpp.
References GoInitCheck(), and Init().
GoGame::~GoGame | ( | ) |
Definition at line 69 of file GoGame.cpp.
References SgNode::DeleteTree(), and m_root.
GoGame::GoGame | ( | const GoGame & | ) | [private] |
Not implemented.
void GoGame::AddComment | ( | const std::string & | comment | ) |
Add a comment to the current node.
Definition at line 222 of file GoGame.h.
References SgNode::AddComment(), and m_current.
Referenced by GoGtpEngine::CmdGenMove().
void GoGame::AddComment | ( | const SgNode & | node, | |
const std::string & | comment | |||
) |
Add a comment to any node.
Definition at line 77 of file GoGame.cpp.
References SgNode::AddComment(), and NonConstNodeRef().
void GoGame::AddMove | ( | SgMove | move, | |
SgBlackWhite | player, | |||
const SgSearchStatistics * | stat = 0 , |
|||
bool | makeMainVariation = true | |||
) |
Add move to the game record and make it the current node.
Add move as the next move at the current position. If a node with that move already exists, then don't add a new one. Return the node with that move. Also add any statistics from 'stat' and time left to that node.
Definition at line 82 of file GoGame.cpp.
References SgNode::AddMoveProp(), SgNode::Get(), GoToNode(), SgNode::HasSon(), SgPropMove::IsPlayer(), SgPropMove::IsValue(), SgNode::LeftMostSon(), m_current, m_numMovesToInsert, m_time, SgNode::NewFather(), SgNode::NewRightMostSon(), SgTimeRecord::PlayedMove(), SgNode::PromoteNode(), SgNode::RightBrother(), and SG_PROP_MOVE.
Referenced by GoGtpEngine::CmdGenMove(), and GoGtpEngine::Play().
const SgNode & GoGame::AddResignNode | ( | SgBlackWhite | player | ) |
Add a node with a comment that a player resigned.
For informational purposes only, the resign node will not be made the current node.
Definition at line 119 of file GoGame.cpp.
References SgNode::AddComment(), m_current, SgNode::NewRightMostSon(), and SG_BLACK.
Referenced by GoGtpEngine::CmdGenMove().
void GoGame::AppendChild | ( | SgNode * | child | ) |
Append a node as a new child to the current node.
child | The new child. The ownership is transfered. The user is responsible that the subtree is consistent and contains no lines with illegal moves with respect to the position at the current node. |
Definition at line 128 of file GoGame.cpp.
References SgNode::AppendTo(), and m_current.
Referenced by GoGtpEngine::GenMove().
const GoBoard & GoGame::Board | ( | ) | const |
Get the board associated with this game record.
Definition at line 227 of file GoGame.h.
References m_board.
Referenced by GoGtpEngine::Board().
bool GoGame::CanGoInDirection | ( | SgNode::Direction | dir | ) | const |
Return whether there is a next node in the given direction.
Definition at line 133 of file GoGame.cpp.
References m_current, and SgNode::NodeInDirection().
Referenced by GoGameUtil::GotoBeforeMove().
SgMove GoGame::CurrentMove | ( | ) | const |
Return the move of the current node.
Return NullMove if no current move.
Definition at line 139 of file GoGame.cpp.
References SgNode::Get(), m_current, SG_NULLMOVE, SG_PROP_MOVE, and SgPropMove::Value().
int GoGame::CurrentMoveNumber | ( | ) | const |
Get the number of moves since root or last node with setup properties.
Definition at line 147 of file GoGame.cpp.
References SgNodeUtil::GetMoveNumber(), and m_current.
Referenced by GoGtpEngine::CheckLegal(), GoGtpEngine::GenMove(), and GoGameUtil::GotoBeforeMove().
const SgNode * GoGame::CurrentNode | ( | ) | const |
Return the current position in the tree.
Definition at line 252 of file GoGame.h.
References m_current.
Referenced by GoGtpEngine::CmdLoadSgf(), GoGtpEngine::CmdPlaySequence(), GoGameUtil::GotoBeforeMove(), and GoGtpEngine::Undo().
bool GoGame::EndOfGame | ( | ) | const |
std::string GoGame::GetGameInfoStringProp | ( | SgPropID | id | ) | const [private] |
Find the game info node with a game info property that determines this property for the current node.
Returns an empty string, if no such node exists.
Definition at line 163 of file GoGame.cpp.
References SgNode::Get(), SgNode::HasProp(), m_current, SgNode::TopProp(), and SgPropText::Value().
Referenced by GetGameName(), GetPlayerName(), and GetResult().
std::string GoGame::GetGameName | ( | ) | const |
Get the game name.
Searches to nearest game info node on the path to the root node that has a game name property. Returns an empty string if unknown.
Definition at line 175 of file GoGame.cpp.
References GetGameInfoStringProp(), and SG_PROP_GAME_NAME.
std::string GoGame::GetPlayerName | ( | SgBlackWhite | player | ) | const |
Get the player name.
Searches to nearest game info node on the path to the root node that has a player property. Returns an empty string if unknown.
Definition at line 180 of file GoGame.cpp.
References GetGameInfoStringProp(), SgProp::PlayerProp(), and SG_PROP_PLAYER_BLACK.
Referenced by GoGtpEngine::CmdGenMove().
std::string GoGame::GetResult | ( | ) | const |
Get the game result.
Searches to nearest game info node on the path to the root node that has a result property. Returns an empty string if unknown.
Definition at line 186 of file GoGame.cpp.
References GetGameInfoStringProp(), and SG_PROP_RESULT.
void GoGame::GoInDirection | ( | SgNode::Direction | dir | ) |
Play to the next node in the given direction.
Definition at line 191 of file GoGame.cpp.
References GoToNode(), m_current, and SgNode::NodeInDirection().
Referenced by GoGameUtil::GotoBeforeMove().
void GoGame::GoToNode | ( | const SgNode * | dest | ) |
Play to the given node.
dest
must be in this tree, or 0. Also updates the clock.
Definition at line 198 of file GoGame.cpp.
References SgTimeRecord::EnterNode(), m_board, m_current, m_time, m_updater, NonConstNodePtr(), GoBoardUtil::RemainingChineseHandicap(), GoBoard::SetToPlay(), SG_BLACK, Time(), GoBoard::ToPlay(), GoBoardUpdater::Update(), and SgNodeUtil::UpdateTime().
Referenced by AddMove(), GoGtpEngine::CmdPlaySequence(), GoInDirection(), Init(), PlaceHandicap(), SetupPosition(), and GoGtpEngine::Undo().
void GoGame::Init | ( | int | size, | |
const GoRules & | rules | |||
) |
Delete the old game record and start with a fresh one.
Init the board with the given parameters, and create a root node to start with.
Definition at line 208 of file GoGame.cpp.
References SgNode::Add(), SgNode::DeleteTree(), GoToNode(), GoBoard::Init(), InitHandicap(), GoKomi::IsUnknown(), GoRules::Komi(), m_board, m_root, SgNode::SetRealProp(), SG_PROP_KOMI, SG_PROP_SIZE, and GoKomi::ToFloat().
void GoGame::Init | ( | SgNode * | root | ) |
Init from an existing game tree.
Takes the ownership of the tree.
Definition at line 222 of file GoGame.cpp.
References SgNode::Add(), SgNode::DeleteTree(), ForceInRange(), SgNode::Get(), GO_DEFAULT_SIZE, GoToNode(), GoRules::Handicap(), GoBoard::Init(), GoRules::Komi(), m_board, m_root, GoBoard::Rules(), SG_MAX_SIZE, SG_MIN_SIZE, SG_PROP_GAME, SG_PROP_SIZE, and SgPropInt::Value().
Referenced by GoGtpEngine::CmdLoadSgf(), GoGame(), and GoGtpEngine::Init().
Definition at line 283 of file GoGame.cpp.
References SgNode::Add(), GoRules::Handicap(), GoRules::JapaneseHandicap(), m_board, SgPointUtil::Pt(), SgVector< T >::PushBack(), SG_PROP_ADD_BLACK, SG_PROP_CHINESE, SG_PROP_HANDICAP, SG_PROP_PLAYER, SG_WHITE, and GoBoard::Size().
Referenced by Init().
Convert a const reference of a node received by the user of this class to a non-const reference.
The user should not be able to modify the game tree directly, so he can only be given const references to nodes. This function exists to convert such a user reference to non-const for internal usage and avoids the spreading of const casts all over the code. It also contains an assertion that the node is part of the current game tree.
Definition at line 253 of file GoGame.cpp.
References m_root, SgNode::Root(), and SG_ASSERT.
Referenced by GoToNode().
See NonConstNodePtr().
Definition at line 260 of file GoGame.cpp.
References m_root, SgNode::Root(), and SG_ASSERT.
Referenced by AddComment().
Set handicap stones at explicitely given points.
If the current node alread has children, a new child is created with the handicap setup (and made the current node), otherwise the handicap stones are added to the current node.
Definition at line 266 of file GoGame.cpp.
References SgNode::Add(), GoToNode(), SgNode::HasSon(), GoBoardUtil::IsBoardEmpty(), SgVector< T >::Length(), m_board, m_current, SgNode::NewRightMostSon(), SgPropAddStone::PushBack(), GoBoard::Rules(), GoRules::SetHandicap(), SG_ASSERT, SG_PROP_ADD_BLACK, SG_PROP_HANDICAP, SG_PROP_PLAYER, and SG_WHITE.
Referenced by GoGtpEngine::PlaceHandicap().
const SgNode & GoGame::Root | ( | ) | const |
Return the root of this tree.
Definition at line 232 of file GoGame.h.
References m_root.
Referenced by GoGameUtil::GotoBeforeMove(), and GoGtpEngine::SaveGame().
void GoGame::SetKomiGlobal | ( | GoKomi | komi | ) |
Set komi property in the root node and delete all komi properties in the tree.
Definition at line 352 of file GoGame.cpp.
References GoKomi::IsUnknown(), m_board, m_root, SgNodeUtil::RemovePropInSubtree(), GoBoard::Rules(), GoRules::SetKomi(), SgNode::SetRealProp(), SG_PROP_KOMI, and GoKomi::ToFloat().
Referenced by GoGtpEngine::CmdKomi(), and SetRulesGlobal().
void GoGame::SetRulesGlobal | ( | const GoRules & | rules | ) |
Definition at line 360 of file GoGame.cpp.
References GoRules::Komi(), m_board, GoBoard::Rules(), and SetKomiGlobal().
Referenced by GoGtpEngine::CmdGenMoveCleanup(), GoGtpEngine::CmdLoadSgf(), GoGtpEngine::CmdParamRules(), and GoGtpEngine::SetNamedRules().
void GoGame::SetTimeSettingsGlobal | ( | const GoTimeSettings & | timeSettings, | |
double | overhead = 0 | |||
) |
Set time settings properties in the root node and delete all such properties in the rest of the tree.
timeSettings | ||
overhead | See SgTimeRecord |
Definition at line 367 of file GoGame.cpp.
References SgNode::Add(), GoTimeSettings::IsUnknown(), m_current, m_root, m_time, m_timeSettings, GoTimeSettings::MainTime(), GoTimeSettings::Overtime(), GoTimeSettings::OvertimeMoves(), SgNodeUtil::RemovePropInSubtree(), SgTimeRecord::SetClock(), SgNode::SetIntProp(), SgTimeRecord::SetOTNumMoves(), SgTimeRecord::SetOTPeriod(), SgTimeRecord::SetOverhead(), SG_BLACK, SG_PROP_OT_NU_MOVES, SG_PROP_OT_PERIOD, SG_PROP_TIME, SG_WHITE, and SgTimeRecord::TurnClockOn().
Referenced by GoGtpEngine::ApplyTimeSettings().
void GoGame::SetToPlay | ( | SgBlackWhite | toPlay | ) |
Set the current player.
Same meaning as the SGF property PL.
Definition at line 400 of file GoGame.cpp.
References SgNode::Add(), SgTimeRecord::EnterNode(), m_board, m_current, m_time, GoBoard::SetToPlay(), SG_PROP_PLAYER, and GoBoard::ToPlay().
Referenced by GoGtpEngine::CmdSetupPlayer().
void GoGame::SetupPosition | ( | const SgBWArray< SgPointSet > & | stones | ) |
Set up a position on the board.
Creates a new child node of the current node and adds the appropriate AB, AW and AE properties to change the board position to the position defined by the given stone lists. Makes the new child node the current node.
Definition at line 409 of file GoGame.cpp.
References SgNode::Add(), GoBoard::GetColor(), GoToNode(), m_board, m_current, SgNode::NewRightMostSon(), SgPropAddStone::PushBack(), SG_BLACK, SG_PROP_ADD_BLACK, SG_PROP_ADD_WHITE, and SG_WHITE.
Referenced by GoGtpEngine::CmdSetup().
SgTimeRecord & GoGame::Time | ( | ) |
Deprecated.
Non-const access to time left records will be removed in the future because its part of the class invariants of this class that they reflect the state corresponding to the current node.
Definition at line 237 of file GoGame.h.
References m_time.
Referenced by GoGtpEngine::CmdClock(), GoGtpEngine::CmdParam(), GoGtpEngine::CmdTimeLeft(), GoGtpEngine::GenMove(), and GoToNode().
const SgTimeRecord & GoGame::Time | ( | ) | const |
const GoTimeSettings & GoGame::TimeSettings | ( | ) | const |
Definition at line 247 of file GoGame.h.
References m_timeSettings.
void GoGame::UpdateDate | ( | const std::string & | date | ) |
Set the date at root node or most recent node with this property.
Definition at line 425 of file GoGame.cpp.
References SG_PROP_DATE, and UpdateGameInfoStringProp().
Referenced by GoGtpEngine::Init().
void GoGame::UpdateGameInfoStringProp | ( | SgPropID | id, | |
const std::string & | value | |||
) | [private] |
Definition at line 430 of file GoGame.cpp.
References m_current, SgNode::SetStringProp(), and SgNode::TopProp().
Referenced by UpdateDate(), UpdateGameName(), UpdatePlayerName(), and UpdateResult().
void GoGame::UpdateGameName | ( | const std::string & | name | ) |
Set the game name at root node or most recent node with this property.
Definition at line 436 of file GoGame.cpp.
References SG_PROP_GAME_NAME, and UpdateGameInfoStringProp().
Referenced by GoGtpEngine::CmdSetInfo().
void GoGame::UpdatePlayerName | ( | SgBlackWhite | player, | |
const std::string & | name | |||
) |
Set the player name at root node or most recent node with this property.
Definition at line 441 of file GoGame.cpp.
References SgProp::PlayerProp(), SG_PROP_PLAYER_BLACK, and UpdateGameInfoStringProp().
Referenced by GoGtpEngine::CmdGenMove(), and GoGtpEngine::CmdSetInfo().
void GoGame::UpdateResult | ( | const std::string & | result | ) |
Set the result at root node or most recent node with this property.
Definition at line 447 of file GoGame.cpp.
References SG_PROP_RESULT, and UpdateGameInfoStringProp().
Referenced by GoGtpEngine::CmdGameOver(), and GoGtpEngine::CmdSetInfo().
GoBoard GoGame::m_board [private] |
Definition at line 187 of file GoGame.h.
Referenced by Board(), EndOfGame(), GoToNode(), Init(), InitHandicap(), PlaceHandicap(), SetKomiGlobal(), SetRulesGlobal(), SetToPlay(), and SetupPosition().
SgNode* GoGame::m_current [private] |
The position in the current tree.
Definition at line 193 of file GoGame.h.
Referenced by AddComment(), AddMove(), AddResignNode(), AppendChild(), CanGoInDirection(), CurrentMove(), CurrentMoveNumber(), CurrentNode(), GetGameInfoStringProp(), GoInDirection(), GoToNode(), PlaceHandicap(), SetTimeSettingsGlobal(), SetToPlay(), SetupPosition(), and UpdateGameInfoStringProp().
int GoGame::m_numMovesToInsert [private] |
SgNode* GoGame::m_root [private] |
The root node of the current tree.
Definition at line 190 of file GoGame.h.
Referenced by Init(), NonConstNodePtr(), NonConstNodeRef(), Root(), SetKomiGlobal(), SetTimeSettingsGlobal(), and ~GoGame().
SgTimeRecord GoGame::m_time [private] |
A record of the clock settings and time left.
Definition at line 200 of file GoGame.h.
Referenced by AddMove(), GoToNode(), SetTimeSettingsGlobal(), SetToPlay(), and Time().
GoTimeSettings GoGame::m_timeSettings [private] |
Definition at line 197 of file GoGame.h.
Referenced by SetTimeSettingsGlobal(), and TimeSettings().
GoBoardUpdater GoGame::m_updater [private] |
Definition at line 195 of file GoGame.h.
Referenced by GoToNode().