Class: GameObject

Inherits:
Object
  • Object
show all
Defined in:
lib/ttr/objects/game.rb

Overview

Copyright © 2011 Jesse Sielaff

Constant Summary collapse

NumberOfPlayers =
Class.new(Exception)
UnfinishedTurn =
Class.new(Exception)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script_names, city_objs, route_objs, ticket_objs) ⇒ GameObject

Returns a new instance of GameObject.

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ttr/objects/game.rb', line 7

def initialize (script_names, city_objs, route_objs, ticket_objs)
  n = script_names.length
  raise(NumberOfPlayers, "#{n} player#{"s" if n != 1} given, 2..5 players expected") unless n.between?(2,5)
  
  @deck = DeckObject.new
  
  @city_objs = city_objs
  @route_objs = route_objs
  @ticket_objs = ticket_objs
  
  @player_objs = []
  
  script_names.each do |script_name|
    script = Script.method(script_name).to_proc.curry[Game.new(self)]
    @player_objs << PlayerObject.new(@deck, @player_objs, @route_objs, script, @ticket_objs)
  end
  
  @deck.fill_selection
end

Instance Attribute Details

#deckObject (readonly)

Returns the value of attribute deck.



30
31
32
# File 'lib/ttr/objects/game.rb', line 30

def deck
  @deck
end

#route_objsObject (readonly)

Returns the value of attribute route_objs.



30
31
32
# File 'lib/ttr/objects/game.rb', line 30

def route_objs
  @route_objs
end

Instance Method Details

#playObject

Causes Players to take turns until every Player has played a turn in which at least one Player has fewer than 3 trains, then scores the Tickets held by each Player.



36
37
38
39
40
41
42
43
# File 'lib/ttr/objects/game.rb', line 36

def play
  @player_objs.cycle do |obj|
    obj.take_a_turn
    break if @player_objs.all? {|obj| obj.played_final_round? }
  end
  
  @player_objs.map {|obj| obj.score_tickets; obj.score }
end