class TestHand(unittest.TestCase): def setUp(self): self.hand_1 = Hand(1) self.hand_2 = Hand(2) def test_add(self): c1 = Card(Colour.RED, 10) c2 = Card(Colour.BLUE, 1) c3 = Card(Colour.GREEN, 5) self.hand_1.add_card(c1) self.hand_1.add_card(c2) self.hand_1.add_card(c3) self.assertTrue(c1 in self.hand_1.cards) self.assertTrue(c2 in self.hand_1.cards) self.assertTrue(c3 in self.hand_1.cards) def test_add_many(self): c1 = Card(Colour.RED, 10) c2 = Card(Colour.BLUE, 1) c3 = Card(Colour.GREEN, 5) l = [c1, c2, c3] self.hand_1.add_cards(l) self.assertTrue(c1 in self.hand_1.cards) self.assertTrue(c2 in self.hand_1.cards) self.assertTrue(c3 in self.hand_1.cards) def test_remove(self): c1 = Card(Colour.RED, 10) c2 = Card(Colour.BLUE, 1) c3 = Card(Colour.GREEN, 5) self.hand_1.add_card(c1) self.hand_1.add_card(c2) self.hand_1.add_card(c3) self.hand_1.remove_card(c1) self.assertFalse(c1 in self.hand_1.cards) self.assertTrue(c2 in self.hand_1.cards) self.assertTrue(c3 in self.hand_1.cards) def test_remove_card_not_in_hand(self): c1 = Card(Colour.RED, 10) c2 = Card(Colour.BLUE, 1) c3 = Card(Colour.GREEN, 5) self.hand_1.add_card(c1) self.hand_1.add_card(c2) self.hand_1.remove_card(c3) self.assertTrue(c1 in self.hand_1.cards) self.assertTrue(c2 in self.hand_1.cards) self.assertFalse(c3 in self.hand_1.cards)
from cards import Card, Deck from hand import Hand from hit_stand import hit, hit_or_stand, show_some_cards, show_all_cards playing = True player_chips = chips() while True: print('WELCOME TO BLACKJACK') deck = Deck() deck.shuffle() player = Hand() player.add_cards(deck.deal()) player.add_cards(deck.deal()) dealer = Hand() dealer.add_cards(deck.deal()) dealer.add_cards(deck.deal()) #bet(player_chips) show_some_cards(player, dealer) while playing: hit_or_stand(deck, player) bet(player_chips)
class GameController: CONST_ISLAND_MASK = 0b10000 CONST_FOREST_MASK = 0b01000 CONST_PLAIN_MASK = 0b00100 CONST_MOUNTAIN_MASK = 0b00010 CONST_SWAMP_MASK = 0b00001 def __init__(self, deck_wanted_size, hand_init_size, enable_mulligan=True): assert deck_wanted_size >= hand_init_size assert deck_wanted_size >= self.get_land_quantity() self.deck = Deck() self.hand = Hand() self.board = Board() self.deck_wanted_size = deck_wanted_size self.hand_init_size = hand_init_size self.enable_mulligan = enable_mulligan self.decision_tree = {} self.compute_tree = [] def init(self): self.deck.clear() self.hand.clear() self.board.clear() def insert_lands_in_deck(self): for land, quantity in binary_lands.items(): card = Card( 'terrain', land & self.CONST_ISLAND_MASK, land & self.CONST_FOREST_MASK, land & self.CONST_PLAIN_MASK, land & self.CONST_MOUNTAIN_MASK, land & self.CONST_SWAMP_MASK, ) self.deck.add_card(card) def fill_deck(self): for i in range(self.deck_wanted_size - self.get_land_quantity()): self.deck.add_card(Card('useless')) def game_set_up(self): self.insert_lands_in_deck() self.fill_deck() self.deck.shuffle() # If we enable mulligans def mulligan_phase(self, hand_size): cards_drawn = self.deck.draw(hand_size) self.hand.clear() self.hand.add_cards(cards_drawn) if not self.enable_mulligan or \ self.deck.get_size() == 0 or \ hand_size == 0 or \ not self.hand.is_improvable(self.get_land_quantity(), self.deck.get_size()): return # Put card back into the deck and redraw self.deck.add_cards(self.hand.get_cards()) self.deck.shuffle() self.hand.clear() self.mulligan_phase(hand_size - 1) def run(self): self.init() self.game_set_up() self.mulligan_phase(self.hand_init_size) print(self.hand) ret = self.game() # game_result(ret) def check_succeed(self, binary_hand_matrix, chain=0b00000): for binary_card in binary_hand_matrix: binary_substract = [ x for x in binary_hand_matrix if x != binary_card ] chain = 0b00000 for elem in binary_card: chain = chain | elem print(chain) if chain == 0b11111: print("Returning True") return True print("Returning False") return False # def place_element_tree(self, tree, elem): # if not tree: # if type(elem) != tuple: # elem = tuple(elem) # for e in elem: # tree[e] = {} # return # for key, val in tree.items(): # self.place_element_tree(val, elem) # # def construct_decision_tree(self): # for card in self.board.get_cards(): # if card.name == 'cavern': # continue # self.place_element_tree(self.decision_tree, card) # # def do_compute_tree(self, next_elem, path=''): # if not next_elem: # return self.compute_tree.append(path) # for key, val in next_elem.items(): # if key in path: # self.do_compute_tree(val, path) # else: # self.do_compute_tree(val, path + key) # # def check_succeed(self): # if self.board.get_size() > 5 and self.board.has_cavern(): # return True # self.construct_decision_tree() # # print(json.dumps(decision_tree, indent=1)) # self.do_compute_tree(self.decision_tree) # # print(json.dumps(compute_tree, indent=1)) # # for elem in self.compute_tree: # if len(elem) == 5:#nb_diff_land_to_collect: # return True # return False def place_land_board(self): weight_matrix = {} for key, card in enumerate(self.hand.get_cards()): if card.name == 'useless': continue if card.name == 'cavern': if len(self.board.get_size()) == 5: weight_matrix[key] = 100 else: weight_matrix[key] = 0.5 if card in self.board.get_cards(): weight_matrix[key] = 1 weight_matrix[key] = len(card.get_mana_colors()) if not weight_matrix: return key = max(weight_matrix, key=weight_matrix.get) card_to_add = self.hand.remove(key) self.board.add_card(card_to_add) # print('%s added to the board...' % (card_to_add)) # print(self.board) def game(self): while self.deck.get_size() > 0: self.decision_tree = {} self.compute_tree = [] card_drawn = self.deck.draw(1) # print("Draw %s" % (card_drawn,)) self.hand.add_cards(card_drawn) self.place_land_board() print(self.board) if self.check_succeed(self.board.get_cards_binary_color_matrix()): return 1 return 0 @staticmethod def get_land_quantity(): return len(binary_lands)
class App: GAMECARD_SIZE = [40, 50] P1_Y_VAL = 400 P2_Y_VAL = 40 SLOTS_Y_VAL = 220 def __init__(self): self._running = True # Display surface for game self._display_surf = None self.size = self.weight, self.height = 640, 500 # Game objects self._game_cards_1 = [] self._game_cards_2 = [] self._game_slots = [] # Hand objects self._h1 = None self._h2 = None self._clicked = [] self._turn = 1 def on_init(self): pygame.init() self._display_surf = pygame.display.set_mode(self.size) self._display_surf.fill((250, 250, 250)) def on_event(self, event): if event.type == pygame.QUIT: self._running = False if event.type == pygame.MOUSEBUTTONDOWN: self._clicked.clear() pos = pygame.mouse.get_pos() if self._turn == 1: clicked_cards = [ o for o in self._game_cards_1 if o.rect.collidepoint(pos) ] else: clicked_cards = [ o for o in self._game_cards_2 if o.rect.collidepoint(pos) ] self._clicked.extend(clicked_cards) if event.type == pygame.MOUSEBUTTONUP: curr_hand = self._h1 if self._turn == 1 else self._h2 game_cards = self._game_cards_1 if self._turn == 1 else self._game_cards_2 pos = pygame.mouse.get_pos() released_on_slots = [ o for o in self._game_slots if o.rect.collidepoint(pos) ] if len(self._clicked) != 0 and len(released_on_slots) != 0: game_slot = released_on_slots[0] if (self._turn == 1 and len(game_slot.slot.p1_played) < 3) or \ (self._turn == 2 and len(game_slot.slot.p2_played) < 3): x, y = self._clicked[0].pos game_slot.handle_release(game_cards, self._clicked[0], curr_hand, self._game_slots) self.draw_card(x, y) self._clicked.clear() self.change_turn() debug_util.print_cards(o.card for o in self._game_cards_1) debug_util.print_cards(o.card for o in self._game_cards_2) def draw_card(self, x, y): hand = self._h1 if self._turn == 1 else self._h2 print("x pos of new card: " + str(x)) print("y pos of new card: " + str(y)) try: new_cards = CardManager.get_instance().pick_random_inactive_cards( 1) new_card = new_cards[0] hand.add_card(new_card) game_cards = self._game_cards_1 if self._turn == 1 else self._game_cards_2 game_cards.append(self.load_game_card(new_card, x, y)) except ValueError: print('no more cards to draw') def change_turn(self): self._turn = 1 if self._turn == 2 else 2 def on_loop(self): pass def on_render(self): self._display_surf.fill((250, 250, 250)) self.render_turn() if self._turn == 1: for obj in self._game_cards_1: obj.render(self._display_surf) else: for obj in self._game_cards_2: obj.render(self._display_surf) for obj in self._game_slots: obj.render(self._display_surf) pygame.display.update() def render_turn(self): turn_font = pygame.font.SysFont('Helvetica Neue', 40) turn_surface = pygame.Surface([40, 50]) pygame.draw.rect(turn_surface, (250, 250, 250), (0, 0, 40, 50)) if self._turn == 1: turn_surface.blit( turn_font.render(str(self._turn), True, (244, 146, 162)), [5, 10]) else: turn_surface.blit( turn_font.render(str(self._turn), True, (160, 244, 200)), [5, 10]) self._display_surf.blit(turn_surface, (0, 0)) def on_cleanup(self): pygame.quit() def on_execute(self): if self.on_init() == False: self._running = False self.load_game_objects() # Game loop while self._running: for event in pygame.event.get(): self.on_event(event) self.on_loop() self.on_render() self.on_cleanup() def load_game_objects(self): # Load Cards cm = CardManager.get_instance() cm.load_all_cards() # Load two Hands and add 7 starting cards to them self._h1 = Hand(1) self._h2 = Hand(2) self._h1.add_cards(cm.pick_random_inactive_cards(7)) self._h2.add_cards(cm.pick_random_inactive_cards(7)) # Load GameCards self._game_cards_1.extend( self.load_game_cards_for_hand(self._h1, self.P1_Y_VAL)) self._game_cards_2.extend( self.load_game_cards_for_hand(self._h2, self.P2_Y_VAL)) # Load Slots slots = [Slot() for i in range(9)] # Load GameSlots self._game_slots.extend(self.load_game_slots(slots, self.SLOTS_Y_VAL)) def load_game_cards_for_hand(self, hand, y_val): game_cards = [] x_val = 100 for card in hand.cards: game_cards.append(self.load_game_card(card, x_val, y_val)) x_val += 50 return game_cards def load_game_card(self, card, x_val, y_val): pos = [x_val, y_val] gc = GameCard(card, pos, self.GAMECARD_SIZE) return gc def load_game_slots(self, slots, y_val): game_slots = [] x_val = 50 for slot in slots: game_slots.append(self.load_game_slot(slot, x_val, y_val)) x_val += 50 return game_slots def load_game_slot(self, slot, x_val, y_val): pos = [x_val, y_val] gs = GameSlot(slot, pos, self.GAMECARD_SIZE) return gs