-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbsquares.py
116 lines (96 loc) · 4.06 KB
/
sbsquares.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Generate a Super Bowl Squares 10x10 grid for an arbitrary number of players,
# filling leftover squares with 'rollover'. Save results as a CSV.
from itertools import product
from random import shuffle
from csv import writer
def generate_squares(players, team1, team2):
# generate (row, col) coordinates for each square of 10x10 grid
# i.e. (0,0),(0,1)...(0,9),(1,0),(1,1)...(1,9),(2,0),(2,1)...(9,8),(9,9)
squares = list(product(range(10), range(10)))
shuffle(squares) # shuffle coordinates
shuffle(players) # shuffle players
# report on whether numbers divide evenly
sq_per = len(squares) // len(players) # squares per player
extras = len(squares) % len(players) # leftover squares
if extras > 0:
print(f"{len(players)} doesn't divide {len(squares)} evenly, {extras} leftover squares.")
else:
print(f"{len(players)} divides {len(squares)} evenly, {sq_per} squares per player.")
# assign each player sq_per squares
sq_dict = {} # key: square, value: player. Will simplify writing to csv.
for idx, p in enumerate(players):
start = idx * sq_per
end = start + sq_per
player_squares = squares[start:end]
for psq in player_squares:
sq_dict[psq] = p
# print(sq_dict)
# assign remaining squares to rollover
rollover_squares = squares[end:]
if len(rollover_squares) > 0:
for rsq in rollover_squares:
sq_dict[rsq] = 'rollover'
print(f"Rollover squares: {rollover_squares}")
# confirm total number and uniqueness of squares
assert len(sq_dict) == 100
assert len(sq_dict.keys()) == len(set(sq_dict.keys()))
# confirm that each player received sq_per unique squares
for player in players:
assert len([p for p in sq_dict.values() if p == player]) == sq_per
# confirm player squares + rollover squares == 100
assert len([p for p in sq_dict.values() if p == 'rollover']) + len(players) * sq_per == 100
# for v in sorted(sq_dict.keys()):
# print(f"{v}: {sq_dict[v]}")
sorted_squares = sorted(sq_dict.keys())
rows = []
for ridx in range(10):
new_row = [sq_dict[ssq] for ssq in sorted_squares[ridx*10:ridx*10+10]]
assert len(new_row) == 10
rows += [new_row]
print(new_row)
# write_csv(rows, "sbsquares2021.csv")
# randomly assign teams to columns (x coordinates) or rows (y coordinates)
teams = [team1, team2]
shuffle(teams)
col_team = teams[0]
row_team = teams[1]
print(f"Columns: {col_team}. Rows: {row_team}")
write_html(rows, row_team, col_team, "sbsquares.html")
def write_csv(rows, filename):
with open(filename, 'w') as csvfile:
w = writer(csvfile)
for r in rows:
w.writerow(r)
def generate_html(rows, team1, team2):
html = f"""<html><head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="sbsquares.js"></script>
<link rel="stylesheet" href="sbsquares.css">
</head>
<body>
<div style="display:inline-block;">
<div style="display:flex;flex-direction:row;">
<div class="team2">{team2}</div>
<div style="display:flex;flex-direction:column;">
<div class="team1">{team1}</div>
<div class="sbtable">
<table>
"""
# score digit header row
html += "<tr><td class='corner'></td>"
for x in range(10):
html += f"<td class='digit'>{x}</td>"
html += "</tr>\n"
for idx, row in enumerate(rows):
html += f" <tr><td class='digit'>{idx}</td>\n"
for player in row:
html += f" <td class='player'>{player}</td>\n"
html += " </tr>\n"
html += "</table></div></div></div></div></body></html>"
return html
def write_html(rows, team1, team2, filename):
with open(filename, 'w') as htmlfile:
htmlfile.write(generate_html(rows, team1, team2))
if __name__ == "__main__":
players = [f"P{n+1}" for n in range(24)] # replace with list of player names
generate_squares(players, "Chiefs", "Buccaneers")