module GooseGame module Cell class Base attr_reader :num, :gameboard def initialize(num, gameboard) @num = num.to_i @gameboard = gameboard @tokens = [] end def call(player, dice) position.tap do |pos| player.move(pos) @tokens.clear @tokens << "#{player} rolls #{dice.d1}, #{dice.d2}" @tokens << "#{player} moves from #{player.prev} to #{dest}" end end def to_s @tokens.join(". ") end private def position num end private def dest num end end class Bounce < Base def call(player, dice) super.tap do |pos| @tokens << "#{player} bounces! #{player} returns to #{pos}" end end private def position FINISH - num % FINISH end private def dest FINISH end end class Winning < Base def call(player, dice) super.tap do |pos| @tokens << "#{player} Wins!!" end end end class Bridge < Base def call(player, dice) super.tap do |pos| @tokens << "#{player} jumps to #{pos}" end end private def position num * 2 end private def dest "The Bridge" end end class Goose < Base def call(player, dice) tokens = [] super.tap do |pos| while goose?(pos) pos += dice.to_i tokens << "The Goose. #{player} moves again and goes to #{pos}" end @tokens << tokens.join(", ") player.position = pos return pos end end private def goose?(n) gameboard[n].instance_of? Goose end end end end