Class: TTT::MoveTraverser

Inherits:
Object
  • Object
show all
Defined in:
lib/ttt/move_traverser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game_history) ⇒ MoveTraverser

Returns a new instance of MoveTraverser.



6
7
8
9
10
# File 'lib/ttt/move_traverser.rb', line 6

def initialize(game_history)
  self.move_index    = 0
  self.game_history  = game_history
  self.current_index = 0
end

Instance Attribute Details

#current_indexObject

Returns the value of attribute current_index.



5
6
7
# File 'lib/ttt/move_traverser.rb', line 5

def current_index
  @current_index
end

#game_historyObject

Returns the value of attribute game_history.



5
6
7
# File 'lib/ttt/move_traverser.rb', line 5

def game_history
  @game_history
end

#move_indexObject

Returns the value of attribute move_index.



5
6
7
# File 'lib/ttt/move_traverser.rb', line 5

def move_index
  @move_index
end

Instance Method Details

#adjust_move_index(distance) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/ttt/move_traverser.rb', line 20

def adjust_move_index(distance)
  if move_index + distance >= 0 && move_index + distance <= max_length
    self.move_index += distance
  elsif move_index + distance < 0
    self.move_index = 0
  else
    self.move_index = max_length
  end
end

#clean_board(board) ⇒ Object



45
46
47
# File 'lib/ttt/move_traverser.rb', line 45

def clean_board(board)
  board.board.each_index { |index| board.update index, " " }
end

#history_board_builder(board, move_number_limit = move_index) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ttt/move_traverser.rb', line 30

def history_board_builder(board, move_number_limit = move_index)
  total_moves    = game_history.history.length
  clone_board    = YAML.load(YAML.dump(board))
  clean_board(clone_board)
  clone_history  = YAML.load(YAML.dump(game_history.history))

  total_moves.times do |n|
    cur_move = clone_history.shift
    if (n + 1) <= move_number_limit
      clone_board.update(cur_move.move, cur_move.side)
    end
  end
  clone_board.board
end

#initialize_historyObject



12
13
14
# File 'lib/ttt/move_traverser.rb', line 12

def initialize_history
  self.move_index = max_length
end

#max_lengthObject



16
17
18
# File 'lib/ttt/move_traverser.rb', line 16

def max_length
  game_history.history.length
end