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

Make winning methods more alike

- show that all winning combinations follow the same pattern
- pattern: starting position, starting position + x, starting position + x * 2
  • Loading branch information...
medwards1771 committed Apr 21, 2018
1 parent 6709dc4 commit a3a07640c868a14911281f892e8c14288cdff805
Showing with 12 additions and 8 deletions.
  1. +12 −8 tic-tac-toe/lib/board.rb
@@ -3,6 +3,10 @@ class Board

NO_MOVE = -1
POSITIONS_ON_BOARD = (0..8)
ROW_STEP = 1
RIGHT_DIAGONAL_STEP = 2
COLUMN_STEP = 3
LEFT_DIAGONAL_STEP = 4

def initialize(state = "---------")
@state = state
@@ -37,26 +41,26 @@ def winner

def winning_row(position)
position_occupied?(position) &&
mark_at(position) == mark_at(position + 1) &&
mark_at(position + 1) == mark_at(position + 2)
mark_at(position) == mark_at(position + ROW_STEP) &&
mark_at(position + ROW_STEP) == mark_at(position + ROW_STEP * 2)
end

def winning_column(position)
position_occupied?(position) &&
mark_at(position) == mark_at(position + 3) &&
mark_at(position + 3) == mark_at(position + 6)
mark_at(position) == mark_at(position + COLUMN_STEP) &&
mark_at(position + COLUMN_STEP) == mark_at(position + COLUMN_STEP * 2)
end

def winning_left_diagonal(position)
position_occupied?(position) &&
mark_at(position) == mark_at(position + 4) &&
mark_at(position + 4) == mark_at(position + 8)
mark_at(position) == mark_at(position + LEFT_DIAGONAL_STEP) &&
mark_at(position + LEFT_DIAGONAL_STEP) == mark_at(position + LEFT_DIAGONAL_STEP * 2)
end

def winning_right_diagonal(position)
position_occupied?(position) &&
mark_at(position) == mark_at(position + 2) &&
mark_at(position + 2) == mark_at(position + 4)
mark_at(position) == mark_at(position + RIGHT_DIAGONAL_STEP) &&
mark_at(position + RIGHT_DIAGONAL_STEP) == mark_at(position + RIGHT_DIAGONAL_STEP * 2)
end

def position_unoccupied?(position)

1 comment on commit a3a0764

@edgenard

This comment has been minimized.

Copy link
Contributor

commented on a3a0764 Apr 24, 2018

What led to the decision to a constant? What about another parameter?

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