Created
June 14, 2013 22:15
-
-
Save marksim/5785703 to your computer and use it in GitHub Desktop.
TicTacToe TDD from my pairing session with @willpragnell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
class TicTacToe | |
def initialize | |
@x_moves = [] | |
@o_moves = [] | |
end | |
def play space | |
return false unless (1..9).include?(space) | |
return false if occupied?(space) | |
next_player << space | |
true | |
end | |
def at space | |
return 'X' if @x_moves.include?(space) | |
return 'O' if @o_moves.include?(space) | |
' ' | |
end | |
private | |
def occupied? space | |
(@x_moves + @o_moves).include?(space) | |
end | |
def next_player | |
@x_moves.count == @o_moves.count ? @x_moves : @o_moves | |
end | |
end | |
describe "TicTacToe" do | |
let(:game) { TicTacToe.new } | |
it "allows a player to play in an unoccupied space" do | |
expect(game.play(5)).to be_true | |
end | |
it "does not allow a player to play in an occupied by X" do | |
game.play(5) | |
expect(game.play(5)).to be_false | |
end | |
it "does not allow a player to play in an occupied by O" do | |
game.play(5) | |
game.play(6) | |
expect(game.play(6)).to be_false | |
end | |
it "does not allow a player to play off the board" do | |
expect(game.play(10)).to be_false | |
end | |
it "alternates between players" do | |
game.play(5) | |
game.play(6) | |
expect(game.at(5)).to eql('X') | |
expect(game.at(6)).to eql('O') | |
end | |
it "lets x go first" do | |
game.play(5) | |
expect(game.at(5)).to eql('X') | |
end | |
it "knows when a draw game occurs" | |
describe "winning" do | |
it "happens when 3 in a row" | |
it "happens when 3 in a col" | |
it "happens when 3 in a diagonal" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment