Class: Baron::Turn::WinnerChooseAuction

Inherits:
Baron::Turn show all
Defined in:
lib/baron/turn/winner_choose_auction.rb

Overview

This is an auction where the players bid, and the winner then chooses which thing to purchase after winning.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Baron::Turn

#perform

Constructor Details

#initialize(players, bank) ⇒ WinnerChooseAuction

Creates an instance of this auction

auction.

Examples:

Baron::Turn::WinnerChooseAuction(player_set)

Parameters:

  • players (Array<Baron::Player>)

    All players participating in this


46
47
48
49
50
# File 'lib/baron/turn/winner_choose_auction.rb', line 46

def initialize(players, bank)
  @active_players = players.dup
  @bank = bank
  @bids = []
end

Instance Attribute Details

#active_playersArray<Baron::Player> (readonly)

Returns the players which are sill in the auction

The first player returned in the player whose turn it currently is. This array will be re-ordered as players take actions, and players will be removed when they pass.

Examples:

auction.active_players

Returns:


18
19
20
# File 'lib/baron/turn/winner_choose_auction.rb', line 18

def active_players
  @active_players
end

#bankBaron::Bank (readonly)

The bank for this game

Examples:

game.bank

Returns:


27
28
29
# File 'lib/baron/turn/winner_choose_auction.rb', line 27

def bank
  @bank
end

#certificateBaron::Certificate (readonly)

The certificate the player chose after winning this auction

Examples:

auction.certificate

Returns:


36
37
38
# File 'lib/baron/turn/winner_choose_auction.rb', line 36

def certificate
  @certificate
end

Instance Method Details

#available_actionsArray<Baron::Action>

Return the actions that the user can perform right now

can perform.

Examples:

auction.available_actions
# returns [Baron::Action::Bid, Baron::Action::Pass]

Returns:

  • (Array<Baron::Action>)

    An array of actions the current player


111
112
113
114
115
116
117
118
119
# File 'lib/baron/turn/winner_choose_auction.rb', line 111

def available_actions
  if certificate
    []
  elsif winner?
    [Action::SelectCertificate]
  else
    [Action::Bid, Action::Pass]
  end
end

#bid(action) ⇒ Array<Baron::Action::Bid>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The player bids the amount in this auction

By bidding, the player remains in the auction, and can win if all others players pass.

Parameters:

  • action (Baron::Action:Bid)

    The bid being made.

Returns:


140
141
142
143
144
# File 'lib/baron/turn/winner_choose_auction.rb', line 140

def bid(action)
  validate_bid(action)
  active_players.rotate!
  @bids << action
end

#current_bidBaron::Turn::Bid

The most recent bid in the auction

If the auction is done, then this is the winning bid.

there are no bids.

Examples:

auction.current_bid

Returns:

  • (Baron::Turn::Bid)

    Returns the most recent bid, nil if


84
85
86
# File 'lib/baron/turn/winner_choose_auction.rb', line 84

def current_bid
  @bids.last
end

#done?Boolean

Returns true if there are no more actions that can be performed

Examples:

auction.done?

Returns:

  • (Boolean)

    True if this auction is complete, false otherwise


128
129
130
# File 'lib/baron/turn/winner_choose_auction.rb', line 128

def done?
  available_actions.empty?
end

#high_bidderBaron::Player

The player who has made the highest bid so far

If the auction is done then this is the auction winner.

nil if there are no bids.

Examples:

auction.high_bidder

Returns:

  • (Baron::Player)

    Returns the player who has bid the highest,


98
99
100
# File 'lib/baron/turn/winner_choose_auction.rb', line 98

def high_bidder
  current_bid.player if current_bid
end

#pass(_) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

The player passes on this auction

The player can not bid again in this auction and will be removed from the list of active players.

Parameters:

  • _ (Baron::Action:Pass)

    The bid being made.


154
155
156
157
# File 'lib/baron/turn/winner_choose_auction.rb', line 154

def pass(_)
  active_players.shift
  high_bidder.give(bank, current_bid.amount) if winner?
end

#playerBaron::Player

Determine the current player

Examples:

auction.player

Returns:


59
60
61
# File 'lib/baron/turn/winner_choose_auction.rb', line 59

def player
  active_players.first
end

#selectcertificate(action) ⇒ Baron::Certificate

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The player selects a certificate with their action

Parameters:

Returns:


164
165
166
# File 'lib/baron/turn/winner_choose_auction.rb', line 164

def selectcertificate(action)
  @certificate = action.certificate
end

#winner?Boolean

Is there a winner of the auction

Examples:

auction.winner

Returns:

  • (Boolean)

    true if the auction is over, false otherwise


70
71
72
# File 'lib/baron/turn/winner_choose_auction.rb', line 70

def winner?
  active_players.count.equal?(1) && @bids.any?
end