Skip to content

Commit

Permalink
Block opponent if player cannot win
Browse files Browse the repository at this point in the history
  • Loading branch information
karayusuf committed Dec 12, 2012
1 parent b49b0ea commit 4e02252
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 72 deletions.
4 changes: 3 additions & 1 deletion lib/player.rb
Expand Up @@ -12,7 +12,9 @@ def opponent
end

def move
@board.winning_space_for(@player)
space = @board.winning_space_for(@player)
space ||= @board.winning_space_for(opponent)
space
end

end
Expand Down
251 changes: 180 additions & 71 deletions spec/player_spec.rb
Expand Up @@ -16,109 +16,218 @@ module TicTacToe
end

describe "#move" do
context "when the player can complete a row" do
it "finds the winning space in the first column" do
player = Player.new("O", [ ["_", "_", "_"],
["_", "O", "O"],
["_", "_", "_"] ])

player.move.row_and_column.should eql "1 0"
context "winner" do
context "when the player can complete a row" do
it "finds the winning space in the first column" do
player = Player.new("O", [ ["_", "_", "_"],
["_", "O", "O"],
["_", "_", "_"] ])

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

it "finds the winning space in the second column" do
player = Player.new("X", [ ["_", "_", "_"],
["X", "_", "X"],
["_", "_", "_"] ])

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

it "finds the winning space in the third column" do
player = Player.new("X", [ ["_", "_", "_"],
["_", "_", "_"],
["X", "X", "_"] ])

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

it "finds the winning space in the second column" do
player = Player.new("X", [ ["_", "_", "_"],
["X", "_", "X"],
["_", "_", "_"] ])
context "when the player can complete a column" do
it "finds the winning space in the first row" do
player = Player.new("O", [ ["_", "_", "_"],
["_", "_", "O"],
["_", "_", "O"] ])

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

it "finds the winning space in the third column" do
player = Player.new("X", [ ["_", "_", "_"],
["_", "_", "_"],
["X", "X", "_"] ])
it "finds the winning space in the second row" do
player = Player.new("X", [ ["_", "X", "_"],
["_", "_", "_"],
["_", "X", "_"] ])

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

context "when the player can complete a column" do
it "finds the winning space in the first row" do
player = Player.new("O", [ ["_", "_", "_"],
["_", "_", "O"],
["_", "_", "O"] ])
it "finds the winning space in the third row" do
player = Player.new("O", [ ["O", "_", "_"],
["O", "_", "_"],
["_", "_", "_"] ])

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

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

player.move.row_and_column.should eql "0 2"
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

player.move.row_and_column.should eql "1 1"
it "finds the winning space in the bottom left corner" do
player = Player.new("O", [ ["_", "_", "O"],
["_", "O", "_"],
["_", "_", "_"] ])

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

it "finds the winning space in the third row" do
player = Player.new("O", [ ["O", "_", "_"],
["O", "_", "_"],
["_", "_", "_"] ])
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

player.move.row_and_column.should eql "2 0"
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

context "when the player can complete a diagonal up" do
it "finds the winning space in the top right corner" do
player = Player.new("O", [ ["_", "_", "_"],
["_", "O", "_"],
["O", "_", "_"] ])
context "block" do
context "row" do
it "blocks the opponent in the first column" do
player = Player.new("X", [ ["_", "O", "O"],
["_", "X", "_"],
["_", "_", "_"] ])

player.move.row_and_column.should eql "0 2"
end
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", "_", "_"] ])
it "blocks the opponent in the second column" do
player = Player.new("X", [ ["O", "_", "O"],
["_", "X", "_"],
["_", "_", "_"] ])

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

it "finds the winning space in the bottom left corner" do
player = Player.new("O", [ ["_", "_", "O"],
["_", "O", "_"],
["_", "_", "_"] ])
it "blocks the opponent in the third column" do
player = Player.new("X", [ ["X", "_", "_"],
["O", "O", "_"],
["_", "_", "_"] ])

player.move.row_and_column.should eql "2 0"
player.move.row_and_column.should eql "1 2"
end
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"] ])
context "column" do
it "blocks the opponent in the first row" do
player = Player.new("O", [ ["_", "_", "_"],
["X", "O", "_"],
["X", "_", "_"] ])

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

it "blocks the opponent in the second row" do
player = Player.new("O", [ ["X", "_", "_"],
["_", "O", "_"],
["X", "_", "_"] ])

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

player.move.row_and_column.should eql "0 0"
it "blocks the opponent in the third row" do
player = Player.new("O", [ ["_", "_", "X"],
["_", "O", "X"],
["_", "_", "_"] ])

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

it "finds the winning space in the center" do
player = Player.new("O", [ ["O", "_", "_"],
["_", "_", "_"],
["_", "_", "O"] ])
context "diagonal up" do
it "blocks the opponent in the bottom left corner" do
player = Player.new("X", [ ["X", "_", "O"],
["_", "O", "_"],
["_", "_", "_"] ])

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

it "blocks the opponent in the center" do
player = Player.new("X", [ ["X", "_", "O"],
["_", "_", "_"],
["O", "_", "_"] ])

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

it "blocks the opponent in the top right corner" do
player = Player.new("X", [ ["X", "_", "_"],
["_", "O", "_"],
["O", "_", "_"] ])

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

it "finds the winning space in the bottom right corner" do
player = Player.new("O", [ ["O", "_", "_"],
["_", "O", "_"],
["_", "_", "_"] ])
context "diagonal down" do
it "blocks the opponent in the top left corner" do
player = Player.new("X", [ ["_", "_", "X"],
["_", "O", "_"],
["_", "_", "O"] ])

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

it "blocks the opponent in the center" do
player = Player.new("X", [ ["O", "_", "X"],
["_", "_", "_"],
["_", "_", "O"] ])

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

it "blocks the opponent in the bottom left corner" do
player = Player.new("X", [ ["O", "_", "X"],
["_", "O", "_"],
["_", "_", "_"] ])

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

end
end

0 comments on commit 4e02252

Please sign in to comment.