class MCTS::Examples::DoubleStep
Constants
- FINAL_POSITION
- MAX_STEP
Attributes
positions[R]
Public Class Methods
new(positions = init_positions, n = 0)
click to toggle source
# File lib/mcts/examples/double_step.rb, line 10 def initialize(positions = init_positions, n = 0) @positions = positions @move_count = n end
Public Instance Methods
all_valid_moves()
click to toggle source
# File lib/mcts/examples/double_step.rb, line 38 def all_valid_moves if finished? [] else [1, 2] end end
dup()
click to toggle source
# File lib/mcts/examples/double_step.rb, line 29 def dup self.class.new @positions.dup, @move_count end
finished?()
click to toggle source
# File lib/mcts/examples/double_step.rb, line 15 def finished? @positions.any? {|_color, position| position >= FINAL_POSITION } end
generate_move()
click to toggle source
# File lib/mcts/examples/double_step.rb, line 19 def generate_move rand(MAX_STEP) + 1 end
last_turn_color()
click to toggle source
# File lib/mcts/examples/double_step.rb, line 46 def last_turn_color @move_count.odd? ? :black : :white end
set_move(move)
click to toggle source
# File lib/mcts/examples/double_step.rb, line 23 def set_move(move) steps = move @positions[next_turn_color] += steps @move_count += 1 end
won?(color)
click to toggle source
# File lib/mcts/examples/double_step.rb, line 33 def won?(color) fail "Game not finished" unless finished? @positions[color] > @positions[other_color(color)] end
Private Instance Methods
init_positions()
click to toggle source
# File lib/mcts/examples/double_step.rb, line 55 def init_positions {black: 0, white: 0} end
next_turn_color()
click to toggle source
# File lib/mcts/examples/double_step.rb, line 51 def next_turn_color @move_count.even? ? :black : :white end
other_color(color)
click to toggle source
# File lib/mcts/examples/double_step.rb, line 59 def other_color(color) if color == :black :white else :black end end