|
| 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 | + play_dealer |
| 26 | + settle |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + private |
| 31 | + |
| 32 | + def collect_bets_and_deal |
| 33 | + puts "BETS" |
| 34 | + |
| 35 | + @players.each_entry do |player| |
| 36 | + print "# #{player.id} ? " |
| 37 | + bet = gets.to_i |
| 38 | + player.deal_initial_hand Model::Hand.new(bet, [@pack.draw, @pack.draw]) |
| 39 | + end |
| 40 | + |
| 41 | + @dealer_hand = Model::Hand.new(0, [@pack.draw, @pack.draw]) |
| 42 | + print_players_and_dealer_hands |
| 43 | + end |
| 44 | + |
| 45 | + def play_players |
| 46 | + @players.each_entry do |player| |
| 47 | + play_hand player, player.hand |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + def play_dealer |
| 52 | + puts "DEALER HAS A \t#{@dealer_hand.cards[1].label} CONCEALED FOR A TOTAL OF #{@dealer_hand.total}" |
| 53 | + |
| 54 | + while @dealer_hand.total(is_dealer: true) < 17 |
| 55 | + card = @pack.draw |
| 56 | + @dealer_hand.hit card |
| 57 | + |
| 58 | + puts "DRAWS #{card.label} \t---TOTAL = #{@dealer_hand.total}" |
| 59 | + end |
| 60 | + |
| 61 | + if !@dealer_hand.is_busted? |
| 62 | + @dealer_hand.stand |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + def settle |
| 67 | + @players.each_entry do |player| |
| 68 | + player_balance_update = player.update_balance @dealer_hand |
| 69 | + @dealer_balance -= player_balance_update |
| 70 | + |
| 71 | + puts "PLAYER #{player.id} #{player_balance_update < 0 ? "LOSES" : "WINS"} \t#{player_balance_update} \tTOTAL=#{player.balance}" |
| 72 | + end |
| 73 | + |
| 74 | + puts "DEALER'S TOTAL = #{@dealer_balance}" |
| 75 | + end |
| 76 | + |
| 77 | + |
| 78 | + def print_players_and_dealer_hands |
| 79 | + puts "PLAYER\t#{@players.map(&:id).join("\t")}\tDEALER" |
| 80 | + # TODO: Check for split hands |
| 81 | + puts " \t#{@players.map {|p| p.hand.cards[0].label}.join("\t")}\t#{@dealer_hand.cards[0].label}" |
| 82 | + puts " \t#{@players.map {|p| p.hand.cards[1].label}.join("\t")}" |
| 83 | + end |
| 84 | + |
| 85 | + def play_hand player, hand |
| 86 | + allowed_actions = ALLOWED_HAND_ACTIONS[(hand.is_split_hand || !hand.can_split?) ? "split" : "normal"] |
| 87 | + name = "PLAYER #{player.id}" |
| 88 | + did_hit = false |
| 89 | + |
| 90 | + while hand.is_playing? |
| 91 | + print "#{name}? " |
| 92 | + |
| 93 | + action = gets.strip |
| 94 | + |
| 95 | + if !allowed_actions.include?(action) |
| 96 | + puts "Possible actions: #{allowed_actions.join(", ")}" |
| 97 | + next |
| 98 | + end |
| 99 | + |
| 100 | + if action === "/" |
| 101 | + player.split |
| 102 | + |
| 103 | + play_hand "#{name} - Hand 1", player.hand |
| 104 | + play_hand "#{name} - Hand 2", player.split_hand |
| 105 | + |
| 106 | + return |
| 107 | + end |
| 108 | + |
| 109 | + if action === "S" |
| 110 | + hand.stand |
| 111 | + end |
| 112 | + |
| 113 | + if action === "D" |
| 114 | + card = @pack.draw |
| 115 | + hand.double_down card |
| 116 | + |
| 117 | + puts "RECEIVED #{card.label}" |
| 118 | + end |
| 119 | + |
| 120 | + if action === "H" |
| 121 | + did_hit = true |
| 122 | + allowed_actions = ALLOWED_HAND_ACTIONS["hit"] |
| 123 | + card = @pack.draw |
| 124 | + hand.hit card |
| 125 | + |
| 126 | + puts "RECEIVED #{card.label}" |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + puts "TOTAL IS #{hand.total}" |
| 131 | + |
| 132 | + if hand.is_busted? |
| 133 | + puts "... BUSTED" |
| 134 | + end |
| 135 | + end |
| 136 | +end |
0 commit comments