class GooseGame::GamePlay

Attributes

dice[R]
gameboard[R]
players[R]
round_player[R]

Public Class Methods

new(players: [], gameboard: GameBoard.new, output: STDOUT) click to toggle source
# File lib/goose_game/gameplay.rb, line 12
def initialize(players: [], gameboard: GameBoard.new, output: STDOUT)
  @players = players
  @gameboard = gameboard
  @output = output
end

Public Instance Methods

call() click to toggle source
# File lib/goose_game/gameplay.rb, line 18
def call
  loop do
    res = play(STDIN.gets)
    break if res == :exit
    break if players.any?(&:won?)
  end
end

Private Instance Methods

add_player(name) click to toggle source
# File lib/goose_game/gameplay.rb, line 43
        def add_player(name)
  existing_player = player(name)
  return Out::EXISTING.call(existing_player) if existing_player
  players << Player.new(name)
  Out::PLAYERS.call(players)
end
move(name) click to toggle source
# File lib/goose_game/gameplay.rb, line 61
        def move(name)
  return Out::NO_PLAYER.call(name) unless round_player
  n = round_player.position.to_i + dice.to_i
  cell = gameboard[n]
  cell.call(round_player, dice)
  cell.to_s << prank_check
end
play(stdin) click to toggle source
# File lib/goose_game/gameplay.rb, line 26
        def play(stdin)
  case stdin.chomp
  when In::HELP
    @output.puts Out::HELP
  when In::PLAYER
    @output.puts add_player($~[:name])
  when In::MOVE, In::MOVE_ROLL
    @dice = (%w[d1 d2] - $~.names).empty? ? Dice.new($~[:d1], $~[:d2]) : Dice.roll
    @round_player = player($~[:name])
    @output.puts move($~[:name])
  when In::EXIT
    return :exit
  else
    @output.puts Out::UNKNOWN
  end
end
player(name) click to toggle source
# File lib/goose_game/gameplay.rb, line 50
        def player(name)
  players.detect { |p| p.name == name }
end
prank_check() click to toggle source
# File lib/goose_game/gameplay.rb, line 54
        def prank_check
  prank = players.detect { |p| p.name != round_player.name && p.position == round_player.position }
  return "" unless prank
  prank.move(round_player.prev)
  Out::PRANK.call(round_player, prank)
end