def play(self): self.start() # First 7 rounds (starting at 7, 1 less card each round until 1 card per person) for i in range(7): r = Round(7 - i, self.playerList) # Gets the round initialized print() r.bets() sleep(1) # One iteration for each trick for k in range(7 - i): self.updateDisplay(self.roundNumber, r.topCard.getSuit(), r.dealer) r.trick() sleep(1) # self.roundNumber += 1 r.end() self.pointsDisplay(self.roundNumber, r.dealer) self.roundNumber += 1 # Remaining 9 rounds (starting with 1 card, 1 more per round until 10) for i in range(9): r = Round(i + 2, self.playerList) # Gets the round initialized r.bets() sleep(1) for k in range(i + 2): self.updateDisplay(self.roundNumber, r.topCard.getSuit(), r.dealer) r.trick() sleep(1) # self.roundNumber += 1 r.end() self.pointsDisplay(self.roundNumber, r.dealer) self.roundNumber += 1 self.endGame()
def create_rounds(self): teams_count = len(self.teams) lower_value, upper_value = self.get_bound_values(teams_count) if teams_count == lower_value: first_round = Round(1) first_round.matches = self.create_matches(self.teams) self.rounds.append(first_round) else: first_round = Round(1) second_round = Round(2) first_round_teams_count = upper_value - lower_value second_round_teams_count = teams_count - first_round_teams_count first_round_teams = self.teams[:first_round_teams_count] first_round.matches = self.create_matches(first_round_teams) print("first round!") for m in first_round.matches: print(m.teams[0].players, 'vs', m.teams[1].players) print(m.map_pool) second_round_teams = self.teams[-second_round_teams_count:] second_round.matches = self.create_matches(second_round_teams, True) print("second round!") for m in second_round.matches: print(m.teams[0].players, 'vs', 'TBD') print(m.map_pool)
def add_player_move(self, player_id, card_text): if not self.rounds: self.rounds.append(Round()) player = self.players[player_id] card = Card.from_text(card_text) self.players[player_id].put_card(card) self.rounds[-1].add_turn(player, card) # if everyone added cards, prepare for a new round if len(self.rounds[-1].turns) == len(self.players): self.rounds.append(Round())
def create_round(self): snakes = [] for player in self.players: snakes.append(player.create_snake()) new_round = Round(snakes) self.current_round = new_round return new_round
def __init__(self, player_name, root): self.player_name = player_name self.score = 0 self.rounds = [Round(i) for i in range(10)] self.player_frame = Frame(root, borderwidth=3, relief="raised") self.name = Label(self.player_frame, borderwidth=5, relief="sunken", text=self.player_name) self.score_frames = [ Frame(self.player_frame, borderwidth=3, relief="raised") for i in range(10) ] self.top_subframes = [ Frame(self.score_frames[i], borderwidth=3, relief="raised") for i in range(10) ] self.bottom_subframes = [ Frame(self.score_frames[i], borderwidth=3, relief="raised") for i in range(10) ] self.throws = [[ Label(self.top_subframes[i], text=self.rounds[i].throw_one), Label(self.top_subframes[i], text=self.rounds[i].throw_two) ] for i in range(10)] self.scores = [ Label(self.bottom_subframes[i], text=self.rounds[i].score) for i in range(10) ] self.total_score_frame = Label(self.player_frame, borderwidth=3, relief="raised", text=self.score) self.display()
def test_guessers_with_judge(self): round1 = Round(TestRound.room1, 1) round1.judge = TestRound.user2 guessers = round1.guessers self.assertEqual(guessers, [TestRound.user1, TestRound.user3])
def start_round(self): team_up = self.team_order[self.order_idx] self.order_idx += 1 self.order_idx = self.order_idx % 4 round = Round(team_up) self.rounds.append(round)
def create_round_and_add_to_list_and_return_new_list(_rounds, _people): round_id = get_id_of_list_item_in_list_and_add_one(_rounds) initiator = get_initiator("initiator", _people) _round = Round(round_id, initiator) _rounds.append(_round) return _rounds
def play_game(self): """ Play a game. - Start score as game_score (usually 0) - New rounds while no player reaches max_score - If winner on round, winner's score is updated - History of game is updated with each round's result (winner and score of round) Return game winner and list of rounds results """ score = self.game_score() while max(score.values()) < self.n_points: new_round = Round(self.players, self.n_tiles, self.max_number) winner, score, _ = new_round.play_round() try: winner.update_score(score) except AttributeError: pass self.history.append({'winner': str(winner), 'score': score}) score = self.game_score() print self.str_game_score() print winner = [ str(player) for player in self.players if player.get_score() == max(score.values()) ][0] return winner, self.history
def run(self): game_over = False while (not game_over): # Run a round roundResult = Round(self.round, self.players, self.pins, self.game_header()) # Adjust instance variables as needed based on round results self.round += 1 self.scores[0] += roundResult.scores[0] self.scores[1] += roundResult.scores[1] self.bags[0] += roundResult.bags[0] self.bags[1] += roundResult.bags[1] # Setup for next round self.score_bags() self.rotate_order() winner = self.winner() game_over = (len(self.winner()) != 0) if (not game_over): print("Here is the current status of your game:\n") print(f"\t{self.game_header()}\n") handle_input("Press any key to continue to the next round...", WIPE) # End of game message handle_input(f"Congrats to Team {winner} on their victory! Here are the final results:\n\n" f"\t{self.game_header()}\n\n" "I hope everyone enjoyed this implementation of Spades! Press any key to end the game...")
def main(): # calculate max total surplus surplus = total_surplus() print(f"max surplus: {surplus}") # repeat simulation for ROUNDS times for counter in range(ROUNDS): # create trading round and set time for round round = Round(TYPES, AMOUNT, VALUATION, COSTS) t_end = time.time() + TIME # while time of round hasn't end while time.time() < t_end: # check if round can continue if not_last_round(round.agents, round.last_round): # select random agent and make offer agent = random.choice(round.agents) price = agent.offer_price() # check if offer was valid if price: procces_offer(price, round, agent) else: print("true") break # prints allocative efficiency print(f"surplus: {round.surplus}") print(f"allocative efficiency: {round.surplus / surplus * 100}") save_plots_transactions(round, counter)
def make_sample_game(): rounds = [Round(0) for _ in range(5)] for r in rounds: r.set_word('blah') players = [ Player('Jordan'), Player('York'), Player('Brittany'), Player('Airin'), ] teams = [ Team(players[:2]), Team(players[2:]) ] rounds[0].turn = 1 rounds[0].score = 6 rounds[1].turn = 0 rounds[1].score = 3 rounds[2].turn = 0 rounds[2].score = 4 rounds[3].turn = 1 rounds[3].score = 5 rounds[4].turn = 1 rounds[4].score = 6 game = Game(teams) game.rounds = rounds return game
def test_guessers_no_judge(self): round1 = Round(TestRound.room1, 1) guessers = round1.guessers self.assertEqual(guessers, [TestRound.user1, TestRound.user2, TestRound.user3])
def test_end_with_votes(self): round1 = Round(TestRound.room1, 1) scores = {TestRound.user1: 3, TestRound.user2: 2, TestRound.user3: 1} # Set initial scores TestRound.user1.current_score = 10 TestRound.user2.current_score = 15 TestRound.user3.current_score = 20 round1.scores = scores # Set voting TestRound.user1.current_vote = "A vote" TestRound.user2.current_vote = "Another vote" TestRound.user3.current_vote = "A vote" round1.end() # New scores self.assertEqual(TestRound.user1.current_score, 13) self.assertEqual(TestRound.user2.current_score, 17) self.assertEqual(TestRound.user3.current_score, 21) # Voting self.assertIsNone(TestRound.user1.current_vote) self.assertIsNone(TestRound.user2.current_vote) self.assertIsNone(TestRound.user3.current_vote)
def blackjack(): """Manages the blackjack game""" print_menu() menu_choice = input("What would you like to do (1/2)? ") while menu_choice == "1": current_round = Round() current_round.print_header() current_round.deal() # Ends the game if the player gets blackjack if current_round.round_winner: print_menu() menu_choice = input("What would you like to do (1/2)? ") continue current_round.print_hands() move = input("What is your move (Hit/Stay)? ") while move == "Hit" and current_round.round_winner == False: current_round.player_hit() if current_round.round_winner == False: move = input("What is your move (Hit/Stay)? ") # Only plays the dealer if the player doesn't bust if not current_round.round_winner: current_round.play_for_dealer() print_menu() menu_choice = input("What would you like to do (1/2)? ")
def update_from_df(self, players_df, rounds_df): for index, row in players_df.iterrows(): name = str(row['name']) self.players[name] = Player(*row) for round_number in rounds_df['round_number'].unique(): courts = rounds_df[rounds_df['round_number'] == round_number].court.max() + 1 teams = [] scores = [] for i, row in rounds_df[rounds_df['round_number'] == round_number].iterrows(): teams.append(row.team_1) teams.append(row.team_2) if row.score_1 > 0: scores.append(row.score_1) scores.append(row.score_2) else: scores.append(None) scores.append(None) self.rounds[round_number] = Round(round_number, courts, None, teams=teams, scores=scores) self.next_round = len(self.rounds) + 1
def __init__(self, starting_amount, small_blind, number_of_players): """ Initializes a new poker game, asks for the starting bank, the small blind amount and the amount of players. Sets big_blind = 2 * small_blind. Initializes a new state. """ self.player_list = [Player("Player" + str(i + 1)) for i in range(number_of_players)] self.round = Round(small_blind, self.player_list) self.starting_amount, self.small_blind, self.big_blind = starting_amount, small_blind, small_blind * 2
def DigitSum(x): suma = 0 while Round(x) != x: x = x * 10 while x > 0: suma += x % 10 x = x // 10 return suma
def test_initializing_round(self): p1, p2, p3, p4 = Player(name='ime1'), Player(name='ime2'),\ Player(name='ime3'), Player(name='ime4') t1, t2 = Team('prqkor1', p1, p3), Team('prqkor2', p2, p4) r = Round(1, t1, t2) self.assertIsNotNone(r)
def test_hash(self, m_room_hash): round1 = Round(TestRound.room1, 1) m_room_hash.return_value = 123 self.assertEqual(hash(round1), hash(f'{hash(123)}1')) m_room_hash.assert_called()
def test_give_points(self): round1 = Round(TestRound.room1, 1) round1.give_points(3, TestRound.user2) scores = {TestRound.user1: 0, TestRound.user2: 3, TestRound.user3: 0} self.assertEqual(round1.scores, scores)
def test_str(self, m_room_repr): round1 = Round(TestRound.room1, 1) m_room_repr.return_value = 'RM01' self.assertEqual(str(round1), 'RM01 - Round 1') m_room_repr.assert_called()
def test_all_votes_in_all_votes(self, m_has_voted): round1 = Round(TestRound.room1, 1) round1.judge = TestRound.user1 m_has_voted.side_effect = [True, True] self.assertTrue(round1.all_votes_in) m_has_voted.assert_called()
def testAddTable(self): round1 = Round() table1 = self.getTable("T1","P1","P2","P3","P4") table2 = self.getTable("T2","P5","P6","P7","P8") round1.addTable(table1) round1.addTable(table2) tables = round1.getTables() self.assertEqual(table1,tables[0]) self.assertEqual(table2,tables[1])
def test_set_order_with_player(self): p1, p2, p3, p4 = Player(name='ime1'), Player(name='ime2'),\ Player(name='ime3'), Player(name='ime4') t1, t2 = Team('prqkor1', p1, p3), Team('prqkor2', p2, p4) r = Round(1, t1, t2) r.set_order(p4) self.assertEqual(r.on_turn, p4)
def MaxDigit(x): max = 0 while Round(x) != x: x = x * 10 while x > 0: if x % 10 > max: max = x % 10 x = x // 10 return max
def init_game(self): ''' Initialilze the game of Limit Texas Hold'em This version supports two-player limit texas hold'em Returns: (tuple): Tuple containing: (dict): The first state of the game (int): Current player's id ''' # Initilize a dealer that can deal cards self.dealer = Dealer() # Initilize two players to play the game self.players = [ Player(i, self.init_chips) for i in range(self.num_players) ] # Initialize a judger class which will decide who wins in the end self.judger = Judger() # Deal cards to each player to prepare for the first round for i in range(self.num_players): self.players[i].hand.append(self.dealer.deal_card()) # Initilize public cards self.public_cards = [] # Randomly choose a big blind and a small blind s = np.random.randint(0, self.num_players) b = (s + 1) % self.num_players self.players[b].in_chips = self.big_blind self.players[s].in_chips = self.small_blind # The player next to the small blind plays the first self.game_pointer = (b + 1) % self.num_players # Initilize a bidding round, in the first round, the big blind and the small blind needs to # be passed to the round for processing. self.round = Round(self.num_players, self.big_blind) self.round.start_new_round(game_pointer=self.game_pointer, raised=[p.in_chips for p in self.players]) # Count the round. There are 4 rounds in each game. self.round_counter = 0 # Save the hisory for stepping back to the last state. self.history = [] self.action_history = [] for i in range(2): self.action_history.append([]) state = self.get_state(self.game_pointer) return state, self.game_pointer
def test_serialize_not_full(self): round1 = Round(TestRound.room1, 1) movie = MagicMock() movie.serialize.return_value = 'A movie' round1.movie = movie ser = {'judge': '', 'movie': 'A movie', 'number': 1} self.assertEqual(round1.serialize(full=False), ser) movie.serialize.assert_called_with(full=False)
def p1_vs_p2(screen, board): board.clean_pieces() board.draw() p1 = Player(screen, board.X) p2 = Player(screen, board.O) r = Round(p1, p2, board) who_win = r.start() board.preview_piece = None board.draw() return who_win
def cpu_vs_cpu(screen, board): board.clean_pieces() board.draw() p1 = AI(ai_tree, 1) p2 = AI(ai_tree, 2) r = Round(p1, p2, board) who_win = r.start() board.preview_piece = None board.draw() return who_win