Class: MCTS::Examples::DoubleStep
- Inherits:
-
Object
- Object
- MCTS::Examples::DoubleStep
- Defined in:
- lib/mcts/examples/double_step.rb
Constant Summary collapse
- FINAL_POSITION =
6
- MAX_STEP =
2
Instance Attribute Summary collapse
-
#positions ⇒ Object
readonly
Returns the value of attribute positions.
Instance Method Summary collapse
- #all_valid_moves ⇒ Object
- #dup ⇒ Object
- #finished? ⇒ Boolean
- #generate_move ⇒ Object
-
#initialize(positions = init_positions, n = 0) ⇒ DoubleStep
constructor
A new instance of DoubleStep.
- #last_turn_color ⇒ Object
- #set_move(move) ⇒ Object
- #won?(color) ⇒ Boolean
Constructor Details
#initialize(positions = init_positions, n = 0) ⇒ DoubleStep
Returns a new instance of DoubleStep.
10 11 12 13 |
# File 'lib/mcts/examples/double_step.rb', line 10 def initialize(positions = init_positions, n = 0) @positions = positions @move_count = n end |
Instance Attribute Details
#positions ⇒ Object (readonly)
Returns the value of attribute positions.
8 9 10 |
# File 'lib/mcts/examples/double_step.rb', line 8 def positions @positions end |
Instance Method Details
#all_valid_moves ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/mcts/examples/double_step.rb', line 38 def all_valid_moves if finished? [] else [1, 2] end end |
#dup ⇒ Object
29 30 31 |
# File 'lib/mcts/examples/double_step.rb', line 29 def dup self.class.new @positions.dup, @move_count end |
#finished? ⇒ Boolean
15 16 17 |
# File 'lib/mcts/examples/double_step.rb', line 15 def finished? @positions.any? {|_color, position| position >= FINAL_POSITION } end |
#generate_move ⇒ Object
19 20 21 |
# File 'lib/mcts/examples/double_step.rb', line 19 def generate_move rand(MAX_STEP) + 1 end |
#last_turn_color ⇒ Object
46 47 48 |
# File 'lib/mcts/examples/double_step.rb', line 46 def last_turn_color @move_count.odd? ? :black : :white end |
#set_move(move) ⇒ Object
23 24 25 26 27 |
# 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) ⇒ Boolean
33 34 35 36 |
# 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 |