Permalink
Browse files

Introduce mark_at method to express intent

  • Loading branch information...
edgenard committed Apr 3, 2018
1 parent 4ccb073 commit 64d02e582a68d2e32e41cee200f2d8003417d9c0
Showing with 10 additions and 6 deletions.
  1. +10 −6 tic-tac-toe/tic_tac_toe.rb
@@ -27,21 +27,25 @@ def winning_move?(square, player)

def winner
first_position_in_rows = [0, 3, 6]
first_position_in_rows.each { |position| return winning_player(position) if winning_player(position) }
first_position_in_rows.each { |position| return mark_at(position) if winning_player(position) }

This comment has been minimized.

@medwards1771

medwards1771 Apr 3, 2018

Collaborator

I've read through this loop a couple times and still can't wrap my head around what's going on. What's happening here?

return '-'
end

def winning_player(position)
if position_occupied?(position) && board[position] == board[position.next] && board[position.next] == board[position.next.next]
return board[position]
end
position_occupied?(position) &&
mark_at(position) == mark_at(position.next) &&
mark_at(position.next) == mark_at(position.next.next)
end

def position_unoccupied?(position)
board[position] == '-'
mark_at(position) == '-'
end

def position_occupied?(position)
board[position] != '-'
mark_at(position) != '-'
end

def mark_at(position)
board[position]
end
end

0 comments on commit 64d02e5

Please sign in to comment.