|
@@ -28,6 +28,12 @@ def winning_move?(square, player) |
|
|
|
def winner |
|
|
|
first_position_in_rows = [0, 3, 6] |
|
|
|
first_position_in_rows.each { |position| return mark_at(position) if winning_row(position) } |
|
|
|
first_position_in_columns = [0, 1, 2] |
|
|
|
first_position_in_columns.each { |position| return mark_at(position) if winning_column(position) } |
|
|
|
first_position_in_left_diagonal = 0 |
|
|
|
return mark_at(first_position_in_left_diagonal) if winning_left_diagonal(first_position_in_left_diagonal) |
|
|
|
first_position_in_right_diagonal = 2 |
|
|
|
return mark_at(first_position_in_right_diagonal) if winning_right_diagonal(first_position_in_right_diagonal) |
|
|
|
return '-' |
|
|
|
end |
|
|
|
|
|
@@ -37,6 +43,24 @@ def winning_row(position) |
|
|
|
mark_at(position.next) == mark_at(position.next.next) |
|
|
|
end |
|
|
|
|
|
|
|
def winning_column(position) |
|
|
|
position_occupied?(position) && |
|
|
|
mark_at(position) == mark_at(position + 3) && |
|
|
|
mark_at(position + 3) == mark_at(position + 6) |
|
|
|
end |
|
|
|
|
|
|
|
def winning_left_diagonal(position) |
|
|
|
position_occupied?(position) && |
|
|
|
mark_at(position) == mark_at(position + 4) && |
|
|
|
mark_at(position + 4) == mark_at(position + 8) |
|
|
|
end |
|
|
|
|
|
|
|
def winning_right_diagonal(position) |
|
|
|
position_occupied?(position) && |
|
|
|
mark_at(position) == mark_at(position + 2) && |
|
|
|
mark_at(position + 4) == mark_at(position + 4) |
|
|
|
end |
|
|
|
|
|
|
|
def position_unoccupied?(position) |
|
|
|
mark_at(position) == '-' |
|
|
|
end |
|
|
You know what I appreciate about this code? That if you died tomorrow, I would be able to pick up the refactor.