Skip to content

Instantly share code, notes, and snippets.

@itzsaga
Created March 21, 2017 02:28
Show Gist options
  • Select an option

  • Save itzsaga/c70c20beb600a7c1118ad573d8656b45 to your computer and use it in GitHub Desktop.

Select an option

Save itzsaga/c70c20beb600a7c1118ad573d8656b45 to your computer and use it in GitHub Desktop.
module Players
class Computer < Player
def move(board)
if board.cells.count{|square| square != " " } == 0
"1"
elsif board.cells.count{|square| square != " " } == 1
board.cells.find_index("X") == 4 ? "1" : "5"
else
possible_moves = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
valid_moves = []
possible_moves.each do |move|
valid_moves << move if board.cells[move.to_i-1] == " "
end
valid_moves.sample
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment