我正在建立一个Tic脚趾游戏,在这个游戏中,用户可以玩电脑,或者电脑可以互相玩。在构建AI时,我遇到了以下错误。我如何调试这个?我知道这与某个地方的循环有关,但我找不到。
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
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
endlib/game.rb
def wargame_turn
input = current_player.move(board)
if !board.valid_move?(input)
wargame_turn
else
board.update(input, current_player)
end
endbin/tictactoe在#run_game中的以下块中调用该方法:
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发布于 2017-10-25 03:28:57
您可以使用ruby 示踪类
ruby -r tracer your_main_script.rb此外,您还可以查看代码并查看循环可能发生的位置:
def wargame_turn
input = current_player.move(board)
if !board.valid_move?(input)
wargame_turn #### HERE'S A POTENTIAL INFINITE LOOP所以问题的核心似乎是:
if !board.valid_move?(input)这可能是个好的开始。
https://stackoverflow.com/questions/46922125
复制相似问题