c++换位5x5 tictactoe错误

c++ transposition 5x5 tictactoe error

本文关键字:错误 tictactoe 5x5 换位 c++      更新时间:2023-10-16

我正在编程一个5x5 tictactoe游戏。我得到一个意外的运行时错误,它返回一个大于4的ROW/COL。

每个玩家的游戏次数:

玩家:3,3
计算机:0,0

玩家:1,3
计算机:0.3

玩家:3,1
计算机:0,1

玩家:0,2
计算机:1407352741721444204747<--在阻止了计算机的获胜机会后,我的换位表将此作为最佳移动生成。

我的代码:

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;

看起来您的代码有一个不选择任何移动的路径。从而得到垃圾结果。

它很容易识别,只需更改如下:

void doCompMove(TicTacToe& t, bool firstMove) {
    TicTacToe::row_index bestRow = -1;
    TicTacToe::column_index bestCol = -1;

看看你会有什么结果。在那之后,你需要在逻辑上找到一个漏洞。