Class: PlayerObject
- Inherits:
-
Object
- Object
- PlayerObject
- Defined in:
- lib/ttr/objects/player.rb
Overview
Copyright © 2011 Jesse Sielaff
Instance Attribute Summary collapse
-
#action_begun ⇒ Object
readonly
Returns the value of attribute action_begun.
-
#all_player_objs ⇒ Object
readonly
Returns the value of attribute all_player_objs.
-
#candidate_ticket_objs ⇒ Object
readonly
Returns the value of attribute candidate_ticket_objs.
-
#cards ⇒ Object
readonly
Returns the value of attribute cards.
-
#claimed_route_objs ⇒ Object
readonly
Returns the value of attribute claimed_route_objs.
-
#deck ⇒ Object
readonly
Returns the value of attribute deck.
-
#draws_remaining ⇒ Object
readonly
Returns the value of attribute draws_remaining.
-
#kept_ticket_objs ⇒ Object
readonly
Returns the value of attribute kept_ticket_objs.
-
#remaining_ticket_objs ⇒ Object
readonly
Returns the value of attribute remaining_ticket_objs.
-
#score ⇒ Object
readonly
Returns the value of attribute score.
-
#selections ⇒ Object
readonly
Returns the value of attribute selections.
-
#trains ⇒ Object
readonly
Returns the value of attribute trains.
-
#turn_completed ⇒ Object
readonly
Returns the value of attribute turn_completed.
Instance Method Summary collapse
-
#add_tickets ⇒ Object
Moves three candidate Tickets from the remaining Tickets to the Player and returns an Array of the Tickets.
-
#claim(route_obj) ⇒ Object
Marks the given Route as claimed by the Player.
-
#clear_ticket_objs ⇒ Object
Removes all candidate Tickets from the Player, marks the Player’s turn as complete, and returns true.
-
#discard(card_objs) ⇒ Object
Moves the given Cards from the Player to the Deck’s discard pile.
-
#initialize(deck_obj, player_objs, route_objs, script, ticket_objs) ⇒ PlayerObject
constructor
A new instance of PlayerObject.
-
#keep_ticket_obj(ticket_obj) ⇒ Object
Marks the given Ticket as kept by the Player.
-
#played_final_round? ⇒ Boolean
Returns true if the Player has taken a turn during the final round, false otherwise.
-
#possible_to_play? ⇒ Boolean
Returns true if the Player will be able to draw Cards, claim a Route, or draw Tickets during this turn, false otherwise.
-
#register_draw(n) ⇒ Object
Decrements the number of Cards left to draw by 1 and disallows the Player from drawing Tickets and claiming Routes for the remainder of this turn.
- #score_tickets ⇒ Object
-
#set_of(size, color, wilds, force_wilds = false) ⇒ Object
Returns an Array of Cards of the given size.
- #take_a_turn ⇒ Object
Constructor Details
#initialize(deck_obj, player_objs, route_objs, script, ticket_objs) ⇒ PlayerObject
Returns a new instance of PlayerObject.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ttr/objects/player.rb', line 7 def initialize (deck_obj, player_objs, route_objs, script, ticket_objs) @deck = deck_obj @script = script @all_player_objs = player_objs @all_route_objs = route_objs @remaining_ticket_objs = ticket_objs @candidate_ticket_objs = [] @kept_ticket_objs = [] @claimed_route_objs = [] @score = 0 @trains = 45 @selections = [] @cards = { wild:[], black:[], blue:[], green:[], orange:[], pink:[], red:[], white:[], yellow:[] } 4.times do card = @deck.draw @cards[card.color] << card end @action_begun = false @turn_completed = false @draws_remaining = 0 end |
Instance Attribute Details
#action_begun ⇒ Object (readonly)
Returns the value of attribute action_begun.
35 36 37 |
# File 'lib/ttr/objects/player.rb', line 35 def action_begun @action_begun end |
#all_player_objs ⇒ Object (readonly)
Returns the value of attribute all_player_objs.
35 36 37 |
# File 'lib/ttr/objects/player.rb', line 35 def all_player_objs @all_player_objs end |
#candidate_ticket_objs ⇒ Object (readonly)
Returns the value of attribute candidate_ticket_objs.
35 36 37 |
# File 'lib/ttr/objects/player.rb', line 35 def candidate_ticket_objs @candidate_ticket_objs end |
#cards ⇒ Object (readonly)
Returns the value of attribute cards.
35 36 37 |
# File 'lib/ttr/objects/player.rb', line 35 def cards @cards end |
#claimed_route_objs ⇒ Object (readonly)
Returns the value of attribute claimed_route_objs.
35 36 37 |
# File 'lib/ttr/objects/player.rb', line 35 def claimed_route_objs @claimed_route_objs end |
#deck ⇒ Object (readonly)
Returns the value of attribute deck.
35 36 37 |
# File 'lib/ttr/objects/player.rb', line 35 def deck @deck end |
#draws_remaining ⇒ Object (readonly)
Returns the value of attribute draws_remaining.
35 36 37 |
# File 'lib/ttr/objects/player.rb', line 35 def draws_remaining @draws_remaining end |
#kept_ticket_objs ⇒ Object (readonly)
Returns the value of attribute kept_ticket_objs.
35 36 37 |
# File 'lib/ttr/objects/player.rb', line 35 def kept_ticket_objs @kept_ticket_objs end |
#remaining_ticket_objs ⇒ Object (readonly)
Returns the value of attribute remaining_ticket_objs.
35 36 37 |
# File 'lib/ttr/objects/player.rb', line 35 def remaining_ticket_objs @remaining_ticket_objs end |
#score ⇒ Object (readonly)
Returns the value of attribute score.
35 36 37 |
# File 'lib/ttr/objects/player.rb', line 35 def score @score end |
#selections ⇒ Object (readonly)
Returns the value of attribute selections.
35 36 37 |
# File 'lib/ttr/objects/player.rb', line 35 def selections @selections end |
#trains ⇒ Object (readonly)
Returns the value of attribute trains.
35 36 37 |
# File 'lib/ttr/objects/player.rb', line 35 def trains @trains end |
#turn_completed ⇒ Object (readonly)
Returns the value of attribute turn_completed.
35 36 37 |
# File 'lib/ttr/objects/player.rb', line 35 def turn_completed @turn_completed end |
Instance Method Details
#add_tickets ⇒ Object
Moves three candidate Tickets from the remaining Tickets to the Player and returns an Array of the Tickets. Disallows the Player from claiming Routes, drawing Tickets, and drawing Cards for the remainder of the turn.
44 45 46 47 48 49 |
# File 'lib/ttr/objects/player.rb', line 44 def add_tickets @action_begun = true @draws_remaining = 0 @candidate_ticket_objs.replace(@remaining_ticket_objs.pop(3)) end |
#claim(route_obj) ⇒ Object
Marks the given Route as claimed by the Player. Increments the Player’s score by the point value of the Route. Decrements the Player’s trains by the length of the Route. Disallows claiming Routes, drawing Tickets, and drawing Cards for the remainder of the turn and marks the Player’s turn as complete.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ttr/objects/player.rb', line 56 def claim (route_obj) route_obj.player_obj = self @claimed_route_objs << route_obj @trains -= route_obj.length @score += route_obj.points @action_begun = true @turn_completed = true @draws_remaining = 0 end |
#clear_ticket_objs ⇒ Object
Removes all candidate Tickets from the Player, marks the Player’s turn as complete, and returns true.
71 72 73 74 |
# File 'lib/ttr/objects/player.rb', line 71 def clear_ticket_objs @candidate_ticket_objs.clear @turn_completed = true end |
#discard(card_objs) ⇒ Object
Moves the given Cards from the Player to the Deck’s discard pile.
78 79 80 81 82 83 |
# File 'lib/ttr/objects/player.rb', line 78 def discard (card_objs) card_objs.each do |obj| @cards.each {|k,v| v.delete(obj) } @deck.discard(obj) end end |
#keep_ticket_obj(ticket_obj) ⇒ Object
Marks the given Ticket as kept by the Player. If all candidate Tickets have been kept, marks the Player’s turn as complete and returns true, otherwise returns nil.
89 90 91 92 |
# File 'lib/ttr/objects/player.rb', line 89 def keep_ticket_obj (ticket_obj) @kept_ticket_objs << ticket_obj @turn_completed = true if @candidate_ticket_objs.empty? end |
#played_final_round? ⇒ Boolean
Returns true if the Player has taken a turn during the final round, false otherwise.
97 98 99 |
# File 'lib/ttr/objects/player.rb', line 97 def played_final_round? @final_round end |
#possible_to_play? ⇒ Boolean
Returns true if the Player will be able to draw Cards, claim a Route, or draw Tickets during this turn, false otherwise.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/ttr/objects/player.rb', line 104 def possible_to_play? return true unless @deck.empty? return true unless @remaining_ticket_objs.length < 3 max_route = @cards.keys.each_with_object({}) do |color, h| next if color == :wild h[color] = @cards[color].length + @cards[:wild].length end max_route[:grey] = max_route.values.max @all_route_objs.any? do |obj| next if obj.player_obj next if obj.length > @trains next if obj.length > max_route[obj.color] next if (twin = obj.twin) && (twin.player_obj == self) next if (twin) && (@all_player_objs.length < 3) && (twin.player_obj) true end end |
#register_draw(n) ⇒ Object
Decrements the number of Cards left to draw by 1 and disallows the Player from drawing Tickets and claiming Routes for the remainder of this turn. If the number of Cards left to draw reaches 0, also disallows the Player from drawing more Cards this turn, and marks the Player’s turn as complete.
130 131 132 133 134 135 136 137 |
# File 'lib/ttr/objects/player.rb', line 130 def register_draw (n) @action_begun = true @draws_remaining -= n if (@draws_remaining < 1) || @deck.empty? || @deck.wilds_only? @turn_completed = true end end |
#score_tickets ⇒ Object
139 140 141 |
# File 'lib/ttr/objects/player.rb', line 139 def score_tickets @kept_ticket_objs.each {|obj| @score += (obj.completed? @claimed_route_objs) ? obj.points : -obj.points } end |
#set_of(size, color, wilds, force_wilds = false) ⇒ Object
Returns an Array of Cards of the given size. If force_wilds is false, will attempt to fill the Array first with Cards of the given color, then with the given number of wild Cards (if necessary). If force_wilds is true, will attempt to fill the Array first with the given number of wild Cards, then with Cards of the given color (if necessary). If the Array is not filled to the required size, returns nil.
150 151 152 153 154 155 |
# File 'lib/ttr/objects/player.rb', line 150 def set_of (size, color, wilds, force_wilds = false) c, w = @cards[color], @cards[:wild].take(wilds) set = (force_wilds) ? (w + c) : (c + w) set.take(size) unless set.length < size end |
#take_a_turn ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/ttr/objects/player.rb', line 159 def take_a_turn if possible_to_play? @script[Player.new(self)] raise Game::UnfinishedTurn unless @turn_completed end @final_round = @all_player_objs.any? {|obj| obj.trains < 3 } @draws_remaining = 2 @action_begun = false @turn_completed = false end |