Skip to content

Commit

Permalink
Choose open edge as a last resort
Browse files Browse the repository at this point in the history
  • Loading branch information
karayusuf committed Dec 16, 2012
1 parent 8d94968 commit 3b0f1c6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/board.rb
Expand Up @@ -19,6 +19,14 @@ def open_spaces
@spaces.select { |space| space.open? }
end

def open_edge_spaces
open_spaces.select { |space| space.edge? }
end

def edge_spaces
@spaces.select { |space| space.edge? }
end

def open_corner_spaces
open_spaces.select { |space| space.corner? }
end
Expand Down
5 changes: 5 additions & 0 deletions lib/player.rb
Expand Up @@ -21,6 +21,7 @@ def move
space ||= center
space ||= opposite_corner
space ||= corner
space ||= edge
space
end

Expand Down Expand Up @@ -55,5 +56,9 @@ def opposite_corner
def corner
@board.open_corner_spaces.first
end

def edge
@board.open_edge_spaces.first
end
end
end
10 changes: 10 additions & 0 deletions spec/player_spec.rb
Expand Up @@ -310,5 +310,15 @@ module TicTacToe
player.move.row_and_column.should eql "0 0"
end
end

context "edge" do
it "chooses the first available edge" do
player = Player.new("X", [ ["X", "O", "X"],
["O", "X", "_"],
["O", "X", "O"] ])

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

0 comments on commit 3b0f1c6

Please sign in to comment.