Class: Cardtable
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Cardtable
- Defined in:
- lib/cardtable.rb
Instance Method Summary collapse
- #deal ⇒ Object
- #double ⇒ Object
- #endGame ⇒ Object
- #hit(thisPlayer, numCards, cardCount) ⇒ Object
- #newgame ⇒ Object
- #setBet(amount) ⇒ Object
- #shuffle ⇒ Object
- #stand ⇒ Object
- #startup(thisplayer) ⇒ Object
Instance Method Details
#deal ⇒ Object
71 72 73 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/cardtable.rb', line 71 def deal # This is the beginning of a new game # Clear out any old game data to reset if self.game.game_num > 0 # We need to call endGame to save the stats from teh previous game. This # is necessary as the player could have hit 'stand' and the lost # immediately to the dealer's 2 cards. In this case, the game will not # have been saved. endGame # Reset the game stuff so it can be used later in the method self.game.win = false self.game.num_cards, self.game.dealer_num_cards = 0, 0 self.game.card_count, self.game.dealer_count = 0, 0 # Reset the player and dealer objects self.player.reset # If we don't make the second dealer card faceup, then this could be # dealt to a player - not sure this is true really as cards are dealt from the deck self.dealer.hand[1].faceup = true self.dealer.reset # Create a new game object and initialise it newgame end # Deal 2 cards to both the dealer and the player self.deck.deal(self.player) self.deck.deal(self.player) self.deck.deal(self.dealer) self.deck.deal(self.dealer) # The second card dealt to the dealer is face down self.dealer.hand[1].faceup = false # Update the number of games played by the player and dealer self.player_record.games_played += 1 self.dealer_record.games_played += 1 # We subtract the bet from the player and add to the dealer # This is in case the player exits the game before it completes # When the game completes naturally, we will take this into consideration # Think of this as the dealer holding the pot while the game is playing self.player_record.balance -= self.bet self.dealer_record.balance += self.bet # Similarly, we assume that the player will lost and the dealer will win # in case the game terminates unexpectedly # Again, we will take this into consideration if the game ends normally self.player_record.games_lost += 1 self.dealer_record.games_won += 1 # Update the games per session average for the player and dealer avg = self.player_record.games_played / self.player_record.num_sessions if avg < 1 self.player_record.games_per_session_avg = 1 else self.player_record.games_per_session_avg = avg.floor end avg = self.dealer_record.games_played / self.dealer_record.num_sessions if avg < 1 self.dealer_record.games_per_session_avg = 1 else self.dealer_record.games_per_session_avg = avg.floor end self.player_record.save self.dealer_record.save # Update the game stats self.game.game_num = self.player_record.games_played self.game.dealer_game_num = self.dealer_record.games_played self.game.amount = self.bet # We will only update the players cards so that a 0 number of cards # for the dealer signifies that the dealer won before playing his hand self.game.num_cards = 2 self.game.card_count = self.player.score.handScore self.game.save end |
#double ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/cardtable.rb', line 215 def double # This is where the player chooses to double their initial bet # Double the bet self.setBet(self.bet * 2) self.game.amount = self.bet # Deal one more card only hit(self.player, self.game.num_cards, self.game.card_count) # If they're not bust, give control to the dealer and play its hand if self.player.score.handScore < 22 stand end end |
#endGame ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/cardtable.rb', line 236 def endGame # Call this function when we've determined that a game has finished. # This will save the stats. if self.game.dealer_count > 21 # If the dealer has gone bust, then the player has won. This is the # only situation where the player wins. We have to reverse the stats # where we assumed that the player would lose before saving the stats # We add twice the bet to the player and subtract from the dealer # This is because we already subtracted the bet from the player and # added to the dealer at teh start of this game self.player_record.balance += self.bet * 2 self.dealer_record.balance -= self.bet * 2 # Similarly, we assumed that the player lost and the dealer won # So, we change this accordingly self.player_record.games_lost -= 1 self.player_record.games_won += 1 self.dealer_record.games_won -= 1 self.dealer_record.games_lost += 1 self.game.win = true end # Save the game stats, and for the dealer and player self.game.save self.player_record.save self.dealer_record.save end |
#hit(thisPlayer, numCards, cardCount) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/cardtable.rb', line 159 def hit(thisPlayer, numCards, cardCount) # This is where the player gets another card # Deal another card to the player self.deck.deal(thisPlayer) # Update the stats and save if thisPlayer === self.player self.game.num_cards += 1 self.game.card_count = thisPlayer.score.handScore else self.game.dealer_num_cards += 1 self.game.dealer_count = thisPlayer.score.handScore end self.game.save # If the player is bust then we don't need to do anything - the stats have # already been saved and the view will take care of starting a new game # Try to rub salt into the player's wound by showing the # dealer's hidden card if the player has bust. if self.game.num_cards == 0 # It's not the dealer who has been hit with a card but the player if self.game.card_count > 21 self.dealer.hand[1].faceup = true endGame end elsif self.game.num_cards > 0 # if the dealer isn't winning yet... if self.game.dealer_count < self.game.card_count # Save the game as usual and wait for the next card to be dealt self.game.save elsif self.game.dealer_count > 21 # The dealer has bust # Should we tidy up the stats to show that the player has won # right here or leave that to 'endGame'? endGame else # The dealer has beaten the player endGame end end end |
#newgame ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cardtable.rb', line 46 def newgame # Create a new game object/record but don't save it just yet # as the player may not actually decide to play a game in this session @game = self.build_game self.game = @game # Initialize the game stats for this game # We do this here for statistical purposes as it is legal for a session # that has no games played self.game.user = self.player_record self.game.session_num = self.player_record.num_sessions self.game.dealer_session_num = self.dealer_record.num_sessions self.game.game_num, self.game.dealer_game_num = 0, 0 self.game.win = false self.game.amount = 0 self.game.num_cards, self.game.dealer_num_cards = 0, 0 self.game.card_count, self.game.dealer_count = 0, 0 end |
#setBet(amount) ⇒ Object
66 67 68 69 |
# File 'lib/cardtable.rb', line 66 def setBet(amount) # Set the bet on the cardtable self.bet = amount end |
#shuffle ⇒ Object
231 232 233 234 |
# File 'lib/cardtable.rb', line 231 def shuffle # Shuffle the deck self.deck.shuffle end |
#stand ⇒ Object
204 205 206 207 208 209 210 211 212 213 |
# File 'lib/cardtable.rb', line 204 def stand # This is where the player chooses to stand # Give control to the dealer and play its hand self.dealer.hand[1].faceup = true self.game.dealer_num_cards = 2 self.game.dealer_count = self.dealer.score.handScore self.game.save end |
#startup(thisplayer) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/cardtable.rb', line 12 def startup(thisplayer) # Setup its dealer, player and game associations # Find the dealer record and associate it with this cardtable object self.dealer_record = User.find(26) # Update the number of game sessions for the dealer self.dealer_record.num_sessions += 1 self.dealer_record.save # Associate the cardtable player with the logged in user self.player_record = thisplayer # Update the user information regarding sessions self.player_record.num_sessions += 1 self.player_record.save # Set up a new game object and initialise it. newgame # Create a new shuffled desk of cards - we shuffle in case our logic always arranges # the cards in the deck the same way. self.deck = Deck.new shuffle # Create a new dealer object self.dealer = Player.new # Create a new player object self.player = Player.new # Set the bet to be 5 initially self.bet = 5 end |