ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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()
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.