Browse files

SenikTony tic_tac_toe

  • Loading branch information...
1 parent c81fd03 commit e40ca11106fac0a7586da5aacb1fe8f84c59fb58 @SenikTony SenikTony committed Jun 18, 2012
Showing with 20 additions and 7 deletions.
  1. +18 −5 lib/tic_tac_toe/game.rb
  2. +2 −2 spec/lib/tic_tac_toe/game_spec.rb
View
23 lib/tic_tac_toe/game.rb
@@ -5,26 +5,39 @@ class Game
attr_reader :players, :turns, :winner, :turns_left, :current_player
def initialize
- @players = []
- @turns = [[], [], []]
- @winner = ''
- @turns_left = 0
- @current_player = ''
+ @players = ['X', '0']
+ @turns = [Array.new(3, ' '), Array.new(3, ' '), Array.new(3, ' ')]
+ @winner = nil
+ @turns_left = 9
+ @current_player = 0
end
def turn(x, y)
+ turns[x][y] = players[current_player]
end
def can_turn?(x, y)
+ !players.include?(turns[x][y])
end
def has_winner?
+ players.include?(winner)
end
def finished?
+ if (has_winner? || !has_turns?)
+ true
+ else
+ false
+ end
end
def has_turns?
+ if turns_left > 0
+ true
+ else
+ false
+ end
end
def print_turns
View
4 spec/lib/tic_tac_toe/game_spec.rb
@@ -40,12 +40,12 @@
end
describe '#has_winner?' do
- it 'should return false when winner is X' do
+ it 'should return true when winner is X' do
subject.should_receive(:winner).and_return('X')
subject.has_winner?.should be_true
end
- it 'should return false when winner is 0' do
+ it 'should return true when winner is 0' do
subject.should_receive(:winner).and_return('0')
subject.has_winner?.should be_true
end

0 comments on commit e40ca11

Please sign in to comment.