Michael Kim wrote:
> 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
>
As Kent pointed out, we need to see the error you are getting in order
to help. That usually shows up as a traceback. When I run your program I
get:
File "j:/python/tictactoe.py", line 53
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))
^
IndentationError: expected an indented block
There are numerous other problems in your code. As you fix one error the
next will be revealed. For example when I fix the indentation error I
next get:
File "J:\python\tictactoe.py", line 124, in <module>
main()
File "J:\python\tictactoe.py", line 105, in main
computer, human = pieces()
File "J:\python\tictactoe.py", line 20, in pieces
human = X
UnboundLocalError: local variable 'X' referenced before assignment
Do you understand that variables must be assigned before reference?
Python does not know what X is.
So please report the specific error. Also try to determine what it means
before reporting it.
>
> blank = " "
>
>
>
> def asknumber(question, low, high):
> response = None
> while response not in range(low, high):
> response = int(raw_input(question))
> return response
>
> def askquestion(question):
> 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 is undefined (here and below)
> X="X"
> computer = O
>
O is undefined (here and below)
> 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:
> move = asknumber("It is your turn. Pick 0-8:", 0, box)
> 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()
>
>
>
> _______________________________________________
> Tutor maillist - [email protected]
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
Bob Gailer
919-636-4239 Chapel Hill, NC
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor