from config import Config
import numpy
class ReplayBuffer(object):

  def __init__(self, config: Config):
      self.window_size = config.window_size
      self.batch_size = config.batch_size
      self.buffer = []

  def save_game(self, game):
      if len(self.buffer) > self.window_size:
        self.buffer.pop(0)
      self.buffer.append(game)

  def sample_batch(self):
    
      # Sample uniformly across positions.
      move_sum = float(sum(len(g.history) for g in self.buffer))
      games = numpy.random.choice(
            self.buffer,
            size=self.batch_size,
            p=[len(g.history) / move_sum for g in self.buffer])
      game_pos = [(g, numpy.random.randint(len(g.history))) for g in games]
      return [(g.make_image(i), g.make_target(i)) for (g, i) in game_pos]
