Search code examples
pythonrecursiontreeminimax

How can I programme a minimax algorithm for nim game python?


I tried to programme a minimax algorithm in python. But it is so confusing. I am new to recursion functions. My mind structure has some error at somewhere but I could not solve it. My minimax tree returns with '-100' that must be 100 to achieve true answer. If anything is missing or not clear, please just let me know. Thank you

def startposition():
    return 2, 'max'


def terminalstate(state):
    if state == (0, 'min') or state == (0, 'max'):
        return True
    else:
        return False


def minimax(state):
    if terminalstate(state):
        return utilitystatic(state)
    else:
        if state[1] == 'min':
            value = -250
            for x in successorsgenerator(state):
                value = max(value, minimax(x))
        elif state[1] == 'max':
            value = 250
            for x in successorsgenerator(state):
                value = min(value, minimax(x))
    return value
def utilitystatic(state):
    assert terminalstate(state)
    if state[1] == 'max':
        return -100
    elif state[1] == 'min':
        return 100
    assert False


def successorsgenerator(state):
    successors = []
    state = toggle(state)
    newstate = decrease(state)
    i = 0
    while newstate[0] >= 0 and i < 3:
        successors.append(newstate)
        i += 1
        newstate = decrease(newstate)

    print('successors:', successors)
    return successors


def toggle(state):
    state = list(state)
    state[1] = 'min' if state[1] == 'max' else 'max'
    state = tuple(state)
    return state


def decrease(state):
    state = state[:0] + (state[0] - 1,) + state[1:2]
    return state


stick = startposition()
exit = minimax(stick)
print('last result', exit)

Solution

  • I solved my problem. I needed to change value = min(value, minimax(x)) to value = max(value, minimax(x)) and 250 to -250. Problem solved.