Any Python Gurus?

Do all return paths return integers?

For example:

Code:
def fac(n):
	if n == 0:
		return None
	else:
		return n * fac(n - 1)

Wont work, as nothing will happen if you multiply None with n.
 

log in or register to remove this ad

I believe so. Here's my function, if you have time to take a peek.

Code:
def strategy(n,b,m,p,g):
    for i in range(0,3):
        for j in range(0,3):
            m = [i,j]
            if viable_move(b,m) == 1:
                print "Ho!"
                b = modify_box(b,m,p)
                v = check_victory(b)
                if v != 0:
                    return (128/(2**n))
                else:
                    if n < 9:
                        g = g + strategy(n+1,b,m,p,g)
                        b = return_box(b,m)
                    else:
                        return 0

The "HO!" is a debug check.

viable_move makes sure the tic-tac-toe spot is empty.

modify_box and return_box create viable and nonviable moves in the move in question (so the function can recurse properly).
 


Ah. The code doesnt return anything here:

Code:
                    if n < 9:
                        g = g + strategy(n+1,b,m,p,g)
                        b = return_box(b,m)

So if n < 9 two or more times in a row nothing can be added to g
 


(I'm trying to create basic brute force A.I. for tic-tac-toe)

I'm having a problem with this code producing the right countermove.

Code:
def create_strat(b,t,n):
    g_perm = 0
    g_temp = 0
    k = 0
    q = 0
    for i in r:
        for j in r:
            if b[i][j] == 0:
                #you get an available move
                b[i][j] = t
                if check_victory(b) != 0:
                    g_temp = g_temp + (256/(2**n))
                elif check_victory(b) == 0 and n < 9:
                    g_temp = create_strat(b,-t,n+1)[0]
                b[i][j] = 0
            if g_temp > g_perm:
                g_perm = g_temp
                k = i
                q = j
                print n
            g_temp = 0
    return [g_perm,k,q]

b is the game face (3x3 list)

t is player turn (1 is player 1, -1 is player 2)

All other functions have been tested to operate correctly.

For a situation where the input is ([[1,1,0],[0,0,0],[0,0,0]],-1,0)

the game would look like this:

Code:
X X -
- - -
- - -

however, the program outputs [128,1,0], clearly the wrong move.

if you input the same box, with the player turn on 1 (t = 1), the correct output ([256,0,2]) is given.

if you would like me to post the entire program I can do that.

thanks in advance.
 
Last edited:


Well, since no one seems to have bit, I restructured it:

Code:
def goodness(t):
    if check_victory(box) == -t:
        return -128
    else:
        g = -200
        for i in r:
            for j in r:
                if box[i][j] == 0:
                    b = 1
                    box[i][j] = t
                    a = -goodness(-t)
                    box[i][j] = 0
                    if a > g:
                        g = a
        return g
                    

def create_strategy(t):
    g = -100
    move = [-1,-1]
    for i in r:
        for j in r:
            if box[i][j] == 0:
                box[i][j] = t
                a = -goodness(-t)
                box[i][j] = 0
                if a > g:
                    g = a
                    move = [i,j]
    print g
    return move

Here is the current function.
 

Post the entire program.

Also, in the future, avoid single-letter variables except for use with iterators (1,2,3). Just name the variable according to what it stores, and debugging your code gets a world easier.
 

Remove ads

Top