我正在用头撞墙,试图为井字游戏实施 negamax
def negamax(board_obj, mark, depth)
if board_obj.game_over?
return value(board_obj)
else
max = -1.0/0 # negative infinity
if mark == @mark
next_mark = @opponent_mark
else
next_mark = @mark
end
board_obj.empty_squares.each do |square|
board_obj[square] = mark
x = -negamax(board_obj, next_mark, depth + 1)
board_obj[square] = ' '
if x > max
max = x
@scores << x
@best_move = square if depth == 1
end
end
return max
end
end
# determines value of final board state
def value(board_obj)
if board_obj.mark_win?(@mark)
return 1
elsif board_obj.mark_win?(@opponent_mark)
return -1
else
return 0
end
end
其余代码在这里:https ://github.com/dave-maldonado/tic-tac-doh/blob/AI/tic-tac-doh.rb
它确实产生了结果,但人工智能很容易被击败,所以我知道出了点问题,感谢任何帮助!