LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-20-2009, 01:53 PM   #1
UniXFedora
LQ Newbie
 
Registered: Oct 2008
Distribution: Slackware 13
Posts: 9

Rep: Reputation: 0
Python help please


I am trying to create a tictactoe game in Python that runs in the command line. I keep getting this error
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

I have tried to figure it out but I can't get it. Can someone please help me?

Code:
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:
        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(board, 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()
 
Old 01-20-2009, 08:53 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
The error message seems pretty clear, you are calling a function called winner, but there is no definition for this function.

Code:
 while not winner(board):
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python update - Unable to load GTK2 Python bindings: No module named gtk itpedersen Linux - Software 2 10-03-2008 03:44 AM
LXer: Charming Python: Python elegance and warts, Part 2 LXer Syndicated Linux News 0 08-31-2007 08:40 AM
python, os.system() function. howto use python variables? jhwilliams Programming 5 07-28-2007 01:56 AM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 07:54 PM
python problem - compiled from source - python -V still showing old version txm123 Linux - Newbie 1 02-15-2006 11:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration