Sha256: a37720b99ee4553a49c719f92ba71656c3cdeca297c82248fade35ce3cf9a31d

Contents?: true

Size: 809 Bytes

Versions: 2

Compression:

Stored size: 809 Bytes

Contents

class MakeMoveCommand
  attr :game, :game_id, :player, :position, :repository

  def initialize(game_id:, repository:, player:, position:)
    @game_id    = game_id
    @player     = player
    @position   = position
    @repository = repository
  end

  def apply!
    @game = repository.find(game_id)

    validate_game_is_in_progress
    validate_player
    validate_tile

    game.apply_move! position: position
  end

  def tile
    game.fetch_tile position
  end

  def validate_game_is_in_progress
    raise BadMove, "Game has already ended!" if game.finished?
  end

  def validate_player
    raise BadMove, "It is #{game.player}'s turn" unless player == game.player
  end

  def validate_tile
    unless tile == :free
      raise BadMove, "Tile at #{position} is already occupied!"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pico-0.1.0 gold_master_apps/tic_tac_toe/game/make_move_command.rb
pico-0.0.1 gold_master_apps/tic_tac_toe/game/make_move_command.rb