Created
May 16, 2013 14:59
-
-
Save marksim/5592334 to your computer and use it in GitHub Desktop.
Tic Tac Toe pairing with @kmeister2000
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
class TicTacToe | |
SIZE = 3 | |
def initialize | |
@board = [] | |
SIZE.times do | |
row = [] | |
SIZE.times do | |
row << nil | |
end | |
@board << row | |
end | |
end | |
def play(mark) | |
@mark = mark | |
self | |
end | |
def at(x, y) | |
if x < 0 || x > SIZE-1 || y < 0 || y > SIZE-1 | |
raise GameRangeError | |
else | |
if @board[x][y].nil? | |
@board[x][y] = @mark | |
true | |
else | |
false | |
end | |
end | |
end | |
def winner | |
@board.each do |e| | |
if e.all? {|r| r == 'X'} | |
return 'X' | |
end | |
end | |
return nil | |
end | |
end | |
class GameRangeError < Exception; end |
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' | |
require './tictactoe' | |
# Observations | |
# TDD is not substitue for lack of domain knowledge | |
# Brittle because based on string... instead of class? Or Constant? | |
# Tic Tac Toe is a game | |
# where there is a 3x3 board of blank spaces | |
# and players take turns filling the spaces with their marks | |
# players cannot play in an occupied space | |
# if a player gets three of the same mark in a column, row or diagonal, they win | |
# if all spaces are filled but no winner exists, the cat wins | |
describe "Tic Tac Toe" do | |
let(:game) { TicTacToe.new } | |
it "does not allow you to play outside of a 3x3 board" do | |
expect { game.play('X').at( 1, 3)}.to raise_error(GameRangeError) | |
expect { game.play('X').at( 3, 1)}.to raise_error(GameRangeError) | |
expect { game.play('X').at( -1, 1)}.to raise_error(GameRangeError) | |
expect { game.play('X').at( 1, -1)}.to raise_error(GameRangeError) | |
end | |
it "allows you to play inside of the 3x3 board" do | |
game.play('X').at(1, 1).should be_true | |
end | |
it "does not allow you to play in a marked space" do | |
game.play('X').at(1,1) | |
game.play('X').at(1,1).should be_false | |
end | |
describe "winning" do | |
it "wins if the same mark is all in the same row" do | |
game.winner.should be_nil | |
game.play('X').at(1, 0) | |
game.play('X').at(1, 1) | |
game.play('X').at(1, 2) | |
game.winner.should == 'X' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment