[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 = " "

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
        computer = O
        computer = X
        human = O
    return computer, human

def newboard():
    board = []
    box = 9
    for square in range(box):
        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):
    for square in range(box):
        if board[square] == blank:
        return moves

def winner(board):
    for row in movestowin:
        if board[row[0]] == board[row[1]] ==board[row[2]] !=blank:
            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
        return X

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

def main():
    computer, human = pieces()
    turn = X
    board = newboard()
    while not winner(board):
        if turn == human:
            move = humanmove(board, human)
            board[move] = human
            move = computermove(board, computer, human)
            board[move] = computer
        turn = whoseturn(turn)

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

More information about the Tutor mailing list