Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. @board = ['(1)', '(2)', '(3)', '(4)', '(5)', '(6)', '(7)', '(8)', '(9)']
  2.  
  3. def is_winner?
  4. row_one = @board[0..2]
  5. row_two = @board[3..5]
  6. row_three = @board[6..8]
  7. column_one = @board[0], @board[3], @board[6]
  8. column_two = @board[1], @board[4], @board[7]
  9. column_three = @board[2], @board[5], @board[8]
  10. diagonal_one = @board[0], @board[4], @board[8]
  11. diagonal_two = @board[2], @board[4], @board[6]
  12. possible_winners = [row_one, row_two, row_three, column_one,
  13. column_two, column_three, diagonal_one, diagonal_two]
  14.  
  15. possible_winners.each do |e|
  16. if e.all? { |x| x.to_s == ' X ' }
  17. puts "#{@player_one} Wins!"
  18. return true
  19. elsif e.all? { |x| x == ' O ' }
  20. puts "#{@player_two} Wins!"
  21. return true
  22. else
  23. return false
  24. end
  25.  
  26. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement