Mar-10-2022, 09:34 PM
(This post was last modified: Mar-11-2022, 06:06 AM by deanhystad.)
Something like this?
import random import collections ALL_GAMES = [ "Candyland", "COD", "Connect 4", "Life", "Monopoly", "Risk", "Skippo", "Uno", "Yahtzee" ] class GamerMeta(type): """So we can do Gamer[player_name]""" def __getitem__(cls, name): return cls.Gamers[name] class Gamer(metaclass=GamerMeta): Gamers = {} # Dictionary of all players choices def __init__(self, name, games): """Create Gamer object. Verify games choices""" for game in games: if game not in ALL_GAMES: raise ValueError(f"{game} is not in our collection") self.name = name self.games = games self.Gamers[name] = self # Add gamer to Gamers list def votes(self): """Get weighted game selections for this Gamer""" if self.games == ALL_GAMES: return ALL_GAMES # Game catalog is not weighted # Weight gamer's choices. First choice gets 5 votes, second 4 votes and so on. vote = [] for game, weight in zip(self.games, [5, 4, 3, 2, 1]): vote += [game] * weight return vote @classmethod def cast_ballots(cls, gamers): """Collect votes from gamers""" ballots = [] for gamer in gamers: ballots += cls.Gamers[gamer].votes() return ballots def __repr__(self): """Get string with info about this gamer""" return f"{self.name} games = {self.games}" def random_choice(*gamers): """Randomly choose from "votes" for all of tonights gamers""" return random.choice(Gamer.cast_ballots(gamers)) def weighted_choice(*gamers): """ Select favorite game as voted on by gamers. If multiple games tie as the favorite, randomly select from those games """ ballots = collections.Counter(Gamer.cast_ballots(gamers)).most_common() return random.choice([game[0] for game in ballots if game[1] == ballots[0][1]]) # Add gamers to the "database" Gamer("Catalog", ALL_GAMES) Gamer("Dad", ("Uno", "Monopoly", "Candyland")), Gamer("Mom", ("Life", "Uno", "Connect 4")), Gamer("Thing 1", ("COD", "Monopoly", "Skippo", "Risk", "Uno")), Gamer("Thing 2", ("Yahtzee", "Skippo", "Uno", "Life")) # Try some random combinations to see how it works. print("Taking a chance") print("Tonight we play", random_choice("Catalog")) print("Tonight we play", random_choice("Thing 1", "Thing 2")) print("Tonight we play", random_choice("Mom", "Dad")) print("Tonight we play", random_choice("Mom", "Dad", "Thing 1", "Thing 2")) print("Tonight we play", random_choice("Mom", "Thing 1", "Catalog")) # Try some weighted combinations to see how it works. print("\nStrict democracy in action") print("Tonight we play", weighted_choice("Catalog")) print("Tonight we play", weighted_choice("Thing 1", "Thing 2")) print("Tonight we play", weighted_choice("Mom", "Dad")) print("Tonight we play", random_choice("Mom", "Dad", "Thing 1", "Thing 2")) print("Tonight we play", weighted_choice("Mom", "Thing 1", "Catalog"))This program can randomly selects a game from "weighted" player selections or it can select the game with the most votes. Weighting and voting are done by giving a player's first choice 5 votes, their second choice 4 votes and so on. Randomly selecting a game from the weighted has the highest probability of picking the game with the most number of votes, but being random can also pick the game with the least.