patternpythonMinor
Tic Tac Toe Game Sequence Generator
Viewed 0 times
toeticsequencetacgeneratorgame
Problem
I want my code to write to a .csv file a bunch of patterns for Tic Tac Toe games (30000). I want to have 9 columns numbered 1 through 9 and in each have the number of the space in the grid that was played during that turn. Next a column with the entire sequence, then the moves the first player made, the moves the second player made and then the winner. My code is incredibly inefficient though, has been running for hours (I also need the second player to be somewhat intelligent but I can't use data from previous games to achieve this).
```
import csv
import random
class TotitoGeneratorManager:
def __init__(self, path, games):
self.path = path
self.games = games
self.winner = None
self.player1 = []
self.player2 = []
self.gamesequence = []
self.turns = 0
self.gameover = False
def game(self):
while self.games > 0:
while not self.gameover:
while self.turns > 4:
player1play()
player2play()
if self.turns > 4:
winner()
if self.gameover:
write_game()
self.games -= 1
else:
player1play()
player2play()
def player1play(self):
play = random.randint(0,9)
played = False
while not played:
if play not in self.gamesequence:
self.player1.append(play)
self.gamesequence.append(play)
played = True
self.turns +=1
else:
play = random.randint(0,9)
def player2play(self):
played = False
while not played:
if 4 not in self.gamesequence:
self.player2.append(4)
self.gamesequence.append(4)
played = True
self.turns += 1
elif 0 not in self.gamese
```
import csv
import random
class TotitoGeneratorManager:
def __init__(self, path, games):
self.path = path
self.games = games
self.winner = None
self.player1 = []
self.player2 = []
self.gamesequence = []
self.turns = 0
self.gameover = False
def game(self):
while self.games > 0:
while not self.gameover:
while self.turns > 4:
player1play()
player2play()
if self.turns > 4:
winner()
if self.gameover:
write_game()
self.games -= 1
else:
player1play()
player2play()
def player1play(self):
play = random.randint(0,9)
played = False
while not played:
if play not in self.gamesequence:
self.player1.append(play)
self.gamesequence.append(play)
played = True
self.turns +=1
else:
play = random.randint(0,9)
def player2play(self):
played = False
while not played:
if 4 not in self.gamesequence:
self.player2.append(4)
self.gamesequence.append(4)
played = True
self.turns += 1
elif 0 not in self.gamese
Solution
Duplicated code blocks
There is an awful lot of repetition here. For example this kind of code:
The branches of the
so this could be a single
Then, the two sets of conditions are also very similar,
first checking if
or if
A shorter way of writing the exact same thing:
Review the rest of the code, and try to eliminate duplicated code in a similar fashion.
The Pythonic way to write this:
Use
When working with file handles,
it's recommended to use
for example given this code:
This is cleaner:
This way you don't need to worry about closing
Python will do it for you automatically.
There is an awful lot of repetition here. For example this kind of code:
if self.player1 == [1,2] or self.player1 == [2,1] or self.player1 == [3,6] or self.player1 == [6,3] or self.player1 == [4,8] or self.player1 == [8,4]:
self.player2.append(0)
self.gamesequence.append(0)
played = True
self.turns +=1
elif self.player2 == [1,2] or self.player2 == [2,1] or self.player2 == [3,6] or self.player2 == [6,3] or self.player2 == [4,8] or self.player2 == [8,4]:
self.player2.append(0)
self.gamesequence.append(0)
played = True
self.turns +=1The branches of the
if-else have the same code,so this could be a single
if without elif, and without the duplication.Then, the two sets of conditions are also very similar,
first checking if
player1 is one of the combinations,or if
player2 is one of the combinations.A shorter way of writing the exact same thing:
positions = [[1,2], [2,1], [3,6], [6,3], [4,8], [8,4]]
if self.player1 in positions or self.player2 in positions:
self.player2.append(0)
self.gamesequence.append(0)
played = True
self.turns +=1Review the rest of the code, and try to eliminate duplicated code in a similar fashion.
while x == FalseThe Pythonic way to write this:
while not x:Use
with to auto-close file handlesWhen working with file handles,
it's recommended to use
with,for example given this code:
write_file = open(self.path, 'wb')
header = [[1, 2, 3, 4, 5, 6, 7, 8, 9, "Secuencia", "Tiros A", "Tiros B", "Ganador"]]
for row in header:
for column in row:
write_file.write('%s,' % column)
write_file.write('\n')
write_file.close()This is cleaner:
with open(self.path, 'wb') as write_file:
header = [[1, 2, 3, 4, 5, 6, 7, 8, 9, "Secuencia", "Tiros A", "Tiros B", "Ganador"]]
for row in header:
for column in row:
write_file.write('%s,' % column)
write_file.write('\n')This way you don't need to worry about closing
write_file, Python will do it for you automatically.
Code Snippets
if self.player1 == [1,2] or self.player1 == [2,1] or self.player1 == [3,6] or self.player1 == [6,3] or self.player1 == [4,8] or self.player1 == [8,4]:
self.player2.append(0)
self.gamesequence.append(0)
played = True
self.turns +=1
elif self.player2 == [1,2] or self.player2 == [2,1] or self.player2 == [3,6] or self.player2 == [6,3] or self.player2 == [4,8] or self.player2 == [8,4]:
self.player2.append(0)
self.gamesequence.append(0)
played = True
self.turns +=1positions = [[1,2], [2,1], [3,6], [6,3], [4,8], [8,4]]
if self.player1 in positions or self.player2 in positions:
self.player2.append(0)
self.gamesequence.append(0)
played = True
self.turns +=1while not x:write_file = open(self.path, 'wb')
header = [[1, 2, 3, 4, 5, 6, 7, 8, 9, "Secuencia", "Tiros A", "Tiros B", "Ganador"]]
for row in header:
for column in row:
write_file.write('%s,' % column)
write_file.write('\n')
write_file.close()with open(self.path, 'wb') as write_file:
header = [[1, 2, 3, 4, 5, 6, 7, 8, 9, "Secuencia", "Tiros A", "Tiros B", "Ganador"]]
for row in header:
for column in row:
write_file.write('%s,' % column)
write_file.write('\n')Context
StackExchange Code Review Q#82004, answer score: 3
Revisions (0)
No revisions yet.