|
| 1 | +from casino.games.blackjack.blackjack import * |
| 2 | +from casino.game_types import * |
| 3 | + |
| 4 | +#Add more tests as needed, potentially testing user inputs |
| 5 | + |
| 6 | +def test_aces(): |
| 7 | + """ |
| 8 | + Tests extreme cases for having aces in your hand to ensure calculation is correct |
| 9 | + """ |
| 10 | + player = Player(account=Account.generate('test', 100)) |
| 11 | + player.hand.append(StandardCard(suit="hearts", rank="A")) |
| 12 | + player.hand.append(StandardCard(suit="hearts", rank="A")) |
| 13 | + assert(player.hand_total == 12) |
| 14 | + player.hand.append(StandardCard(suit="hearts", rank="A")) |
| 15 | + player.hand[0].rank = 6 |
| 16 | + assert(player.hand_total == 18) |
| 17 | + player.hand.pop(1) |
| 18 | + assert(player.hand_total == 17) |
| 19 | + player.hand[0].rank = 'A' |
| 20 | + total = 12 |
| 21 | + while total < 21: |
| 22 | + player.hand.append(StandardCard(suit="hearts", rank="A")) |
| 23 | + total += 1 |
| 24 | + assert(player.hand_total == total) |
| 25 | + while len(player.hand) > 3: |
| 26 | + player.hand.pop() |
| 27 | + player.hand[0].rank = 10 |
| 28 | + player.hand[1].rank = 10 |
| 29 | + assert(player.hand_total == 21) |
| 30 | + player.hand.pop(0) |
| 31 | + assert(player.hand_total == 21) |
| 32 | + |
| 33 | +def test_bust(): |
| 34 | + """ |
| 35 | + tests scenarios in which the dealer or player busts |
| 36 | + """ |
| 37 | + ctx = GameContext(account=Account.generate('test', 100),config=Config.default()) |
| 38 | + blackjack = StandardBlackjack(ctx) |
| 39 | + player = blackjack.players[0] |
| 40 | + player.bet = 10 |
| 41 | + #First test case where the player busts and loses |
| 42 | + blackjack.deal_cards() |
| 43 | + #Add cards to the player's hand to ensure that they lose |
| 44 | + while player.hand_total <= 21: |
| 45 | + player.hand.append(StandardCard(suit="hearts",rank="K")) |
| 46 | + blackjack.check_win() |
| 47 | + assert(blackjack.player_win_status[0]=='lose') |
| 48 | + assert ('bust' in blackjack.round_results[len(blackjack.round_results) - 1]) |
| 49 | + |
| 50 | + #Next case: Dealer busts and loses |
| 51 | + blackjack.deal_cards() |
| 52 | + while blackjack.dealer.hand_total <= 21: |
| 53 | + blackjack.dealer.hand.append(StandardCard(suit="hearts",rank="K")) |
| 54 | + blackjack.check_win() |
| 55 | + assert(blackjack.player_win_status[0]=='win') |
| 56 | + assert ('bust' in blackjack.round_results[len(blackjack.round_results) - 1]) |
| 57 | + |
| 58 | + #Next case: Player has 10-10-A and Dealer busts |
| 59 | + blackjack.deal_cards() |
| 60 | + player.hand[0] = StandardCard(suit="hearts",rank="K") |
| 61 | + player.hand[1] = StandardCard(suit="hearts", rank="K") |
| 62 | + player.hand.append(StandardCard(suit="hearts", rank="A")) |
| 63 | + blackjack.dealer.hand[0] = StandardCard(suit="hearts",rank="K") |
| 64 | + blackjack.dealer.hand[1] = StandardCard(suit="hearts",rank="K") |
| 65 | + blackjack.dealer.hand.append(StandardCard(suit="hearts",rank="K")) |
| 66 | + blackjack.check_win() |
| 67 | + assert(blackjack.player_win_status[0]=='win') |
| 68 | + assert('bust' in blackjack.round_results[len(blackjack.round_results)-1]) |
| 69 | + #We will test the payout in this scenario as well |
| 70 | + balance = player.account.balance |
| 71 | + blackjack.payout() |
| 72 | + assert(player.account.balance == (balance+player.bet*2)) |
| 73 | + |
| 74 | +def test_payout(): |
| 75 | + """ |
| 76 | + Tests the changes in the user's account for every standard scenario |
| 77 | + Normal win: profit = amount bet |
| 78 | + Push: profit = 0 |
| 79 | + Loss: profit = -bet |
| 80 | + """ |
| 81 | + ctx = GameContext(account=Account.generate('test', 100), config=Config.default()) |
| 82 | + blackjack = StandardBlackjack(ctx) |
| 83 | + player = blackjack.players[0] |
| 84 | + dealer = blackjack.dealer |
| 85 | + player.bet = 10 |
| 86 | + #Case 1: the player beats the dealer normally |
| 87 | + player.hand.append(StandardCard(suit="hearts",rank="K")) |
| 88 | + player.hand.append(StandardCard(suit="hearts", rank="K")) |
| 89 | + dealer.hand.append(StandardCard(suit="hearts", rank="K")) |
| 90 | + dealer.hand.append(StandardCard(suit="hearts", rank=9)) |
| 91 | + dealer.hand.append(StandardCard(suit="hearts", rank=3)) |
| 92 | + #Make sure the win result is correct |
| 93 | + blackjack.check_win() |
| 94 | + assert(blackjack.player_win_status[0] == 'win') |
| 95 | + #Test the payout |
| 96 | + balance = player.account.balance |
| 97 | + blackjack.payout() |
| 98 | + #Should be balance before payout + bet*2 because you get back the original money you bet plus the profit |
| 99 | + assert(player.account.balance == (balance+player.bet*2)) |
| 100 | + |
| 101 | + #Case 2: the dealer beats the player normally |
| 102 | + player.hand.pop() |
| 103 | + player.hand.pop() |
| 104 | + dealer.hand.pop() |
| 105 | + dealer.hand.pop() |
| 106 | + dealer.hand.pop() |
| 107 | + player.hand.append(StandardCard(suit="hearts", rank="10")) |
| 108 | + player.hand.append(StandardCard(suit="hearts", rank="10")) |
| 109 | + dealer.hand.append(StandardCard(suit="hearts", rank="10")) |
| 110 | + dealer.hand.append(StandardCard(suit="hearts", rank="9")) |
| 111 | + dealer.hand.append(StandardCard(suit="hearts", rank="2")) |
| 112 | + #Make sure the win result is correct |
| 113 | + blackjack.check_win() |
| 114 | + assert (blackjack.player_win_status[0] == 'lose') |
| 115 | + #Test the payout |
| 116 | + balance = player.account.balance |
| 117 | + blackjack.payout() |
| 118 | + #Balance should be the same as before because you lost the amount you bet already |
| 119 | + assert(player.account.balance == balance) |
| 120 | + |
| 121 | + #Case 3: Ties |
| 122 | + player.hand.pop() |
| 123 | + player.hand.pop() |
| 124 | + dealer.hand.pop() |
| 125 | + dealer.hand.pop() |
| 126 | + dealer.hand.pop() |
| 127 | + player.hand.append(StandardCard(suit="hearts", rank="K")) |
| 128 | + player.hand.append(StandardCard(suit="hearts", rank="K")) |
| 129 | + dealer.hand.append(StandardCard(suit="hearts", rank="K")) |
| 130 | + dealer.hand.append(StandardCard(suit="hearts", rank="K")) |
| 131 | + #Make sure the win result is correct |
| 132 | + blackjack.check_win() |
| 133 | + assert (blackjack.player_win_status[0] == 'tie') |
| 134 | + #Test the payout |
| 135 | + balance = player.account.balance |
| 136 | + blackjack.payout() |
| 137 | + #Balance should be higher by the amount bet since it is returned in the case of a tie |
| 138 | + assert(player.account.balance == (balance+player.bet)) |
| 139 | + |
| 140 | +def test_blackjack_payout(): |
| 141 | + """ |
| 142 | + Tests the wins/payouts for every blackjack scenario |
| 143 | + Both get blackjack round 1: Tie |
| 144 | + Player gets blackjack and dealer does not: Blackjack win (profit = 1.5*amount bet) |
| 145 | + Player gets blackjack round 1 and dealer has 21 in round 2: Blackjack win |
| 146 | + Player gets 21 in round 2 and dealer has blackjack: Loss |
| 147 | + Both player and dealer have 21 but neither have blackjack: Tie |
| 148 | + """ |
| 149 | + ctx = GameContext(account=Account.generate('test', 100), config=Config.default()) |
| 150 | + blackjack = StandardBlackjack(ctx) |
| 151 | + player = blackjack.players[0] |
| 152 | + dealer = blackjack.dealer |
| 153 | + player.bet = 10 |
| 154 | + #Case 1: Both the dealer and the player get blackjack |
| 155 | + player.hand.append(StandardCard(suit="hearts", rank="A")) |
| 156 | + player.hand.append(StandardCard(suit="hearts", rank="K")) |
| 157 | + dealer.hand.append(StandardCard(suit="hearts", rank="A")) |
| 158 | + dealer.hand.append(StandardCard(suit="hearts", rank=10)) |
| 159 | + #Make sure the win result is correct |
| 160 | + blackjack.blackjack_check() |
| 161 | + assert player.has_blackjack and dealer.has_blackjack |
| 162 | + blackjack.check_win() |
| 163 | + assert(blackjack.player_win_status[0] == 'tie') |
| 164 | + assert('blackjack' in blackjack.round_results[len(blackjack.round_results) - 1]) |
| 165 | + #Test the payout |
| 166 | + balance = player.account.balance |
| 167 | + blackjack.payout() |
| 168 | + assert(player.account.balance == (balance+player.bet)) |
| 169 | + |
| 170 | + #Case 2: The player gets blackjack and the dealer does not |
| 171 | + dealer.hand[0].rank = 10 |
| 172 | + dealer.hand[1].rank = 'A' |
| 173 | + dealer.hand.append(StandardCard(suit="hearts", rank="A")) |
| 174 | + #Make sure the results are correct |
| 175 | + blackjack.blackjack_check() |
| 176 | + assert(player.has_blackjack and not dealer.has_blackjack) |
| 177 | + blackjack.check_win() |
| 178 | + assert(blackjack.player_win_status[0] == 'win') |
| 179 | + assert('blackjack' in blackjack.round_results[len(blackjack.round_results) - 1]) |
| 180 | + #Test the payout |
| 181 | + balance = player.account.balance |
| 182 | + blackjack.payout() |
| 183 | + assert(player.account.balance == (balance+player.bet*(1+BLACKJACK_MULTIPLIER))) |
| 184 | + |
| 185 | + #Case 3: Player has blackjack, dealer 21 with more than 2 cards |
| 186 | + dealer.hand[1].rank = 10 |
| 187 | + blackjack.blackjack_check() |
| 188 | + assert player.has_blackjack and not dealer.has_blackjack |
| 189 | + blackjack.check_win() |
| 190 | + assert(blackjack.player_win_status[0] == 'win') |
| 191 | + assert('blackjack' in blackjack.round_results[len(blackjack.round_results) - 1]) |
| 192 | + #Test the payout |
| 193 | + balance = player.account.balance |
| 194 | + blackjack.payout() |
| 195 | + assert(player.account.balance == (balance+player.bet*(1+BLACKJACK_MULTIPLIER))) |
| 196 | + |
| 197 | + #Case 4: Both dealer and player have 21 but more than 2 cards |
| 198 | + player.hand[0].rank = 'K' |
| 199 | + player.hand.append(StandardCard(suit="hearts", rank='A')) |
| 200 | + blackjack.blackjack_check() |
| 201 | + assert not(player.has_blackjack or dealer.has_blackjack) |
| 202 | + blackjack.check_win() |
| 203 | + assert(blackjack.player_win_status[0] == 'tie') |
| 204 | + assert ('blackjack' not in blackjack.round_results[len(blackjack.round_results) - 1]) |
| 205 | + #Test the payout |
| 206 | + balance = player.account.balance |
| 207 | + blackjack.payout() |
| 208 | + assert(player.account.balance == (balance+player.bet)) |
| 209 | + |
| 210 | + #Case 5: Player has 21 but dealer has blackjack |
| 211 | + dealer.hand.pop() |
| 212 | + dealer.hand[0].rank = 'A' |
| 213 | + dealer.hand[1].rank = 'Q' |
| 214 | + blackjack.blackjack_check() |
| 215 | + assert not player.has_blackjack and dealer.has_blackjack |
| 216 | + blackjack.check_win() |
| 217 | + assert(blackjack.player_win_status[0] == 'lose') |
| 218 | + assert 'blackjack' in blackjack.round_results[len(blackjack.round_results) - 1] |
| 219 | + #Test the payout |
| 220 | + balance = player.account.balance |
| 221 | + blackjack.payout() |
| 222 | + assert(player.account.balance == balance) |
| 223 | + |
| 224 | +def test_dealer_decision(): |
| 225 | + """ |
| 226 | + Tests the dealer's decisions in certain situations |
| 227 | + Dealer total is less than or equal to 16: Hit |
| 228 | + Dealer total is greater than or equal to 17: Stand |
| 229 | + Edge case: Dealer stands on Ace + 6 |
| 230 | + """ |
| 231 | + ctx = GameContext(account=Account.generate('test', 100), config=Config.default()) |
| 232 | + blackjack = StandardBlackjack(ctx) |
| 233 | + dealer = blackjack.dealer |
| 234 | + dealer.hand.append(StandardCard(suit="hearts", rank="K")) |
| 235 | + dealer.hand.append(StandardCard(suit="hearts", rank=8)) |
| 236 | + blackjack.dealer_draw() |
| 237 | + assert(len(dealer.hand) == 2) |
| 238 | + dealer.hand[1].rank = 6 |
| 239 | + blackjack.dealer_draw() |
| 240 | + assert(len(dealer.hand) == 3) |
| 241 | + dealer.hand.pop() |
| 242 | + dealer.hand[0].rank = 'A' |
| 243 | + blackjack.dealer_draw() |
| 244 | + assert(len(dealer.hand) == 2) |
0 commit comments