Sha256: bc24d59a94b37197afd67bd18c040b7ce55c3f4ff2b16c5b6db1d42d41e1085a

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module MCTS
  module Examples
    class DoubleStep

      FINAL_POSITION = 6
      MAX_STEP = 2

      attr_reader :positions

      def initialize(positions = init_positions, n = 0)
        @positions = positions
        @move_count = n
      end

      def finished?
        @positions.any? {|_color, position| position >= FINAL_POSITION }
      end

      def generate_move
        rand(MAX_STEP) + 1
      end

      def set_move(move)
        steps = move
        @positions[next_turn_color] += steps
        @move_count += 1
      end

      def dup
        self.class.new @positions.dup, @move_count
      end

      def won?(color)
        fail "Game not finished" unless finished?
        @positions[color] > @positions[other_color(color)]
      end

      def all_valid_moves
        if finished?
          []
        else
          [1, 2]
        end
      end

      def last_turn_color
        @move_count.odd? ? :black : :white
      end

      private
      def next_turn_color
        @move_count.even? ? :black : :white
      end

      def init_positions
        {black: 0, white: 0}
      end

      def other_color(color)
        if color == :black
          :white
        else
          :black
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubykon-0.3.1 lib/mcts/examples/double_step.rb
rubykon-0.3.0 lib/mcts/examples/double_step.rb