class Model::GameTree

Attributes

board[R]
previous_move[R]

Public Class Methods

generate_game_tree(board, previous_move = nil) click to toggle source
# File lib/tic_tac_toe/model/game_tree.rb, line 14
def generate_game_tree(board, previous_move = nil)
  new(board, previous_move)
end
new(board, previous_move = nil) click to toggle source
# File lib/tic_tac_toe/model/game_tree.rb, line 7
def initialize(board, previous_move = nil)
  @board = board
  @previous_move = previous_move
  @equivalent = {}
end

Public Instance Methods

complete?() click to toggle source
# File lib/tic_tac_toe/model/game_tree.rb, line 23
def complete?
  @board.complete? || next_game_trees.count.zero?
end
current_team() click to toggle source
# File lib/tic_tac_toe/model/game_tree.rb, line 19
def current_team
  @board.current_team
end
next_game_trees() click to toggle source

Produces children based on the number of unique tile collection orientations. For more info, check out Model::TileCollection.

# File lib/tic_tac_toe/model/game_tree.rb, line 33
def next_game_trees
  return @next_game_trees unless @next_game_trees.nil?

  moves = @board.available_moves

  @next_game_trees = moves.each_with_object([]) { |move, game_trees| add_game_trees(game_trees, move) }

  @next_game_trees
end
rating(team) click to toggle source
# File lib/tic_tac_toe/model/game_tree.rb, line 27
def rating(team)
  @board.rating(team)
end

Private Instance Methods

add_equivalents(tile_collection) click to toggle source
# File lib/tic_tac_toe/model/game_tree.rb, line 68
def add_equivalents(tile_collection)
  tile_collection.equivalents.each { |tc| @equivalent[tc.id] = true }
end
add_game_trees(game_trees, move) click to toggle source
# File lib/tic_tac_toe/model/game_tree.rb, line 45
def add_game_trees(game_trees, move)
  tile = move.tile
  board = @board.clone

  board.set_piece(tile.row, tile.col, move.piece)

  tile_collection = board.tile_collection

  unless equivalent?(tile_collection.id)
    add_equivalents(tile_collection)

    board.cycle_teams

    game_trees << self.class.generate_game_tree(board, move)
  end

  game_trees
end
equivalent?(tile_collection_id) click to toggle source
# File lib/tic_tac_toe/model/game_tree.rb, line 64
def equivalent?(tile_collection_id)
  @equivalent[tile_collection_id]
end