Class: BetweenTheSheets::Game

Inherits:
Object
  • Object
show all
Extended by:
Messages
Includes:
Constants, Screens
Defined in:
lib/between_the_sheets/game.rb,
lib/between_the_sheets/game/screens.rb,
lib/between_the_sheets/game/messages.rb,
lib/between_the_sheets/game/constants.rb,
lib/between_the_sheets/game/rules/win/joker.rb,
lib/between_the_sheets/game/rules/bust/joker.rb,
lib/between_the_sheets/game/rules/win/same_cards.rb,
lib/between_the_sheets/game/rules/bust/consecutive.rb

Defined Under Namespace

Modules: Constants, Messages, Rules, Screens

Constant Summary

Constants included from Messages

Messages::MESSAGES

Constants included from Constants

Constants::ANTE, Constants::JACKPOT_ANTE, Constants::MINIMUM_BET, Constants::STARTING_CREDITS, Constants::STARTING_JACKPOT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Messages

help, message

Methods included from Screens

#intro_screen, #is_between_screen, #transition_screen

Constructor Details

#initialize(jackpot = STARTING_JACKPOT) ⇒ Game

Returns a new instance of Game.



17
18
19
20
21
# File 'lib/between_the_sheets/game.rb', line 17

def initialize(jackpot = STARTING_JACKPOT)
  @rounds = 1
  @credits = STARTING_CREDITS
  @jackpot = jackpot
end

Instance Attribute Details

#card1Object (readonly)

Returns the value of attribute card1.



15
16
17
# File 'lib/between_the_sheets/game.rb', line 15

def card1
  @card1
end

#card2Object (readonly)

Returns the value of attribute card2.



15
16
17
# File 'lib/between_the_sheets/game.rb', line 15

def card2
  @card2
end

#card3Object (readonly)

Returns the value of attribute card3.



15
16
17
# File 'lib/between_the_sheets/game.rb', line 15

def card3
  @card3
end

#jackpotObject (readonly)

Returns the value of attribute jackpot.



15
16
17
# File 'lib/between_the_sheets/game.rb', line 15

def jackpot
  @jackpot
end

#playObject (readonly)

Returns the value of attribute play.



15
16
17
# File 'lib/between_the_sheets/game.rb', line 15

def play
  @play
end

Instance Method Details

#draw_final_cardObject

Loads the 3rd card and checks for rules.

Raises:



39
40
41
42
43
44
45
# File 'lib/between_the_sheets/game.rb', line 39

def draw_final_card
  @card3 = Card.draw(@card1.id, @card2.id)
  puts "The next card is the #{@card3}"

  # player wins if card is Joker
  raise Rules::Win::Joker if @card3.joker?
end

#draw_initial_cardsObject

Loads the first 2 cards and checks rules.

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/between_the_sheets/game.rb', line 24

def draw_initial_cards
  @card1 = Card.draw
  @card2 = Card.draw(@card1.id)

  # check for joker
  raise Rules::Bust::Joker if @card1.joker? || @card2.joker?

  # check for consecutive
  raise Rules::Bust::Consecutive if (@card1.value - @card2.value).abs == 1

  # player wins if the cards are the same
  raise Rules::Win::SameCards if @card1.value == @card2.value
end

#executeObject

The main game loop.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/between_the_sheets/game.rb', line 74

def execute
  loop do
    transition_screen if @rounds > 1

    # Show the intro screen
    intro_screen

    @rounds += 1
    @credits -= ANTE
    @jackpot += JACKPOT_ANTE

    puts "\nAfter ante you have #{@credits.to_s.colorize(:green)} credits"

    continue = begin
      draw_initial_cards
    rescue Rules::Bust::Joker
      puts "House drew a Joker "
      lose
    rescue Rules::Bust::Consecutive
      puts "House drew consecutive cards #{@card1} & #{@card2} "
      lose
    rescue Rules::Win::SameCards
      puts "House drew #{@card1} and #{@card2} "
      win_jackpot
    else
      true
    end

    next unless !!continue

    # Ask player if next card is between
    is_between_screen

    # Ask player for their wager
    wager = Wager.get(@credits, @jackpot)

    continue = begin
      draw_final_card
    rescue Rules::Win::Joker
      puts "You drew a #{@card3}"
      win wager
    else
      true
    end

    next unless !!continue

    # check the between rules
    if is_between?
      win wager
    else
      lose wager
    end
  end
end

#is_between?Boolean

Bool whether card 3 is between cards 1 & 2

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/between_the_sheets/game.rb', line 48

def is_between?
  is_between = @card3.value.between?(@card1.value, @card2.value)

  is_between && @play == "y" || !is_between && @play == "n"
end

#lose(credits = nil) ⇒ Object

Prints the lost message and decrements the players credits.



68
69
70
71
# File 'lib/between_the_sheets/game.rb', line 68

def lose(credits=nil)
  @credits -= credits if credits
  puts self.class.message(:lose).colorize :red
end

#win(credits = nil) ⇒ Object

Prints the win message and increments the players credits.



55
56
57
58
# File 'lib/between_the_sheets/game.rb', line 55

def win(credits=nil)
  @credits += credits if credits
  puts self.class.message(:win).colorize :green
end

#win_jackpotObject

Prints the jackpot message and increments the players credits.



61
62
63
64
65
# File 'lib/between_the_sheets/game.rb', line 61

def win_jackpot
  @credits += @jackpot
  @jackpot = STARTING_JACKPOT
  puts self.class.message(:jackpot).colorize :green
end