def test_bet(self): game = Game() p = AIPlayer(Game=game) deck = Deck() p.hands.append([deck.draw_card() for i in range(2)]) a, b = p.act(game) self.assertEqual(a, 0) self.assertEqual(b, 20)
def test_bet(self): game = Game() p = HumanPlayer(Game=game) p.standing = [False, False] deck = Deck() p.hands.append([deck.draw_card() for i in range(2)]) a, b = p.act(game) self.assertEqual(a, 0) self.assertEqual(b, 50)
def test_stand(self): game = Game() game.state = Game.STATE_ACT p = HumanPlayer(Game=game) p.standing = [False, False] deck = Deck() p.hands.append([deck.draw_card() for i in range(2)]) game.dealer.hands.append([deck.draw_card() for i in range(2)]) a, b = p.act(game) self.assertEqual(a, 1) self.assertEqual(b, 0)
def __init__(self, n_decks): """ Initialize a Deck for blackjack, consisting of cards from n_decks standard 52 card Decks. Args: :Int n_decks: Number of decks to include in the blackjack deck """ # Initalize a Deck Deck.__init__(self) self.n_decks = n_decks # Extend the blackjack deck to consist of n_decks Decks of cards self._expand_deck()
def test_single_deck(self): deck = Deck(1) cards = {} for i in range(52): c = deck.draw_card() cards[c.get_type()] = cards.get(c.get_type(), 0) + 1 ct = 0 for k in cards.keys(): self.assertEqual(cards[k], 4) ct += 1 self.assertEqual(ct, 13)
def test_bust(self): game = Game() game.state = Game.STATE_ACT p = HumanPlayer(Game=game) p.standing = [False, False] deck = Deck() p.hands.append([deck.draw_card() for i in range(2)]) game.dealer.hands.append([deck.draw_card() for i in range(2)]) a = Actions.HIT while a is not Actions.STAND: a, b = p.act(game) p.hands[0].append(deck.draw_card()) self.assertEqual(a, 1) self.assertEqual(b, 0)
def __init__(self, num_players=3, num_start_chips=100): self.dealer = Dealer(Game=self) self.players = [] self.deck = Deck(5) self.state = self.STATE_BET self.bets = [] self.insurance_bets = [] if num_players < 1: raise ValueError("Number of players must be greater than 0!") for i in range(num_players - 1): self.players.append( AIPlayer(Game=self, chips=num_start_chips, player_id=i)) self.players[i].standing = [False, False] self.players.append( HumanPlayer(Game=self, chips=num_start_chips, player_id=num_players - 1)) self.players[len(self.players) - 1].standing = [False, False]
def test_actions(self): game = Game() p = AIPlayer(Game=game) deck = Deck() p.hands.append([deck.draw_card() for i in range(2)]) a, b = p.act(game) self.assertEqual(a, 0) self.assertEqual(b, 20) game.state = Game.STATE_ACT a, b, = p.act(game) self.assertEqual(a, 2) self.assertEqual(b, 0) p.hands[0].append(deck.draw_card()) a, b = p.act(game) self.assertEqual(a, 1) self.assertEqual(b, 0)
def GetAllDecks(self): self._callstack.info("DbIf.GetAllDecks: Called") next_transaction = "SELECT UniqueCards.ImageLocation, Decks.DeckName \ FROM UniqueCards \ RIGHT JOIN Decks \ ON UniqueCards.CardID = Decks.Headliner" self._sql_log.info(next_transaction) self._cur.execute(next_transaction) sql_result = self._cur.fetchall() deck_list = set() for entry in sql_result: deck_list.add(Deck(self._callstack, entry[1], entry[0])) self._callstack.info("DbIf.GetAllDecks: Returned") return deck_list
def test_reset_deck(self): deck = Deck(4) seq_1 = [] for i in range(208): c = deck.draw_card() seq_1.append(c.get_type()) deck.reset_deck() seq_2 = [] for i in range(208): c = deck.draw_card() seq_2.append(c.get_type()) self.assertNotEqual(seq_1, seq_2)
class Game(): STATE_BET = 1 STATE_ACT = 2 def __init__(self, num_players=3, num_start_chips=100): self.dealer = Dealer(Game=self) self.players = [] self.deck = Deck(5) self.state = self.STATE_BET self.bets = [] self.insurance_bets = [] if num_players < 1: raise ValueError("Number of players must be greater than 0!") for i in range(num_players - 1): self.players.append( AIPlayer(Game=self, chips=num_start_chips, player_id=i)) self.players[i].standing = [False, False] self.players.append( HumanPlayer(Game=self, chips=num_start_chips, player_id=num_players - 1)) self.players[len(self.players) - 1].standing = [False, False] def play_game(self): stop = False while not stop: print("You are Player %s" % str(len(self.players) - 1)) # bet for i in range(len(self.players)): act, bet = self.players[i].act(self, 0) self.players[i].chips -= bet self.bets.append([]) self.bets[i].append(bet) self.insurance_bets.append(0) self.state = self.STATE_ACT # deal # deal the dealer's cards self.dealer.hands.append([self.deck.draw_card() for i in range(2)]) self.dealer.hands[0][1].reveal_card() # deal each player's cards for i in range(len(self.players)): self.players[i].hands.append( [self.deck.draw_card() for i in range(2)]) # sequential play for i in range(len(self.players)): j = 0 while j < len(self.players[i].hands): a, bet = self.players[i].act(self, hand=j) self.handle_action(a, self.players[i], j) while a is not Actions.STAND: a, bet = self.players[i].act(self, hand=j) self.handle_action(a, self.players[i], j) self.players[i].standing[j] = True j += 1 # dealer plays self.dealer.hands[0][1].visible = True while self.dealer.get_sum()[0] < 17: self.dealer.hands[0].append(self.deck.draw_card()) # settle bets print("Final board state:") for i in range(len(self.players)): for j in range(len(self.players[i].hands)): for k in range(len(self.players[i].hands[j])): self.players[i].hands[j][k].visible = True print(self) # if there's a natural for i in range(len(self.players)): for j in range(len(self.players[i].hands)): if self.players[i].get_sum()[j] == 21: print( "Player %s got a natural! They receive %s chips." % (i, int(self.bets[i][j] / 2))) self.players[i].chips += int(self.bets[i][j] / 2) # if dealer has a natural if self.dealer.get_sum()[0] == 21: print( "Dealer has a natural. Taking chips from all who do not have a natural." ) for i in range(len(self.players)): for j in range(len(self.players[i].hands)): if not (self.players[i].get_sum()[j] == 21): self.bets[i][j] = 0 dealer_val = self.dealer.get_sum()[0] # find max of up to 21 for i in range(len(self.players)): for j in range(len(self.players[i].hands)): s = self.players[i].get_sum()[j] if (dealer_val > 21 or s > dealer_val) and s <= 21: print("Player %s won %s chips!" % (i, self.bets[i][j])) self.players[i].chips += self.bets[i][j] * 2 else: print("Player %s lost %s chips!" % (i, self.bets[i][j])) if self.insurance_bets[i] > 0 and self.dealer.hands[0][ 1].get_type() == "10": print("Player %s won %s chips from INSURE!" % (i, self.insurance_bets[i] * 2)) self.players[i].chips += self.insurance_bets[i] * 2 elif self.insurance_bets[i] > 0: print("Player %s lost %s chips from INSURE!" % (i, self.insurance_bets[i])) for i in range(len(self.players)): if self.players[i].chips <= 0: stop = True print("\n") print("---- Chip balances -----") for i in range(len(self.players)): print("Player %s: %s" % (i, self.players[i].chips)) for i in range(len(self.players)): self.players[i].hands = [] self.players[i].standing = [False, False] self.dealer.hands = [] self.state = self.STATE_BET def handle_action(self, action, player, j): if action is Actions.HIT: player.hands[j].append(self.deck.draw_card()) elif action is Actions.SPLIT: player.hands.append([]) player.hands[1].append(player.hands[0].pop()) self.bets[player.player_id].append(self.bets[player.player_id][0]) player.chips -= self.bets[player.player_id][0] elif action is Actions.DOUBLE_DOWN: player.hands.append([self.deck.draw_card()]) player.hands[1][0].visible = False self.bets[player.player_id].append(self.bets[player.player_id][0]) player.chips -= self.bets[player.player_id][0] elif action is Actions.INSURANCE: player.chips -= int(self.bets[player.player_id][0] / 2) self.insurance_bets[player.player_id] += int( self.bets[player.player_id][0] / 2) def __str__(self): output_str = "" if self.dealer.hands: output_str += "Dealer | %s\n" % str( [str(j) for j in self.dealer.hands[0]])[1:-1] for p in self.players: for i in range(len(p.hands)): output_str += "Player %s | %s chips bet | %s\n" % \ (p.player_id, self.bets[p.player_id][i], str([str(j) for j in p.hands[i]])[1:-1]) return output_str
def test_invalid_num_deck(self): with self.assertRaises(ValueError): deck = Deck(-1) with self.assertRaises(ValueError): deck = Deck(0)
from Deck.Deck import Deck from Players.Player import Player from Players.Dealer import Dealer deck = Deck() shuffled_deck = deck.getShuffledDeck() players = [] player1hand = [] player2hand = [] dealerhand = [] players.append(Player("Diamonds", player1hand, False)) players.append(Player("Rust", player2hand, False)) dealer = Dealer("Lefty", dealerhand, False) for player in players: player.addCard(shuffled_deck.pop(0)) dealer.addCard(shuffled_deck.pop(0)) for player in players: player.addCard(shuffled_deck.pop(0)) dealer.addCard(shuffled_deck.pop(0)) for player in players: print(player) player.reportHand() dealer.reportUpCard() print('\n' * 1)