python中的Tic tac toe游戏无法正常工作

时间:2018-03-06 19:43:09

标签: python tic-tac-toe

作为初学者编码器,我正试图在python中制作tic tac toe。问题是,在问“开始新游戏?”之后你的回答无关紧要。游戏开始并提示玩家进行移动。它将适当的位置更改为x,然后计算机移动没有任何问题。但在那之后它会自动变成平局(有时是一场胜利)并且游戏结束了。我做错了什么?

我意识到代码可能非常笨拙 - 但我是初学者;对我很轻松,但现在是:

import random

def pl_mov():
    global a
    global b
    global c
    global d
    global e
    global f
    global g
    global h
    global i 
    while True:
        PL_INP = eval(input('ENTER THE NUMBER OF THE POSITION WHERE YOU WANT TO PUT YOUR CROSS:\n'))
        MOV = ''
        if PL_INP == 1 and a == '1':
            a = 'x'
            break
        if PL_INP == 2 and b == '2':
            b = 'x'
            break
        if PL_INP == 3 and c == '3':
            c = 'x'
            break
        if PL_INP == 4 and d == '4':
            d = 'x'
            break
        if PL_INP == 5 and e == '5':
            e = 'x'
            break
        if PL_INP == 6 and f == '6':
            f = 'x'
            break
        if PL_INP == 7 and g == '7':
            g = 'x'
            break
        if PL_INP == 8 and h == '8':
            h = 'x'
            break
        if PL_INP == 9 and i == '9':
            i = 'x'
            break
        else:
            print('NOT A VALID MOVE!')

def COM_MOV():
    global a
    global b
    global c
    global d
    global e
    global f
    global g
    global h
    global i
    mov = 0

    POSSB = ['']
    if a == '1':
        POSSB.append('a')
    if b == '2':
        POSSB.append('b')
    if c == '3':
        POSSB.append('c')
    if d == '4':
        POSSB.append('d')
    if e == '5':
        POSSB.append('e')
    if f == '6':
        POSSB.append('f')
    if g == '7':
        POSSB.append('g')
    if h == '8':
        POSSB.append('h')
    if i == '9':
        POSSB.append('i')

    if a and b == 'o' and c == '3':
        if mov == 0:
            c = 'o'
            mov += 1
    if a and d == 'o' and g == '7':
        if mov == 0:
            g = 'o'
            mov += 1
    if b and c == 'o' and a == '1':
        if mov == 0:
            a = 'o'
            mov += 1
    if b and e == 'o' and h == '8':
        if mov == 0:
            h = 'o'
            mov += 1
    if c and f == 'o' and i == '9':
        if mov == 0:
            i = 'o'
            mov += 1
    if a and d == 'o' and g == '7':
        if mov == 0:
            g = 'o'
            mov += 1
    if d and e == 'o' and f == '6':
        if mov == 0:
            c = 'o'
            mov += 1
    if d and g == 'o' and a == '1':
        if mov == 0:
            a = 'o'
            mov += 1
    if e and b == 'o' and h == '8':
        if mov == 0:
            h = 'o'
            mov += 1
    if e and f == 'o' and g == '7':
        if mov == 0:
            g = 'o'
            mov += 1
    if e and h == 'o' and b == '2':
        if mov == 0:
            b = 'o'
            mov += 1
    if f and c == 'o' and i == '9':
        if mov == 0:
            i = 'o'
            mov += 1
    if f and i == 'o' and c == '3':
        if mov == 0:
            c = 'o'
            mov += 1
    if g and h == 'o' and i == '9':
        if mov == 0:
            i = 'o'
            mov += 1
    if h and i == 'o' and g == '7':
        if mov == 0:
            g = 'o'
            mov += 1
    if a and b == 'x' and c == '3':
        if mov == 0:
            c = 'o'
            mov += 1
    if a and d == 'x' and g == '7':
        if mov == 0:
            g = 'o'
            mov += 1
    if b and c == 'x' and a == '1':
        if mov == 0:
            a = 'o'
            mov += 1
    if b and e == 'x' and h == '8':
        if mov == 0:
            h = 'o'
            mov += 1
    if c and f == 'x' and i == '9':
        if mov == 0:
            i = 'o'
            mov += 1
    if a and d == 'x' and g == '7':
        if mov == 0:
            g = 'o'
            mov += 1
    if d and e == 'x' and f == '6':
        if mov == 0:
            c = 'o'
            mov += 1
    if d and g == 'x' and a == '1':
        if mov == 0:
            a = 'o'
            mov += 1
    if e and b == 'x' and h == '8':
        if mov == 0:
            h = 'o'
            mov += 1
    if e and f == 'x' and g == '7':
        if mov == 0:
            g = 'o'
            mov += 1
    if e and h == 'x' and b == '2':
        if mov == 0:
            b = 'o'
            mov += 1
    if f and c == 'x' and i == '9':
        if mov == 0:
            i = 'o'
            mov += 1
    if f and i == 'x' and c == '3':
        if mov == 0:
            c = 'o'
            mov += 1
    if g and h == 'x' and i == '9':
        if mov == 0:
            i = 'o'
            mov += 1
    if h and i == 'x' and g == '7':
        if mov == 0:
            g = 'o'
            mov += 1
    else:
        temp = random.choice(POSSB)
        if temp == 'a':
            a = 'o'
        if temp == 'b':
            b = 'o'
        if temp == 'c':
            c = 'o'
        if temp == 'd':
            d = 'o'
        if temp == 'e':
            r = 'o'
        if temp == 'f':
            f = 'o'
        if temp == 'g':
            g = 'o'
        if temp == 'h':
            h = 'o'
        if temp == 'i':
            i = 'o'
