Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @board = ['(1)', '(2)', '(3)', '(4)', '(5)', '(6)', '(7)', '(8)', '(9)']
- def is_winner?
- row_one = @board[0..2]
- row_two = @board[3..5]
- row_three = @board[6..8]
- column_one = @board[0], @board[3], @board[6]
- column_two = @board[1], @board[4], @board[7]
- column_three = @board[2], @board[5], @board[8]
- diagonal_one = @board[0], @board[4], @board[8]
- diagonal_two = @board[2], @board[4], @board[6]
- possible_winners = [row_one, row_two, row_three, column_one,
- column_two, column_three, diagonal_one, diagonal_two]
- possible_winners.each do |e|
- if e.all? { |x| x.to_s == ' X ' }
- puts "#{@player_one} Wins!"
- return true
- elsif e.all? { |x| x == ' O ' }
- puts "#{@player_two} Wins!"
- return true
- else
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement