Class: Table

Inherits:
Object
  • Object
show all
Defined in:
lib/rora/model/table.rb

Overview

A poker table where players compete for pots.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seats = nil) ⇒ Table

Returns a new instance of Table.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rora/model/table.rb', line 7

def initialize seats=nil
  seats = seats.nil? ? 9 : seats
  raise ArgumentError, "A table must have at least two seats" if seats < 2
  @seats = Array.new seats
  @board = Board.new
  @pot = Pot.new
  @deck = Deck.new

  @seats.each_index {|index| @seats[index] = Seat.new(index + 1)}
  @seats.each_index do |index|
    @seats[index].next = @seats[index == (seats - 1) ? 0 : index + 1]
    @seats[index].prev = @seats[index == 0 ? seats - 1 : index - 1]
  end
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



5
6
7
# File 'lib/rora/model/table.rb', line 5

def board
  @board
end

#deckObject (readonly)

Returns the value of attribute deck.



5
6
7
# File 'lib/rora/model/table.rb', line 5

def deck
  @deck
end

#potObject (readonly)

Returns the value of attribute pot.



5
6
7
# File 'lib/rora/model/table.rb', line 5

def pot
  @pot
end

Instance Method Details

#add(player, seat_number = nil) ⇒ Object

Raises:

  • (RuntimeError)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rora/model/table.rb', line 105

def add player, seat_number=nil
  raise RuntimeError, "Cannot add player, the table is full" if full?
  if !seat_number.nil?
    raise ArgumentError, "Seat number #{seat_number} does not exist at this table" if seat_number > @seats.size || seat_number < 1
    raise ArgumentError, "Seat number #{seat_number} is already taken by another player" if @seats[seat_number - 1].taken?
    @seats[seat_number - 1].player = player
    return self
  end
  @seats.each_index do |x|
    if !@seats[x].taken?
      @seats[x].player = player
      return self
    end
  end
  self
end

#empty?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/rora/model/table.rb', line 101

def empty?
  @seats.select{|x| x.taken?}.empty?
end

#full?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/rora/model/table.rb', line 97

def full?
  @seats.select{|x| !x.taken?}.empty?
end

#pass_the_buckObject



22
23
24
25
26
# File 'lib/rora/model/table.rb', line 22

def pass_the_buck
  index = the_button.number - 1
  @seats[index].button = nil
  the_seat_after(@seats[index]).button = true
end

#playersObject



89
90
91
92
93
94
95
# File 'lib/rora/model/table.rb', line 89

def players
  players = []
  @seats.each do |seat|
    players << seat.player if seat.taken?
  end
  players
end

#remove(player) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/rora/model/table.rb', line 122

def remove player
  @seats.each_index do |x|
    if @seats[x].player == player
      @seats[x].player = nil
    end
  end
end

#resetObject



83
84
85
86
87
# File 'lib/rora/model/table.rb', line 83

def reset
  @board = Board.new
  @pot = Pot.new
  @deck = Deck.new
end

#seat(i) ⇒ Object



75
76
77
# File 'lib/rora/model/table.rb', line 75

def seat i
  @seats[i - 1]
end

#sizeObject



79
80
81
# File 'lib/rora/model/table.rb', line 79

def size
  @seats.size
end

#the_big_blindObject



42
43
44
# File 'lib/rora/model/table.rb', line 42

def the_big_blind
  the_seat_after the_small_blind
end

#the_buttonObject

Raises:

  • (RuntimeError)


28
29
30
31
32
33
34
35
36
# File 'lib/rora/model/table.rb', line 28

def the_button
  raise RuntimeError, "There are fewer than two players at the table" if players.size < 2
  @seats.each_index do |x|
    return @seats[x] if @seats[x].button?
  end
  @seats.each_index do |x|
    return @seats[x] if @seats[x].taken?
  end
end

#the_seat_after(seat) ⇒ Object

Raises:

  • (RuntimeError)


50
51
52
53
54
# File 'lib/rora/model/table.rb', line 50

def the_seat_after seat
  raise RuntimeError, "There are fewer than two players at the table" if players.size < 2
  this_seat = seat.next
  return this_seat.taken? ? this_seat : the_seat_after(this_seat)
end

#the_seat_before(seat) ⇒ Object

Raises:

  • (RuntimeError)


56
57
58
59
60
# File 'lib/rora/model/table.rb', line 56

def the_seat_before seat
  raise RuntimeError, "There are fewer than two players at the table" if players.size < 2
  this_seat = seat.prev
  return this_seat.taken? ? this_seat : the_seat_before(this_seat)
end

#the_small_blindObject



38
39
40
# File 'lib/rora/model/table.rb', line 38

def the_small_blind
  players.size == 2 ? the_button : the_seat_after(the_button)
end

#under_the_gunObject



46
47
48
# File 'lib/rora/model/table.rb', line 46

def under_the_gun
  the_seat_after(the_big_blind)
end

#with(argument) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rora/model/table.rb', line 62

def with argument
  if argument.kind_of? Deck
    @deck = argument
  end
  if argument.kind_of? Pot
    @pot = argument
  end
  if argument.kind_of? Board
    @board = argument
  end
  self
end