while True:
    new_game = input('START NEW GAME?\n')
    if new_game == 'y' or 'yes' or 'Y' or 'YES':
        win = False
        loss_type = 0
        a = '1'
        b = '2'
        c = '3'
        d = '4'
        e = '5'
        f = '6'
        g = '7'
        h = '8'
        i = '9'
        print('WELCOME TO TIC TAC TOE!\n')
        while True:
            print('', a, '|', b, '|', c, '\n', d, '|', e, '|', f, '\n', g, '|', h, '|', i, '\n')

            if a and b and c == 'x':
                win = True
                break
            if d and e and f == 'x':
                win = True
                break
            if g and h and i == 'x':
                win = True
                break
            if a and d and g == 'x':
                win = True
                break
            if b and e and h == 'x':
                win = True
                break
            if c and f and i == 'x':
                win = True
                break
            if a and b and c == 'o':
                break
            if d and e and f == 'o':
                break
            if g and h and i == 'o':
                break
            if a and d and g == 'o':
                break
            if b and e and h == 'o':
                break
            if c and f and i == 'o':
                break
            if a and e and i == 'x':
                win = True
                break
            if a and e and 1 == 'o':
                break
            if c and e and g == 'x':
                win = True
                break
            if c and e and g == 'o':
                break
            if a and b and c and d and e and f and g and h and i == 'x' or 'o':
                loss_type = 1

            pl_mov()
            if a and b and c and d and e and f and g and h and i == 'x' or 'o':
                loss_type = 1
            COM_MOV()

        print('GAME OVER!\n')
        if win == True:
            print('YOU WON! CONGRATS!')
        if win == False:
            if loss_type == 0:
                print('YOU LOST.')
            if loss_type == 1:
                print('DRAW!')
    if new_game == 'n' or 'no' or 'N' or 'NO':
        break
    else:
        print('INVALID ANSWER!')

1 个答案:

答案 0 :(得分:1)

如果a and b == 'o'TrueaTrue,则python中的b == 'o'表达式将评估为True。这可能不是你想要的。 and之前和之后的所有内容都应该是它自己的独立表达。

a and b == 'o'

应该是:

a == 'o' and b == 'o'

类似地:

a and b and c == 'x'

应该是:

a == 'x' and b == 'x' and c == 'x'

等等。