Python help please

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Python help please

Post by The Lieutenant on Mon Jan 19, 2009 7:46 pm
([msg=16620]see Python help please[/msg])

I am making a command line tictactoe game in python, and I keep getting this error but I don't understand why

Traceback (most recent call last):
File "./game.py", line 117, in <module>
main()
File "./game.py", line 105, in main
while not winner(board):
NameError: global name 'winner' is not defined

Here is the code
Code: Select all
#!/usr/bin/python
###############################

Square_Num = 9
X = "X"
O = "O"
blank = " "
human = X
comp = O
Tie = "Tie"
#create board
def new_board():
   board = []
   for square in range(Square_Num):
       board.append(blank)
   return board
      
def display_board(board):
   print "\n\t", board[0], "|", board[1], "|", board[2]
   print "\t", "---------"
   print "\t", board[3], "|", board[4], "|", board[5]
   print "\t", "---------"
   print "\t", board[6], "|", board[7], "|", board[8], "\n"
#declare moves
def legal_moves(board):
   moves = []
   for square in range(Square_Num):
      if board[square] == blank:
         moves.append(square)
   return moves
#declare board number that can win
def moves():
   WIN_MOVES = ((0, 1, 2),
                     (3, 4, 5),
           (6, 7, 8),
           (0, 3, 6),
           (1, 4, 7),
           (2, 5, 8),
           (0, 4, 8),
           (2, 4, 6))

   
   for row in WIN_MOVES:
      if board[row[0]] == board[row[1]] == board[row[2]] != blank:
         winner = board[row[0]]
         return winner
      
      if blank not in board:
         return Tie
      return None

def  human_move(board, human):
   legal = legal_move(board)
   move = None
   while move not in legal:
      move = ask_move("Enter move number "), 0, Square_Num
      if move not in legal:
         print "\nPlease choose again\n"
   return move
   
def comp_move(board, comp, human):
    board = board[:]
    GOOD_MOVES = (4, 0, 8, 1, 6, 2, 3, 5, 7)
   
    for move in legal_moves(board):
        board[move] = human
        if winner(board) == human:
            print move
            return move
        board[move] = EMPTY
       
    # since no one can win on next move, pick best open square
    for move in GOOD_MOVES:
        if move in legal_moves(board):
            print move
            return move
           
def next_turn(turn):
    if turn == X:
        return 0
    else:
        return X
       
def congrat_winner(the_winner, comp, human):
    if the_winner != TIE:
        print the_winner, "won!\n"
    else:
        print "It's a tie!\n"
    if the_winner == computer:
        print "Player looses \n"  \
           
    elif the_winner == human:           board[move] = human
        else:
            move = comp_move(board, comp, human)
            board[move] = comp
        display_board(board)
        turn = next_turn(turn)
        the_winner = winner(board)
        congrat_winner(the_winner, computer, human)
        print "Player wins \n" \
           
    elif the_winner == TIE:
        print "The game is a tie.  \n"  \
           
def main():
    turn = X
    board = new_board()
    display_board(board)
   
    while not winner(board):
        if turn == human:
            move = human_move(bo           board[move] = human
        else:
            move = comp_move(board, comp, human)
            board[move] = comp
        display_board(board)
        turn = next_turn(turn)
        the_winner = winner(board)
        congrat_winner(the_winner, computer, human)ard, human)
            board[move] = human
        else:
            move = comp_move(board, comp, human)
            board[move] = comp
        display_board(board)
        turn = next_turn(turn)
        the_winner = winner(board)
        congrat_winner(the_winner, computer, human)
       
main()

Any help would be appreciated
User avatar
The Lieutenant
New User
New User
 
Posts: 30
Joined: Tue May 27, 2008 7:06 pm
Blog: View Blog (0)


Re: Python help please

Post by tgoe on Wed Jan 21, 2009 3:14 pm
([msg=16743]see Re: Python help please[/msg])

There is no winner() function and you never call moves() ;)
User avatar
tgoe
Contributor
Contributor
 
Posts: 639
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests