require "goose_game/dice" require "goose_game/gameboard" require "goose_game/messages" require "goose_game/player" module GooseGame class GamePlay include Messages attr_reader :players, :round_player, :gameboard, :dice def initialize(players: [], gameboard: GameBoard.new, output: STDOUT) @players = players @gameboard = gameboard @output = output end def call loop do res = play(STDIN.gets) break if res == :exit break if players.any?(&:won?) end end private 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 private 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 private def player(name) players.detect { |p| p.name == name } end private 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 private 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 end end