class Game(object): def __init__(self, hacker, zardus, id): self.player1 = hacker self.player2 = zardus self.competitor_bet1 = random.randint(0, 1) self.competitor_bet2 = random.randint(0, 1) self.player2_bet = self.player2.bet(id, self.competitor_bet2) self.coin = Coin(id) self.id = id def error(self): print(f"Selection does not exist. {LOSE_MSG}") return LOSE def run(self): print( f"[Round {self.id}]: Your competitor bets on {self.competitor_bet1}" ) print(OPTIONS) selection = input().strip() if selection == COIN: print(COIN_ROTATE) selection = input().strip() if selection == LEFT: self.coin.rotate_left() elif selection == RIGHT: self.coin.rotate_right() elif selection != NOT_CHANGE: return self.error() player1_bet = self.coin.flip(self.competitor_bet1) elif selection == ZERO: player1_bet = 0 elif selection == ONE: player1_bet = 1 elif selection == "q": print("Magic quit option") sys.exit(0) else: return self.error() print( f"[Round {self.id}]: zardus's competitor bets on {self.competitor_bet2}, " + f"you bet on {player1_bet}") return self.play(player1_bet, self.player2_bet) def play(self, p1_bet, p2_bet): if p1_bet ^ p2_bet == self.competitor_bet1 * self.competitor_bet2: print(WIN_MSG) return WIN else: print(LOSE_MSG) return LOSE
def startGame(): print('::::::::::::::: Kelly Criterion Game :::::::::::::::') print(' Press CTRL + C to exit game ') print() # empty line cash = 25.00 coin = Coin(weight=0.60) # set weigthed coin to 60% heads while True: # user interface print('Account size: ${0}'.format(cash)) betInput = input("Bet size: $") bet = float(betInput) bet = round(bet, 2) # round to 2 decimal places # prevent user from wagering more than account size if (bet > cash): bet = cash # prevent user from wagering negative or 0 elif (bet < 0.01): bet = 0.01 if (coin.flip() == Face.HEADS): cash += bet # heads pays print('Result: Heads | last bet ${0} | won ${0}'.format(bet)) else: cash -= bet # tails lose print('Result: Tails | last bet ${0} | lost ${0}'.format(bet)) # clear account size and bet size lines sys.stdout.write(CURSOR_UP_ONE) # up to result line sys.stdout.write(CURSOR_UP_ONE) # up to bet line sys.stdout.write(ERASE_LINE) # erase bet line sys.stdout.write(CURSOR_UP_ONE) # up to account size line sys.stdout.write(ERASE_LINE) # erase account size line if (cash == 0.00): print('Game over!') return False
def flip_coin_2x_n_times(self, n): c = Coin() experiments = [sum([c.flip(), c.flip()]) == 2 for _ in range(n)] return sum(experiments) / n
def flip_coin_x_times(self, x): # return the probability of head c = Coin() return sum((c.flip() for _ in range(x))) / x
def flip_coin_with_pattern_n_times(self, pattern, n, flips): c = Coin() experiments = [[c.flip() for f in range(flips)] == pattern for e in range(n)] return sum(experiments) / n
i+=1 plt.tight_layout() plt.show() '''On a single graph, Use the coin.py random coin generator and overlay the initial uniform prior with the prior after 1, 2, 10, 50 and 250 flips.. Use the color parameter to give a different color to each layer. Use the label parameter to label each label. This simulation gives us the Beta Distribution! The shape parameters (alpha and beta) are 1 + # heads and 1 + # tails! We'll get into this more later.''' mycoin = Coin() # print(mycoin.flip()) # print(mycoin.flip()) fig, ax = plt.subplots(1,1, figsize=(14,9)) scenarios = [] num_flips = [1,2,10,50,250] colors = ['red','green','blue','orange','purple'] for flips in num_flips: scenario = [mycoin.flip() for flip in range(flips)] scenarios.append(scenario) bayes = Bayes(prior_dict.copy(),likelihood_func=likelihood) bayes.plot(ax,color='black',label='Number of flips: 0') for flips, color, scenario in zip(num_flips, colors, scenarios): bayes = Bayes(prior_dict.copy(),likelihood_func=likelihood) for flip in scenario: bayes.update(flip) bayes.plot(ax,color=color,label='Number of flips: '+str(flips),title='Beta Distribution for Unknown Biased Coin') plt.legend() plt.show()
name = input("What is your name? ") player_choice: str computer_choice: str # Loop forever getting input from player until it's a valid choice while True: player_choice = input("Choose Heads or Tails: ") if player_choice == "Heads" or player_choice == "Tails": break # The computer chooses the opposite of the player computer_choice = "Heads" if player_choice == "Tails" else "Tails" # Create a Person object for the player with their name and choice player1 = Person(name, player_choice) # Create a person object for the computer player2 = Person("Computer", computer_choice) # Create a coin object coin = Coin() # Flip the coin and store the result outcome = coin.flip() # Print the outcome if outcome == player1.chosen_side: print(player1.name + " wins the toss with a " + outcome) else: print(player2.name + " wins the toss with a " + outcome)
open("test_results.txt", "a+") open("card_results.txt", "a+") coinFlips = {'heads': 0, 'tails': 0} diceRolls = {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0} cardPicks = {} for card in deck.cards: cardPicks[card.show()] = 0 #Running through the outcomes 10 x 10k for x in range(0, 10): for i in range(0, iterations): coinFlip = coin.flip() coinFlips[coinFlip] += 1 diceRoll = dice.roll() diceRolls[str(diceRoll)] += 1 deck.shuffle() cardPick = deck.pickFirst() cardPicks[cardPick.show()] += 1 deck.returnCard(cardPick) with open('test_results.txt', "a") as result_file: result_file.write('Run ' + str(x + 1) + str(coinFlips) + ' ' + str(diceRolls) + '\n') with open('card_results.txt', "a") as card_results_file: card_results_file.write('Run ' + str(x + 1) + str(cardPicks))
def startGame(): print('::::::::::::::: Kelly Criterion Game :::::::::::::::') print(' Press CTRL + C to exit game ') print() # empty line player = Player(balance=25.00) player.call(Face.HEADS) player.bet(1.00) coin = Coin(weight=0.60) # set weigthed coin to 60% heads while True: line_count = 0 # user interface print('Account size: ${0}'.format(player.balance)) line_count += 1 print('[1] BET: ${0}'.format(player.bet_size)) line_count += 1 if player.called == Face.HEADS: print('[2] CALL: HEADS') elif player.called == Face.TAILS: print('[2] CALL: TAILS') line_count += 1 print('[ENTER] FLIP!') line_count += 1 userInput = input("option: ") line_count += 1 # option 1 if (userInput == "1"): betInput = round(float(input("Bet size: $")), 2) # convert to float -> round to 2 decimals line_count += 1 player.bet(betInput) # option 2 elif (userInput == "2"): print("[H] Heads") line_count += 1 print("[T] Tails") line_count += 1 callInput = input(">") line_count += 1 if (callInput == "H"): player.call(Face.HEADS) elif (callInput == "T"): player.call(Face.TAILS) # option ENTER elif (userInput == ""): # win if (coin.flip() == player.called): player.balance += player.bet_size # loss else: player.balance -= player.bet_size if player.bet_size > player.balance: player.bet(player.balance) # clear account size and bet size lines for iter in range(0, line_count): sys.stdout.write(CURSOR_UP_ONE) # up a line sys.stdout.write(ERASE_LINE) # erase line if (player.balance <= 0.00): print('Game over!') return False
from coin import Coin, RiggedCoin coin = Coin() riggedCoin = RiggedCoin() print(coin.flip()) print(riggedCoin.flip()) print(riggedCoin.rig())