Skip to content

Instantly share code, notes, and snippets.

@kmeister2000
Created May 15, 2013 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmeister2000/5581429 to your computer and use it in GitHub Desktop.
Save kmeister2000/5581429 to your computer and use it in GitHub Desktop.
Pair Programming Session 1 - Tic Tac Toe
class TicTacToe
end
class Player
class << self
attr_accessor :players
def clear_players
@players = []
end
end
def initialize
@moves = []
self.class.players ||= []
self.class.players << self
end
attr_accessor :moves
def play(x,y)
raise "x is out of bounds" if x < 0 || x > 2
raise "y is out of bounds" if y < 0 || y > 2
unless opponents_moves.include?([x, y])
@moves << [x, y]
else
raise "already used"
end
@winner = true
end
def opponents
self.class.players
end
def opponents_moves
opponents.map(&:moves).flatten(1)
end
def winner?
@winner
end
end
require 'rspec/autorun'
require './tictactoe'
describe TicTacToe do
before(:each) do
Player.clear_players
end
let(:player) { Player.new }
it "allows a player to win" do
player.play(0, 0)
player.play(0, 1)
player.play(0, 2)
player.should be_winner
end
it "allows a player to lose" do
player.should_not be_winner
end
it "does not allow plays outside of bounds" do
expect { player.play(-1, 0) }.to raise_error
expect { player.play(1, -1) }.to raise_error
expect { player.play(3, 1) }.to raise_error
expect { player.play(1, 4) }.to raise_error
end
it "does not allow two players to make the same move" do
player1 = Player.new
player2 = Player.new
player1.play(0,0)
expect { player2.play(0,0) }.to raise_error
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment