Class Position->Game
This commit is contained in:
parent
b330dd28ce
commit
cd800ec73a
|
@ -21,7 +21,7 @@
|
|||
|
||||
#include "evaluate.h"
|
||||
|
||||
value_t Evaluation::getValue(Position &dummyPosition, PositionContext *positionContext, MillGameAi_ab::Node *node)
|
||||
value_t Evaluation::getValue(Game &dummyPosition, PositionContext *positionContext, MillGameAi_ab::Node *node)
|
||||
{
|
||||
// 初始评估值为0,对先手有利则增大,对后手有利则减小
|
||||
value_t value = VALUE_ZERO;
|
||||
|
|
|
@ -34,7 +34,7 @@ public:
|
|||
|
||||
Evaluation &operator=(const Evaluation &) = delete;
|
||||
|
||||
static value_t getValue(Position &dummyPosition, PositionContext *positionContext, MillGameAi_ab::Node *node);
|
||||
static value_t getValue(Game &dummyPosition, PositionContext *positionContext, MillGameAi_ab::Node *node);
|
||||
|
||||
// 评估子力
|
||||
#ifdef EVALUATE_ENABLE
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
#include "player.h"
|
||||
#include "misc.h"
|
||||
|
||||
void MoveList::generateLegalMoves(MillGameAi_ab &ai_ab, Position &dummyPosition,
|
||||
void MoveList::generateLegalMoves(MillGameAi_ab &ai_ab, Game &dummyPosition,
|
||||
MillGameAi_ab::Node *node, MillGameAi_ab::Node *rootNode,
|
||||
move_t bestMove)
|
||||
{
|
||||
|
@ -158,7 +158,7 @@ void MoveList::generateLegalMoves(MillGameAi_ab &ai_ab, Position &dummyPosition,
|
|||
}
|
||||
}
|
||||
|
||||
void MoveList::createMoveTable(Position &position)
|
||||
void MoveList::createMoveTable(Game &position)
|
||||
{
|
||||
// Note: 未严格按 direction_t 中枚举的顺序从左到右排列
|
||||
#if 1
|
||||
|
@ -374,7 +374,7 @@ void MoveList::createMoveTable(Position &position)
|
|||
#endif
|
||||
}
|
||||
|
||||
void MoveList::shuffleMovePriorityTable(Position &position)
|
||||
void MoveList::shuffleMovePriorityTable(Game &position)
|
||||
{
|
||||
array<move_t, 4> movePriorityTable0 = { (move_t)17, (move_t)19, (move_t)21, (move_t)23 }; // 中圈四个顶点 (星位)
|
||||
array<move_t, 8> movePriorityTable1 = { (move_t)25, (move_t)27, (move_t)29, (move_t)31, (move_t)9, (move_t)11, (move_t)13, (move_t)15 }; // 外圈和内圈四个顶点
|
||||
|
|
|
@ -34,15 +34,15 @@ public:
|
|||
MoveList &operator=(const MoveList &) = delete;
|
||||
|
||||
// 生成所有合法的着法并建立子节点
|
||||
static void generateLegalMoves(MillGameAi_ab &ai_ab, Position &dummyPosition,
|
||||
static void generateLegalMoves(MillGameAi_ab &ai_ab, Game &dummyPosition,
|
||||
MillGameAi_ab::Node *node, MillGameAi_ab::Node *rootNode,
|
||||
move_t bestMove);
|
||||
|
||||
// 生成着法表
|
||||
static void createMoveTable(Position &position);
|
||||
static void createMoveTable(Game &position);
|
||||
|
||||
// 随机打乱着法搜索顺序
|
||||
static void shuffleMovePriorityTable(Position &position);
|
||||
static void shuffleMovePriorityTable(Game &position);
|
||||
|
||||
// 着法表 // TODO: Move to private
|
||||
inline static move_t moveTable[Board::N_LOCATIONS][DIRECTIONS_COUNT] = { {MOVE_NONE} };
|
||||
|
|
|
@ -257,7 +257,7 @@ void MillGameAi_ab::deleteTree(Node *node)
|
|||
#endif
|
||||
}
|
||||
|
||||
void MillGameAi_ab::setPosition(const Position &position)
|
||||
void MillGameAi_ab::setPosition(const Game &position)
|
||||
{
|
||||
// 如果规则改变,重建hashmap
|
||||
if (strcmp(this->position_.currentRule.name, position.currentRule.name) != 0) {
|
||||
|
|
|
@ -97,7 +97,7 @@ public:
|
|||
MillGameAi_ab();
|
||||
~MillGameAi_ab();
|
||||
|
||||
void setPosition(const Position &position);
|
||||
void setPosition(const Game &position);
|
||||
|
||||
void quit()
|
||||
{
|
||||
|
@ -182,15 +182,15 @@ protected:
|
|||
|
||||
private:
|
||||
// 原始模型
|
||||
Position position_;
|
||||
Game position_;
|
||||
|
||||
// 演算用的模型
|
||||
Position dummyPosition;
|
||||
Game dummyPosition;
|
||||
|
||||
PositionContext *positionContext {};
|
||||
|
||||
// hash 计算时,各种转换用的模型
|
||||
Position dummyPositionShift;
|
||||
Game dummyPositionShift;
|
||||
|
||||
// 根节点
|
||||
Node *rootNode {nullptr};
|
||||
|
|
|
@ -61,7 +61,7 @@ AiThread::~AiThread()
|
|||
wait();
|
||||
}
|
||||
|
||||
void AiThread::setAi(const Position &position)
|
||||
void AiThread::setAi(const Game &position)
|
||||
{
|
||||
mutex.lock();
|
||||
|
||||
|
@ -78,7 +78,7 @@ void AiThread::setAi(const Position &position)
|
|||
mutex.unlock();
|
||||
}
|
||||
|
||||
void AiThread::setAi(const Position &position, depth_t depth, int time)
|
||||
void AiThread::setAi(const Game &position, depth_t depth, int time)
|
||||
{
|
||||
mutex.lock();
|
||||
this->position_ = &position;
|
||||
|
|
|
@ -55,8 +55,8 @@ protected:
|
|||
|
||||
public:
|
||||
// AI设置
|
||||
void setAi(const Position &position);
|
||||
void setAi(const Position &position, depth_t depth, int time);
|
||||
void setAi(const Game &position);
|
||||
void setAi(const Game &position, depth_t depth, int time);
|
||||
|
||||
Server *getServer()
|
||||
{
|
||||
|
@ -110,7 +110,7 @@ private:
|
|||
QWaitCondition pauseCondition;
|
||||
|
||||
// 主线程棋对象的引用
|
||||
const Position *position_;
|
||||
const Game *position_;
|
||||
|
||||
// Alpha-Beta剪枝算法类
|
||||
MillGameAi_ab ai_ab;
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
#include "player.h"
|
||||
#include "zobrist.h"
|
||||
|
||||
Position::Position()
|
||||
Game::Game()
|
||||
{
|
||||
// 单独提出 boardLocations 等数据,免得每次都写 boardLocations;
|
||||
boardLocations = context.board.locations;
|
||||
|
@ -46,14 +46,14 @@ Position::Position()
|
|||
score[1] = score[2] = score_draw = 0;
|
||||
}
|
||||
|
||||
Position::~Position() = default;
|
||||
Game::~Game() = default;
|
||||
|
||||
Position::Position(const Position &position)
|
||||
Game::Game(const Game &position)
|
||||
{
|
||||
*this = position;
|
||||
}
|
||||
|
||||
Position &Position::operator= (const Position &position)
|
||||
Game &Game::operator= (const Game &position)
|
||||
{
|
||||
if (this == &position)
|
||||
return *this;
|
||||
|
@ -80,7 +80,7 @@ Position &Position::operator= (const Position &position)
|
|||
}
|
||||
|
||||
// 设置配置
|
||||
bool Position::configure(bool giveUpIfMostLose, bool randomMove)
|
||||
bool Game::configure(bool giveUpIfMostLose, bool randomMove)
|
||||
{
|
||||
// 设置是否必败时认输
|
||||
this->giveUpIfMostLose_ = giveUpIfMostLose;
|
||||
|
@ -92,7 +92,7 @@ bool Position::configure(bool giveUpIfMostLose, bool randomMove)
|
|||
}
|
||||
|
||||
// 设置棋局状态和棋盘数据,用于初始化
|
||||
bool Position::setContext(const struct Rule *rule, step_t maxStepsLedToDraw, int maxTimeLedToLose,
|
||||
bool Game::setContext(const struct Rule *rule, step_t maxStepsLedToDraw, int maxTimeLedToLose,
|
||||
step_t initialStep,
|
||||
phase_t phase, player_t turn, action_t action,
|
||||
const char *locations,
|
||||
|
@ -221,7 +221,7 @@ bool Position::setContext(const struct Rule *rule, step_t maxStepsLedToDraw, int
|
|||
return false;
|
||||
}
|
||||
|
||||
bool Position::reset()
|
||||
bool Game::reset()
|
||||
{
|
||||
if (context.phase == PHASE_NOTSTARTED &&
|
||||
elapsedSeconds[1] == elapsedSeconds[2] == 0) {
|
||||
|
@ -294,7 +294,7 @@ bool Position::reset()
|
|||
return false;
|
||||
}
|
||||
|
||||
bool Position::start()
|
||||
bool Game::start()
|
||||
{
|
||||
switch (context.phase) {
|
||||
// 如果游戏已经开始,则返回false
|
||||
|
@ -317,7 +317,7 @@ bool Position::start()
|
|||
}
|
||||
}
|
||||
|
||||
bool Position::place(int location, int time_p, int8_t rs)
|
||||
bool Game::place(int location, int time_p, int8_t rs)
|
||||
{
|
||||
// 如果局面为“结局”,返回false
|
||||
if (context.phase == PHASE_GAMEOVER)
|
||||
|
@ -489,7 +489,7 @@ out:
|
|||
return true;
|
||||
}
|
||||
|
||||
bool Position::_place(int r, int s, int time_p)
|
||||
bool Game::_place(int r, int s, int time_p)
|
||||
{
|
||||
// 转换为 location
|
||||
int location = context.board.polarToLocation(r, s);
|
||||
|
@ -497,7 +497,7 @@ bool Position::_place(int r, int s, int time_p)
|
|||
return place(location, time_p, true);
|
||||
}
|
||||
|
||||
bool Position::_capture(int r, int s, int time_p)
|
||||
bool Game::_capture(int r, int s, int time_p)
|
||||
{
|
||||
// 转换为 location
|
||||
int location = context.board.polarToLocation(r, s);
|
||||
|
@ -505,7 +505,7 @@ bool Position::_capture(int r, int s, int time_p)
|
|||
return capture(location, time_p, 1);
|
||||
}
|
||||
|
||||
bool Position::capture(int location, int time_p, int8_t cp)
|
||||
bool Game::capture(int location, int time_p, int8_t cp)
|
||||
{
|
||||
// 如果局面为"未开局"或“结局”,返回false
|
||||
if (context.phase == PHASE_NOTSTARTED || context.phase == PHASE_GAMEOVER)
|
||||
|
@ -643,7 +643,7 @@ out:
|
|||
return true;
|
||||
}
|
||||
|
||||
bool Position::choose(int location)
|
||||
bool Game::choose(int location)
|
||||
{
|
||||
// 如果局面不是"中局”,返回false
|
||||
if (context.phase != PHASE_MOVING)
|
||||
|
@ -672,12 +672,12 @@ bool Position::choose(int location)
|
|||
return false;
|
||||
}
|
||||
|
||||
bool Position::choose(int r, int s)
|
||||
bool Game::choose(int r, int s)
|
||||
{
|
||||
return choose(context.board.polarToLocation(r, s));
|
||||
}
|
||||
|
||||
bool Position::giveup(player_t loser)
|
||||
bool Game::giveup(player_t loser)
|
||||
{
|
||||
if (context.phase == PHASE_NOTSTARTED ||
|
||||
context.phase == PHASE_GAMEOVER ||
|
||||
|
@ -702,7 +702,7 @@ bool Position::giveup(player_t loser)
|
|||
}
|
||||
|
||||
// 打算用个C++的命令行解析库的,简单的没必要,但中文编码有极小概率出问题
|
||||
bool Position::command(const char *cmd)
|
||||
bool Game::command(const char *cmd)
|
||||
{
|
||||
int r;
|
||||
unsigned t;
|
||||
|
@ -783,7 +783,7 @@ bool Position::command(const char *cmd)
|
|||
return false;
|
||||
}
|
||||
|
||||
bool Position::command(int move)
|
||||
bool Game::command(int move)
|
||||
{
|
||||
if (move < 0) {
|
||||
return capture(-move);
|
||||
|
@ -800,7 +800,7 @@ bool Position::command(int move)
|
|||
return false;
|
||||
}
|
||||
|
||||
inline int Position::update(int time_p /*= -1*/)
|
||||
inline int Game::update(int time_p /*= -1*/)
|
||||
{
|
||||
int ret = -1;
|
||||
time_t *player_ms = &elapsedSeconds[context.turnId];
|
||||
|
@ -831,13 +831,13 @@ inline int Position::update(int time_p /*= -1*/)
|
|||
}
|
||||
|
||||
// 是否分出胜负
|
||||
bool Position::win()
|
||||
bool Game::win()
|
||||
{
|
||||
return win(false);
|
||||
}
|
||||
|
||||
// 是否分出胜负
|
||||
bool Position::win(bool forceDraw)
|
||||
bool Game::win(bool forceDraw)
|
||||
{
|
||||
if (context.phase == PHASE_GAMEOVER) {
|
||||
return true;
|
||||
|
@ -951,7 +951,7 @@ bool Position::win(bool forceDraw)
|
|||
}
|
||||
|
||||
// 计算玩家1和玩家2的棋子活动能力之差
|
||||
int Position::getMobilityDiff(enum player_t turn, const Rule &rule, int nPiecesOnBoard[], bool includeFobidden)
|
||||
int Game::getMobilityDiff(enum player_t turn, const Rule &rule, int nPiecesOnBoard[], bool includeFobidden)
|
||||
{
|
||||
int *locations = boardLocations;
|
||||
int mobility1 = 0;
|
||||
|
@ -974,7 +974,7 @@ int Position::getMobilityDiff(enum player_t turn, const Rule &rule, int nPiecesO
|
|||
return diff;
|
||||
}
|
||||
|
||||
void Position::cleanForbiddenLocations()
|
||||
void Game::cleanForbiddenLocations()
|
||||
{
|
||||
int location = 0;
|
||||
|
||||
|
@ -990,7 +990,7 @@ void Position::cleanForbiddenLocations()
|
|||
}
|
||||
}
|
||||
|
||||
void Position::setTurn(player_t player)
|
||||
void Game::setTurn(player_t player)
|
||||
{
|
||||
// 设置轮到谁走
|
||||
context.turn = player;
|
||||
|
@ -1006,12 +1006,12 @@ void Position::setTurn(player_t player)
|
|||
//context.opponentStr = Player::chToStr(context.opponentChar);
|
||||
}
|
||||
|
||||
void Position::changeTurn()
|
||||
void Game::changeTurn()
|
||||
{
|
||||
setTurn(Player::getOpponent(context.turn));
|
||||
}
|
||||
|
||||
void Position::setTips()
|
||||
void Game::setTips()
|
||||
{
|
||||
string winnerStr, t;
|
||||
int winnerId;
|
||||
|
@ -1066,7 +1066,7 @@ void Position::setTips()
|
|||
}
|
||||
}
|
||||
|
||||
void Position::getElapsedTime(time_t &p1_ms, time_t &p2_ms)
|
||||
void Game::getElapsedTime(time_t &p1_ms, time_t &p2_ms)
|
||||
{
|
||||
update();
|
||||
|
||||
|
@ -1084,12 +1084,12 @@ void Position::getElapsedTime(time_t &p1_ms, time_t &p2_ms)
|
|||
* 0位:轮流标识,0为先手,1为后手
|
||||
*/
|
||||
|
||||
void Position::constructHash()
|
||||
void Game::constructHash()
|
||||
{
|
||||
context.hash = 0;
|
||||
}
|
||||
|
||||
hash_t Position::getHash()
|
||||
hash_t Game::getHash()
|
||||
{
|
||||
// TODO: 每次获取哈希值时更新 hash 值低8位,放在此处调用不优雅
|
||||
updateHashMisc();
|
||||
|
@ -1097,7 +1097,7 @@ hash_t Position::getHash()
|
|||
return context.hash;
|
||||
}
|
||||
|
||||
hash_t Position::updateHash(int location)
|
||||
hash_t Game::updateHash(int location)
|
||||
{
|
||||
// PieceType is boardLocations[location]
|
||||
|
||||
|
@ -1110,12 +1110,12 @@ hash_t Position::updateHash(int location)
|
|||
return context.hash;
|
||||
}
|
||||
|
||||
hash_t Position::revertHash(int location)
|
||||
hash_t Game::revertHash(int location)
|
||||
{
|
||||
return updateHash(location);
|
||||
}
|
||||
|
||||
hash_t Position::updateHashMisc()
|
||||
hash_t Game::updateHashMisc()
|
||||
{
|
||||
// 清除标记位
|
||||
context.hash &= static_cast<hash_t>(~0xFF);
|
||||
|
|
|
@ -74,7 +74,7 @@ public:
|
|||
// 棋类(在数据模型内,玩家只分先后手,不分黑白)
|
||||
// 注意:Position 类不是线程安全的!
|
||||
// 所以不能跨线程修改 Position 类的静态成员变量,切记!
|
||||
class Position
|
||||
class Game
|
||||
{
|
||||
// AI友元类
|
||||
friend class MillGameAi_ab;
|
||||
|
@ -90,14 +90,14 @@ private:
|
|||
void constructHash();
|
||||
|
||||
public:
|
||||
explicit Position();
|
||||
virtual ~Position();
|
||||
explicit Game();
|
||||
virtual ~Game();
|
||||
|
||||
// 拷贝构造函数
|
||||
explicit Position(const Position &);
|
||||
explicit Game(const Game &);
|
||||
|
||||
// 运算符重载
|
||||
Position &operator=(const Position &);
|
||||
Game &operator=(const Game &);
|
||||
|
||||
// 设置配置
|
||||
bool configure(bool giveUpIfMostLose, bool randomMove);
|
||||
|
|
|
@ -970,7 +970,7 @@ bool GameController::updateScence()
|
|||
return updateScence(position_);
|
||||
}
|
||||
|
||||
bool GameController::updateScence(Position &game)
|
||||
bool GameController::updateScence(Game &game)
|
||||
{
|
||||
const int *board = game.getBoardLocations();
|
||||
QPointF pos;
|
||||
|
|
|
@ -191,7 +191,7 @@ public slots:
|
|||
|
||||
// 更新棋局显示,每步后执行才能刷新局面
|
||||
bool updateScence();
|
||||
bool updateScence(Position &game);
|
||||
bool updateScence(Game &game);
|
||||
|
||||
// 显示网络配置窗口
|
||||
void showNetworkWindow();
|
||||
|
@ -203,10 +203,10 @@ protected:
|
|||
|
||||
private:
|
||||
// 棋对象的数据模型
|
||||
Position position_;
|
||||
Game position_;
|
||||
|
||||
// 棋对象的数据模型(临时)
|
||||
Position dummyPosition;
|
||||
Game dummyPosition;
|
||||
|
||||
// 2个AI的线程
|
||||
AiThread *ai[3];
|
||||
|
|
Loading…
Reference in New Issue