Last active
August 29, 2015 13:56
-
-
Save LNA/9332397 to your computer and use it in GitHub Desktop.
Example of Public versus Private Interface In Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GameState | |
# omitted code | |
def winner | |
[first_row, | |
second_row, | |
third_row, | |
left_column, | |
middle_column, | |
right_column, | |
left_diag_winner, | |
right_diag_winner | |
].each do |line| | |
if line.uniq.count == 1 | |
return line[0] | |
end | |
end | |
false | |
end | |
def tie? | |
full_board? == true && winner == false | |
end | |
def game_over? | |
winner || tie? | |
end | |
private | |
def first_row | |
@board[0..2] | |
end | |
def second_row | |
@board[3..5] | |
end | |
def third_row | |
@board[6..8] | |
end | |
def left_column | |
[@board[0], @board[3], @board[6]] | |
end | |
def middle_column | |
[@board[1], @board[4], @board[7]] | |
end | |
def right_column | |
[@board[2], @board[5], @board[8]] | |
end | |
def left_diag_winner | |
[@board[0], @board[4], @board[8]] | |
end | |
def right_diag_winner | |
[@board[2], @board[4], @board[6]] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment