I'm trying to design a program that create random tic-tac-toe players who all move randomly depending on what the other player does. To do this I want to store all the possible board outcomes and move the player would make depending on each senario, but I don't believe my tree is implemented or storing the data correctly.
Here is my code:
import random
import time
class player:
def __init__(self):
self.firstMoveGen = None
self.secondMoveGen = []
self.numOfWins = 0
self.genome = []
def createNewGenome(self):
self.firstMoveGen = createGene([], None, self)
print("\n")
for i in range(9):
self.secondMoveGen.append(createGene([i], i, self))
print("\n")
print("---------END GENOME----------\n\n")
class gene:
def __init__(self):
self.move = None
self.lastPlayed = None
self.children = []
def addChild(self, child):
self.children.append(child)
def printNumChildren(self):
print(" ch:" + str(len(self.children)) + " ", end = "")
allMoves = [0,1,2,3,4,5,6,7,8]
def createGene(takenMoves, lastPlayed, player):
if len(takenMoves) != 9:
newGene = gene()
newGene.lastPlayed = lastPlayed
possibleMoves = list(set(allMoves).symmetric_difference(set(takenMoves)))
picker = random.randrange(0, len(possibleMoves))
newGene.move = possibleMoves[picker]
print(newGene.move, end="")
player.genome.append(newGene.move)
possibleMoves.pop(picker)
savedMoves = list(possibleMoves)
for enemyMove in possibleMoves:
possibleMoves.remove(enemyMove)
newGene.addChild(createGene(list(set(allMoves).symmetric_difference(set(possibleMoves))), enemyMove, player))
possibleMoves = list(savedMoves)
return newGene
What I have tried:
Perhaps I have calculated incorrectly, but I believe that in the end, each "player's genome" should contain 652 moves. However, when I run my program they contain 964.