Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/dmorrill10-utils/integer.rb

Overview

Table game specific ##########

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.

Raises:

  • (SeatOutOfBounds)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dmorrill10-utils/integer.rb', line 17

def position_relative_to(seat, number_of_players)
  raise SeatOutOfBounds unless seat.seat_in_bounds?(number_of_players) &&
    seat_in_bounds?(number_of_players)

  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.

Raises:

  • (SeatOutOfBounds)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dmorrill10-utils/integer.rb', line 45

def seat_from_relative_position(relative_position_of_self_to_result,
                                number_of_players)
  raise SeatOutOfBounds unless seat_in_bounds?(number_of_players)

  unless relative_position_of_self_to_result.seat_in_bounds?(number_of_players)
    raise RelativePositionOutOfBounds
  end

  position_adjustment = relative_position_of_self_to_result + 1

  to_seat = 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.



66
67
68
# File 'lib/dmorrill10-utils/integer.rb', line 66

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