require "twenty_one/player" require "twenty_one/dealer" require "twenty_one/deck" require "twenty_one/ace_card" module TwentyOne class Game @@BLACKJACK = 21 @@DEALER_MIN = 17 @@MAX_BET = 20 @@MIN_BET = 1 @@SHOE_DECKS_COUNT = 4 def self.play @playing = true @phase = :bet @player = Player.new "Alex" shoe = [] @@SHOE_DECKS_COUNT.times do shoe.concat Deck.new.shuffle end @dealer = Dealer.new "Harold", shoe puts "Welcome to the twenty_one table! Today's dealer is #{@dealer.name}" puts "Press any key to begin the game..." gets while @playing case @phase when :bet puts "How much would you like to bet? (minimum of $1)" amount = gets.chomp.to_i if Chip.get_amount(@player.chips) == 0 puts "Oh man! You're completely out of money! It's time you see a specialist..." @pahse = :gameover elsif Chip.get_amount(@player.chips) <= amount puts "Woah! You don't have enough money to bet that amount." elsif amount > 0 @phase = :deal end when :deal @player.make_bet amount puts "Deal!" # Deal cards 2.times do @dealer.hit(@player) end 2.times do @dealer.hit(@dealer) end puts "You have:" @player.hand.cards.each { |card| puts card } puts "The dealer has a #{@dealer.hand.cards.first.to_s} and one card face-down" @phase = :player_turn when :side_rules #TODO: Side Rules when :player_turn puts "What would you like to do? hit, stand" choice = gets.chomp case choice when "hit" @dealer.hit @player puts "You got a #{@player.hand.cards.last.to_s}" if @player.hand.cards.last.is_a?(AceCard) && @player.hand.cards.last.name == :ace puts "You got an #{@player.hand.cards.last.to_s}!" puts "Would you like it to value 1 or 11?" new_ace_value = gets case new_ace_value when "1" @player.hand.cards.last.use_lower when "11" @player.hand.cards.last.use_upper end puts "Your ace's new value is #{@player.hand.cards.last.value}" end if @player.hand.value > @@BLACKJACK puts "Aaaaah shucks! You have more than 21 which means you lose." @phase = :results end when "stand" puts "Alright! It's the dealer's turn." @phase = :showdown end when :dealer_turn when :showdown puts "The dealer reveals his card!" puts "It's a #{@dealer.hand.cards.last.to_s}" while @dealer.hand.value < @@DEALER_MIN && @dealer.hand.value != @@BLACKJACK @dealer.hit(@dealer) puts "The dealer drew a #{@dealer.hand.cards.last.to_s}" end result = @dealer.showdown(@player) case result when :twenty_one puts "TwentyOne!" when :win puts "You won!" when :bust puts "You lost!" when :push puts "Tie!" end @phase = :results when :results puts "Here are your game stats:" puts "Chips: #{@player.chips.size}" puts "Total value: $#{Chip.get_amount(@player.chips)}" puts "TwentyOnes: #{@player.twenty_ones}" puts "Wins: #{@player.wins}" puts "Busts: #{@player.busts}" puts "Pushes: #{@player.pushes}" puts "Play again?" answer = gets.chomp case answer when "no" @playing = false when "yes" @phase = :bet end end end end end end