Class: Gambler::Game::BasicGame

Inherits:
Object
  • Object
show all
Defined in:
lib/gambler/game/basic_game.rb

Overview

The basic game class in which all others can inherit. Feel free to create your own game class which does not inherit from BasicGame, but uses Player, Deck, etc. directly.

BasicGame gives some free functionality to it’s children:

  • players: Instance reader for the game’s Player(s).

  • ante: Instance accessor for the game’s current ante.

  • deck: Brand new, fully shuffled Deck to play with.

  • pot: Instance accessor for the game’s current pot.

The options hash only requires one element, players, which must be an array of Gambler::Player object(s).

Example:

class Poker < Gambler::Game::BasicGame
  def initialize(options = {})
    # .. do custom shit (if needed) ..
    super(options) # Required to recieve free functionality.
    # .. more custom shit (if needed) ..
  end

  def poker_stuff
    !cheat
  end
end

# Create some players.
@dale  = Gambler::Player.new('Dale')
@kenny = Gambler::Player.new('Kenny')

# This will be passed to Poker.new.
options = {
  :players => [@dale, @kenny], # The only required option.
  :ante => 50,
  :pot => 1_000
}

@game = Poker.new(options)
@game.poker_stuff

Direct Known Subclasses

Blackjack

Constant Summary collapse

INITIAL_ANTE =
10
INITIAL_POT =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BasicGame

Returns a new instance of BasicGame.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gambler/game/basic_game.rb', line 51

def initialize(options = {})
  raise Exceptions::NoPlayers unless options[:players].size > 0
  options[:players].each do |player|
    raise Exceptions::InvalidPlayers unless player.is_a? Player
  end
  @players = options[:players]

  @deck = Deck.new
  3.times { @deck.shuffle! }

  @ante = options[:ante] || INITIAL_ANTE
  @pot  = options[:pot]  || INITIAL_POT
end

Instance Attribute Details

#anteObject

Returns the value of attribute ante.



49
50
51
# File 'lib/gambler/game/basic_game.rb', line 49

def ante
  @ante
end

#deckObject

Returns the value of attribute deck.



49
50
51
# File 'lib/gambler/game/basic_game.rb', line 49

def deck
  @deck
end

#playersObject (readonly)

Returns the value of attribute players.



48
49
50
# File 'lib/gambler/game/basic_game.rb', line 48

def players
  @players
end

#potObject

Returns the value of attribute pot.



49
50
51
# File 'lib/gambler/game/basic_game.rb', line 49

def pot
  @pot
end