Skip to content

Instantly share code, notes, and snippets.

@rubyonrailstutor
Created October 19, 2012 23:35
Show Gist options
  • Save rubyonrailstutor/3921299 to your computer and use it in GitHub Desktop.
Save rubyonrailstutor/3921299 to your computer and use it in GitHub Desktop.
threadflip interview
require 'rspec'
class TicTacToe
attr_accessor :board, :turns
def initialize
@turns = 0
@board = []
9.times do
@board << "#"
end
# @board
# @turns
end
def move(location, mark)
if @board[location.to_i-1] =~ /(x|o)/
return
else
@board[location.to_i-1] = mark.to_s.downcase
@turns += 1
end
@board
end
def play
self.print_board
p "input turns by entering 1,x 2,o where 1-9 are the spaces on a board"
p "x == lower case X and o == lower case O"
while @turns < 10
print "please enter a turn=>"
input = gets.chomp
if input =~ /[1-9]{1},{1}(x|o){1}/
input = input.split(",")
move(input[0],input[1])
self.print_board
p "#{9 - @turns} spaces left in the game"
if self.won?("x") == true
p "x has won the game"
return
elsif self.won?("o") == true
p "o has won the game"
return
elsif self.won?("o") == false && self.won?("x") == false && 9-@turns == 0
p "this game has ended in a draw"
return
end
else
p "please input turn correctly"
p "input turns by entering 1,x 2,o where 1-9 are the spaces on a board"
end
end
end
def won?(letter)
if @board[0] + @board[1] + @board[2] == letter*3
true
elsif @board[3]+@board[4]+@board[5] == letter*3
true
elsif @board[6]+@board[7]+@board[8] == letter*3
true
elsif @board[0]+@board[3]+@board[6] == letter*3
true
elsif @board[1]+@board[4]+@board[7] == letter*3
true
elsif @board[2]+@board[5]+@board[8] == letter*3
true
elsif @board[0]+@board[4]+@board[8] == letter*3
true
elsif @board[2]+@board[4]+@board[6] == letter*3
true
else
false
end
end
def print_board
top = @board[0..2]
middle = @board[3..5]
bottom = @board[6..8]
p top
p middle
p bottom
end
end
describe "TicTacToe" do
context "it should initialize with an empty board" do
it { TicTacToe.new.board.should eq ['#','#','#','#','#','#','#','#','#'] }
end
context "#move" do
before :each do
@threadflip = TicTacToe.new
end
it { @threadflip.move(1,"x").should == ['x','#','#','#','#','#','#','#','#'] }
it { @threadflip.move(1,"X").should == ['x','#','#','#','#','#','#','#','#'] }
it { @threadflip.move(5,"o").should == ['#','#','#','#','o','#','#','#','#'] }
end
context "#game_won" do
it "should return return true won" do
game = TicTacToe.new
game.move(1,"x"); game.move(2,"x"); game.move(3,"x"); game.won?("x").should be true
end
it "should return true when a diagnol win occurs" do
game = TicTacToe.new
game.move(1,"x"); game.move(5,"x"); game.move(9,"x"); game.won?("x").should be true
end
it "should return false in all other situations" do
game = TicTacToe.new
game.move(1,"x")
game.won?("x").should be false
end
end
context "#play" do
it "should allow players to play until win or draw occurs" do
end
end
end
# threadflip = TicTacToe.new
# threadflip.play
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment