Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.0-flash-grounding· GoogleHugging Face LMArena Search Arena 24K

#include <iostream> #include <string> #include <cstdlib> #include <ctime> using…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-4o-search-preview’s answer.
  1. User

    #include #include #include #include

    using namespace std;

    int currBotColor; // 我所执子颜色(1为黑,-1为白,棋盘状态亦同) int gridInfo[7][7] = { 0 }; // 先x后y,记录棋盘状态 int blackPieceCount = 2, whitePieceCount = 2; int startX, startY, resultX, resultY; static int delta[24][2] = { { 1,1 },{ 0,1 },{ -1,1 },{ -1,0 }, { -1,-1 },{ 0,-1 },{ 1,-1 },{ 1,0 }, { 2,0 },{ 2,1 },{ 2,2 },{ 1,2 }, { 0,2 },{ -1,2 },{ -2,2 },{ -2,1 }, { -2,0 },{ -2,-1 },{ -2,-2 },{ -1,-2 }, { 0,-2 },{ 1,-2 },{ 2,-2 },{ 2,-1 } };

    // 判断是否在地图内 inline bool inMap(int x, int y) { if (x < 0 || x > 6 || y < 0 || y > 6) return false; return true; }

    // 向Direction方向改动坐标,并返回是否越界 inline bool MoveStep(int& x, int& y, int Direction) { x = x + delta[Direction][0]; y = y + delta[Direction][1]; return inMap(x, y); }

    // 在坐标处落子,检查是否合法或模拟落子 bool ProcStep(int x0, int y0, int x1, int y1, int color) { if (color == 0) return false; if (x1 == -1) // 无路可走,跳过此回合 return true; if (!inMap(x0, y0) || !inMap(x1, y1)) // 超出边界 return false; if (gridInfo[x0][y0] != color) return false; int dx, dy, x, y, currCount = 0, dir; int effectivePoints[8][2]; dx = abs((x0 - x1)), dy = abs((y0 - y1)); if ((dx == 0 && dy == 0) || dx > 2 || dy > 2) // 保证不会移动到原来位置,而且移动始终在5×5区域内 return false; if (gridInfo[x1][y1] != 0) // 保证移动到的位置为空 return false; if (dx == 2 || dy == 2) // 如果走的是5×5的外围,则不是复制粘贴 gridInfo[x0][y0] = 0; else { if (color == 1) blackPieceCount++; else whitePieceCount++; }

        gridInfo[x1][y1] = color;
        for (dir = 0; dir < 8; dir++) // 影响邻近8个位置
        {
                x = x1 + delta[dir][0];
                y = y1 + delta[dir][1];
                if (!inMap(x, y))
                        continue;
                if (gridInfo[x][y] == -color) {
                        effectivePoints[currCount][0] = x;
                        effectivePoints[currCount][1] = y;
                        currCount++;
                        gridInfo[x][y] = color;
                }
        }
        if (currCount != 0) {
                if (color == 1) {
                        blackPieceCount += currCount;
                        whitePieceCount -= currCount;
                }
                else {
                        whitePieceCount += currCount;
                        blackPieceCount -= currCount;
                }
        }
        return true;
    

    } int EvaluateBoard() { return currBotColor == 1 ? blackPieceCount - whitePieceCount : whitePieceCount - blackPieceCount; } void CopyBoard(int src[7][7], int dest[7][7]) { for (int i = 0; i < 7; ++i) for (int j = 0; j < 7; ++j) dest[i][j] = src[i][j]; } void GenerateMoves(int color, int moves[][4], int& moveCount) { moveCount = 0; for (int y0 = 0; y0 < 7; y0++) { for (int x0 = 0; x0 < 7; x0++) { if (gridInfo[x0][y0] != color) continue; for (int dir = 0; dir < 24; dir++) { int x1 = x0 + delta[dir][0]; int y1 = y0 + delta[dir][1]; if (!inMap(x1, y1)) continue; if (gridInfo[x1][y1] != 0) continue; moves[moveCount][0] = x0; moves[moveCount][1] = y0; moves[moveCount][2] = x1; moves[moveCount][3] = y1; moveCount++; } } } }

    int AlphaBeta(int depth, int alpha, int beta, bool isMax, int color) { if (depth == 0) { return EvaluateBoard(); }

        int moves[100][4], moveCount = 0;
        GenerateMoves(color, moves, moveCount);
    
        if (moveCount == 0)
                return EvaluateBoard(); // 无路可走,返回当前估值
    
        int backupGrid[7][7];
        int backupBlack = blackPieceCount;
        int backupWhite = whitePieceCount;
    
        if (isMax) {
                int maxEval = -100000;
                for (int i = 0; i < moveCount; i++) {
                        int x0 = moves[i][0], y0 = moves[i][1];
                        int x1 = moves[i][2], y1 = moves[i][3];
    
                        CopyBoard(gridInfo, backupGrid);
                        int tempBlack = blackPieceCount;
                        int tempWhite = whitePieceCount;
    
                        ProcStep(x0, y0, x1, y1, color);
    
                        int eval = AlphaBeta(depth - 1, alpha, beta, false, -color);
    
                        maxEval = max(maxEval, eval);
                        alpha = max(alpha, eval);
    
                        // 恢复现场
                        CopyBoard(backupGrid, gridInfo);
                        blackPieceCount = tempBlack;
                        whitePieceCount = tempWhite;
    
                        if (beta <= alpha)
                                break; // β剪枝
                }
                return maxEval;
        }
        else {
                int minEval = 100000;
                for (int i = 0; i < moveCount; i++) {
                        int x0 = moves[i][0], y0 = moves[i][1];
                        int x1 = moves[i][2], y1 = moves[i][3];
    
                        CopyBoard(gridInfo, backupGrid);
                        int tempBlack = blackPieceCount;
                        int tempWhite = whitePieceCount;
    
                        ProcStep(x0, y0, x1, y1, color);
    
                        int eval = AlphaBeta(depth - 1, alpha, beta, true, -color);
    
                        minEval = min(minEval, eval);
                        beta = min(beta, eval);
    
                        // 恢复现场
                        CopyBoard(backupGrid, gridInfo);
                        blackPieceCount = tempBlack;
                        whitePieceCount = tempWhite;
    
                        if (beta <= alpha)
                                break; // α剪枝
                }
                return minEval;
        }
    

    } void GetBestMove(int& nextX, int& nextY, int& nextX1, int& nextY1) { int moves[100][4], moveCount = 0; GenerateMoves(currBotColor, moves, moveCount);

        int bestScore = -100000;
    
        int backupGrid[7][7];
        int backupBlack = blackPieceCount;
        int backupWhite = whitePieceCount;
    
        for (int i = 0; i < moveCount; i++) {
                int x0 = moves[i][0], y0 = moves[i][1];
                int x1 = moves[i][2], y1 = moves[i][3];
    
                CopyBoard(gridInfo, backupGrid);
                int tempBlack = blackPieceCount;
                int tempWhite = whitePieceCount;
    
                ProcStep(x0, y0, x1, y1, currBotColor);
    
                // alpha = -∞, beta = +∞,从我方角度开始(极大层)
                int score = AlphaBeta(2, -100000, 100000, false, -currBotColor); // 可调节搜索深度
    
                if (score > bestScore) {
                        bestScore = score;
                        nextX = x0;
                        nextY = y0;
                        nextX1 = x1;
                        nextY1 = y1;
                }
    
                // 恢复现场
                CopyBoard(backupGrid, gridInfo);
                blackPieceCount = tempBlack;
                whitePieceCount = tempWhite;
        }
    

    } int main() { istream::sync_with_stdio(false);

        int x0, y0, x1, y1;
    
        // 初始化棋盘
        gridInfo[0][0] = gridInfo[6][6] = 1;  //黑
        gridInfo[6][0] = gridInfo[0][6] = -1; //白
    
        // 分析自己收到的输入和自己过往的输出,并恢复状态
        int turnID;
        currBotColor = -1; // 假定我方是白方
        cin >> turnID;
        for (int i = 0; i < turnID - 1; i++) {
                // 根据这些输入输出逐渐恢复状态到当前回合
                cin >> x0 >> y0 >> x1 >> y1;
                if (x1 >= 0)
                        ProcStep(x0, y0, x1, y1, -currBotColor); // 模拟对方落子
                else
                        currBotColor = 1; // 第一回合收到坐标是-1, -1,-1, -1说明我是黑方
                cin >> x0 >> y0 >> x1 >> y1;
                if (x1 >= 0)
                        ProcStep(x0, y0, x1, y1, currBotColor); // 模拟己方落子
        }
    
        // 看看自己本回合输入,即对方上一轮的决策结果
        cin >> x0 >> y0 >> x1 >> y1;
        if (x1 >= 0)
                ProcStep(x0, y0, x1, y1, -currBotColor); // 模拟对方落子
        else
                currBotColor = 1;
    
        //此时gridInfo[][]里存储的就是当前棋盘的所有棋子信息
    
    
    
        // 找出合法落子点
        int beginPos[1000][2], possiblePos[1000][2], posCount = 0, choice, dir;
        // 看我方可否将(x0,y0)的棋子移动至(x1,y1)
        for (y0 = 0; y0 < 7; y0++)
                for (x0 = 0; x0 < 7; x0++) {
                        if (gridInfo[x0][y0] != currBotColor) //若(x0,y0)不是我方的棋子,则忽略
                                continue;
                        for (dir = 0; dir < 24; dir++) {
                                x1 = x0 + delta[dir][0]; //考察可能的落子点(x1,y1)
                                y1 = y0 + delta[dir][1];
                                if (!inMap(x1, y1))  //若(x1,y1)不在棋盘里,则不能落子
                                        continue;
                                if (gridInfo[x1][y1] != 0) //若(x1,y1)不是空位,则不能落子
                                        continue;
                                beginPos[posCount][0] = x0;
                                beginPos[posCount][1] = y0;
                                possiblePos[posCount][0] = x1;
                                possiblePos[posCount][1] = y1;
                                posCount++;
                        }
                }
        //上述循环结束后,beginPos存的是可取的合法棋子位置,possiblePos的是合法的落子位置
    
        // 做出决策
        int nextX = -1, nextY = -1, nextX1 = -1, nextY1 = -1;
    
        if (posCount > 0) {
                GetBestMove(nextX, nextY, nextX1, nextY1);
                startX = nextX;
                startY = nextY;
                resultX = nextX1;
                resultY = nextY1;
        }
        else {
                startX = -1;
                startY = -1;
                resultX = -1;
                resultY = -1;
        }
    
        // 决策结束,输出结果(你只需修改以上部分)
        //将我方(startX,startY)的棋子移动至(resultX,resultY)
        cout << startX << " " << startY << " " << resultX << " " << resultY;
        return 0;
    

    } 程序运行会崩溃使用中文回复

  2. Gemini Assistant

    你的程序在运行过程中崩溃,这通常是由于以下几个原因造成的:

    1. 数组越界访问: delta 数组的大小是 [24][2]。在 ProcStep 函数中,对邻近8个位置进行影响时,使用了 delta 数组的前8个元素(0-7),这是安全的。但如果在其他地方,比如循环遍历 delta 数组的时候,访问了超出范围的索引,会导致程序崩溃。

    2. 非法内存访问: 在 CopyBoard 函数中,如果 src 或 dest 指针为空,或者指向的内存区域无效,会导致程序崩溃。尽管你的代码看起来没有直接的指针操作,但数组名在某种程度上可以被视为指针。

    3. 递归过深导致堆栈溢出: AlphaBeta 函数是一个递归函数。如果搜索深度过大,可能导致堆栈溢出,从而使程序崩溃。你设置的搜索深度为2,相对较小,一般不会导致堆栈溢出,但如果棋局复杂,moveCount很大,也会加剧这个问题。

    4. 未初始化的变量: 虽然你的代码中变量大多都进行了初始化,但仍然需要检查,特别是与棋盘状态相关的变量,确保在每次使用前都已正确初始化。

    5. 逻辑错误导致死循环或无限递归: 某些逻辑错误可能导致程序进入死循环或无限递归,最终耗尽系统资源而崩溃。例如,ProcStep 函数如果因为某种原因没有正确更新棋盘状态,可能导致 AlphaBeta 函数无限递归。

    6. blackPieceCount 和 whitePieceCount 的同步问题: 在 ProcStep 中,blackPieceCount 和 whitePieceCount 需要正确更新。如果在更新过程中出现错误,可能会导致评估函数返回错误的结果,进而影响搜索算法。

    7. GenerateMoves 函数的效率: 每次调用 AlphaBeta 都会重新生成所有可能的 move,如果你的棋盘状态比较复杂,可能的 move 数量会非常大,这会显著降低搜索效率。

    以下是一些建议,可以帮助你调试并修复程序:

    1. 添加边界检查: 在所有访问数组的地方,特别是 delta 数组,添加边界检查,确保索引值在有效范围内。
    2. 使用调试器: 使用Visual Studio Code, GDB或者其他调试器来单步执行程序,观察变量的值,找出程序崩溃的具体位置。
    3. 缩小问题范围: 注释掉部分代码,例如 AlphaBeta 函数中的循环,逐步缩小问题范围,找出导致崩溃的具体代码段。
    4. 打印调试信息: 在关键函数中添加 cout 语句,打印变量的值,例如 x0, y0, x1, y1, color, blackPieceCount, whitePieceCount 等,帮助你理解程序的运行状态。
    5. 检查递归深度: 在 AlphaBeta 函数中添加深度限制,避免无限递归。
    6. 优化代码: 考虑优化代码,例如使用更高效的数据结构或算法,减少计算量。
    7. 确保同步: 仔细检查 ProcStep 函数中 blackPieceCount 和 whitePieceCount 的更新逻辑,确保与棋盘状态同步。

    我将提供一些改进后的代码片段,重点关注可能出错的地方:

    // 确保在地图内
    inline bool inMap(int x, int y) {
        return (x &gt;= 0 &amp;&amp; x &lt; 7 &amp;&amp; y &gt;= 0 &amp;&amp; y &lt; 7);
    }
    
    // ProcStep 函数
    bool ProcStep(int x0, int y0, int x1, int y1, int color) {
        if (color == 0)
            return false;
        if (x1 == -1) // 无路可走,跳过此回合
            return true;
        if (!inMap(x0, y0) || !inMap(x1, y1)) // 超出边界
            return false;
        if (gridInfo[x0][y0] != color)
            return false;
        int dx, dy, x, y, currCount = 0, dir;
        int effectivePoints[8][2];
        dx = abs((x0 - x1)), dy = abs((y0 - y1));
        if ((dx == 0 &amp;&amp; dy == 0) || dx &gt; 2 || dy &gt; 2) // 保证不会移动到原来位置,而且移动始终在5×5区域内
            return false;
        if (gridInfo[x1][y1] != 0) // 保证移动到的位置为空
            return false;
        if (dx == 2 || dy == 2) // 如果走的是5×5的外围,则不是复制粘贴
            gridInfo[x0][y0] = 0;
        else {
            if (color == 1)
                blackPieceCount++;
            else
                whitePieceCount++;
        }
    
        gridInfo[x1][y1] = color;
        for (dir = 0; dir &lt; 8; dir++) // 影响邻近8个位置
        {
            x = x1 + delta[dir][0];
            y = y1 + delta[dir][1];
            if (!inMap(x, y))
                continue;
            if (gridInfo[x][y] == -color) {
                effectivePoints[currCount][0] = x;
                effectivePoints[currCount][1] = y;
                currCount++;
                gridInfo[x][y] = color;
            }
        }
        if (currCount != 0) {
            if (color == 1) {
                blackPieceCount += currCount;
                whitePieceCount -= currCount;
            }
            else {
                whitePieceCount += currCount;
                blackPieceCount -= currCount;
            }
        }
        return true;
    }
    
    // AlphaBeta 函数 (添加深度限制)
    int AlphaBeta(int depth, int alpha, int beta, bool isMax, int color) {
        if (depth == 0) {
            return EvaluateBoard();
        }
    
        int moves[100][4], moveCount = 0;
        GenerateMoves(color, moves, moveCount);
    
        if (moveCount == 0)
            return EvaluateBoard(); // 无路可走,返回当前估值
    
        int backupGrid[7][7];
        int backupBlack = blackPieceCount;
        int backupWhite = whitePieceCount;
    
        if (isMax) {
            int maxEval = -100000;
            for (int i = 0; i &lt; moveCount; i++) {
                int x0 = moves[i][0], y0 = moves[i][1];
                int x1 = moves[i][2], y1 = moves[i][3];
    
                CopyBoard(gridInfo, backupGrid);
                int tempBlack = blackPieceCount;
                int tempWhite = whitePieceCount;
    
                bool validMove = ProcStep(x0, y0, x1, y1, color);
                if (!validMove) continue; // 如果move不合法,则跳过
    
                int eval = AlphaBeta(depth - 1, alpha, beta, false, -color);
    
                maxEval = max(maxEval, eval);
                alpha = max(alpha, eval);
    
                // 恢复现场
                CopyBoard(backupGrid, gridInfo);
                blackPieceCount = tempBlack;
                whitePieceCount = tempWhite;
    
                if (beta &lt;= alpha)
                    break; // β剪枝
            }
            return maxEval;
        }
        else {
            int minEval = 100000;
            for (int i = 0; i &lt; moveCount; i++) {
                int x0 = moves[i][0], y0 = moves[i][1];
                int x1 = moves[i][2], y1 = moves[i][3];
    
                CopyBoard(gridInfo, backupGrid);
                int tempBlack = blackPieceCount;
                int tempWhite = whitePieceCount;
    
                bool validMove = ProcStep(x0, y0, x1, y1, color);
                 if (!validMove) continue; // 如果move不合法,则跳过
    
                int eval = AlphaBeta(depth - 1, alpha, beta, true, -color);
    
                minEval = min(minEval, eval);
                beta = min(beta, eval);
    
                // 恢复现场
                CopyBoard(backupGrid, gridInfo);
                blackPieceCount = tempBlack;
                whitePieceCount = tempWhite;
    
                if (beta &lt;= alpha)
                    break; // α剪枝
            }
            return minEval;
        }
    }
    

    请按照上述建议进行调试和修改,找出程序崩溃的根本原因。逐步添加调试信息,缩小问题范围,最终解决问题。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.