首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调试堆栈级别太深的错误- Ruby

调试堆栈级别太深的错误- Ruby
EN

Stack Overflow用户
提问于 2017-10-25 00:41:51
回答 1查看 820关注 0票数 1

我正在建立一个Tic脚趾游戏,在这个游戏中,用户可以玩电脑,或者电脑可以互相玩。在构建AI时,我遇到了以下错误。我如何调试这个?我知道这与某个地方的循环有关,但我找不到。

代码语言:javascript
复制
ttt-with-ai-project-cb-000/lib/players/computer.rb:13:in `each': stack level too deep (SystemStackError)
        from ttt-with-ai-project-cb-000/lib/players/computer.rb:13:in `detect'
        from ttt-with-ai-project-cb-000/lib/players/computer.rb:13:in `check_move'
        from ttt-with-ai-project-cb-000/lib/players/computer.rb:8:in `move'
        from ttt-with-ai-project-cb-000/lib/game.rb:61:in `wargame_turn'
        from ttt-with-ai-project-cb-000/lib/game.rb:64:in `wargame_turn'
        from ttt-with-ai-project-cb-000/lib/game.rb:64:in `wargame_turn'
        from ttt-with-ai-project-cb-000/lib/game.rb:64:in `wargame_turn'
        from ttt-with-ai-project-cb-000/lib/game.rb:64:in `wargame_turn'
         ... 11900 levels...
        from bin/tictactoe:37:in `block in run_game'
        from bin/tictactoe:35:in `times'
        from bin/tictactoe:35:in `run_game'
        from bin/tictactoe:79:in `<main>'

违规方法

自由球员/球员/计算机.lib

代码语言:javascript
复制
def move(board)
    check_move(board)
end

def check_move(board)
    win_combo = Game::WIN_COMBINATIONS.detect do |indices|
    board.cells[indices[0]] == token && board.cells[indices[1]] == token || board.cells[indices[1]] == token && board.cells[indices[2]] == token || board.cells[indices[0]] == token && board.cells[indices[2]] == token
    end

    win_combo.detect {|index| board.cells[index] == " "}.join if win_combo
end

lib/game.rb

代码语言:javascript
复制
def wargame_turn
    input = current_player.move(board)

    if !board.valid_move?(input)
        wargame_turn
    else
        board.update(input, current_player)
    end
end

bin/tictactoe在#run_game中的以下块中调用该方法:

代码语言:javascript
复制
100.times do 
    game = Game.new(Players::Computer.new("X"), player_2 = Players::Computer.new("O"))
    game.wargames
    if game.winner == "X"
        x_wins += 1
    elsif game.winner == "O"
        x_wins += 1
    elsif game.draw?
        draws += 1
    end
end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-25 03:28:57

您可以使用ruby 示踪类

代码语言:javascript
复制
ruby -r tracer your_main_script.rb

此外,您还可以查看代码并查看循环可能发生的位置:

代码语言:javascript
复制
def wargame_turn

  input = current_player.move(board)

  if !board.valid_move?(input)

    wargame_turn #### HERE'S A POTENTIAL INFINITE LOOP

所以问题的核心似乎是:

代码语言:javascript
复制
if !board.valid_move?(input)

这可能是个好的开始。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46922125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档