class Tictactoe::Game

Attributes

board_size[RW]
current_player[RW]
o_type[RW]
players_factory[RW]
state[RW]
x_type[RW]

Public Class Methods

new(players_factory, board_size, x_type, o_type) click to toggle source
# File lib/tictactoe/game.rb, line 10
def initialize(players_factory, board_size, x_type, o_type)
  self.players_factory = players_factory
  self.board_size = board_size
  self.x_type = x_type
  self.o_type = o_type
  reset
end

Public Instance Methods

available() click to toggle source
# File lib/tictactoe/game.rb, line 42
def available
  state.available_moves
end
is_finished?() click to toggle source
# File lib/tictactoe/game.rb, line 30
def is_finished?
  state.is_finished?
end
marks() click to toggle source
# File lib/tictactoe/game.rb, line 38
def marks
  state.marks
end
ready_to_tick?() click to toggle source
# File lib/tictactoe/game.rb, line 26
def ready_to_tick?
  !is_finished? && current_player.value.ready_to_move?
end
tick() click to toggle source
# File lib/tictactoe/game.rb, line 18
def tick
  move = get_move
  if is_valid?(move) && !is_finished?
    update_state(move)
    advance_player
  end
end
winner() click to toggle source
# File lib/tictactoe/game.rb, line 34
def winner
  state.winner
end

Private Instance Methods

advance_player() click to toggle source
# File lib/tictactoe/game.rb, line 57
def advance_player
  self.current_player = current_player.next
end
get_move() click to toggle source
# File lib/tictactoe/game.rb, line 61
def get_move
  current_player.value.get_move(state)
end
is_valid?(move) click to toggle source
# File lib/tictactoe/game.rb, line 49
def is_valid?(move)
  move && state.available_moves.include?(move)
end
reset() click to toggle source
# File lib/tictactoe/game.rb, line 65
def reset
  reset_players
  reset_state
end
reset_players() click to toggle source
# File lib/tictactoe/game.rb, line 70
def reset_players
  first_mark = Sequence.new([:x, :o]).first
  players = [first_mark, first_mark.next].zip([x_type, o_type]).map do |mark, type|
    players_factory.create(type, mark)
  end

  self.current_player = Sequence.new(players).first
end
reset_state() click to toggle source
# File lib/tictactoe/game.rb, line 79
def reset_state
  self.state = State.new(Boards::Square.new(board_size))
end
update_state(move) click to toggle source
# File lib/tictactoe/game.rb, line 53
def update_state(move)
  self.state = state.make_move(move, current_player.value.mark.value)
end