我正在构建一个井字游戏,用户可以在其中玩电脑,或者电脑可以互相玩。在构建 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/players/computer.rb
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
库/游戏.rb
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 中以下块中的方法:
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