def possible_move_set(self): ''' Returns a dictionary of lists in the form of [card,position] where the keys are the score that will be earned with the placement of the var card in var position ''' temp_dict = {} temp_deck = carddeck(0, 0) temp_deck.deck = self.card_hand.get_deck() for i in temp_deck.get_unique_cards_in_deck(): for j in self.gameboard.get_spots_open_on_board(): potential_score = self.gameboard.get_score_to_anchor_card(i, j) if potential_score in list(temp_dict.keys()): temp_dict[potential_score].append([i, j]) elif potential_score: temp_dict[potential_score] = [[i, j]] if not temp_dict and self.card_hand.get_number_of_cards_in_deck() == 1: for j in self.gameboard.get_spots_open_on_board(): potential_score = self.gameboard.get_score_to_anchor_card( i, j, mercy=True) if potential_score in list(temp_dict.keys()): temp_dict[potential_score].append([i, j]) elif potential_score: temp_dict[potential_score] = [[i, j]] if temp_dict: self.mercy = True return temp_dict
def setup_players(self, player_count=None): ''' if player_count != None: self.player = [game_player.game_player("Player "+str(i+1),0,self.main_board) for i in range(player_count)] else: self.player = [game_player.game_player("Player "+str(i+1),0,self.main_board) for i in range(2)] ''' self.player = self.parent.players print("Player_count!", len(self.player)) for i in self.player: i.reset_player_hand_and_score() self.player_turn = 0 self.player_count = len(self.player) self.card_dock.set_player(self.player[0]) count = 0 print('full deck', self.main_board.game_deck.get_deck(1)) print('len full deck', len(self.main_board.game_deck.get_deck(1))) while not self.main_board.game_deck.is_deck_empty(): dealt = self.main_board.game_deck.deal_card()[0] print("dealt", dealt) self.player[count % self.player_count].card_hand.take_card(dealt) count += 1 print('Player count', len(self.player)) for i in self.player: sorter = carddeck.carddeck(0) sorter.deck = i.card_hand.get_deck() sorter.sort_deck_by_card_suit() sorter.sort_deck_by_card_value() print(i.get_name(), "hand:", sorter.get_deck(1)) self.player[0].set_human(1)
def __init__(self, name=None, human_player=None, main_gameboard=None): self.name = name self.human_player = True if human_player else False self.mercy = False self.set_computer_level() self.last_score = 0 self.score = 0 self.card_hand = carddeck(0, 0) self.gameboard = main_gameboard or gameboard()
def get_cards_not_on_board(self): temp_deck = carddeck(0, 0, 0) temp_deck.deck = list( set.difference(set(self.game_deck.master_deck), set(self.get_cards_on_board()))) temp_deck.sort_deck_by_card_suit() temp_deck.sort_deck_by_card_value() temp_deck.print_readable_deck_color() return temp_deck.deck
def __init__(self, large_board=False, gamemode=0): self.game_board = [] self.GRID_SIZE_LARGE = 7 self.GRID_SIZE_SMALL = 4 self.EXTRA_SUIT = False self.MAX_GRID_HEIGHT_INDEX = None self.MIN_GRID_HEIGHT_INDEX = None self.MIDDLE_GRID_HEIGHT_INDEX = None self.MAX_GRID_WIDTH_INDEX = None self.MIN_GRID_WIDTH_INDEX = None self.MIDDLE_GRID_WIDTH_INDEX = None self.grid_size = self.GRID_SIZE_SMALL self.GAMEMODE = gamemode self.game_deck = carddeck(2, 0, self.EXTRA_SUIT) self.game_deck.deal_card(self.game_deck.find_card([6, 7, 8, 9, 10, 11])) self.game_kings_deck = carddeck(0, 0, self.EXTRA_SUIT) self.game_kings_deck.take_card_from_different_deck(self.game_deck, 'K', all_instances=1) self.game_deck.master_deck = self.game_deck.deck self.game_deck.reset_deck() self.game_deck.print_readable_deck() self.game_kings_deck.master_deck = self.game_kings_deck.deck self.game_kings_deck.reset_deck() self.trump_suit = 0 if large_board: self.grid_size = self.GRID_SIZE_LARGE self.initialize_game_board()
def update_dock(self): if self.player: for i in self.player_deck: self.scene.removeItem(i) player_deck = self.player.card_hand.get_deck() if not len(player_deck): return 1 sorter = carddeck.carddeck(0) sorter.deck = player_deck sorter.sort_deck_by_card_suit() sorter.sort_deck_by_card_value() player_deck = sorter.deck player_deck = [ playing_card_graphic(card=i, dock=self, parent=self.card_dock_back, scene=self.scene) for i in player_deck ] self.player_deck = player_deck length_deck = len(player_deck) count = 0 for i in player_deck: i.set_click_on() if count < 9: i.setX(self.top_row[count]) if length_deck <= 9: i.setY(self.mid_y) else: i.setY(self.top_y) i.setZValue(count) else: i.setX(self.bott_row[count % 9]) i.setY(self.bott_y) i.setZValue(count) count += 1 self.addToGroup(i)
def __init__(self, large_board=False): num_of_decks = 1 self.game_board = None if large_board: num_of_decks = 2 self.game_board = gameboard(1) else: self.game_board = gameboard() self.game_deck = self.game_board.game_deck self.game_deck.master_deck = self.game_deck.find_card( ['A', 'K', 1, 2, 3, 4, 5], all_instances=True) self.game_deck.reset_deck() self.kings_deck = carddeck(0, 0) self.kings_deck.take_card_from_different_deck(self.game_deck, 'K', all_instances=1) self.game_deck.shuffle_deck()
def computer_turns(self): self.player_turn = self.player_turn % self.player_count if self.is_game_over(): temp = game_over_screen(self.player, scene=self) temp.setX(0) temp.setY(0) temp.setZValue(200) self.addItem(temp) print("Screen Added!") if self.player[self.player_turn].is_human(): self.card_dock.set_player(self.player[self.player_turn]) self.card_dock.update_dock() return 0 else: while self.player[self.player_turn].is_computer(): comp_move = self.player[self.player_turn].get_computer_move() print('comp_move', comp_move) if not comp_move: self.player_turn = (self.player_turn + 1) % self.player_count else: self.player[self.player_turn].set_score( self.main_board.get_score_to_anchor_card( comp_move[0], comp_move[1], mercy=self.player[self.player_turn].has_mercy())) self.main_board.set_card_on_board(comp_move[0], comp_move[1]) self.player[self.player_turn].card_hand.deal_card( [comp_move[0]]) self.player_turn = (self.player_turn + 1) % self.player_count self.player_turn = self.player_turn % self.player_count if self.player_turn == 0: self.player[self.player_turn].read_all_moves() self.layout_board() for i in range(5): QtGui.QApplication.processEvents() if self.is_game_over(): temp = game_over_screen(self.player, scene=self) temp.setX(0) temp.setY(0) temp.setZValue(200) sewdakjlf.addItem(temp) print("Screen Added!") for i in self.player: sorter = carddeck.carddeck(0) sorter.deck = i.card_hand.get_deck() sorter.sort_deck_by_card_suit() sorter.sort_deck_by_card_value() print(i.get_name(), "hand:", sorter.get_deck(1)) if self.player[self.player_turn].is_human(): self.card_dock.set_player(self.player[self.player_turn]) self.card_dock.update_dock() ''' temp=game_over_screen(self.player,scene=self) temp.setX(0) temp.setY(0) temp.setZValue(200) self.addItem(temp) print("Screen Added!") ''' if self.is_human_game_over(): self.player_turn = (self.player_turn + 1) % self.player_count self.player_turn = self.player_turn % self.player_count return 0
def get_computer_move_v2(self): ''' Returns a move based on a newer AI algorithm that uses known probabilities and other known information about the current game that is available. All information used is fair to the game. For instance, this algorithm knows all the cards that aren't on the board or in its own hand, but it has no way of knowing which players have which cards. ''' open_spots = self.gameboard.get_spots_open_on_board() open_spots_playable = self.gameboard.get_open_spots_legally_playable() #standard_odds = [1, 5/6, 25/36, 125/216, 625/1296] standard_odds = [(5**i) / (6**i) for i in range(5)] standard_odds_comb = [5 / (6 + i) for i in range(5)] side_neighbor_counts = [ self.gameboard.get_spot_number_of_side_neighbors(i) for i in open_spots ] side_neighbor_counts_legal = [ self.gameboard.get_spot_number_of_side_neighbors(i) for i in open_spots_playable ] average = sum([standard_odds[i] for i in side_neighbor_counts ]) / len(side_neighbor_counts) average_legal = sum([ standard_odds[i] for i in side_neighbor_counts_legal ]) / len(side_neighbor_counts_legal) average_comb = sum( [standard_odds_comb[i] for i in side_neighbor_counts]) / len(side_neighbor_counts) average_comb_legal = sum([ standard_odds_comb[i] for i in side_neighbor_counts_legal ]) / len(side_neighbor_counts_legal) temp_deck = carddeck(1, False) temp_deck.deal_card( temp_deck.find_card(value=[6, 7, 8, 9, 10, 11, 12], all_instances=True)) temp_deck.deal_card( temp_deck.find_card(suit=[0, 1, 2], all_instances=True)) value_chain = [0 for i in range(6)] value_chain_future = [0 for i in range(6)] for i in temp_deck.get_deck(): for j in open_spots: value_chain_future[i.get_value()] += 1 for k in self.gameboard.get_spot_neighbor_cards_sides(j): if k.get_value() == i.get_value(): value_chain_future[i.get_value()] -= 1 break if self.gameboard.get_score_to_anchor_card(i, j, legal_score=True): value_chain[i.get_value()] += 1 value_chain = [i / len(open_spots) for i in value_chain] value_chain_spots = [i * len(open_spots) for i in value_chain] value_chain_future = [i / len(open_spots) for i in value_chain_future] value_chain_future_spots = [ i * len(open_spots) for i in value_chain_future ] value_chain_legal = [0 for i in range(6)] for i in temp_deck.get_deck(): for j in open_spots_playable: if self.gameboard.get_score_to_anchor_card(i, j, legal_score=True): value_chain_legal[i.get_value()] += 1 value_chain_legal = [ i / len(open_spots_playable) for i in value_chain_legal ] value_chain_spots_legal = [ i * len(open_spots_playable) for i in value_chain_legal ] print(len(open_spots), '\n', value_chain, '\n', value_chain_spots, sum(value_chain_spots) / len(value_chain_spots), '\n', value_chain_future, '\n', value_chain_future_spots, sum(value_chain_future_spots) / len(value_chain_future_spots), '\n', len(open_spots_playable), '\n', value_chain_legal, '\n', value_chain_spots_legal, sum(value_chain_spots_legal) / len(value_chain_spots_legal)) moves_dict = {i: [] for i in self.card_hand.get_deck()} for i in moves_dict.keys(): for j in open_spots: legal = self.gameboard.get_score_to_anchor_card( i, j, mercy=self.mercy, legal_score=True) if legal: score = self.gameboard.get_score_to_anchor_card( i, j, mercy=self.mercy) moves_dict[i].append([score, j]) print(i.get_info_color(), sum([k[0] for k in moves_dict[i]]) / len(moves_dict[i]), sorted(moves_dict[i], key=lambda x: x[0]), sep='\n') len(moves_dict[i]) / len(open_spots) possible_moves = self.possible_move_set() print("side_neigh_count=", side_neighbor_counts) print("average=", average, average * len(open_spots)) print("average legal=", average_legal, average_legal * len(open_spots)) print("average comb=", average_comb, average_comb * len(open_spots)) print("average comb legal=", average_comb_legal, average_comb_legal * len(open_spots))