# [Tutor] HELP!!!!!

Michael Kim kimgaang at aol.com
Thu Apr 17 09:40:11 CEST 2008

```Hi I am having a really hard time making my tictactoe program work.  I
was wondering if you could could check it out and help me with the
error.  Thanks

blank = " "

response = None
while response not in range(low, high):
response = int(raw_input(question))
return response

response = None
while response not in ("y", "n"):
response = raw_input (question).lower()
return response

def pieces():
whosefirst=askquestion("Do you want to go first? (y/n): ")
if whosefirst == "y":
human = X
X="X"
computer = O
O="O"
else:
computer = X
human = O
return computer, human

def newboard():
board = []
box = 9
for square in range(box):
blank = " "
board.append(blank)
return board

def createboard(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"

def allowedmove(board):
moves=[]
for square in range(box):
if board[square] == blank:
moves.append(square)
return moves

def winner(board):

movestowin=((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 movestowin:
if board[row[0]] == board[row[1]] ==board[row[2]] !=blank:
winner=board[row[0]]
return winner
if blank not in board:
tie = "tie"
return tie
return None

def humanmove (board, human):
allowed = allowedmove(board)
move = None
while move not in allowed:
if move not in allowed:
print "\nPick again.\n"
return move

def computermove (board, computer, human):
board = board[:]
winnermoves = (4, 0, 2, 6, 8, 1, 3, 5, 7)
for move in allowedmoves(board):
board[move] = computer
if winner (board) == computer:
print move
return move
board[move] = blank
for move in allowedmoves(board):
board[move] = human
if winner(board) == human:
print move
return move
board[move] = blank
for move in winnermoves:
if move in allwedmoves(board):
print move
return move

def whoseturn(turn):
if turn == X:
return O
else:
return X

def whowon(iwon, computer, human):
if iwon !=tie:
print iwon, "you win!"
else:
print "a tie"

def main():
computer, human = pieces()
turn = X
board = newboard()
createboard(board)

while not winner(board):
if turn == human:
move = humanmove(board, human)
board[move] = human
else:
move = computermove(board, computer, human)
board[move] = computer
createboard(board)
turn = whoseturn(turn)

iwon =  winner(board)
whowon(iwon, computer, human)

main()

```