Skip to content

Instantly share code, notes, and snippets.

@pumpkincouture
Created August 4, 2014 03:29
Show Gist options
  • Save pumpkincouture/b07a0ecf1f5f70b512fe to your computer and use it in GitHub Desktop.
Save pumpkincouture/b07a0ecf1f5f70b512fe to your computer and use it in GitHub Desktop.
def winner?(board)
winning_combos=[[1,2,3], [4,5,6], [7,8,9],
[1,4,7], [2,5,8], [3,6,9],
[1,5,9], [3,5,7]]
human_win=[]
computer_win=[]
board.cells.each do |k,v|
if board.cells[k]=="X"
computer_win << k
elsif board.cells[k]=="O"
human_win << k
else
false
end
end
human_win.map!(&:to_i)
computer_win.map!(&:to_i)
winning_combos.each do |sub_array|
if sub_array.all? {|x|human_win.include?(x)}
puts "Human won!"
return true
elsif sub_array.all? {|y|computer_win.include?(y)}
puts "Computer won!"
return true
end
end
return false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment