position_ -> game_ (2)
This commit is contained in:
parent
596e672f2f
commit
143dc12e15
|
@ -27,7 +27,7 @@
|
||||||
AiThread::AiThread(int id, QObject *parent) :
|
AiThread::AiThread(int id, QObject *parent) :
|
||||||
QThread(parent),
|
QThread(parent),
|
||||||
waiting_(false),
|
waiting_(false),
|
||||||
position_(nullptr),
|
game_(nullptr),
|
||||||
aiDepth(2),
|
aiDepth(2),
|
||||||
aiTime(3600)
|
aiTime(3600)
|
||||||
{
|
{
|
||||||
|
@ -65,8 +65,8 @@ void AiThread::setAi(const Game &game)
|
||||||
{
|
{
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
|
|
||||||
this->position_ = &game;
|
this->game_ = &game;
|
||||||
ai_ab.setGame(*(this->position_));
|
ai_ab.setGame(*(this->game_));
|
||||||
|
|
||||||
#ifdef TRANSPOSITION_TABLE_ENABLE
|
#ifdef TRANSPOSITION_TABLE_ENABLE
|
||||||
// 新下一盘前清除哈希表 (注意可能同时存在每步之前清除)
|
// 新下一盘前清除哈希表 (注意可能同时存在每步之前清除)
|
||||||
|
@ -81,7 +81,7 @@ void AiThread::setAi(const Game &game)
|
||||||
void AiThread::setAi(const Game &game, depth_t depth, int time)
|
void AiThread::setAi(const Game &game, depth_t depth, int time)
|
||||||
{
|
{
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
this->position_ = &game;
|
this->game_ = &game;
|
||||||
ai_ab.setGame(game);
|
ai_ab.setGame(game);
|
||||||
aiDepth = depth;
|
aiDepth = depth;
|
||||||
aiTime = time;
|
aiTime = time;
|
||||||
|
@ -108,7 +108,7 @@ void AiThread::run()
|
||||||
while (!isInterruptionRequested()) {
|
while (!isInterruptionRequested()) {
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
|
|
||||||
i = Player::toId(position_->context.turn);
|
i = Player::toId(game_->context.turn);
|
||||||
|
|
||||||
if (i != id || waiting_) {
|
if (i != id || waiting_) {
|
||||||
pauseCondition.wait(&mutex);
|
pauseCondition.wait(&mutex);
|
||||||
|
@ -116,7 +116,7 @@ void AiThread::run()
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
ai_ab.setGame(*position_);
|
ai_ab.setGame(*game_);
|
||||||
emit calcStarted();
|
emit calcStarted();
|
||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
|
|
||||||
|
|
|
@ -110,7 +110,7 @@ private:
|
||||||
QWaitCondition pauseCondition;
|
QWaitCondition pauseCondition;
|
||||||
|
|
||||||
// 主线程棋对象的引用
|
// 主线程棋对象的引用
|
||||||
const Game *position_;
|
const Game *game_;
|
||||||
|
|
||||||
// Alpha-Beta剪枝算法类
|
// Alpha-Beta剪枝算法类
|
||||||
MillGameAi_ab ai_ab;
|
MillGameAi_ab ai_ab;
|
||||||
|
|
|
@ -122,9 +122,9 @@ const QMap<int, QStringList> GameController::getActions()
|
||||||
|
|
||||||
void GameController::gameStart()
|
void GameController::gameStart()
|
||||||
{
|
{
|
||||||
position_.configure(giveUpIfMostLose_, randomMove_);
|
game_.configure(giveUpIfMostLose_, randomMove_);
|
||||||
position_.start();
|
game_.start();
|
||||||
dummyGame = position_;
|
dummyGame = game_;
|
||||||
|
|
||||||
// 每隔100毫秒调用一次定时器处理函数
|
// 每隔100毫秒调用一次定时器处理函数
|
||||||
if (timeID == 0) {
|
if (timeID == 0) {
|
||||||
|
@ -142,15 +142,15 @@ void GameController::gameReset()
|
||||||
timeID = 0;
|
timeID = 0;
|
||||||
|
|
||||||
// 棋未下完,则算对手得分
|
// 棋未下完,则算对手得分
|
||||||
if (position_.getPhase() == PHASE_MOVING &&
|
if (game_.getPhase() == PHASE_MOVING &&
|
||||||
position_.whoWin() == PLAYER_NOBODY) {
|
game_.whoWin() == PLAYER_NOBODY) {
|
||||||
giveUp();
|
giveUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重置游戏
|
// 重置游戏
|
||||||
position_.configure(giveUpIfMostLose_, randomMove_);
|
game_.configure(giveUpIfMostLose_, randomMove_);
|
||||||
position_.reset();
|
game_.reset();
|
||||||
dummyGame = position_;
|
dummyGame = game_;
|
||||||
|
|
||||||
// 停掉线程
|
// 停掉线程
|
||||||
if (!isAutoRestart) {
|
if (!isAutoRestart) {
|
||||||
|
@ -166,7 +166,7 @@ void GameController::gameReset()
|
||||||
currentPiece = nullptr;
|
currentPiece = nullptr;
|
||||||
|
|
||||||
// 重新绘制棋盘
|
// 重新绘制棋盘
|
||||||
scene.setDiagonal(position_.getRule()->hasObliqueLines);
|
scene.setDiagonal(game_.getRule()->hasObliqueLines);
|
||||||
|
|
||||||
// 绘制所有棋子,放在起始位置
|
// 绘制所有棋子,放在起始位置
|
||||||
// 0: 先手第1子; 1:后手第1子
|
// 0: 先手第1子; 1:后手第1子
|
||||||
|
@ -175,7 +175,7 @@ void GameController::gameReset()
|
||||||
PieceItem::Models md;
|
PieceItem::Models md;
|
||||||
PieceItem *newP;
|
PieceItem *newP;
|
||||||
|
|
||||||
for (int i = 0; i < position_.getRule()->nTotalPiecesEachSide; i++) {
|
for (int i = 0; i < game_.getRule()->nTotalPiecesEachSide; i++) {
|
||||||
// 先手的棋子
|
// 先手的棋子
|
||||||
md = isInverted ? PieceItem::whitePiece : PieceItem::blackPiece;
|
md = isInverted ? PieceItem::whitePiece : PieceItem::blackPiece;
|
||||||
newP = new PieceItem;
|
newP = new PieceItem;
|
||||||
|
@ -184,7 +184,7 @@ void GameController::gameReset()
|
||||||
newP->setNum(i + 1);
|
newP->setNum(i + 1);
|
||||||
|
|
||||||
// 如果重复三连不可用,则显示棋子序号,九连棋专用玩法
|
// 如果重复三连不可用,则显示棋子序号,九连棋专用玩法
|
||||||
if (!(position_.getRule()->allowRemovePiecesRepeatedly))
|
if (!(game_.getRule()->allowRemovePiecesRepeatedly))
|
||||||
newP->setShowNum(true);
|
newP->setShowNum(true);
|
||||||
|
|
||||||
pieceList.append(newP);
|
pieceList.append(newP);
|
||||||
|
@ -198,7 +198,7 @@ void GameController::gameReset()
|
||||||
newP->setNum(i + 1);
|
newP->setNum(i + 1);
|
||||||
|
|
||||||
// 如果重复三连不可用,则显示棋子序号,九连棋专用玩法
|
// 如果重复三连不可用,则显示棋子序号,九连棋专用玩法
|
||||||
if (!(position_.getRule()->allowRemovePiecesRepeatedly))
|
if (!(game_.getRule()->allowRemovePiecesRepeatedly))
|
||||||
newP->setShowNum(true);
|
newP->setShowNum(true);
|
||||||
|
|
||||||
pieceList.append(newP);
|
pieceList.append(newP);
|
||||||
|
@ -206,7 +206,7 @@ void GameController::gameReset()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取规则限时要求
|
// 读取规则限时要求
|
||||||
timeLimit = position_.getRule()->maxTimeLedToLose;
|
timeLimit = game_.getRule()->maxTimeLedToLose;
|
||||||
|
|
||||||
// 如果规则不要求计时,则time1和time2表示已用时间
|
// 如果规则不要求计时,则time1和time2表示已用时间
|
||||||
if (timeLimit <= 0) {
|
if (timeLimit <= 0) {
|
||||||
|
@ -220,7 +220,7 @@ void GameController::gameReset()
|
||||||
// 更新棋谱
|
// 更新棋谱
|
||||||
manualListModel.removeRows(0, manualListModel.rowCount());
|
manualListModel.removeRows(0, manualListModel.rowCount());
|
||||||
manualListModel.insertRow(0);
|
manualListModel.insertRow(0);
|
||||||
manualListModel.setData(manualListModel.index(0), position_.getCmdLine());
|
manualListModel.setData(manualListModel.index(0), game_.getCmdLine());
|
||||||
currentRow = 0;
|
currentRow = 0;
|
||||||
|
|
||||||
// 发出信号通知主窗口更新LCD显示
|
// 发出信号通知主窗口更新LCD显示
|
||||||
|
@ -229,13 +229,13 @@ void GameController::gameReset()
|
||||||
emit time2Changed(qtime.toString("hh:mm:ss"));
|
emit time2Changed(qtime.toString("hh:mm:ss"));
|
||||||
|
|
||||||
// 发信号更新状态栏
|
// 发信号更新状态栏
|
||||||
message = QString::fromStdString(position_.getTips());
|
message = QString::fromStdString(game_.getTips());
|
||||||
emit statusBarChanged(message);
|
emit statusBarChanged(message);
|
||||||
|
|
||||||
// 更新比分 LCD 显示
|
// 更新比分 LCD 显示
|
||||||
emit score1Changed(QString::number(position_.score[1], 10));
|
emit score1Changed(QString::number(game_.score[1], 10));
|
||||||
emit score2Changed(QString::number(position_.score[2], 10));
|
emit score2Changed(QString::number(game_.score[2], 10));
|
||||||
emit scoreDrawChanged(QString::number(position_.score_draw, 10));
|
emit scoreDrawChanged(QString::number(game_.score_draw, 10));
|
||||||
|
|
||||||
// 播放音效
|
// 播放音效
|
||||||
//playSound(":/sound/resources/sound/newgame.wav");
|
//playSound(":/sound/resources/sound/newgame.wav");
|
||||||
|
@ -280,8 +280,8 @@ void GameController::setRule(int ruleNo, step_t stepLimited /*= -1*/, int timeLi
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置模型规则,重置游戏
|
// 设置模型规则,重置游戏
|
||||||
position_.setContext(&RULES[ruleNo], stepsLimit, timeLimit);
|
game_.setContext(&RULES[ruleNo], stepsLimit, timeLimit);
|
||||||
dummyGame = position_;
|
dummyGame = game_;
|
||||||
|
|
||||||
// 重置游戏
|
// 重置游戏
|
||||||
gameReset();
|
gameReset();
|
||||||
|
@ -289,12 +289,12 @@ void GameController::setRule(int ruleNo, step_t stepLimited /*= -1*/, int timeLi
|
||||||
|
|
||||||
void GameController::setEngine(int id, bool arg)
|
void GameController::setEngine(int id, bool arg)
|
||||||
{
|
{
|
||||||
position_.configure(giveUpIfMostLose_, randomMove_);
|
game_.configure(giveUpIfMostLose_, randomMove_);
|
||||||
|
|
||||||
isAiPlayer[id] = arg;
|
isAiPlayer[id] = arg;
|
||||||
|
|
||||||
if (arg) {
|
if (arg) {
|
||||||
ai[id]->setAi(position_);
|
ai[id]->setAi(game_);
|
||||||
if (ai[id]->isRunning())
|
if (ai[id]->isRunning())
|
||||||
ai[id]->resume();
|
ai[id]->resume();
|
||||||
else
|
else
|
||||||
|
@ -325,8 +325,8 @@ void GameController::setAiDepthTime(depth_t depth1, int time1, depth_t depth2, i
|
||||||
ai[2]->wait();
|
ai[2]->wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
ai[1]->setAi(position_, depth1, time1);
|
ai[1]->setAi(game_, depth1, time1);
|
||||||
ai[2]->setAi(position_, depth2, time2);
|
ai[2]->setAi(game_, depth2, time2);
|
||||||
|
|
||||||
if (isAiPlayer[1]) {
|
if (isAiPlayer[1]) {
|
||||||
ai[1]->start();
|
ai[1]->start();
|
||||||
|
@ -398,13 +398,13 @@ void GameController::flip()
|
||||||
ai[2]->wait();
|
ai[2]->wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
position_.context.board.mirror(position_.cmdlist, position_.cmdline, position_.move_, position_.currentRule, position_.currentLocation);
|
game_.context.board.mirror(game_.cmdlist, game_.cmdline, game_.move_, game_.currentRule, game_.currentLocation);
|
||||||
position_.context.board.rotate(180, position_.cmdlist, position_.cmdline, position_.move_, position_.currentRule, position_.currentLocation);
|
game_.context.board.rotate(180, game_.cmdlist, game_.cmdline, game_.move_, game_.currentRule, game_.currentLocation);
|
||||||
dummyGame = position_;
|
dummyGame = game_;
|
||||||
|
|
||||||
// 更新棋谱
|
// 更新棋谱
|
||||||
int row = 0;
|
int row = 0;
|
||||||
for (const auto &str : *(position_.getCmdList())) {
|
for (const auto &str : *(game_.getCmdList())) {
|
||||||
manualListModel.setData(manualListModel.index(row++), str.c_str());
|
manualListModel.setData(manualListModel.index(row++), str.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -414,8 +414,8 @@ void GameController::flip()
|
||||||
else
|
else
|
||||||
phaseChange(currentRow, true);
|
phaseChange(currentRow, true);
|
||||||
|
|
||||||
ai[1]->setAi(position_);
|
ai[1]->setAi(game_);
|
||||||
ai[2]->setAi(position_);
|
ai[2]->setAi(game_);
|
||||||
|
|
||||||
if (isAiPlayer[1]) {
|
if (isAiPlayer[1]) {
|
||||||
ai[1]->start();
|
ai[1]->start();
|
||||||
|
@ -438,13 +438,13 @@ void GameController::mirror()
|
||||||
ai[2]->wait();
|
ai[2]->wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
position_.context.board.mirror(position_.cmdlist, position_.cmdline, position_.move_, position_.currentRule, position_.currentLocation);
|
game_.context.board.mirror(game_.cmdlist, game_.cmdline, game_.move_, game_.currentRule, game_.currentLocation);
|
||||||
dummyGame = position_;
|
dummyGame = game_;
|
||||||
|
|
||||||
// 更新棋谱
|
// 更新棋谱
|
||||||
int row = 0;
|
int row = 0;
|
||||||
|
|
||||||
for (const auto &str : *(position_.getCmdList())) {
|
for (const auto &str : *(game_.getCmdList())) {
|
||||||
manualListModel.setData(manualListModel.index(row++), str.c_str());
|
manualListModel.setData(manualListModel.index(row++), str.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -456,8 +456,8 @@ void GameController::mirror()
|
||||||
else
|
else
|
||||||
phaseChange(currentRow, true);
|
phaseChange(currentRow, true);
|
||||||
|
|
||||||
ai[1]->setAi(position_);
|
ai[1]->setAi(game_);
|
||||||
ai[2]->setAi(position_);
|
ai[2]->setAi(game_);
|
||||||
|
|
||||||
if (isAiPlayer[1]) {
|
if (isAiPlayer[1]) {
|
||||||
ai[1]->start();
|
ai[1]->start();
|
||||||
|
@ -480,13 +480,13 @@ void GameController::turnRight()
|
||||||
ai[2]->wait();
|
ai[2]->wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
position_.context.board.rotate(-90, position_.cmdlist, position_.cmdline, position_.move_, position_.currentRule, position_.currentLocation);
|
game_.context.board.rotate(-90, game_.cmdlist, game_.cmdline, game_.move_, game_.currentRule, game_.currentLocation);
|
||||||
dummyGame = position_;
|
dummyGame = game_;
|
||||||
|
|
||||||
// 更新棋谱
|
// 更新棋谱
|
||||||
int row = 0;
|
int row = 0;
|
||||||
|
|
||||||
for (const auto &str : *(position_.getCmdList())) {
|
for (const auto &str : *(game_.getCmdList())) {
|
||||||
manualListModel.setData(manualListModel.index(row++), str.c_str());
|
manualListModel.setData(manualListModel.index(row++), str.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -496,8 +496,8 @@ void GameController::turnRight()
|
||||||
else
|
else
|
||||||
phaseChange(currentRow, true);
|
phaseChange(currentRow, true);
|
||||||
|
|
||||||
ai[1]->setAi(position_);
|
ai[1]->setAi(game_);
|
||||||
ai[2]->setAi(position_);
|
ai[2]->setAi(game_);
|
||||||
|
|
||||||
if (isAiPlayer[1]) {
|
if (isAiPlayer[1]) {
|
||||||
ai[1]->start();
|
ai[1]->start();
|
||||||
|
@ -520,20 +520,20 @@ void GameController::turnLeft()
|
||||||
ai[2]->wait();
|
ai[2]->wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
position_.context.board.rotate(90, position_.cmdlist, position_.cmdline, position_.move_, position_.currentRule, position_.currentLocation);
|
game_.context.board.rotate(90, game_.cmdlist, game_.cmdline, game_.move_, game_.currentRule, game_.currentLocation);
|
||||||
dummyGame = position_;
|
dummyGame = game_;
|
||||||
|
|
||||||
// 更新棋谱
|
// 更新棋谱
|
||||||
int row = 0;
|
int row = 0;
|
||||||
for (const auto &str : *(position_.getCmdList())) {
|
for (const auto &str : *(game_.getCmdList())) {
|
||||||
manualListModel.setData(manualListModel.index(row++), str.c_str());
|
manualListModel.setData(manualListModel.index(row++), str.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 刷新显示
|
// 刷新显示
|
||||||
updateScence();
|
updateScence();
|
||||||
|
|
||||||
ai[1]->setAi(position_);
|
ai[1]->setAi(game_);
|
||||||
ai[2]->setAi(position_);
|
ai[2]->setAi(game_);
|
||||||
if (isAiPlayer[1]) {
|
if (isAiPlayer[1]) {
|
||||||
ai[1]->start();
|
ai[1]->start();
|
||||||
}
|
}
|
||||||
|
@ -548,7 +548,7 @@ void GameController::timerEvent(QTimerEvent *event)
|
||||||
static QTime qt1, qt2;
|
static QTime qt1, qt2;
|
||||||
|
|
||||||
// 玩家的已用时间
|
// 玩家的已用时间
|
||||||
position_.getElapsedTime(remainingTime[1], remainingTime[2]);
|
game_.getElapsedTime(remainingTime[1], remainingTime[2]);
|
||||||
|
|
||||||
// 如果规则要求计时,则time1和time2表示倒计时
|
// 如果规则要求计时,则time1和time2表示倒计时
|
||||||
if (timeLimit > 0) {
|
if (timeLimit > 0) {
|
||||||
|
@ -564,7 +564,7 @@ void GameController::timerEvent(QTimerEvent *event)
|
||||||
emit time2Changed(qt2.toString("hh:mm:ss"));
|
emit time2Changed(qt2.toString("hh:mm:ss"));
|
||||||
|
|
||||||
// 如果胜负已分
|
// 如果胜负已分
|
||||||
if (position_.whoWin() != PLAYER_NOBODY) {
|
if (game_.whoWin() != PLAYER_NOBODY) {
|
||||||
// 停止计时
|
// 停止计时
|
||||||
killTimer(timeID);
|
killTimer(timeID);
|
||||||
|
|
||||||
|
@ -572,7 +572,7 @@ void GameController::timerEvent(QTimerEvent *event)
|
||||||
timeID = 0;
|
timeID = 0;
|
||||||
|
|
||||||
// 发信号更新状态栏
|
// 发信号更新状态栏
|
||||||
message = QString::fromStdString(position_.getTips());
|
message = QString::fromStdString(game_.getTips());
|
||||||
emit statusBarChanged(message);
|
emit statusBarChanged(message);
|
||||||
|
|
||||||
// 弹框
|
// 弹框
|
||||||
|
@ -609,7 +609,7 @@ void GameController::timerEvent(QTimerEvent *event)
|
||||||
|
|
||||||
bool GameController::isAIsTurn()
|
bool GameController::isAIsTurn()
|
||||||
{
|
{
|
||||||
return isAiPlayer[position_.context.turnId];
|
return isAiPlayer[game_.context.turnId];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 关键槽函数,根据QGraphicsScene的信号和状态来执行选子、落子或去子
|
// 关键槽函数,根据QGraphicsScene的信号和状态来执行选子、落子或去子
|
||||||
|
@ -642,17 +642,17 @@ bool GameController::actionPiece(QPointF pos)
|
||||||
|
|
||||||
if (QMessageBox::Ok == msgBox.exec()) {
|
if (QMessageBox::Ok == msgBox.exec()) {
|
||||||
#endif /* !MOBILE_APP_UI */
|
#endif /* !MOBILE_APP_UI */
|
||||||
position_ = dummyGame;
|
game_ = dummyGame;
|
||||||
manualListModel.removeRows(currentRow + 1, manualListModel.rowCount() - currentRow - 1);
|
manualListModel.removeRows(currentRow + 1, manualListModel.rowCount() - currentRow - 1);
|
||||||
|
|
||||||
// 如果再决出胜负后悔棋,则重新启动计时
|
// 如果再决出胜负后悔棋,则重新启动计时
|
||||||
if (position_.whoWin() == PLAYER_NOBODY) {
|
if (game_.whoWin() == PLAYER_NOBODY) {
|
||||||
|
|
||||||
// 重新启动计时
|
// 重新启动计时
|
||||||
timeID = startTimer(100);
|
timeID = startTimer(100);
|
||||||
|
|
||||||
// 发信号更新状态栏
|
// 发信号更新状态栏
|
||||||
message = QString::fromStdString(position_.getTips());
|
message = QString::fromStdString(game_.getTips());
|
||||||
emit statusBarChanged(message);
|
emit statusBarChanged(message);
|
||||||
#ifndef MOBILE_APP_UI
|
#ifndef MOBILE_APP_UI
|
||||||
}
|
}
|
||||||
|
@ -663,7 +663,7 @@ bool GameController::actionPiece(QPointF pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果未开局则开局
|
// 如果未开局则开局
|
||||||
if (position_.getPhase() == PHASE_NOTSTARTED)
|
if (game_.getPhase() == PHASE_NOTSTARTED)
|
||||||
gameStart();
|
gameStart();
|
||||||
|
|
||||||
// 判断执行选子、落子或去子
|
// 判断执行选子、落子或去子
|
||||||
|
@ -671,10 +671,10 @@ bool GameController::actionPiece(QPointF pos)
|
||||||
PieceItem *piece = nullptr;
|
PieceItem *piece = nullptr;
|
||||||
QGraphicsItem *item = scene.itemAt(pos, QTransform());
|
QGraphicsItem *item = scene.itemAt(pos, QTransform());
|
||||||
|
|
||||||
switch (position_.getAction()) {
|
switch (game_.getAction()) {
|
||||||
case ACTION_PLACE:
|
case ACTION_PLACE:
|
||||||
if (position_._place(r, s)) {
|
if (game_._place(r, s)) {
|
||||||
if (position_.getAction() == ACTION_CAPTURE) {
|
if (game_.getAction() == ACTION_CAPTURE) {
|
||||||
// 播放成三音效
|
// 播放成三音效
|
||||||
playSound(":/sound/resources/sound/capture.wav");
|
playSound(":/sound/resources/sound/capture.wav");
|
||||||
} else {
|
} else {
|
||||||
|
@ -692,7 +692,7 @@ bool GameController::actionPiece(QPointF pos)
|
||||||
piece = qgraphicsitem_cast<PieceItem *>(item);
|
piece = qgraphicsitem_cast<PieceItem *>(item);
|
||||||
if (!piece)
|
if (!piece)
|
||||||
break;
|
break;
|
||||||
if (position_.choose(r, s)) {
|
if (game_.choose(r, s)) {
|
||||||
// 播放选子音效
|
// 播放选子音效
|
||||||
playSound(":/sound/resources/sound/choose.wav");
|
playSound(":/sound/resources/sound/choose.wav");
|
||||||
result = true;
|
result = true;
|
||||||
|
@ -703,7 +703,7 @@ bool GameController::actionPiece(QPointF pos)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ACTION_CAPTURE:
|
case ACTION_CAPTURE:
|
||||||
if (position_._capture(r, s)) {
|
if (game_._capture(r, s)) {
|
||||||
// 播放音效
|
// 播放音效
|
||||||
playSound(":/sound/resources/sound/remove.wav");
|
playSound(":/sound/resources/sound/remove.wav");
|
||||||
result = true;
|
result = true;
|
||||||
|
@ -720,7 +720,7 @@ bool GameController::actionPiece(QPointF pos)
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
// 发信号更新状态栏
|
// 发信号更新状态栏
|
||||||
message = QString::fromStdString(position_.getTips());
|
message = QString::fromStdString(game_.getTips());
|
||||||
emit statusBarChanged(message);
|
emit statusBarChanged(message);
|
||||||
|
|
||||||
// 将新增的棋谱行插入到ListModel
|
// 将新增的棋谱行插入到ListModel
|
||||||
|
@ -728,7 +728,7 @@ bool GameController::actionPiece(QPointF pos)
|
||||||
int k = 0;
|
int k = 0;
|
||||||
|
|
||||||
// 输出命令行
|
// 输出命令行
|
||||||
for (const auto & i : *(position_.getCmdList())) {
|
for (const auto & i : *(game_.getCmdList())) {
|
||||||
// 跳过已添加的,因标准list容器没有下标
|
// 跳过已添加的,因标准list容器没有下标
|
||||||
if (k++ <= currentRow)
|
if (k++ <= currentRow)
|
||||||
continue;
|
continue;
|
||||||
|
@ -738,16 +738,16 @@ bool GameController::actionPiece(QPointF pos)
|
||||||
|
|
||||||
// 播放胜利或失败音效
|
// 播放胜利或失败音效
|
||||||
#ifndef DONOT_PLAY_WIN_SOUND
|
#ifndef DONOT_PLAY_WIN_SOUND
|
||||||
if (position_.whoWin() != PLAYER_NOBODY &&
|
if (game_.whoWin() != PLAYER_NOBODY &&
|
||||||
(manualListModel.data(manualListModel.index(currentRow - 1))).toString().contains("Time over."))
|
(manualListModel.data(manualListModel.index(currentRow - 1))).toString().contains("Time over."))
|
||||||
playSound(":/sound/resources/sound/win.wav");
|
playSound(":/sound/resources/sound/win.wav");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// AI设置
|
// AI设置
|
||||||
if (&position_ == &(this->position_)) {
|
if (&game_ == &(this->game_)) {
|
||||||
// 如果还未决出胜负
|
// 如果还未决出胜负
|
||||||
if (position_.whoWin() == PLAYER_NOBODY) {
|
if (game_.whoWin() == PLAYER_NOBODY) {
|
||||||
if (position_.context.turn == PLAYER_1) {
|
if (game_.context.turn == PLAYER_1) {
|
||||||
if (isAiPlayer[1]) {
|
if (isAiPlayer[1]) {
|
||||||
ai[1]->resume();
|
ai[1]->resume();
|
||||||
}
|
}
|
||||||
|
@ -767,7 +767,7 @@ bool GameController::actionPiece(QPointF pos)
|
||||||
ai[2]->stop();
|
ai[2]->stop();
|
||||||
|
|
||||||
// 弹框
|
// 弹框
|
||||||
//message = QString::fromStdString(position_.getTips());
|
//message = QString::fromStdString(game_.getTips());
|
||||||
//QMessageBox::about(NULL, "游戏结果", message);
|
//QMessageBox::about(NULL, "游戏结果", message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -779,7 +779,7 @@ bool GameController::actionPiece(QPointF pos)
|
||||||
|
|
||||||
bool GameController::giveUp()
|
bool GameController::giveUp()
|
||||||
{
|
{
|
||||||
bool result = position_.giveup(position_.context.turn);
|
bool result = game_.giveup(game_.context.turn);
|
||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -790,7 +790,7 @@ bool GameController::giveUp()
|
||||||
int k = 0;
|
int k = 0;
|
||||||
|
|
||||||
// 输出命令行
|
// 输出命令行
|
||||||
for (const auto & i : *(position_.getCmdList())) {
|
for (const auto & i : *(game_.getCmdList())) {
|
||||||
// 跳过已添加的,因标准list容器没有下标
|
// 跳过已添加的,因标准list容器没有下标
|
||||||
if (k++ <= currentRow)
|
if (k++ <= currentRow)
|
||||||
continue;
|
continue;
|
||||||
|
@ -798,7 +798,7 @@ bool GameController::giveUp()
|
||||||
manualListModel.setData(manualListModel.index(currentRow), i.c_str());
|
manualListModel.setData(manualListModel.index(currentRow), i.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (position_.whoWin() != PLAYER_NOBODY)
|
if (game_.whoWin() != PLAYER_NOBODY)
|
||||||
playSound(":/sound/resources/sound/loss.wav");
|
playSound(":/sound/resources/sound/loss.wav");
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -819,7 +819,7 @@ bool GameController::command(const QString &cmd, bool update /* = true */)
|
||||||
// 声音
|
// 声音
|
||||||
QString sound;
|
QString sound;
|
||||||
|
|
||||||
switch (position_.getAction()) {
|
switch (game_.getAction()) {
|
||||||
case ACTION_CHOOSE:
|
case ACTION_CHOOSE:
|
||||||
case ACTION_PLACE:
|
case ACTION_PLACE:
|
||||||
sound = ":/sound/resources/sound/drog.wav";
|
sound = ":/sound/resources/sound/drog.wav";
|
||||||
|
@ -832,44 +832,44 @@ bool GameController::command(const QString &cmd, bool update /* = true */)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果未开局则开局
|
// 如果未开局则开局
|
||||||
if (position_.getPhase() == PHASE_NOTSTARTED) {
|
if (game_.getPhase() == PHASE_NOTSTARTED) {
|
||||||
gameStart();
|
gameStart();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!position_.command(cmd.toStdString().c_str()))
|
if (!game_.command(cmd.toStdString().c_str()))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (sound == ":/sound/resources/sound/drog.wav" && position_.getAction() == ACTION_CAPTURE) {
|
if (sound == ":/sound/resources/sound/drog.wav" && game_.getAction() == ACTION_CAPTURE) {
|
||||||
sound = ":/sound/resources/sound/capture.wav";
|
sound = ":/sound/resources/sound/capture.wav";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (update) {
|
if (update) {
|
||||||
playSound(sound);
|
playSound(sound);
|
||||||
updateScence(position_);
|
updateScence(game_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发信号更新状态栏
|
// 发信号更新状态栏
|
||||||
message = QString::fromStdString(position_.getTips());
|
message = QString::fromStdString(game_.getTips());
|
||||||
emit statusBarChanged(message);
|
emit statusBarChanged(message);
|
||||||
|
|
||||||
// 对于新开局
|
// 对于新开局
|
||||||
if (position_.getCmdList()->size() <= 1) {
|
if (game_.getCmdList()->size() <= 1) {
|
||||||
manualListModel.removeRows(0, manualListModel.rowCount());
|
manualListModel.removeRows(0, manualListModel.rowCount());
|
||||||
manualListModel.insertRow(0);
|
manualListModel.insertRow(0);
|
||||||
manualListModel.setData(manualListModel.index(0), position_.getCmdLine());
|
manualListModel.setData(manualListModel.index(0), game_.getCmdLine());
|
||||||
currentRow = 0;
|
currentRow = 0;
|
||||||
}
|
}
|
||||||
// 对于当前局
|
// 对于当前局
|
||||||
else {
|
else {
|
||||||
currentRow = manualListModel.rowCount() - 1;
|
currentRow = manualListModel.rowCount() - 1;
|
||||||
// 跳过已添加行,迭代器不支持+运算符,只能一个个++
|
// 跳过已添加行,迭代器不支持+运算符,只能一个个++
|
||||||
auto i = (position_.getCmdList()->begin());
|
auto i = (game_.getCmdList()->begin());
|
||||||
for (int r = 0; i != (position_.getCmdList())->end(); i++) {
|
for (int r = 0; i != (game_.getCmdList())->end(); i++) {
|
||||||
if (r++ > currentRow)
|
if (r++ > currentRow)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// 将新增的棋谱行插入到ListModel
|
// 将新增的棋谱行插入到ListModel
|
||||||
while (i != position_.getCmdList()->end()) {
|
while (i != game_.getCmdList()->end()) {
|
||||||
manualListModel.insertRow(++currentRow);
|
manualListModel.insertRow(++currentRow);
|
||||||
manualListModel.setData(manualListModel.index(currentRow), (*i++).c_str());
|
manualListModel.setData(manualListModel.index(currentRow), (*i++).c_str());
|
||||||
}
|
}
|
||||||
|
@ -877,17 +877,17 @@ bool GameController::command(const QString &cmd, bool update /* = true */)
|
||||||
|
|
||||||
// 播放胜利或失败音效
|
// 播放胜利或失败音效
|
||||||
#ifndef DONOT_PLAY_WIN_SOUND
|
#ifndef DONOT_PLAY_WIN_SOUND
|
||||||
if (position_.whoWin() != PLAYER_NOBODY &&
|
if (game_.whoWin() != PLAYER_NOBODY &&
|
||||||
(manualListModel.data(manualListModel.index(currentRow - 1))).toString().contains("Time over.")) {
|
(manualListModel.data(manualListModel.index(currentRow - 1))).toString().contains("Time over.")) {
|
||||||
playSound(":/sound/resources/sound/win.wav");
|
playSound(":/sound/resources/sound/win.wav");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// AI设置
|
// AI设置
|
||||||
if (&position_ == &(this->position_)) {
|
if (&game_ == &(this->game_)) {
|
||||||
// 如果还未决出胜负
|
// 如果还未决出胜负
|
||||||
if (position_.whoWin() == PLAYER_NOBODY) {
|
if (game_.whoWin() == PLAYER_NOBODY) {
|
||||||
if (position_.context.turn == PLAYER_1) {
|
if (game_.context.turn == PLAYER_1) {
|
||||||
if (isAiPlayer[1]) {
|
if (isAiPlayer[1]) {
|
||||||
ai[1]->resume();
|
ai[1]->resume();
|
||||||
}
|
}
|
||||||
|
@ -920,7 +920,7 @@ bool GameController::command(const QString &cmd, bool update /* = true */)
|
||||||
|
|
||||||
#ifdef MESSAGEBOX_ENABLE
|
#ifdef MESSAGEBOX_ENABLE
|
||||||
// 弹框
|
// 弹框
|
||||||
message = QString::fromStdString(position_.getTips());
|
message = QString::fromStdString(game_.getTips());
|
||||||
QMessageBox::about(NULL, "游戏结果", message);
|
QMessageBox::about(NULL, "游戏结果", message);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -957,7 +957,7 @@ bool GameController::phaseChange(int row, bool forceUpdate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 下面这步关键,会让悔棋者承担时间损失
|
// 下面这步关键,会让悔棋者承担时间损失
|
||||||
dummyGame.setStartTime(position_.getStartTimeb());
|
dummyGame.setStartTime(game_.getStartTimeb());
|
||||||
|
|
||||||
// 刷新棋局场景
|
// 刷新棋局场景
|
||||||
updateScence(dummyGame);
|
updateScence(dummyGame);
|
||||||
|
@ -967,7 +967,7 @@ bool GameController::phaseChange(int row, bool forceUpdate)
|
||||||
|
|
||||||
bool GameController::updateScence()
|
bool GameController::updateScence()
|
||||||
{
|
{
|
||||||
return updateScence(position_);
|
return updateScence(game_);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GameController::updateScence(Game &game)
|
bool GameController::updateScence(Game &game)
|
||||||
|
|
|
@ -203,7 +203,7 @@ protected:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// 棋对象的数据模型
|
// 棋对象的数据模型
|
||||||
Game position_;
|
Game game_;
|
||||||
|
|
||||||
// 棋对象的数据模型(临时)
|
// 棋对象的数据模型(临时)
|
||||||
Game dummyGame;
|
Game dummyGame;
|
||||||
|
|
Loading…
Reference in New Issue