Skip to content

Instantly share code, notes, and snippets.

@rewinfrey
Created November 29, 2012 17:50
Show Gist options
  • Save rewinfrey/4170724 to your computer and use it in GitHub Desktop.
Save rewinfrey/4170724 to your computer and use it in GitHub Desktop.
Basic minimax recursion
def minimax(max_player = true, ply = 0, min_score = 1000, max_score = -1000)
if board.winner?
return(max_player ? (-1000 + ply) : (1000 - ply))
elsif board.draw_game?
return 0
end
best_move = 0
available_moves.each do |index|
board[index] = ( max_player ? side : opposite_side(side) )
score = minimax(!max_player, ply + 1, min_score, max_score)
undo_move(index)
if max_player && score > max_score
max_score = score
best_move = index
elsif !max_player && score < min_score
min_score = score
end
end
return( ply == 0 ? best_move : ( max_player ? max_score : min_score ) )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment