Skip to content
This repository has been archived by the owner on Apr 24, 2019. It is now read-only.

Commit

Permalink
Make winning methods more alike
Browse files Browse the repository at this point in the history
- 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 a3a0764
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions tic-tac-toe/lib/board.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

1 comment on commit a3a0764

@edgenard
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.