def __init__(self, shoe: Shoe, players: List[Player], minimum_bet=10): self.shoe = shoe self.players = players self.house = Player("House", HouseStrategy(), 10000000) self.dealer_card = None self.hand_stack = [] self.minimum_bet = minimum_bet
def main(): players = [] hands = 100000 initial_money = 100000 table_minimum = 10 for name in ["Caroline", "Tom"]: player = Player(name, BookStrategy(), money=initial_money) players.append(player) for name in ["Barbara", "Percy", "Daniel"]: player = Player(name, OptimalStrategy(), money=initial_money) players.append(player) shoe = Shoe(decks=8) driver = GameDriver(shoe, players, minimum_bet=table_minimum) for x in range(hands): if shoe.needs_shuffling(): shoe.shuffle() driver.play_hand(x) print() for player in players: money_lost_per_hand_percentage = ((player.money - initial_money) / hands) / table_minimum odds = 0.50 + (money_lost_per_hand_percentage / 2) print(f"{player.name} Odds: {odds*100:.3f}%") plt.plot(range(0, hands + 1), player.money_history, label=player.name) plt.legend() plt.show()
def restart_game(self): self.game = TicTacToeGame() self.player1 = Player("Player1", "X") self.player2 = Player("Player2", "O") self.game.add_player([ self.player1, self.player2 ]) self.player1.start_first() self.game.start() self.reset_button()
def restart_game(self): self.game = TicTacToeGame() self.player = Player("Human", "X") self.enemy = RLBot("AI", "O") self.game.add_player([self.player, self.enemy]) self.player.start_first() self.game.start() self.state_space = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']] self.reset_button()
def restart_game(self): self.game = TicTacToeGame() self.player = Player("Player", "X") self.enemy = MMBot("Computer", "O") self.game.add_player([self.player, self.enemy]) self.state_space = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']] self.enemy.start_first() self.game.start() self.reset_button()
def restart_game(self): self.game = TicTacToeGame() self.player = Player("Player", "X") self.enemy = Bot("Computer", "O") self.game.add_player([self.player, self.enemy]) self.player.start_first() self.game.start() self.reset_button()
def restart_game(self): self.game = TicTacToeGame() self.player1 = Player("Player1", "X") self.player2 = Player("Player2", "O") self.game.add_player([self.player1, self.player2]) self.player1.start_first() self.game.start() self.reset_button()
def render_action(self, player: Player, hand: BlackjackHand, action: BlackjackAction): if action == BlackjackAction.Hit: card = self.shoe.deal_card() hand.hit(card) print(f"\tHits {card}") self.hand_stack.append((player, hand)) elif action == BlackjackAction.Double: player.remove_money(hand.bet) card = self.shoe.deal_card() hand.double_up(card) print(f"\tDoubles {card}") elif action == BlackjackAction.Split: player.remove_money(hand.bet) hand1, hand2 = BlackjackHand.split_hand(hand) hand1.hit(self.shoe.deal_card()) hand2.hit(self.shoe.deal_card()) print(f"Split {hand1} {hand2}") self.hand_stack.append((player, hand1)) self.hand_stack.append((player, hand2)) elif action == BlackjackAction.Stay: pass elif action == BlackjackAction.Bust: pass elif action == BlackjackAction.Surrender: if not Ruleset.rules().get("late_surrender"): raise RuntimeError("Late Surrender is not Allowed!") player.add_money(hand.bet / 2) print(f"Surrendered! +{hand.bet / 2}") else: raise RuntimeError("Dunno how to handle this Action")
class LocalMultiplayerView(Screen): dictIndexToButtonName = { 1: "btn1", 2: "btn2", 3: "btn3", 4: "btn4", 5: "btn5", 6: "btn6", 7: "btn7", 8: "btn8", 9: "btn9" } soundClick = SoundLoader.load("assets/menu_selection_click.ogg") game = TicTacToeGame() player1 = Player("Player1", "X") player2 = Player("Player2", "O") game.add_player( [player1, player2] ) player1.start_first() game.start() def set_all_button_disable(self, isDisable): for index in range(1, len(self.dictIndexToButtonName) + 1): self.ids[ self.dictIndexToButtonName[index] ].disabled = isDisable def set_all_button_text(self, value): for index in range(1, len(self.dictIndexToButtonName) + 1): self.ids[ self.dictIndexToButtonName[index] ].text = value def restart_game(self): self.game = TicTacToeGame() self.player1 = Player("Player1", "X") self.player2 = Player("Player2", "O") self.game.add_player([ self.player1, self.player2 ]) self.player1.start_first() self.game.start() self.reset_button() def reset_button(self): self.set_all_button_text("") self.set_all_button_disable(False) def btnRestart_press(self, btn): self.restart_game() self.reset_button() def btnMainMenu_press(self, btn): self.restart_game() self.manager.current = "mainmenu" def btnGame_press(self, btn): if (self.soundClick): self.soundClick.play() if (not self.game.isOver): if (self.player1.isTurn): selectedNum = 0 totalButton = len(self.dictIndexToButtonName) for index in range(1, totalButton + 1): if (btn == self.ids[ self.dictIndexToButtonName[index] ]): selectedNum = index break self.player1.pick(selectedNum) self.game.remove_choice(selectedNum) btn.text = self.player1.marking btn.disabled = True self.game.check_winner() if (self.game.isHasWinner or len(self.game.lstAvailableChoice) == 0): self.game.over() self.game.next_turn() elif (self.player2.isTurn): selectedNum = 0 totalButton = len(self.dictIndexToButtonName) for index in range(1, totalButton + 1): if (btn == self.ids[ self.dictIndexToButtonName[index] ]): selectedNum = index break self.player2.pick(selectedNum) self.game.remove_choice(selectedNum) btn.text = self.player2.marking btn.disabled = True self.game.check_winner() if (self.game.isHasWinner or len(self.game.lstAvailableChoice) == 0): self.game.over() self.game.next_turn() def btnGame_release(self, btn): if (self.game.isOver): self.set_all_button_disable(True) boxLayout = BoxLayout(orientation = "vertical") dlgGameOver = Popup(title = "GameOver", size_hint = (None, None), size = (400, 400), auto_dismiss = False) lblWinner = Label(text = "Winner : ") lblWinner.font_size = 24 btnRestart = Button(text = "Restart") btnRestart.bind(on_press = self.btnRestart_press) btnRestart.bind(on_release = dlgGameOver.dismiss) btnMainMenu = Button(text = "MainMenu") btnMainMenu.bind(on_press = self.btnMainMenu_press) btnMainMenu.bind(on_release = dlgGameOver.dismiss) if (self.player1.isWin): lblWinner.text += self.player1.name + " (" + self.player1.marking + ")" elif (self.player2.isWin): lblWinner.text += self.player2.name + " (" + self.player2.marking + ")" else: lblWinner.text = "Tie" boxLayout.add_widget(lblWinner) boxLayout.add_widget(btnRestart) boxLayout.add_widget(btnMainMenu) dlgGameOver.content = boxLayout dlgGameOver.open()
class RLSinglePlayerView(Screen): dictIndexToButtonName = { 1: "btn1", 2: "btn2", 3: "btn3", 4: "btn4", 5: "btn5", 6: "btn6", 7: "btn7", 8: "btn8", 9: "btn9" } soundClick = SoundLoader.load("assets/menu_selection_click.ogg") state_space = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']] game = TicTacToeGame() player = Player("Player", "X") enemy = RLBot("Computer", "O") game.add_player([player, enemy]) player.start_first() game.start() def set_all_button_disable(self, isDisable): for index in range(1, len(self.dictIndexToButtonName) + 1): self.ids[self.dictIndexToButtonName[index]].disabled = isDisable def set_all_button_text(self, value): for index in range(1, len(self.dictIndexToButtonName) + 1): self.ids[self.dictIndexToButtonName[index]].text = value def restart_game(self): self.game = TicTacToeGame() self.player = Player("Human", "X") self.enemy = RLBot("AI", "O") self.game.add_player([self.player, self.enemy]) self.player.start_first() self.game.start() self.state_space = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']] self.reset_button() def reset_button(self): self.set_all_button_text("") self.set_all_button_disable(False) def btnRestart_press(self, btn): self.restart_game() self.reset_button() self.state_space = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']] def btnMainMenu_press(self, btn): self.restart_game() self.manager.current = "selection" def btnRLGame_press(self, btn): if (self.soundClick): self.soundClick.play() if (not self.game.isOver): if (self.player.isTurn): selectedNum = 0 totalButton = len(self.dictIndexToButtonName) for index in range(1, totalButton + 1): if (btn == self.ids[self.dictIndexToButtonName[index]]): selectedNum = index break self.player.pick(selectedNum) self.game.remove_choice(selectedNum) self.game.play(self.state_space, "X", selectedNum) print("Human placed at :" + str(selectedNum)) btn.text = self.player.marking btn.disabled = True self.game.check_winner() if (self.game.isHasWinner or len(self.game.lstAvailableChoice) == 0): self.game.over() self.game.next_turn() if (self.enemy.isTurn): selectedNum = self.enemy.get_Predicted_Values(self.state_space) if (selectedNum == 0): self.game.over() elif (selectedNum > 0): self.game.play(self.state_space, "O", selectedNum) print("AI placed at :" + str(selectedNum)) if (selectedNum > 0): self.enemy.pick(selectedNum) self.game.remove_choice(selectedNum) self.ids[self.dictIndexToButtonName[ selectedNum]].text = self.enemy.marking self.ids[self.dictIndexToButtonName[ selectedNum]].disabled = True self.game.check_winner() if (self.game.isHasWinner or len(self.game.lstAvailableChoice) == 0): self.game.over() self.game.next_turn() def btnRLGame_release(self, btn): if (self.game.isOver): self.set_all_button_disable(True) boxLayout = BoxLayout(orientation="vertical") dlgGameOver = Popup(title="Game Over", size_hint=(None, None), size=(500, 400), auto_dismiss=False) lblWinner = Label(text="") lblWinner.font_size = 24 btnRestart = Button(text="Restart") btnRestart.bind(on_press=self.btnRestart_press) btnRestart.bind(on_release=dlgGameOver.dismiss) btnMainMenu = Button(text="MainMenu") btnMainMenu.bind(on_press=self.btnMainMenu_press) btnMainMenu.bind(on_release=dlgGameOver.dismiss) if (self.player.isWin): lblWinner.text += "You won!" elif (self.enemy.isWin): lblWinner.text += "Yay!! I won" else: lblWinner.text = "Its a draw! Hmmm...." boxLayout.add_widget(lblWinner) boxLayout.add_widget(btnRestart) boxLayout.add_widget(btnMainMenu) dlgGameOver.content = boxLayout dlgGameOver.open()
def game(): shoe = Shoe() game = GameDriver(shoe, Player("Test", OptimalStrategy())) game.dealer_card = Card(CardValue.King, CardSuit.Spades) return game
class LocalMultiplayerView(Screen): dictIndexToButtonName = { 1: "btn1", 2: "btn2", 3: "btn3", 4: "btn4", 5: "btn5", 6: "btn6", 7: "btn7", 8: "btn8", 9: "btn9" } soundClick = SoundLoader.load("assets/menu_selection_click.ogg") game = TicTacToeGame() player1 = Player("Player1", "X") player2 = Player("Player2", "O") game.add_player([player1, player2]) player1.start_first() game.start() def set_all_button_disable(self, isDisable): for index in range(1, len(self.dictIndexToButtonName) + 1): self.ids[self.dictIndexToButtonName[index]].disabled = isDisable def set_all_button_text(self, value): for index in range(1, len(self.dictIndexToButtonName) + 1): self.ids[self.dictIndexToButtonName[index]].text = value def restart_game(self): self.game = TicTacToeGame() self.player1 = Player("Player1", "X") self.player2 = Player("Player2", "O") self.game.add_player([self.player1, self.player2]) self.player1.start_first() self.game.start() self.reset_button() def reset_button(self): self.set_all_button_text("") self.set_all_button_disable(False) def btnRestart_press(self, btn): self.restart_game() self.reset_button() def btnMainMenu_press(self, btn): self.restart_game() self.manager.current = "mainmenu" def btnGame_press(self, btn): if (self.soundClick): self.soundClick.play() if (not self.game.isOver): if (self.player1.isTurn): selectedNum = 0 totalButton = len(self.dictIndexToButtonName) for index in range(1, totalButton + 1): if (btn == self.ids[self.dictIndexToButtonName[index]]): selectedNum = index break self.player1.pick(selectedNum) self.game.remove_choice(selectedNum) btn.text = self.player1.marking btn.disabled = True self.game.check_winner() if (self.game.isHasWinner or len(self.game.lstAvailableChoice) == 0): self.game.over() self.game.next_turn() elif (self.player2.isTurn): selectedNum = 0 totalButton = len(self.dictIndexToButtonName) for index in range(1, totalButton + 1): if (btn == self.ids[self.dictIndexToButtonName[index]]): selectedNum = index break self.player2.pick(selectedNum) self.game.remove_choice(selectedNum) btn.text = self.player2.marking btn.disabled = True self.game.check_winner() if (self.game.isHasWinner or len(self.game.lstAvailableChoice) == 0): self.game.over() self.game.next_turn() def btnGame_release(self, btn): if (self.game.isOver): self.set_all_button_disable(True) boxLayout = BoxLayout(orientation="vertical") dlgGameOver = Popup(title="GameOver", size_hint=(None, None), size=(400, 400), auto_dismiss=False) lblWinner = Label(text="Winner : ") lblWinner.font_size = 24 btnRestart = Button(text="Restart") btnRestart.bind(on_press=self.btnRestart_press) btnRestart.bind(on_release=dlgGameOver.dismiss) btnMainMenu = Button(text="MainMenu") btnMainMenu.bind(on_press=self.btnMainMenu_press) btnMainMenu.bind(on_release=dlgGameOver.dismiss) if (self.player1.isWin): lblWinner.text += self.player1.name + " (" + self.player1.marking + ")" elif (self.player2.isWin): lblWinner.text += self.player2.name + " (" + self.player2.marking + ")" else: lblWinner.text = "Tie" boxLayout.add_widget(lblWinner) boxLayout.add_widget(btnRestart) boxLayout.add_widget(btnMainMenu) dlgGameOver.content = boxLayout dlgGameOver.open()
class GameDriver: def __init__(self, shoe: Shoe, players: List[Player], minimum_bet=10): self.shoe = shoe self.players = players self.house = Player("House", HouseStrategy(), 10000000) self.dealer_card = None self.hand_stack = [] self.minimum_bet = minimum_bet def play_hand(self, id=0): print(f"####### Playing Hand #{id} ######################") finished_hands = [] # Check shoe to make sure it has enough cards for player in self.players: try: bet = player.get_initial_bet(self) except NoMoneyBucko as ex: continue # I don't think dealing two cards in this order will change any odds hand = BlackjackHand( [self.shoe.deal_card(), self.shoe.deal_card()], bet) player.get_dealt(hand) self.hand_stack.append((player, hand)) dealer_hand = BlackjackHand( [self.shoe.deal_card(), self.shoe.deal_card()], 0) self.house.get_dealt(dealer_hand) self.dealer_card = self.house.get_showing_card() self.hand_stack.insert(0, (self.house, dealer_hand)) last_hand = None # While not everyone has stayed/busted while self.hand_stack and not dealer_hand.is_blackjack(): player, hand = self.hand_stack.pop() if last_hand != hand: print(f"{hand}:") last_hand = hand action = player.get_action(self, hand) self.render_action(player, hand, action) if action in [ BlackjackAction.Double, BlackjackAction.Stay, BlackjackAction.Bust, BlackjackAction.Surrender, ]: finished_hands.append((player, hand)) if dealer_hand.is_blackjack(): finished_hands = self.hand_stack self.hand_stack = [] # Calculate self.payout(finished_hands, dealer_hand) for player in self.players: player.record_money_value() def render_action(self, player: Player, hand: BlackjackHand, action: BlackjackAction): if action == BlackjackAction.Hit: card = self.shoe.deal_card() hand.hit(card) print(f"\tHits {card}") self.hand_stack.append((player, hand)) elif action == BlackjackAction.Double: player.remove_money(hand.bet) card = self.shoe.deal_card() hand.double_up(card) print(f"\tDoubles {card}") elif action == BlackjackAction.Split: player.remove_money(hand.bet) hand1, hand2 = BlackjackHand.split_hand(hand) hand1.hit(self.shoe.deal_card()) hand2.hit(self.shoe.deal_card()) print(f"Split {hand1} {hand2}") self.hand_stack.append((player, hand1)) self.hand_stack.append((player, hand2)) elif action == BlackjackAction.Stay: pass elif action == BlackjackAction.Bust: pass elif action == BlackjackAction.Surrender: if not Ruleset.rules().get("late_surrender"): raise RuntimeError("Late Surrender is not Allowed!") player.add_money(hand.bet / 2) print(f"Surrendered! +{hand.bet / 2}") else: raise RuntimeError("Dunno how to handle this Action") def payout(self, hands: List[Tuple[Player, BlackjackHand]], dealer_hand: BlackjackHand): dealer_total = dealer_hand.get_hand_total() print("\nResults:") print(f"Hand - {dealer_hand} - {dealer_total}") summary_table = [] for player, hand in hands: hand_summary = [] player_total = hand.get_hand_total() if player == self.house: continue hand_summary += [ player.name, ",".join([str(x) for x in hand.cards]), hand.get_hand_total(), ] if dealer_hand.is_blackjack(): if hand.is_blackjack(): player.add_money(hand.bet) hand_summary.append("Bump") hand_summary.append(0) else: hand_summary.append("Got jacked!") hand_summary.append(-1 * hand.bet) else: if hand.is_busted(): hand_summary.append("Busted") hand_summary.append(-1 * hand.bet) elif hand.is_blackjack(): hand_summary.append("Blackjack") blackjack_win = (1.5 if Ruleset.rules().get( "blackjack_pays_3_to_2", False) else 1.2) * hand.bet hand_summary.append(blackjack_win) player.add_money(blackjack_win + hand.bet) elif dealer_total > 21: player.add_money(hand.bet * 2) # original and winnings hand_summary.append("Dealer Busted!") hand_summary.append(hand.bet) elif dealer_total > player_total: hand_summary.append("Dealer wins") hand_summary.append(-1 * hand.bet) elif player_total > dealer_total: player.add_money(hand.bet * 2) hand_summary.append("In the money") hand_summary.append(hand.bet) elif player_total == dealer_total: player.add_money(hand.bet) hand_summary.append("Bump") hand_summary.append(0) summary_table.append(hand_summary) print(tabulate(summary_table))