Skip to content

Commit

Permalink
Player will win with a diagonal down
Browse files Browse the repository at this point in the history
  • Loading branch information
karayusuf committed Dec 12, 2012
1 parent e44454e commit b49b0ea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def marked_by? player
def winning_space_for? player
completes_row_for?(player) ||
completes_column_for?(player) ||
completes_diagonal_up_for?(player)
completes_diagonal_up_for?(player) ||
completes_diagonal_down_for?(player)
end

def row_neighbors
Expand All @@ -54,6 +55,12 @@ def diagonal_up_neighbors
end
end

def diagonal_down_neighbors
@board.find_spaces_excluding(self) do |space|
space.row == space.column
end
end

private

def completes_row_for? player
Expand All @@ -67,5 +74,9 @@ def completes_column_for? player
def completes_diagonal_up_for? player
open? && diagonal_up_neighbors.all? { |space| space.marked_by? player }
end

def completes_diagonal_down_for? player
open? && diagonal_down_neighbors.all? { |space| space.marked_by? player }
end
end
end
26 changes: 26 additions & 0 deletions spec/player_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ module TicTacToe
player.move.row_and_column.should eql "2 0"
end
end

context "when the player can complete a diagonal down" do
it "finds the winning space in the top left corner" do
player = Player.new("O", [ ["_", "_", "_"],
["_", "O", "_"],
["_", "_", "O"] ])

player.move.row_and_column.should eql "0 0"
end

it "finds the winning space in the center" do
player = Player.new("O", [ ["O", "_", "_"],
["_", "_", "_"],
["_", "_", "O"] ])

player.move.row_and_column.should eql "1 1"
end

it "finds the winning space in the bottom right corner" do
player = Player.new("O", [ ["O", "_", "_"],
["_", "O", "_"],
["_", "_", "_"] ])

player.move.row_and_column.should eql "2 2"
end
end
end
end
end

0 comments on commit b49b0ea

Please sign in to comment.