def create_deck(self): self.deck = Deck(randint(1, 500)) # creating back card image back_card_image = ImageTk.PhotoImage(Image.open("Cards/green_back.png")) self.board.create_image(130, 370, image=back_card_image, tags=('card_back', 'this'), state=HIDDEN) self.all_hand_cards = [self.deck.get_card() for _ in range(1, 18)] self.all_hand_cards.insert(0, back_card_image)
def test_other_player_health_gets_to_zero__game_won(self): attacker = Player('attacker', Deck()) victim = Player('victim', Deck()) game = Game(attacker, victim) victim.health = 2 kill_card = Card(4) attacker.mana_slots = 8 attacker.mana = 4 attacker.hand = [Card(2), kill_card, Card(8)] game.attacker = attacker assert game.status['finished'] is False game.play_card(kill_card) assert game.status['finished'] is True assert game.status['winner'] == 'attacker'
def __init__(self): self.state = self.States.INIT self.current_player_index = 0 self.players = [] self.deck = Deck() self.rules = Rules() self.stack = Stack() self.cheat_card = ""
def test_draw_all_cards(self, _random_module): deck = Deck() # Not using fixture because of 'hypothesis' for _ in range(0, 20): deck.draw_card() with pytest.raises(RuntimeError, match=r'.*empty.*'): deck.draw_card()
def _reset_play(self) -> None: self._community_cards = [] self._current_bet = 0 self._current_raise = 0 self._is_round_active = True self._raise_cnt = 0 self._deck = Deck() for player in self._players: player.reset()
def index(): title = 'random' deck = Deck() deck.shuffle() card = deck.drawCard() return render_template('index.html', title=title, card=card)
def draw_card(self): """ Draws a card from the deck. If the deck is exhausted, a new deck is shuffled and used. """ card = self.deck.draw() if card is None: self.deck = Deck() card = self.deck.draw() return card
def __init__(self, players_classes: List[Type[BasicPlayer]]) -> None: self._init_chips = self.INIT_CHIPS self._deck = Deck() self._players = self._create_players(players_classes) self._total_players = self._players.count() self._pot = 0 self._pot_leftover = 0 self._community_cards = [] self._current_bet = 0 self._small_bet = self.SMALL_BET self._big_bet = self.BIG_BET self._current_raise = self._small_bet self._raise_cnt = 0 self._is_round_active = True self._is_game_active = True self._observers = Observers() self._players_who_lost = [] self._current_phase = None
def _create_community_combinations(self, opp_hand: Tuple[Card, Card], state: State) -> List[List[Card]]: deck = Deck().get_cards() indexes = [] popped_cards = 0 community_combos = [] nbr_of_comm_cards = len(state.community_cards) if nbr_of_comm_cards >= 5: community_combos.append(state.community_cards) else: for i, c in enumerate(deck): if c in self.get_hand( ) or c in state.community_cards or c in opp_hand: indexes.append(i) for i in indexes: deck.pop(i - popped_cards) popped_cards += 1 if nbr_of_comm_cards < 3: for i1, c1 in enumerate(deck): for i2, c2 in enumerate(deck[i1 + 1:]): for i3, c3 in enumerate(deck[i2 + 1:]): for i4, c4 in enumerate(deck[i3 + 1:]): for c5 in deck[i4 + 1:]: community_combos.append( [c1, c2, c3, c4, c5]) elif nbr_of_comm_cards < 4: for i, c1 in enumerate(deck): for c2 in deck[i + 1:]: community_combos.append( list(state.community_cards) + [c1, c2]) elif nbr_of_comm_cards < 5: for c in deck: community_combos.append(list(state.community_cards) + [c]) return community_combos
class TestBlackjack(unittest.TestCase): deck = Deck(BuildDeck().create_deck()) blackjack = Blackjack(deck) def test_data(self): self.assertEqual(self.blackjack.deck, self.deck) self.assertEqual(self.blackjack.draw_count, 0) self.assertEqual(self.blackjack.bet, 0) def test_restart(self): self.blackjack.draw_count = 5 self.blackjack.bet = 10 self.assertEqual(self.blackjack.draw_count, 5) self.assertEqual(self.blackjack.bet, 10) self.blackjack.restart() self.assertIsNone(self.blackjack.deck) self.assertEqual(self.blackjack.draw_count, 0) self.assertEqual(self.blackjack.bet, 0)
def _create_opponent_hand_combinations( self, state: State) -> List[Tuple[Card, Card]]: deck = Deck().get_cards() indexes = [] popped_cards = 0 opponent_hand_combinations = [] for i, c in enumerate(deck): if c in self.get_hand() or c in state.community_cards: indexes.append(i) for i in indexes: deck.pop(i - popped_cards) popped_cards += 1 for i, c1 in enumerate(deck): for c2 in deck[i + 1:]: opponent_hand_combinations.append((c1, c2)) return opponent_hand_combinations
def __init__(self): """ Initializing game variables and the variables that are applied to the layout. """ # Initially shuffled deck self.deck = Deck() # Player's chips, hand and a text representing the cards self.player_holdings_var = tk.IntVar(value=200) self.player_hand = None self.player_cards_var = tk.StringVar(value="") # Dealer's house bank, hand and a text representing the cards self.dealer_holdings_var = tk.IntVar(value=5000) self.dealer_hand = None self.dealer_cards_var = tk.StringVar(value="") # Holders for player's bet, the pot and a description of the winning hand self.bet_var = tk.IntVar(value=0) self.pot_var = tk.IntVar(value=0) self.winner_var = tk.StringVar(value="")
def test_draw_card_draws_random_card_from_deck(self, _random_module): deck = Deck() # Not using fixture because of 'hypothesis' cards_in_deck_before_draw = deck.cards.copy() card = deck.draw_card() assert card in cards_in_deck_before_draw
def p0(self): return Player('First', Deck())
from flask import render_template, request, url_for, redirect, send_file from app import app from game import Deck, Card import random deck = Deck() @app.route('/', methods=['GET', 'POST']) @app.route('/index', methods=['GET', 'POST']) def index(): title = 'random' deck = Deck() deck.shuffle() card = deck.drawCard() return render_template('index.html', title=title, card=card) @app.route('/card', methods=['GET', 'POST']) def card(): title = 'random' card = deck.drawCard() return render_template('index.html', title=title, card=card)
from pprint import pprint from game import Game, Player, Deck if __name__ == '__main__': p1_name = input('Player 1:') p2_name = input('Player 2:') p1 = Player(p1_name, Deck()) p2 = Player(p2_name, Deck()) game = Game(p1, p2) while not game.status['finished']: pprint(game.status) print('') print(f"It's {game.status['current_player']}'s turn") card_index = int(input('Which card? (index (<0 to pass))')) if card_index < 0: game.finish_turn() else: card = game.status['players'][ game.status['current_player']]['hand'][card_index] game.play_card(card) print(f"You planed {card}") print('') print('') print("Game Finished!! :D :D") pprint(game.status)
def other_player(self): return Player('Opponent', Deck())
def test_cards_left(self, _random_module): deck = Deck() deck.cards = [Card(0), Card(0), Card(8)] assert deck.cards_left() == 3 deck.draw_card() assert deck.cards_left() == 2
def test_initial_values(self): player = Player('Frank', Deck()) assert player.name == 'Frank' assert player.health == 30 assert player.mana_slots == 0 assert player.mana == 0
def test_draws_3_cards(self, draw_card_mock): _player = Player('Frank', Deck()) assert draw_card_mock.call_count == 3
def test_drawn_card_is_not_in_deck_anymore(self, _random_module): deck = Deck() # Not using fixture because of 'hypothesis' card = deck.draw_card() cards_in_deck_after_draw = deck.cards.copy() assert card not in cards_in_deck_after_draw
from __future__ import division import numpy as np #import xrange from game import CardGame, Deck, Player from mc import MonteCarloLearning CARDS_IN_DECK = {0.25: 16, 0.50: 28, 0.75: 16} NUM_PLAYERS = 2 HAND_SIZE = 5 ACTIONS = ['small_spoil', 'median', 'large_max'] NUM_GAMES_TO_PLAY = 2000000 deck = Deck(CARDS_IN_DECK) card_game = CardGame(deck, NUM_PLAYERS, ACTIONS, HAND_SIZE) def play_action(card_showing, player): if card_showing == 0: # player goes first if player.next_action == ACTIONS.index('small_spoil'): card_value = player.play_card(0) elif player.next_action == ACTIONS.index('large_max'): card_value = player.play_card(-1) else: card_value = player.play_card(card_game.hand_size // 2) else: # opponent went first, player's turn if player.next_action == ACTIONS.index('small_spoil'): for c, card in enumerate(player.hand): if card + card_showing > 1.0: # can spoil, play this card
def deck(self): return Deck()
def p1(self): return Player('Second', Deck())