Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:


Python
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:
            # make ne gene
            newGene = gene()
            # input the last enemy move to be made
            newGene.lastPlayed = lastPlayed
            # find all moves left
            possibleMoves = list(set(allMoves).symmetric_difference(set(takenMoves)))
            # pick a random move from the ones left
            picker = random.randrange(0, len(possibleMoves))
            newGene.move = possibleMoves[picker]
            print(newGene.move, end="")
            player.genome.append(newGene.move)
            # take currently picked move out of possiblilities
            possibleMoves.pop(picker)
            
            savedMoves = list(possibleMoves)
            for enemyMove in possibleMoves:
                # delete the move the next player takes
                possibleMoves.remove(enemyMove)
                # pass on to the next gene all the taken moves
                newGene.addChild(createGene(list(set(allMoves).symmetric_difference(set(possibleMoves))), enemyMove, player))
                # add the move the next player takes back
                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.
Posted
Updated 24-Nov-16 17:52pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900