1 Star 2 Fork 1

liuyinghua/six

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
hexgamestate.cpp 2.09 KB
一键复制 编辑 原始数据 按行查看 历史
liuyinghua 提交于 2018-07-06 16:06 . 第一次提交
#include "hexgamestate.h"
#include <cassert>
using std::endl;
HexGameState::HexGameState(const HexBoard &b, HexMark next, bool swappable,
HexMark winner)
: _board(b), _next(next), _swappable(swappable), _winner(winner)
{
assert(_next != HEX_MARK_EMPTY);
if(_winner == HEX_MARK_EMPTY)
_winner = _board.winner();
}
const HexBoard &HexGameState::board() const
{
return _board;
}
HexMark HexGameState::next() const
{
return _next;
}
bool HexGameState::swappable() const
{
return _swappable;
}
bool HexGameState::isValidMove(const HexMove &move) const
{
return (_winner == HEX_MARK_EMPTY && !move.isNull() &&
move.mark() == _next &&
((move.isSwap() && _swappable && _board.nMark() == 1) ||
move.isResign() || move.isForfeit() ||
(move.isNormal() &&
_board.isNormalField(move.field()) &&
_board.get(move.field()) == HEX_MARK_EMPTY)));
}
HexMark HexGameState::winner() const
{
return _winner;
}
void HexGameState::play(const HexMove &move)
{
assert(isValidMove(move));
if(move.isSwap()) {
_board = _board.transvert();
_swappable = false;
} else if(move.isNormal()) {
_board.set(move.field(), move.mark());
_winner = _board.winner();
} else if(move.isResign() || move.isForfeit()) {
_winner = INVERT_HEX_MARK(move.mark());
}
_next = INVERT_HEX_MARK(_next);
}
void HexGameState::printMove(ostream &os, const HexMove &m,
bool withMark) const
{
assert(!m.isNull());
if(m.isSwap()) {
os << "Swap";
} else if(m.isResign()) {
os << "Resign";
} else if(m.isForfeit()) {
os << "Forfeit";
} else {
if(withMark)
os << m.mark();
_board.printField(os, m.field());
}
}
HexGameState HexGameState::transvert() const
{
return HexGameState(_board.transvert(), INVERT_HEX_MARK(_next),
_swappable, INVERT_HEX_MARK(_winner));
}
ostream &operator <<(ostream &os, const HexGameState &g)
{
os << "Position:" << endl << g._board;
os << "Next: " << g._next << endl;
os << "Swap: " << g._swappable << endl;
return os;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/mongodb_client/six.git
git@gitee.com:mongodb_client/six.git
mongodb_client
six
six
master

搜索帮助