-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtournament.py
83 lines (63 loc) · 2.8 KB
/
tournament.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import pandas as pd
import matplotlib.pyplot as plt
def play_match(player1, player2, rounds):
'''Plays a match between two players'''
player1.reset()
player2.reset()
for _ in range(rounds):
p1_move = player1.strategy(player2.history)
p2_move = player2.strategy(player1.history)
# Update histories
player1.history.append(p1_move)
player2.history.append(p2_move)
# Update scores
if p1_move == 'C' and p2_move == "C":
player1.score += 3
player2.score += 3
elif p1_move == "C" and p2_move == "D":
player1.score += 0
player2.score += 5
elif p1_move == "D" and p2_move == "C":
player1.score += 5
player2.score += 0
else:
player1.score += 1
player2.score += 1
def run_basic_tournament(players, rounds=100, average=3):
'''Runs basic tournament with round-robin format between every strategy included, not including noise'''
scores = {player.name: 0 for player in players} # Dictionary to store the results of the tournament
results = {}
matchups_played = set() # Set to keep track of which matchups have already occurred
for i, player1 in enumerate(players):
for j,player2 in enumerate(players):
matchup = tuple(sorted([player1.name, player2.name]))
if matchup in matchups_played:
continue # Skip this matchup if it's already played
# Record this matchup as played
matchups_played.add(matchup)
# Play the match
play_match(player1, player2, rounds)
# Store the result (can store both scores if needed)
results[(player1.name, player2.name)] = (player1.score, player2.score)
scores[player1.name] += player1.score
if player1.name != player2.name: # Prevents same player from adding score to itself twice (when played against itself)
# Automatically chooses first player if both the same
scores[player2.name] += player2.score
results = pd.DataFrame(results.items(), columns=['Match', 'Score'])
scores = pd.DataFrame(scores.items(), columns=['Player', 'Score'])
scores = scores.sort_values(by='Score', ascending=False).reset_index(drop=True)
return results, scores
def duel(player1, player2, rounds=100, average=3):
'''To view a full one-on-one match between two strategies for analysis'''
play_match(player1, player2, rounds)
scores = {
player1.name : player1.score,
player2.name : player2.score
}
results = {
'Round' : list(range(1, rounds+1)),
player1.name : player1.history,
player2.name : player2.history
}
results = pd.DataFrame(results).T
return scores, results