def end(self): self.logger.info("Game ended! Determining winner...") players = self.players players.remove(self.declarer) defenders_points = 0 for player in players: defenders_points += player.result() declarer_points = self.declarer.result() # For now: adding points of "Skat" to Declarer skat = Trick() skat.add_card(self.deck.draw()) skat.add_card(self.deck.draw()) skat_points = skat.value() declarer_points += skat_points self.logger.info("Defenders: " + str(defenders_points) + ", Declarer: " + str(declarer_points)) if declarer_points >= 61: self.logger.info("Declarer " + str(self.declarer) + " won with " + str(declarer_points) + " points.") else: self.logger.info("Defenders " + str(players[0]) + " and " + str(players[1]) + " won with " + str(defenders_points) + " points.")
def get_trick(self, starting_player, spades_broken): """"Play a trick by getting a card from each player in order.""" current_trick = Trick() for i in range(4): player_id = (starting_player + i) % 4 card_played = self.players[player_id].play_card(current_trick.make_copy(player_id), self.get_valid_cards(player_id, current_trick, spades_broken)) assert self.verify_card_validity(card_played, player_id, current_trick, spades_broken) current_trick.add_card(card_played, player_id) self.hands[player_id].remove(card_played) return current_trick
def all_relevant_snapshots(self) -> \ Tuple[List[List[Player]], List[Trick], List[Card]]: """ Get all relevant data from DataGame: snapshots from all tricks, for the positions of both winners. :return: tuple of 3 elements: 1. list of all sets-of-hands during game, one set-of-hands for every trick 2. list of all open cards in specific trick, one set-of-cards for every trick 3. list of all real cards the player should act, one for every trick all above are *concatenation* of results of both winners. For example, cards_list = [cards_list_winner_1] + [cards_list_winner_2] """ winners_indices = [w.value - 1 for w in self.winner] # List of hands and trick for every winner hands_list_1: List[List[Player]] = [] curr_hands = deepcopy(self.players) for trick_idx in range(len(self.tricks)): hands_list_1.append(deepcopy(curr_hands)) for player_idx, player in enumerate(curr_hands): curr_hands[player_idx].hand.play_card( self.tricks[trick_idx].trick[player]) hands_list_2 = deepcopy(hands_list_1) trick_list_1: List[Trick] = [] trick_list_2: List[Trick] = [] trick_list = [trick_list_1, trick_list_2] chosen_cards_list_1: List[Card] = [] chosen_cards_list_2: List[Card] = [] chosen_cards_list = [chosen_cards_list_1, chosen_cards_list_2] for winner_idx, winner_list in enumerate([hands_list_1, hands_list_2]): for trick_idx, hands in enumerate(winner_list): curr_player = self.position_to_player( self.tricks[trick_idx].first_position) curr_trick = Trick({}) while curr_player.position != POSITIONS[ winners_indices[winner_idx]]: winner_list[trick_idx][PLAYERS_DICT[ curr_player.position.name]].play_card( self.tricks[trick_idx].trick[curr_player]) curr_trick.add_card( curr_player, self.tricks[trick_idx].trick[curr_player]) curr_player = self.players[PLAYERS_DICT[PLAYERS_CYCLE[ curr_player.position].name]] trick_list[winner_idx].append(curr_trick) chosen_cards_list[winner_idx].append( self.tricks[trick_idx].trick[curr_player]) return hands_list_1 + hands_list_2, trick_list_1 + trick_list_2, \ chosen_cards_list_1 + chosen_cards_list_2
def snapshot(self, trick_idx: int, position: PositionEnum) -> \ Tuple[List[Player], Trick, Card]: """ Image of one moment in game, in trick_idx trick, when player should play :param trick_idx: first trick is 0 :param position: the player to commit its turn now :return: current hands situation (ordered list of players), trick on desk and the chosen card (by player in given position). """ if trick_idx >= len(self.tricks): raise IndexError(f"trick_idx argument has to be smaller then " f"{len(self.tricks)}") # Load initial hands situation curr_hands = deepcopy(self.players) # Remove cards from all last tricks from hands for i in range(trick_idx): for j, p in enumerate(curr_hands): curr_hands[j].hand.play_card(self.tricks[i].trick[p]) # Remove cards of current trick from hands of all players which play # before given player. In addition, store these cards curr_pos = self.tricks[trick_idx].first_position curr_player = self.position_to_player(curr_pos) curr_trick = Trick({}) while curr_player.position != position: curr_hands[PLAYERS_DICT[curr_player.position.name]].play_card( self.tricks[trick_idx].trick[curr_player]) curr_trick.add_card(curr_player, self.tricks[trick_idx].trick[curr_player]) curr_player = self.players[PLAYERS_DICT[PLAYERS_CYCLE[ curr_player.position].name]] chosen_card = self.tricks[trick_idx].trick[curr_player] return curr_hands, curr_trick, chosen_card
def main(): pygame.init() pygame.font.init() name_font = pygame.font.SysFont('arial', 20) message_font = pygame.font.SysFont('arial', 16) clock = pygame.time.Clock() screen_size = (900, 600) # place_holder for board size diagram_width = 600 message_screen_height = 150 game_display = pygame.display.set_mode((screen_size[0] + diagram_width, screen_size[1] + message_screen_height)) game_display.fill((205, 205, 255), pygame.Rect(0, 0, screen_size[0], screen_size[1])) game_display.fill((255, 255, 255), pygame.Rect(screen_size[0] + 2, 0, diagram_width, screen_size[1])) game_display.fill((255, 255, 255), pygame.Rect(0, screen_size[1] + 2, screen_size[0] + diagram_width, message_screen_height)) pygame.display.update() #while not game_over: pygame.time.wait(1000) #initialize randomized cards and trump if fixed_hands: trump = 'Diamonds' else: trump = SUITS[random.randint(0, 3)] print(str(trump) + ' is trump') ord_cards = [] cards = [] for suit in SUITS: for face in FACES: rank = NORMAL_POINTS[face] if suit != trump else TRUMP_POINTS[face] ord_cards.append((suit, face, rank)) if fixed_hands: #SUITS = ['Hearts', 'Spades', 'Diamonds', 'Clubs'] #FACES = ['ace', 'king', 'queen', 'jack', 'ten', 'nine', 'eight', 'seven'] fixed_cards = {} # the hands are fixed, because we know the order of ord_cards, from the order of SUITS and FACES, we can use this to pick cards # for the hands, first number determines suit, second number determines rank #fixed_cards['South'] = [ord_cards[8 + 7], ord_cards[0 + 4], ord_cards[16 + 4], ord_cards[0 + 1], ord_cards[0 + 6], ord_cards[16 + 5], ord_cards[16 + 6], ord_cards[16 + 3]] #fixed_cards['West'] = [ord_cards[8 + 3], ord_cards[8 + 0], ord_cards[24 + 0], ord_cards[24 + 2], ord_cards[24 + 5], ord_cards[16 + 1], ord_cards[0 + 7], ord_cards[0 + 5]] #fixed_cards['North'] = [ord_cards[8 + 5], ord_cards[8 + 2], ord_cards[8 + 6], ord_cards[24 + 1], ord_cards[24 + 7], ord_cards[16 + 0], ord_cards[16 + 2], ord_cards[0 + 2]] #fixed_cards['East'] = [ord_cards[8 + 4], ord_cards[8 + 1], ord_cards[0 + 3], ord_cards[24 + 4], ord_cards[24 + 6], ord_cards[24 + 3], ord_cards[16 + 7], ord_cards[0 + 0]] fixed_cards['South'] = [ord_cards[0 + 1], ord_cards[0 + 2], ord_cards[0 + 3], ord_cards[0 + 7], ord_cards[16 + 0], ord_cards[16 + 4], ord_cards[16 + 5], ord_cards[16 + 6]] fixed_cards['West'] = [ord_cards[0 + 0], ord_cards[0 + 5], ord_cards[0 + 6], ord_cards[0 + 4], ord_cards[16 + 3], ord_cards[16 + 1], ord_cards[16 + 2], ord_cards[16 + 7]] fixed_cards['North'] = [ord_cards[8 + 7], ord_cards[8 + 4], ord_cards[8 + 2], ord_cards[8 + 1], ord_cards[24 + 6], ord_cards[24 + 5], ord_cards[24 + 7], ord_cards[24 + 0]] fixed_cards['East'] = [ord_cards[8 + 3], ord_cards[8 + 0], ord_cards[24 + 1], ord_cards[24 + 2], ord_cards[8 + 6], ord_cards[8 + 5], ord_cards[24 + 3], ord_cards[24 + 4]] cards = list(ord_cards) random.shuffle(cards) # print(cards) # initialize players assert NUM_PLAYERS == 4 player_1 = Player(Team('South', 'North', '1'), 0, 'South', cards) player_2 = Player(Team('East', 'West', '2'), 1, 'West', cards) player_3 = Player(Team('South', 'North', '1'), 2, 'North', cards) player_4 = Player(Team('East', 'West', '2'), 3, 'East', cards) players = [player_1, player_2, player_3, player_4] common_knowledge = [('GAME_RULE_TRUMP', trump, True)] idx = 0 idx2 = CLOSED_CARDS #assert CLOSED_CARDS == OPEN_CARDS for player in players: # Card division between players if not fixed_hands: player.closed_cards = cards[idx:idx2] idx = idx2 idx2 += OPEN_CARDS player.open_cards = cards[idx:idx2] idx = idx2 idx2 += CLOSED_CARDS else: idx = 0 idx2 = CLOSED_CARDS player.closed_cards = fixed_cards[player.name][idx:idx2] idx = idx2 idx2 += OPEN_CARDS player.open_cards = fixed_cards[player.name][idx:idx2] player.own_cards = list(player.closed_cards + player.open_cards) for open_card in player.open_cards: # Add open cards to common knowledge common_knowledge.append((player.name, open_card, False)) print(str(player.name) + ' has ' + str(player.closed_cards) + ' and ' + str(player.open_cards)) #print(str(player.name) + ' has ' + str(len(player.closed_cards)) + ' closed cards and ' + str(len(player.open_cards)) + ' open cards') for player in players: # Knowledge init player.knowledge.extend(common_knowledge) for closed_card in player.closed_cards: player.knowledge.append((player.name, closed_card, False)) player.create_possibles() if debug: print(player.name + " knows (initial) " + str(player.knowledge)) score = {'1' : 0, '2' : 0} # First rounds HE RE THE GAME BEGINS!!!!!!!!!!!!!!!!!! game_pause = True for game_round in range(NUM_ROUNDS): game_display.fill((255, 255, 255), pygame.Rect(screen_size[0] + 2, 0, diagram_width, screen_size[1])) model_title = 'Current card knowledge of players:' model_title_display = name_font.render(model_title, 1, (0, 0, 0)) game_display.blit(model_title_display, (screen_size[0] + 10, 10)) dropdown_width = 140 dropdown = pygame.Rect(screen_size[0] + 10, 35, dropdown_width, 20) pygame.draw.rect(game_display, (0, 0, 0), dropdown, 1) dropdown_display = message_font.render('choose a card', 1, (0, 0, 0)) game_display.blit(dropdown_display, ((dropdown.left + 5), dropdown.top)) for player in players: # Each following player picks card to play and plays pygame.draw.line(game_display, (0, 0, 0), (screen_size[0], screen_size[1]), (screen_size[0] + diagram_width, screen_size[1]), 2) pygame.display.update() message_counter = 0 turn = True menu_open = False select_available = False while turn: if player == players[0] and game_round == 0: run_one_frame = True else: run_one_frame = False for event in pygame.event.get(): if event.type == pygame.QUIT: return if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: game_pause = not game_pause print('PAUSED ' + str(game_pause)) if event.key == pygame.K_RIGHT: run_one_frame = True x, y = pygame.mouse.get_pos() #print(x, y) m_pressed = pygame.mouse.get_pressed() x_hover_dropdown = dropdown.left < x < dropdown.left + dropdown.width y_hover_dropdown = dropdown.top < y < dropdown.top + dropdown.height if x_hover_dropdown and y_hover_dropdown and m_pressed[0]: for i in range(1, len(ord_cards) + 1): dropdown_opt = pygame.Rect(screen_size[0] + 10, 35 + 20*i, dropdown_width, 20) pygame.draw.rect(game_display, (255, 255, 255), dropdown_opt, 0) pygame.draw.rect(game_display, (0, 0, 0), dropdown_opt, 1) card = ord_cards[i - 1] card_display = message_font.render(card[1] + ' of ' + card[0], 1, (0, 0, 0)) game_display.blit(card_display, ((dropdown_opt.left + 5), dropdown_opt.top)) pygame.display.update() menu_open = True if menu_open and not m_pressed[0]: select_available = True if select_available and m_pressed[0]: if x_hover_dropdown and dropdown.top + dropdown.height < y < dropdown.top + dropdown.height*(len(ord_cards) + 1): game_display.fill((255, 255, 255), pygame.Rect(screen_size[0] + dropdown_width + 10, 55, diagram_width, screen_size[1] - 55)) i = int((y - 35) / 20 - 1) card = ord_cards[i] picked_card = message_font.render('card is: ' + card[1] + ' of ' + card[0], 1, (0, 0, 0)) game_display.blit(picked_card, (screen_size[0] + 200, 60)) draw_model(game_display, players, message_font, card, screen_size[0], 55, diagram_width, screen_size[1] - 55) #else: # game_display.fill((255, 255, 255), pygame.Rect(screen_size[0] + 2, 55, diagram_width, screen_size[1] + message_screen_height - 55)) # pygame.draw.line(game_display, (0, 0, 0), (screen_size[0], screen_size[1]), (screen_size[0] + diagram_width, screen_size[1]), 2) pygame.display.update() select_available = False if not game_pause or run_one_frame: if player == players[0]: game_display.fill((205, 205, 255), pygame.Rect(0, 0, screen_size[0], screen_size[1])) for i in range(len(PLAYERS)): name_display = name_font.render(PLAYERS[i], 1, (0, 0, 0)) game_display.blit(name_display, name_pos(PLAYERS[i], 8, screen_size)) trump_display = name_font.render(str(trump) + ' is trump', 1, (0, 0, 0)) open_close_info = 'Game with ' + str(OPEN_CARDS) + ' open cards and ' + str( CLOSED_CARDS) + ' closed cards.' open_close_display = message_font.render(open_close_info, 1, (0, 0, 0)) continue_info1 = 'Press right arrow to continue' continue_info2 = 'Press space bar to skip to end and close' continue_display1 = message_font.render(continue_info1, 1, (0, 0, 0)) continue_display2 = message_font.render(continue_info2, 1, (0, 0, 0)) score_update1 = 'South and North: ' + str(score['1']) score_update2 = 'East and West: ' + str(score['2']) score_update_display1 = message_font.render(score_update1, 1, (0, 0, 0)) score_update_display2 = message_font.render(score_update2, 1, (0, 0, 0)) game_display.blit(score_update_display1, (20, screen_size[1] - 50)) game_display.blit(score_update_display2, (20, screen_size[1] - 35)) game_display.blit(trump_display, (10, 10)) game_display.blit(open_close_display, (10, 35)) game_display.blit(continue_display1, (600, 10)) game_display.blit(continue_display2, (600, 25)) else: clear_hands(game_display, (205, 205, 255), len(player.closed_cards)+len(player.open_cards), screen_size) game_display.fill((255, 255, 255), pygame.Rect(screen_size[0] + 2, 55, diagram_width, screen_size[1] - 55)) for pla in players: for i in range(len(pla.closed_cards)): # Draw hands closed cards card = pla.closed_cards[i] if pla == player: file_name = IMAGE_DICT[card[1]] + IMAGE_DICT[card[0]] + '.gif' else: file_name = 'b.gif' image, im_rect = load_image(file_name) game_display.blit(image, calc_offset(pla.name, len(pla.closed_cards)+len(pla.open_cards), screen_size, i)) for i in range(len(pla.open_cards)): card = pla.open_cards[i] if pla == player: file_name = IMAGE_DICT[card[1]] + IMAGE_DICT[card[0]] + '.gif' else: file_name = IMAGE_DICT[card[1]] + IMAGE_DICT[card[0]] + '.gif' image, im_rect = load_image(file_name) game_display.blit(image, calc_offset(pla.name, len(pla.closed_cards)+len(pla.open_cards), screen_size, i+len(pla.closed_cards))) pygame.display.update() if game_pause: if fast: pygame.time.delay(100) else: pygame.time.delay(1500) clear_hands(game_display, (205, 205, 255), len(player.closed_cards) + len(player.open_cards), screen_size) if player == players[0]: print('\nnew round') # First player plays a card here card, thoughts = players[0].play_card(trump) trick = Trick(trump, players[0], card) else: card, thoughts = player.play_card(trump, trick) trick.add_card(player, card) # game_display.fill((255, 255, 255), pygame.Rect(0, screen_size[1] + 2, screen_size[0] + diagram_width, message_screen_height)) for i in range(len(thoughts)): message = message_font.render(thoughts[i], 1, (0, 0, 0)) game_display.blit(message, (10, screen_size[1] + 10 + (message_counter * 20))) message_counter += 1 # Draw trick card = trick.cards[-1] file_name = IMAGE_DICT[card[1]] + IMAGE_DICT[card[0]] + '.gif' image, im_rect = load_image(file_name) game_display.blit(image, (screen_size[0]/2 + CENTER_POS[player.name][0], screen_size[1]/2 + CENTER_POS[player.name][1])) public_ann_played_card = 'public announcement: ' + player.name + ' plays ' + trick.cards[-1][1] + ' of ' + trick.cards[-1][0] played_card_display = message_font.render(public_ann_played_card, 1, (0, 0, 0)) game_display.blit(played_card_display, (10, screen_size[1] + 10 + (message_counter * 20))) message_counter += 1 pa_updated = False for pla in players: for i in range(len(pla.closed_cards)): # Draw hands closed cards card = pla.closed_cards[i] if pla == player: file_name = IMAGE_DICT[card[1]] + IMAGE_DICT[card[0]] + '.gif' else: file_name = 'b.gif' image, im_rect = load_image(file_name) game_display.blit(image, calc_offset(pla.name, len(pla.closed_cards)+len(pla.open_cards), screen_size, i)) for i in range(len(pla.open_cards)): card = pla.open_cards[i] if pla == player: file_name = IMAGE_DICT[card[1]] + IMAGE_DICT[card[0]] + '.gif' else: file_name = IMAGE_DICT[card[1]] + IMAGE_DICT[card[0]] + '.gif' image, im_rect = load_image(file_name) game_display.blit(image, calc_offset(pla.name, len(pla.closed_cards)+len(pla.open_cards), screen_size, i+len(pla.closed_cards))) pygame.display.update() try: # Knowledge update for all players pla.knowledge.remove((trick.players[-1].name, trick.cards[-1], False)) except ValueError: pass pla.knowledge.append((trick.players[-1].name, trick.cards[-1], True)) notices = pla.update_possibles(trick) if not pa_updated: for i in range(len(notices)): message = message_font.render(notices[i], 1, (0, 0, 0)) game_display.blit(message, (10, screen_size[1] + 10 + (message_counter * 20))) message_counter += 1 pa_updated = True if debug: print(pla.name + ' Knows (update): ' + str(pla.knowledge)) if game_pause: if fast: pygame.time.delay(100) else: pygame.time.delay(1500) if player.turn == 3: trick.check_bonus() if game_round == (NUM_ROUNDS - 1): score[trick.winner.team.nr] += int(trick.score + 10) # Final round is worth 10 points end = trick.winner.name + ' wins the final trick with highest card ' + str( trick.high_card) + ', trick score ' + str(int(trick.score + 10)) print(end) end_display = message_font.render(end, 1, (0, 0, 0)) game_display.blit(end_display, (10, screen_size[1] + message_screen_height - 25)) else: score[trick.winner.team.nr] += int(trick.score) # Score is added to winning team end = trick.winner.name + ' wins the trick with highest card ' + str( trick.high_card) + ', trick score ' + str(int(trick.score)) print(end) end_display = message_font.render(end, 1, (0, 0, 0)) game_display.blit(end_display, (10, screen_size[1] + message_screen_height - 25)) pygame.display.update() if not game_pause or run_one_frame: turn = False players = players[trick.winner.turn:] + players[:trick.winner.turn] # Winning player is new starter for i in range(NUM_PLAYERS): players[i].turn = i print(score) pit = False total = score['1']+score['2'] if score['1'] == 0: score['2'] = total + 100 print('team 2: PIT!') pit = True elif score['2'] == 0: score['1'] = total + 100 print('team 1: PIT!') pit = True if score['1'] <= (total/2) and not pit: print('team 1: NAT!') score['1'] = 0 score['2'] = total print(score) game_display.fill((205, 205, 255), pygame.Rect(20, screen_size[1] - 50, 150, 30)) pygame.display.update() score_update1 = 'South and North: ' + str(score['1']) score_update2 = 'East and West: ' + str(score['2']) score_update_display1 = message_font.render(score_update1, 1, (0, 0, 0)) score_update_display2 = message_font.render(score_update2, 1, (0, 0, 0)) game_display.blit(score_update_display1, (20, screen_size[1] - 50)) game_display.blit(score_update_display2, (20, screen_size[1] - 35)) pygame.display.update() pygame.time.delay(2500)
class Game(object): """ This class represents a game of UNO """ current_player = None current_trick = None trump = None started = False starter = None highest_bid = 0 highest_bidder = None reizen_done = False weitersagen = False reizen_sagenoderhoeren = 0 sager = None hoerer = None out = None #owner = ADMIN_LIST #open = OPEN_LOBBY def __init__(self, chat): self.chat = chat self.last_card = None self.last_trick_winner = None self.tricks_played = 0 self.declarer = None self.current_trick = Trick() self.deck = Deck() self.logger = logging.getLogger(__name__) @property def players(self): """Returns a list of all players in this game""" players = list() if not self.current_player: return players current_player = self.current_player itplayer = current_player.next players.append(current_player) while itplayer and itplayer is not current_player: players.append(itplayer) itplayer = itplayer.next return players def start(self): if len(self.players) < 3: self.logger.info("Can't start, not enough players.") else: self.started = True self.deck._fill_classic_() self.logger.info("Game started.") self.sagen_oder_hoeren = 0 self.sager = self.current_player.next self.hoerer = self.current_player self.logger.info("Geben: " + str(self.current_player.prev) + " Hören: " + str(self.current_player) + " Sagen: " + str(self.current_player.next)) #self. def turn(self): """Marks the turn as over and change the current player""" self.logger.debug("Next Player") self.current_player = self.current_player.next def play_card(self, card): """ Plays a card and triggers its effects. Should be called only from Player.play. Card is added to current trick. """ self.last_card = card self.current_trick.add_card(card) self.logger.info("Player " + str(card.owner) + " is playing card " + repr(card)) if len(self.current_trick.cards) == 3: self.tricks_played += 1 # Determine trick winner winnercard = self.current_trick.winner(self.trump) # Add trick to trick winner winnercard.owner.won_tricks.append(self.current_trick) # Set last trick winner of game self.last_trick_winner = winnercard.owner self.logger.info("Player " + str(winnercard.owner) + " won the trick " + str(self.current_trick.cards)) # Start a new trick self.current_trick = Trick() self.last_card = None # Trick winner starts new trick self.current_player = self.last_trick_winner if self.tricks_played == 10: self.end() else: self.turn() def end(self): self.logger.info("Game ended! Determining winner...") players = self.players players.remove(self.declarer) defenders_points = 0 for player in players: defenders_points += player.result() declarer_points = self.declarer.result() # For now: adding points of "Skat" to Declarer skat = Trick() skat.add_card(self.deck.draw()) skat.add_card(self.deck.draw()) skat_points = skat.value() declarer_points += skat_points self.logger.info("Defenders: " + str(defenders_points) + ", Declarer: " + str(declarer_points)) if declarer_points >= 61: self.logger.info("Declarer " + str(self.declarer) + " won with " + str(declarer_points) + " points.") else: self.logger.info("Defenders " + str(players[0]) + " and " + str(players[1]) + " won with " + str(defenders_points) + " points.")