Permalink
Browse files

Shameless green varied winning positions

  • Loading branch information...
edgenard committed Apr 3, 2018
1 parent 15166c7 commit 8b25d5e31b8f2593df66b1db27a29db1faf0f362
Showing with 25 additions and 1 deletion.
  1. +24 −0 tic-tac-toe/tic_tac_toe.rb
  2. +1 −1 tic-tac-toe/tic_tac_toe_test.rb
@@ -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]

This comment has been minimized.

@medwards1771

medwards1771 Apr 3, 2018

Collaborator

You know what I appreciate about this code? That if you died tomorrow, I would be able to pick up the refactor.

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
@@ -1,5 +1,5 @@
require 'test/unit'
require './tic_tac_toe'
require_relative './tic_tac_toe'

class GameTest < Test::Unit::TestCase

0 comments on commit 8b25d5e

Please sign in to comment.