|
| 1 | +require_relative "./model/hand.rb" |
| 2 | +require_relative "./model/player.rb" |
| 3 | +require_relative "./model/card_kind.rb" |
| 4 | +require_relative "./model/pack.rb" |
| 5 | + |
| 6 | +class Game |
| 7 | + |
| 8 | + ALLOWED_HAND_ACTIONS = { |
| 9 | + "hit" => ["H", "S"], |
| 10 | + "split" => ["H", "S", "D"], |
| 11 | + "normal" => ["H", "S", "/", "D"] |
| 12 | + } |
| 13 | + |
| 14 | + def initialize(players_count) |
| 15 | + @pack = Model::Pack.new |
| 16 | + @dealer_balance = 0 |
| 17 | + @dealer_hand = nil |
| 18 | + @players = 1.upto(players_count).map { |id| Model::Player.new(id) } |
| 19 | + end |
| 20 | + |
| 21 | + def start |
| 22 | + loop do |
| 23 | + collect_bets_and_deal |
| 24 | + play_players |
| 25 | + check_for_insurance_bets |
| 26 | + play_dealer |
| 27 | + settle |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + private |
| 32 | + |
| 33 | + def collect_bets_and_deal |
| 34 | + puts "BETS" |
| 35 | + |
| 36 | + @players.each_entry do |player| |
| 37 | + print "# #{player.id} ? " |
| 38 | + bet = gets.to_i |
| 39 | + player.deal_initial_hand Model::Hand.new(bet, [@pack.draw, @pack.draw]) |
| 40 | + end |
| 41 | + |
| 42 | + @dealer_hand = Model::Hand.new(0, [@pack.draw, @pack.draw]) |
| 43 | + print_players_and_dealer_hands |
| 44 | + end |
| 45 | + |
| 46 | + def play_players |
| 47 | + @players.each_entry do |player| |
| 48 | + play_hand player, player.hand |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + def check_for_insurance_bets |
| 53 | + return if @dealer_hand.cards[0].label != "A" |
| 54 | + |
| 55 | + print "ANY INSURANCE? " |
| 56 | + return if gets.strip != "Y" |
| 57 | + |
| 58 | + @players.each_entry do |player| |
| 59 | + print "PLAYER #{player.id} INSURANCE BET? " |
| 60 | + player.bet_insurance(gets.to_i) |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + def play_dealer |
| 65 | + puts "DEALER HAS A \t#{@dealer_hand.cards[1].label} CONCEALED FOR A TOTAL OF #{@dealer_hand.total}" |
| 66 | + |
| 67 | + while @dealer_hand.total(is_dealer: true) < 17 |
| 68 | + card = @pack.draw |
| 69 | + @dealer_hand.hit card |
| 70 | + |
| 71 | + puts "DRAWS #{card.label} \t---TOTAL = #{@dealer_hand.total}" |
| 72 | + end |
| 73 | + |
| 74 | + if !@dealer_hand.is_busted? |
| 75 | + @dealer_hand.stand |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + def settle |
| 80 | + @players.each_entry do |player| |
| 81 | + player_balance_update = player.update_balance @dealer_hand |
| 82 | + @dealer_balance -= player_balance_update |
| 83 | + |
| 84 | + puts "PLAYER #{player.id} #{player_balance_update < 0 ? "LOSES" : "WINS"} \t#{player_balance_update} \tTOTAL=#{player.balance}" |
| 85 | + end |
| 86 | + |
| 87 | + puts "DEALER'S TOTAL = #{@dealer_balance}" |
| 88 | + end |
| 89 | + |
| 90 | + |
| 91 | + def print_players_and_dealer_hands |
| 92 | + puts "PLAYER\t#{@players.map(&:id).join("\t")}\tDEALER" |
| 93 | + puts " \t#{@players.map {|p| p.hand.cards[0].label}.join("\t")}\t#{@dealer_hand.cards[0].label}" |
| 94 | + puts " \t#{@players.map {|p| p.hand.cards[1].label}.join("\t")}" |
| 95 | + end |
| 96 | + |
| 97 | + def play_hand player, hand |
| 98 | + allowed_actions = ALLOWED_HAND_ACTIONS[(hand.is_split_hand || !hand.can_split?) ? "split" : "normal"] |
| 99 | + name = "PLAYER #{player.id}" |
| 100 | + if hand.is_split_hand |
| 101 | + name += " - HAND #{hand === player.hand ? 1 : 2}" |
| 102 | + end |
| 103 | + |
| 104 | + did_hit = false |
| 105 | + |
| 106 | + while hand.is_playing? |
| 107 | + print "#{name}? " |
| 108 | + |
| 109 | + action = gets.strip |
| 110 | + |
| 111 | + if !allowed_actions.include?(action) |
| 112 | + puts "Possible actions: #{allowed_actions.join(", ")}" |
| 113 | + next |
| 114 | + end |
| 115 | + |
| 116 | + if action === "/" |
| 117 | + player.split |
| 118 | + |
| 119 | + play_hand player, player.hand |
| 120 | + play_hand player, player.split_hand |
| 121 | + |
| 122 | + return |
| 123 | + end |
| 124 | + |
| 125 | + if action === "S" |
| 126 | + hand.stand |
| 127 | + end |
| 128 | + |
| 129 | + if action === "D" |
| 130 | + card = @pack.draw |
| 131 | + hand.double_down card |
| 132 | + |
| 133 | + puts "RECEIVED #{card.label}" |
| 134 | + end |
| 135 | + |
| 136 | + if action === "H" |
| 137 | + did_hit = true |
| 138 | + allowed_actions = ALLOWED_HAND_ACTIONS["hit"] |
| 139 | + card = @pack.draw |
| 140 | + hand.hit card |
| 141 | + |
| 142 | + puts "RECEIVED #{card.label}" |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + puts "TOTAL IS #{hand.total}" |
| 147 | + |
| 148 | + if hand.is_busted? |
| 149 | + puts "... BUSTED" |
| 150 | + end |
| 151 | + end |
| 152 | +end |
0 commit comments