class Bot attr_reader :best_choice, :val def initialize board, piece @val = piece @piece = piece @opponent = switch piece end def move board return "\nGame is done!" if board.is_full? return "\nSomeone won!" if board.any_winner? minmax board, @piece board.set @piece, @best_choice end def minmax board, cur_player return score(board) if game_done?(board) scores = {} board.get_free.each do |space| # dup and clone only create a shallow clone, so it doesn't clone the array inside the board object. board.set cur_player, space scores[space] = minmax(board, switch(cur_player)) board.remove space end @best_choice, best_score = best_choice cur_player, scores best_score end def switch cur cur == " X " ? " O " : " X " end def game_done? board board.any_winner? || board.is_full? end def best_choice piece, scores if piece == @piece then #p scores scores.max_by { |_k, v| v} else scores.min_by { |_k, v| v} end end def score board #p @piece #p @opponent if board.is_winner? @piece return 10 elsif board.is_winner? @opponent return -10 end 0 end end