Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Inject direction param into #winning_combo

- this allows us to use a single method to determine winning combination
  • Loading branch information...
medwards1771 committed Apr 21, 2018
1 parent a3a0764 commit f21ac81812713a0bbf19faca7b2bb5c967251324
Showing with 13 additions and 23 deletions.
  1. +13 −23 tic-tac-toe/lib/board.rb
@@ -29,38 +29,28 @@ def winning_move?(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_rows.each { |position| return mark_at(position) if winning_combo?(position, "ROW") }

first_position_in_columns = [0, 1, 2]
first_position_in_columns.each { |position| return mark_at(position) if winning_column(position) }
first_position_in_columns.each { |position| return mark_at(position) if winning_combo?(position, "COLUMN") }

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
return mark_at(first_position_in_left_diagonal) if winning_combo?(first_position_in_left_diagonal, "LEFT_DIAGONAL")

def winning_row(position)
position_occupied?(position) &&
mark_at(position) == mark_at(position + ROW_STEP) &&
mark_at(position + ROW_STEP) == mark_at(position + ROW_STEP * 2)
end
first_position_in_right_diagonal = 2
return mark_at(first_position_in_right_diagonal) if winning_combo?(first_position_in_right_diagonal, "RIGHT_DIAGONAL")

def winning_column(position)
position_occupied?(position) &&
mark_at(position) == mark_at(position + COLUMN_STEP) &&
mark_at(position + COLUMN_STEP) == mark_at(position + COLUMN_STEP * 2)
return '-'
end

def winning_left_diagonal(position)
def winning_combo?(position, direction)
position_occupied?(position) &&
mark_at(position) == mark_at(position + LEFT_DIAGONAL_STEP) &&
mark_at(position + LEFT_DIAGONAL_STEP) == mark_at(position + LEFT_DIAGONAL_STEP * 2)
mark_at(position) == mark_at(position + step_for(direction)) &&
mark_at(position + step_for(direction)) == mark_at(position + step_for(direction) * 2)
end

def winning_right_diagonal(position)
position_occupied?(position) &&
mark_at(position) == mark_at(position + RIGHT_DIAGONAL_STEP) &&
mark_at(position + RIGHT_DIAGONAL_STEP) == mark_at(position + RIGHT_DIAGONAL_STEP * 2)
def step_for(direction)
self.class.const_get("#{direction}_STEP")
end

def position_unoccupied?(position)

0 comments on commit f21ac81

Please sign in to comment.
You can’t perform that action at this time.