Class: Player
Overview
Copyright © 2011 Jesse Sielaff
Instance Method Summary collapse
-
#candidate_tickets ⇒ Object
Returns an Array of the Tickets currently available for the Player to choose to keep or discard.
-
#cards(color) ⇒ Object
Returns the number of Cards of the given color held by the Player.
-
#claim(route, color) ⇒ Object
Claims the given Route using Cards of the given color and the minimum number of wild Cards needed, then returns an Array of the Routes claimed by the Player.
-
#claim_max_wild(route, color) ⇒ Object
Claims the given Route using all the Player’s wild Cards and as many Cards of the given color as needed, then returns an Array of the Routes claimed by the Player.
-
#claim_n_wild(route, color, n) ⇒ Object
Claims the given Route using the given number of wild Cards and as many Cards of the given color as needed, then returns an Array of the Routes claimed by the Player.
-
#claim_no_wild(route, color) ⇒ Object
Claims the given Route using only Cards of the given color (no wild Cards), then returns an Array of the Routes claimed by the Player.
-
#claimed_routes ⇒ Object
Returns an Array of the Routes claimed by the Player.
-
#discard_tickets ⇒ Object
Removes all candidate Tickets from the Player, marks the Player’s turn as complete, and returns true.
-
#draw_random ⇒ Object
Adds one Card to the Player and returns the color of that Card.
-
#draw_tickets ⇒ Object
Adds three candidate Tickets to the Player and returns an Array of the Tickets.
-
#keep_ticket(ticket) ⇒ Object
Marks the given Ticket as kept by the Player.
-
#kept_tickets ⇒ Object
Returns an Array of the Tickets kept by the Player.
-
#opponents ⇒ Object
Returns an Array of the Player’s Opponents.
-
#score ⇒ Object
Returns the current score of the Player.
-
#select_card(color) ⇒ Object
Moves one Card of the given Color from the selection to the Player and returns the number of Cards of that color held by the Player.
-
#select_wild ⇒ Object
Moves one wild Card from the selection to the Player and returns the number of wild Cards held by the Player.
-
#trains ⇒ Object
Returns the number of unplayed trains held by the Player.
-
#turn_completed? ⇒ Boolean
Returns true if the Player has completed the current turn.
-
#wilds ⇒ Object
Returns the number of wild Cards held by the Player.
Methods inherited from Entity
Constructor Details
This class inherits a constructor from Entity
Instance Method Details
#candidate_tickets ⇒ Object
Returns an Array of the Tickets currently available for the Player to choose to keep or discard.
10 11 12 |
# File 'lib/ttr/entities/player.rb', line 10 def candidate_tickets Ticket.entities(@obj.candidate_ticket_objs) end |
#cards(color) ⇒ Object
Returns the number of Cards of the given color held by the Player.
16 17 18 |
# File 'lib/ttr/entities/player.rb', line 16 def cards (color) @obj.cards[color].length end |
#claim(route, color) ⇒ Object
Claims the given Route using Cards of the given color and the minimum number of wild Cards needed, then returns an Array of the Routes claimed by the Player. If the Player is unable to claim the Route with the given color, returns nil.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ttr/entities/player.rb', line 25 def claim (route, color) return if @obj.action_begun return if route.claimed? return unless route.match?(color) return if route.length > @obj.trains return if (twin = Route.object(route).twin) && (twin.player_obj == Player.object(self)) return if (twin) && (opponents.length < 2) && (twin.player_obj) return unless card_objs = @obj.set_of(route.length, color, wilds) @obj.discard card_objs @obj.claim Route.object(route) end |
#claim_max_wild(route, color) ⇒ Object
Claims the given Route using all the Player’s wild Cards and as many Cards of the given color as needed, then returns an Array of the Routes claimed by the Player. If the Player is unable to claim the Route with the given color, returns nil.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ttr/entities/player.rb', line 43 def claim_max_wild (route, color) return if @obj.action_begun return if route.claimed? return unless route.match?(color) return if route.length > @obj.trains return if (twin = Route.object(route).twin) && (twin.player_obj == Player.object(self)) return if (twin) && (opponents.length < 2) && (twin.player_obj) return unless card_objs = @obj.set_of(route.length, color, wilds, true) @obj.discard card_objs @obj.claim Route.object(route) end |
#claim_n_wild(route, color, n) ⇒ Object
Claims the given Route using the given number of wild Cards and as many Cards of the given color as needed, then returns an Array of the Routes claimed by the Player. If the Player is unable to claim the Route with the given color, returns nil.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ttr/entities/player.rb', line 61 def claim_n_wild (route, color, n) return if @obj.action_begun return if route.claimed? return unless route.match?(color) return if route.length > @obj.trains return if (twin = Route.object(route).twin) && (twin.player_obj == Player.object(self)) return if (twin) && (opponents.length < 2) && (twin.player_obj) return unless card_objs = @obj.set_of(route.length, color, n, true) @obj.discard card_objs @obj.claim Route.object(route) end |
#claim_no_wild(route, color) ⇒ Object
Claims the given Route using only Cards of the given color (no wild Cards), then returns an Array of the Routes claimed by the Player. If the Player is unable to claim the Route with the given color, returns nil.
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ttr/entities/player.rb', line 78 def claim_no_wild (route, color) return if @obj.action_begun return if route.claimed? return unless route.match?(color) return if route.length > @obj.trains return if (twin = Route.object(route).twin) && (twin.player_obj == Player.object(self)) return if (twin) && (opponents.length < 2) && (twin.player_obj) return unless card_objs = @obj.set_of(route.length, color, 0) @obj.discard card_objs @obj.claim Route.object(route) end |
#claimed_routes ⇒ Object
Returns an Array of the Routes claimed by the Player.
93 94 95 |
# File 'lib/ttr/entities/player.rb', line 93 def claimed_routes Route.entities(@obj.claimed_route_objs) end |
#discard_tickets ⇒ Object
Removes all candidate Tickets from the Player, marks the Player’s turn as complete, and returns true. If the Player is unable to discard Tickets, returns nil.
101 102 103 104 105 106 |
# File 'lib/ttr/entities/player.rb', line 101 def discard_tickets return if @obj.kept_ticket_objs.length < 2 return if [0,3].include?(@obj.candidate_ticket_objs.length) @obj.clear_ticket_objs end |
#draw_random ⇒ Object
Adds one Card to the Player and returns the color of that Card. If the Player is unable to draw a Card, returns nil.
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/ttr/entities/player.rb', line 111 def draw_random return if @obj.draws_remaining < 1 return unless card = @obj.deck.draw @obj.cards[card.color] << card @obj.selections.unshift(:grey) @obj.register_draw(1) card.color end |
#draw_tickets ⇒ Object
Adds three candidate 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. Returns nil if the Player is unable to draw Tickets.
127 128 129 130 131 132 |
# File 'lib/ttr/entities/player.rb', line 127 def draw_tickets return if @obj.action_begun return if @obj.remaining_ticket_objs.length < 3 Ticket.entities(@obj.add_tickets) end |
#keep_ticket(ticket) ⇒ 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. Returns false if some candidate Tickets remain, or nil if the Ticket was not a candidate Ticket.
139 140 141 142 143 144 |
# File 'lib/ttr/entities/player.rb', line 139 def keep_ticket (ticket) ticket_obj = Ticket.object(ticket) return unless @obj.candidate_ticket_objs.delete(ticket_obj) @obj.keep_ticket_obj(ticket_obj) || false end |
#kept_tickets ⇒ Object
Returns an Array of the Tickets kept by the Player.
148 149 150 |
# File 'lib/ttr/entities/player.rb', line 148 def kept_tickets Ticket.entities(@obj.kept_ticket_objs) end |
#opponents ⇒ Object
Returns an Array of the Player’s Opponents.
154 155 156 |
# File 'lib/ttr/entities/player.rb', line 154 def opponents Opponent.entities(@obj.all_player_objs - [@obj]) end |
#score ⇒ Object
Returns the current score of the Player.
160 161 162 |
# File 'lib/ttr/entities/player.rb', line 160 def score @obj.score end |
#select_card(color) ⇒ Object
Moves one Card of the given Color from the selection to the Player and returns the number of Cards of that color held by the Player. If the Player is not currently allowed to draw a Card of the given color, returns nil.
168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/ttr/entities/player.rb', line 168 def select_card (color) return select_wild if color == :wild return if @obj.draws_remaining < 1 return unless card = @obj.deck.select(color) @obj.cards[color] << card @obj.selections.unshift(card.color) @obj.register_draw(1) cards(color) end |
#select_wild ⇒ Object
Moves one wild Card from the selection to the Player and returns the number of wild Cards held by the Player. If the Player is not currently allowed to draw a wild Card, returns nil.
185 186 187 188 189 190 191 192 193 194 |
# File 'lib/ttr/entities/player.rb', line 185 def select_wild return if @obj.draws_remaining < 2 return unless card = @obj.deck.select(:wild) @obj.cards[:wild] << card @obj.selections.unshift(:wild) @obj.register_draw(2) wilds end |
#trains ⇒ Object
Returns the number of unplayed trains held by the Player.
198 199 200 |
# File 'lib/ttr/entities/player.rb', line 198 def trains @obj.trains end |
#turn_completed? ⇒ Boolean
Returns true if the Player has completed the current turn.
204 205 206 |
# File 'lib/ttr/entities/player.rb', line 204 def turn_completed? @obj.turn_completed end |
#wilds ⇒ Object
Returns the number of wild Cards held by the Player.
210 211 212 |
# File 'lib/ttr/entities/player.rb', line 210 def wilds cards(:wild) end |