Module: AcpcPokerTypes::SeatLike

Included in:
Seat
Defined in:
lib/acpc_poker_types/seat.rb

Instance Method Summary collapse

Instance Method Details

#position_relative_to(seat, number_of_players) ⇒ Integer

Returns The relative position of self to seat, given the number of players at the table, number_of_players, indexed such that the seat immediately to the left of seat has a position_relative_to of zero.

Examples:

1.position_relative_to 0, 3 == 0

1.position_relative_to 1, 3 == 2

Parameters:

  • seat (Integer)

    The seat to which the relative position is desired.

  • number_of_players (Integer)

    The number of players at the table.

Returns:

  • (Integer)

    The relative position of self to seat, given the number of players at the table, number_of_players, indexed such that the seat immediately to the left of seat has a position_relative_to of zero.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/acpc_poker_types/seat.rb', line 13

def position_relative_to(seat, number_of_players)
  unless seat.seat_in_bounds?(number_of_players) &&
    seat_in_bounds?(number_of_players)
    raise "Seat #{seat} out of bounds for #{number_of_players} players"
  end

  adjusted_seat = if self > seat
    self
  else
    self + number_of_players
  end
  adjusted_seat - seat - 1
end

#seat_from_relative_position(relative_position_of_self_to_result, number_of_players) ⇒ Integer

Inverse operation of position_relative_to. Given

<code>relative_position = seat.position_relative_to to_seat, number_of_players</code>

then

<code>to_seat = seat.seat_from_relative_position relative_position, number_of_players</code>

Examples:

1.seat_from_relative_position 0, 3 == 0

1.seat_from_relative_position 2, 3 == 1

Parameters:

  • relative_position_of_self_to_result (Integer)

    The relative position of seat self to the seat that is returned by this function.

  • number_of_players (Integer)

    The number of players at the table.

Returns:

  • (Integer)

    The seat to which the relative position, relative_position_of_self_to_result, of self was derived, given the number of players at the table, number_of_players, indexed such that the seat immediately to the left of from_seat has a position_relative_to of zero.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/acpc_poker_types/seat.rb', line 43

def seat_from_relative_position(
  relative_position_of_self_to_result,
  number_of_players
)
  unless seat_in_bounds?(number_of_players)
    raise "Seat #{seat} out of bounds for #{number_of_players} players"
  end

  unless relative_position_of_self_to_result.seat_in_bounds?(
    number_of_players
  )
    raise "Relative position #{relative_position_of_self_to_result} out of bounds for #{number_of_players} players"
  end

  position_adjustment = relative_position_of_self_to_result + 1

  to_seat = self.class.new(
    self + number_of_players - position_adjustment
  )
  if self > to_seat || !to_seat.seat_in_bounds?(number_of_players)
    self - position_adjustment
  else
    to_seat
  end
end

#seat_in_bounds?(number_of_players) ⇒ Bool

Returns Reports whether or not self represents an out of bounds seat.

Parameters:

  • number_of_players (Integer)

    The number of players at the table.

Returns:

  • (Bool)

    Reports whether or not self represents an out of bounds seat.



72
73
74
# File 'lib/acpc_poker_types/seat.rb', line 72

def seat_in_bounds?(number_of_players)
  self < number_of_players && self >= 0
end