【发布时间】:2015-11-18 12:50:52
【问题描述】:
我正在编写一个 5x5 tictactoe 游戏。 我收到一个意外的运行时错误,它返回大于 4 的 ROW/COL。
每位玩家的播放次数:
玩家:3,3
电脑:0,0
玩家:1,3
电脑:0,3
玩家:3,1
电脑:0,1
玩家:0,2
Computer: 140735274172144, 4204747
我的代码:
void doCompMove(TicTacToe& t, bool firstMove) {
TicTacToe::row_index bestRow;
TicTacToe::column_index bestCol;
#ifndef ANALYSE
static int gameNum(0);
if (!(firstMove))
#else
Stopwatch sw;
sw.start();
#endif
t.clearTrans();
t.chooseMove(TicTacToe::COMPUTER, bestRow, bestCol);
#ifndef ANALYSE
else {
bestRow=gameNum%5;
bestCol=(gameNum/5)%5;
++gameNum;
}
#else
sw.stop();
//if(bestRow > 4) bestRow=rand()%5;
//if(bestCol > 4) bestCol=rand()%5;
cout<<"Tijdsduur: "<<sw<<endl;
cout<<"Transposition table size is: "<<t.getTransSize()<<endl;
cout<<"Moves considered: "<<t.getAndResetMovesConsidered()<<endl;
#endif
cout<<"Computer plays: ROW = "<<bestRow<<" COL = "<<bestCol<<endl;
t.playMove(TicTacToe::COMPUTER, bestRow, bestCol);
}
这是选择移动功能:
TicTacToe::PositionVal TicTacToe::chooseMove(Side s, row_index& bestRow, column_index& bestColumn,
PositionVal alpha, PositionVal beta, int depth) {
#ifdef ANALYSE
++movesConsidered;
#endif
static const int MAX_TABLE_DEPTH(5); //7
static const int MIN_TABLE_DEPTH(3); //5
if(depth>MAX_TABLE_DEPTH)
return UNCLEAR;
Position thisPosition(board);
if (depth>=MIN_TABLE_DEPTH && depth<=MAX_TABLE_DEPTH) {
MapItr itr(transpositions.find(thisPosition));
if (itr!=transpositions.end())
return (*itr).second;
}
Side opp(s==COMPUTER ? HUMAN : COMPUTER);
PositionVal simpleEval(positionValue());
if (simpleEval!=UNCLEAR)
return simpleEval;
PositionVal bestValue(s==COMPUTER ? alpha : beta);
for (row_index row(0); alpha<beta && row<board.numrows(); ++row)
for (column_index column(0); alpha<beta && column<board.numcols(); ++column)
if (squareIsEmpty(row, column)) {
place(row, column, s);
row_index dr;
column_index dc;
PositionVal reply(chooseMove(opp, dr, dc, alpha, beta, depth+1));
place(row, column, EMPTY);
if (s==COMPUTER && reply>bestValue || s==HUMAN && reply<bestValue) {
bestValue=reply;
if (s==COMPUTER)
alpha=bestValue;
else
beta=bestValue;
bestRow=row;
bestColumn=column;
}
}
if (depth>=MIN_TABLE_DEPTH && depth<=MAX_TABLE_DEPTH) {
transpositions[thisPosition]=bestValue;
}
return bestValue;
}
转置表是否可能已达到最大尺寸?
MapItr itr(transpositions.find(thisPosition));
if (itr!=transpositions.end())
return (*itr).second;
【问题讨论】:
-
当你说你“得到一个错误”你是什么意思?构建错误?运行时错误?出乎意料的输出?请指定并包括相关输出(构建错误、预期/实际输出、崩溃位置等)
-
@JoachimPileborg 抱歉,我更新了帖子,提供了更详细的解释。
-
尝试使用调试器。
标签: c++ transpose tic-tac-toe alpha-beta-pruning