You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Solidity井字棋合约解码输出错误求助:hex数据长度异常

井字棋合约输出解码错误排查求助

我开发了基于Minimax算法的TicTacToe(井字棋)合约,实现人机对战逻辑。玩家调用makeMove落子后,合约会自动执行makeMoveY让电脑回应对手,但调用过程中出现输出解码错误,错误信息如下:

"error": "Failed to decode output: Error: hex data is odd-length (argument="value", value="0x0", code=INVALID_ARGUMENT, version=bytes/5.7.0)"

相关函数代码如下:

function makeMove(
    uint256 _gameID,
    uint256 _xCoordinate,
    uint256 _yCoordinate
) public returns (bool success, string memory reason) {
    if (_gameID > nrOfGames) return (false, "No such game exists.");

    Game storage game = games[_gameID - 1];

    if (game.winner != Winners.None)
        return (false, "The game has already ended.");

    if (msg.sender != getCurrentPlayer(game))
        return (false, "It is not your turn.");

    if (game.board[_xCoordinate][_yCoordinate] != Players.None)
        return (false, "There is already a mark at the given coordinates.");

    game.board[_xCoordinate][_yCoordinate] = game.playerTurn;

    emit PlayerMadeMove(_gameID, msg.sender, _xCoordinate, _yCoordinate);

    Winners winner = calculateWinner(game.board);
    if (winner != Winners.None) {
        game.winner = winner;
        emit GameOver(_gameID, winner);

        return (true, "The game is over.");
    }

    nextPlayer(game);

    if (game.gameType == GameType.PlayerVsComputer) {
        makeMoveY(game, game.board);
        return (true, "Computer played. Your turn again");
    }

    return (true, "");
}

function MiniMax(
    Players[3][3] storage _board,
    uint256 _depth,
    bool _isMax
) private returns (int256) {
    if (calculateWinner(_board) == Winners.PlayerOne) return 10;
    if (calculateWinner(_board) == Winners.PlayerTwo) return -10;

    if (isBoardFull(_board)) return 0;

    int256 best;

    if (_isMax) {
        best = -1000;

        for (uint8 i = 0; i < 3; i++) {
            for (uint8 j = 0; j < 3; j++) {
                if (_board[i][j] == Players.None){
                        _board[i][j] == Players.PlayerOne;
                        best = max(best, MiniMax(_board, _depth + 1, !_isMax));
                        _board[i][j] = Players.None;
                }
            }
        }
        return best;
    } else {
        best = 1000;

        for (uint8 i = 0; i < 3; i++) {
            for (uint8 j = 0; j < 3; j++) {
                if (_board[i][j] == Players.None)
                    {
                        _board[i][j] == Players.PlayerTwo;
                        best = min(best, MiniMax(_board, _depth + 1, !_isMax));
                        _board[i][j] = Players.None;
                    }
            }
        }
        return best;
    }
}
 
function findBestMoveY(Players[3][3] storage _board)
    private
    returns (uint8[2] memory)
{
    int256 bestVal = 1000;
    int256 moveVal;
    uint8[2] memory bestMove;

    for (uint8 i = 0; i < 3; i++) {
        for (uint8 j = 0; j < 3; j++) {
            if (_board[i][j] == Players.None) {
                _board[i][j] = Players.PlayerTwo;

                moveVal = MiniMax(_board, 0, true);

                _board[i][j] = Players.None;

                if (moveVal < bestVal) {
                    bestMove[0] = i;
                    bestMove[1] = j;
                    bestVal = moveVal;
                }
            }
        }
    }
    return bestMove;
}

function makeMoveY(Game storage _game, Players[3][3] storage _board)
    private
    returns (bool success, string memory reason)
{
    if (_game.playerTurn != Players.PlayerTwo)
        return (false, "Not second players turn");
    uint8[2] memory move = findBestMoveY(_board);
    _board[move[0]][move[1]] = Players.PlayerTwo;
    nextPlayer(_game);
    return (true, "");
}

我是区块链开发新手,若有必要信息遗漏请告知,恳请帮忙排查问题。

内容的提问来源于stack exchange,提问作者umut_satir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 15:53:13