Python Code for β-Zero


from itertools import combinations
import copy

#This is for a game where Beta wans the final element to be 0 at the end for even n.
#Each level will be a list of [[tuples where Beta wins],[tuples where Alpha wins]].  A tuple will look like (game state, [remaining elements to play]), where the game state is the value of e_n.

#This if for a game on Z_n for even n.    We will work backwards from e_(i+1) to e_i, where previousLevel is the lists of tuples for Beta and Alpha.
def getLevel(n,i,previousLevel):
            
        #The current player who is going to define e_(previous) from e_(current)
        currentPlayer=(n-i+1)%2
        otherPlayer=(currentPlayer+1)%2
        currentPlayerWinsPrevious=previousLevel[currentPlayer]
        otherPlayerWinsPrevious=previousLevel[otherPlayer]
        currentPlayerWinsCurrent=[]         
        otherPlayerWinsCurrent=[]           
        returnList=[0]*2            
            
        #We are now going to loop over possible current states of the game to determine whether they are a win for the current player.  
        remainingElementsCombinations=combinations(range(0,n),n-i)
        for remainingElements in remainingElementsCombinations:
                for currentState in range(0,n):
                        currentTuple=(currentState,list(remainingElements))
                        for r in remainingElements:
                                tempRemainingElements=copy.copy(list(remainingElements))
                                tempRemainingElements.remove(r)
                                if ((currentState+r)%n,tempRemainingElements) in currentPlayerWinsPrevious or ((currentState*r)%n,tempRemainingElements) in currentPlayerWinsPrevious:
                                        currentPlayerWinsCurrent.append(currentTuple)
                                        break
                        if not currentTuple in currentPlayerWinsCurrent:
                                otherPlayerWinsCurrent.append(currentTuple)

        returnList[currentPlayer]=currentPlayerWinsCurrent    
        returnList[otherPlayer]=otherPlayerWinsCurrent  
        return returnList

#This outputs a list [[values of e_0 where Beta wins],[values of e_0 where Alpha wins]].
def determineWinner(n):
        currentLevel=[[(0,[])]]+[[(i,[]) for i in range(1,n)]]
        for i in range(n-1,-1,-1):
                currentLevel=getLevel(n,i,currentLevel)
        betaWins=[x[0] for x in currentLevel[0]]
        alphaWins=[x[0] for x in currentLevel[1]]
        return [set(betaWins),set(alphaWins)]