Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys im working on a example of a 4conector game i saw on a quick search. I want to insert the depth of the search tree (SEARCH_DEPTH), but im having some dificulties.This is a part of the code:

BOARD_SIZE_X = 7
BOARD_SIZE_Y = 6
SEARCH_DEPTH = 6

def playGame():
    gameState = [[0 for col in range(BOARD_SIZE_X)] for row in range(BOARD_SIZE_Y)]
    moveHeights = [0] * BOARD_SIZE_X
    player = COMPUTER_PLAYER
    opponent = HUMAN_PLAYER
    winner = 0
    gameOver = False
    remainingColumns = BOARD_SIZE_X
    print ("=========================")
    print ("= WELCOME TO CONNECT 4! =")
    print ("=========================\n")
    printBoard(gameState)
    SEARCH_DEPTH = int(input("What is your deep? "))
    print(SEARCH_DEPTH)


    while True:
        while True:
            try:
                move = int(input("What is your move? (Choose from 1 to %d)" % BOARD_SIZE_X))
            except ValueError:
                print ("That wasn't a number! Try again.")
                continue
            if move < 1 or move > BOARD_SIZE_X:
                print ("That is not a valid move. Try again.")
            elif moveHeights[move - 1] == BOARD_SIZE_Y:
                print ("The chosen column is already full. Try again.")
            else:
                break

        moveHeights[move - 1] += 1
        gameState[BOARD_SIZE_Y - moveHeights[move - 1]][move - 1] = opponent
        printBoard(gameState)

        if moveHeights[move - 1] == BOARD_SIZE_Y:
            remainingColumns -= 1
        if remainingColumns == 0:
            gameOver = True
        if gameOver:
            break

        score = checkWin(gameState)
        if score == player:
            winner = player
            break
        elif score == opponent:
            winner = opponent
            break
        else:
            score = 0

        print ("Now it's the computer's turn!")
        move = bestMove(gameState, player, opponent)
        if move == None:
            break

        moveHeights[move] += 1
        gameState[BOARD_SIZE_Y - moveHeights[move]][move] = player
        printBoard(gameState)

        if moveHeights[move] == BOARD_SIZE_Y:
            remainingColumns -= 1
        if remainingColumns == 0:
            gameOver = True
        if gameOver:
            break

        score = checkWin(gameState)
        if score == player:
            winner = player
            break
        elif score == opponent:
            winner = opponent
            break
        else:
            score = 0

    return winner


The line thats giving me trouble is this:

SEARCH_DEPTH = int(input("What is your deep? "))
    print(SEARCH_DEPTH)


What I have tried:

I have already try diferent ways but still with no result. When i run the game it basicaly ignores what i have inserted. I tryed to do a local variable but still i dont get the pretended result.

SEARCH_DEPTH = INT

SEARCH_DEPTH = input("whats is your depth"?)
Posted
Updated 15-Dec-21 4:25am
v2

1 solution

You need to tell the playGame function that SEARCH_DEPTH is a global variable. Otherwise when it gets to the line
Python
SEARCH_DEPTH = int(input("What is your deep? "))

it creates a new local variable with that name, and ignores the one that is declared outside the function. Python's rules are that any variable reference search always starts at the innermost level.

Just add the global command thus:
Python
def playGame():
    global SEARCH_DEPTH
 
Share this answer
 
Comments
Richard MacCutchan 15-Dec-21 10:30am    
Which line causes the TypeError?
Diogo Rocha 2021 15-Dec-21 10:30am    
Its working thanks men.I apreciate your help

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