def add_content(): draw_sympolygon(4, Point((500, 500, 100), renderer), 75, renderer) draw_sympolygon(4, Point((500, 500, 175), renderer), 75, renderer) draw_starfield(renderer) draw_pointfield(renderer)
def __init__(self, surface, player_a_label, player_b_label, sets_in_match=3): """ Creates a simulator :param surface: The surface the match is played on :param player_a_label: The label for player a :param player_b_label: The label for player b :param sets_in_match: The number of sets in a match. Default is 3. """ self.player_a_label = player_a_label self.player_b_label = player_b_label self.sets_in_match = sets_in_match # By tennis rules, must win at least 6 games to win a set self.min_games_to_win_set = 6 self.surface = surface self.point = Point(surface) self.debug_enabled = False
def generate_bridge(self): # Y width of bridge, including walls bridge_width = random.randint(5, 12) upper_right_corner_of_bridge = Point( self.map.width - 2, random.randint(1, self.map.height - bridge_width - 1)) bridge_length = random.randint(4, 8) for y in (upper_right_corner_of_bridge.y - 1, upper_right_corner_of_bridge.y + bridge_width): for x in range(upper_right_corner_of_bridge.x - bridge_width - 1, upper_right_corner_of_bridge.x + 1): print(y, x) self.map[y][x] = wall_tile() for y in range(upper_right_corner_of_bridge.y, upper_right_corner_of_bridge.y + bridge_width): for x in range(upper_right_corner_of_bridge.x - bridge_width, upper_right_corner_of_bridge.x): print(y, x) self.map[y][x] = floor_tile('Bridge') self.map[upper_right_corner_of_bridge.y][ upper_right_corner_of_bridge.x] = wall_tile() self.map[upper_right_corner_of_bridge.y + bridge_width - 1][upper_right_corner_of_bridge.x] = wall_tile() for y in range(upper_right_corner_of_bridge.y + 1, upper_right_corner_of_bridge.y + bridge_width - 1): self.map[y][upper_right_corner_of_bridge.x] = floor_tile() for y in range(upper_right_corner_of_bridge.y, upper_right_corner_of_bridge.y + bridge_width): self.map[y][upper_right_corner_of_bridge.x + 1] = viewport_tile() print(self.map)
def draw_pointfield(renderer): """ Generate 250 random points. """ for point in range(250): renderer.camera.objectsInWorld.append( Point((randrange( 0, renderer.screenWidth), randrange(0, renderer.screenHeight), randrange(0, renderer.desiredDepth)), renderer)) renderer.camera.drawScene()
def mainloop(renderer): """ Main "game" loop. """ print("Press a to scale scene down, s to scale scene up, and r to rotate.") add_content() while 1: renderer.render_screen() rotate_space(1, Point((0, 0, 0), renderer), Point((1000, 1000, 1000), renderer), renderer) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_a: scale_space(0.5, renderer) elif event.key == pygame.K_s: scale_space(2.0, renderer) elif event.key == pygame.K_r: rotate_space(90, Point((0, 0, 0), renderer), Point((1000, 1000, 1000), renderer), renderer)
class TennisMonteCarloSimulator: """ Monte-Carlo simulator for a tennis match. Should not be re-used to simulate another tennis match after calling simulate()! """ def __init__(self, surface, player_a_label, player_b_label, sets_in_match=3): """ Creates a simulator :param surface: The surface the match is played on :param player_a_label: The label for player a :param player_b_label: The label for player b :param sets_in_match: The number of sets in a match. Default is 3. """ self.player_a_label = player_a_label self.player_b_label = player_b_label self.sets_in_match = sets_in_match # By tennis rules, must win at least 6 games to win a set self.min_games_to_win_set = 6 self.surface = surface self.point = Point(surface) self.debug_enabled = False def simulate(self): """ Runs the monte-carlo simulation for a match. :param player_a_label: The string to return if player a wins :param player_b_label: The string to return if player b wins :return: The value passed as player_a_label or player_b_label as the winner. Does not return any game statistics. (future option?) """ # Instantiates a new point with player A having initiative (serve). server = self.player_a_label player_a_games = 0 player_b_games = 0 player_a_sets = 0 player_b_sets = 0 self.points_won = Counter() while True: # Get the result of the next game game_winner = self.get_next_game_winner(server) if game_winner is self.player_a_label: player_a_games += 1 if player_a_games >= self.min_games_to_win_set and player_a_games - player_b_games >= 2: if self.debug_enabled: print 'set winner', self.player_a_label, player_a_games, player_b_games player_a_sets += 1 # if player has won enough sets if player_a_sets >= self.sets_in_match // 2 + 1: print 'match winner', self.player_a_label, str(player_a_sets), 'to', str(player_b_sets), 'with', self.points_won[self.player_a_label], 'points to', self.points_won[self.player_b_label] return self.player_a_label # reset for the next set player_a_games = 0 player_b_games = 0 else: player_b_games += 1 if player_b_games >= self.min_games_to_win_set and player_b_games - player_a_games >= 2: if self.debug_enabled: print 'set winner', self.player_b_label, player_b_games, player_a_games player_b_sets += 1 # if player has won enough sets if player_b_sets >= self.sets_in_match // 2 + 1: print 'match winner', self.player_b_label, str(player_b_sets), 'to', str(player_a_sets), 'with', self.points_won[self.player_b_label], 'points to', self.points_won[self.player_a_label] return self.player_b_label # reset for the next set player_a_games = 0 player_b_games = 0 # Flip the server each game server = self.player_b_label if server is self.player_a_label else self.player_a_label def get_next_game_winner(self, server): """ Uses get_next_point_winner to assign points to players until a player wins the game :param server: The label of the player that is the server :return: The label of the player that won the game """ player_a_points = 0 player_b_points = 0 # All games start by serving from the right serve_left = False while True: if self.get_next_point_winner(server, serve_left) is self.player_a_label: self.points_won[self.player_a_label] += 1 player_a_points += 1 if self.debug_enabled: print 'point winner', self.player_a_label, player_a_points, player_b_points, 'served by', server if player_a_points >= 4 and player_a_points - player_b_points >= 2: if self.debug_enabled: print 'game winner', self.player_a_label, player_a_points, player_b_points, 'served by', server return self.player_a_label else: self.points_won[self.player_b_label] += 1 player_b_points += 1 if self.debug_enabled: print 'point winner', self.player_b_label, player_a_points, player_b_points, 'served by', server if player_b_points >= 4 and player_b_points - player_a_points >= 2: if self.debug_enabled: print 'game winner', self.player_b_label, player_b_points, player_a_points, 'served by', server return self.player_b_label # switch serve side serve_left = not serve_left def get_next_point_winner(self, server, serve_left=True): """ Uses the two player objects self.player_a and self.player_b to simulate a point. :param server: The label of the player that is the server :param serve_left: boolean of whether the server serves from the left. :return: The label of the player that won the point """ if server is self.player_b_label: self.point.player1 = self.player_b_label self.point.player2 = self.player_a_label else: self.point.player1 = self.player_a_label self.point.player2 = self.player_b_label self.point.serveSide = 'adv' if serve_left else 'deuce' return self.point.serve()
with open(LO_file, 'r') as f: cr = csv.reader(f) for row in cr: LO_matches.append(row[0]) winners[row[0]] = row[3] loo = LeaveOneOut() # point = Point(valid_matches, 'Grass') # print len(all_matches) for _, test in loo.split(LO_matches): to_leave_out = set([LO_matches[test[0]]]) valid_matches = all_matches - to_leave_out point = Point(valid_matches, 'Hard') to_leave_out = to_leave_out.pop() p1 = match_players[to_leave_out]['player1'] p2 = match_players[to_leave_out]['player2'] # surface = surfaces[to_leave_out].strip() # point.surface = surface res = overallSim(p1, p2, point) winner = winners[to_leave_out] if res == winner: correct += 1 total += 1 if total % 10 == 0: print total, correct / float(total) print p1, p2, winner, res