문제

I'm trying to learn python and have been writing a (very) simple tic-tac-toe program. However, I've got stuck because it won't seem to execute beyond letting the user enter a number. After that, nothing happens. I can't figure out where I have gone wrong. Sure there are tons of errors, but help much appreciated!

Thanks!

import random

board = range(0,9)

def print_board():
    print board[0], "|", board[1], "|", board[2]
    print board[3], "|", board[4], "|", board[5]
    print board[6], "|", board[7], "|", board[8]

def checkAll():
        if board[0] == board[1] == board[2]:
            True
        if board[3] == board[4] == board[5]:
            True
        if board[6] == board[7] == board[8]:
            True
        if board[0] == board[3] == board[6]:
            True
        if board[1] == board[4] == board[7]:
            True
        if board[2] == board[5] == board[8]:
            True
        if board[0] == board[4] == board[8]:
            True
        if board[6] == board[4] == board[2]:
            True

print_board()

while True:
    input = int(raw_input("Choose a number to place your X: "))

    if input <= 8:
        if board[input] != "x" or board[input] != "o": 
            board[input] = "x" # places x if board[input] is neither x or o

            # Check for winner

            if checkAll() == True:
                "The game is over!"
                break;

            finding = True
            while finding:

                random.seed() # gives a random generator
                opponent = random.randrange(0,8) # generates a random integer between 1 and 8

                if board[opponent] != "x" or board[opponent] != "o":
                    board[opponent] = "o"

                    # Check for winner

                    if checkAll() == True:
                        "The game is over!"
                        break;

        else:
            print "This spot is taken."
        print_board()

    else: "Please choose a number between O and 8."
도움이 되었습니까?

해결책

Well there're many things which need improvement in your code (you have to rewrite your CheckAll function at least so it could check board of any size), but two things will help you to debug - first, you have to actually return something from your CheckAll function:

...
if board[0] == board[1] == board[2]:
    return True
if board[3] == board[4] == board[5]:
    return True
...

second, you may actually want to print output:

if checkAll() == True:
   print "The game is over!"
   break;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top