-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
88 lines (74 loc) · 3.33 KB
/
generator.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
import sys
with open('template') as file:
template = file.read()
total_count = 0
total_win_x = 0
total_win_y = 0
class Stroke:
def __init__ (self, url = ''):
# Global variables
global total_count
global total_win_x
global total_win_y
global template
# Construct
self.table = [' '] * 9
self.links = ['false'] * 9
total_count += 1
# Analysis self url & Make Table
for index in range (0, len(url), 2):
self.table[int(url[index])] = url[index + 1]
# State detect
if self.is_winner():
self.winner = True
self.player = url[-1]
self.layout = 'winner_' + self.player
else:
self.winner = False
self.player = 'o' if len(url) != 0 and url[-1] == 'x' else 'x'
self.layout = 'player_' + self.player
# Make others url's
for index in range(9):
if self.table[index] == ' ' and not self.winner:
self.links[index] = ''.join([(str(i) + self.table[i]) for i in range(9) if self.table[i] != ' '])
self.links[index] += str(index) + self.player
# Block new stroke
if self.winner:
self.links = ['false'] * 9
total_win_x += int(self.player == 'x')
total_win_y += int(self.player == 'o')
# Make webpage file
url = 'index' if url == '' else url
sys.stderr.write(f"[{total_count}] writing file {url}.html\n")
with open(f"./multiplayer/{url}.html", "w") as file:
html = template.format(layout = self.layout, links = self.links, table = self.table)
html = html.replace('href="false"', '')
html = html.replace('href="', 'href="{{ \'multiplayer/')
html = html.replace('">&', '.html\' | relative_url }}">&')
html = html.replace('">x', '.html\' | relative_url }}">x')
html = html.replace('">o', '.html\' | relative_url }}">o')
file.write(html)
# Next
[Stroke(self.links[i]) for i in range(9) if self.links[i] != 'false']
def is_winner(self):
winner = False
# Horizontal detect
winner |= self.table[0] == self.table[1] and self.table[1] == self.table[2] and self.table[2] != ' '
winner |= self.table[3] == self.table[4] and self.table[4] == self.table[5] and self.table[5] != ' '
winner |= self.table[6] == self.table[7] and self.table[7] == self.table[8] and self.table[8] != ' '
# Diagonal detect
winner |= self.table[0] == self.table[4] and self.table[4] == self.table[8] and self.table[8] != ' '
winner |= self.table[2] == self.table[4] and self.table[4] == self.table[6] and self.table[6] != ' '
# Verical detect
winner |= self.table[0] == self.table[3] and self.table[3] == self.table[6] and self.table[6] != ' '
winner |= self.table[1] == self.table[4] and self.table[4] == self.table[7] and self.table[7] != ' '
winner |= self.table[2] == self.table[5] and self.table[5] == self.table[8] and self.table[8] != ' '
return winner
# bootstrap
Stroke()
# logs
sys.stderr.write(f"""[!] Done.
> Files: {total_count}
> Player X Winners: {total_win_x}
> Player Y Winners: {total_win_y}
""")