而比较列表时循环永远不会退出

时间:2017-12-14 20:41:22

标签: python

我正在尝试比较列表,以便它会播放TocTacToe,直到某人获胜,当其中一个列,行或对角线(或者你想要称之为它们)中的1或2时,它会退出即使while循环只应该在列和行不等于1,1,1或2,2,2时运行。

我的代码:

import random
import sys
import os
import time
clear = lambda: os.system('cls')
clear()

pWin = [1, 1, 1]
cWin = [2, 2, 2]

bV = ["1","2","3","4","5","6","7","8","9"]

squares = [0,0,0,0,0,0,0,0,0]

s1 = squares[0]
s2 = squares[1]
s3 = squares[2]
s4 = squares[3]
s5 = squares[4]
s6 = squares[5]
s7 = squares[6]
s8 = squares[7]
s9 = squares[8]

c1 = [s1,s4,s7]
c2 = [s2,s5,s7]
c3 = [s3,s6,s9]
r1 = [s1,s2,s3]
r2 = [s4,s5,s6]
r3 = [s7,s8,s9]
d1 = [s1,s5,s9]
d2 = [s3,s5,s7]

test = 4

board1 = [bV[0], bV[1], bV[2]]
board2 = [bV[3], bV[4], bV[5]]
board3 = [bV[6], bV[7], bV[8]]

while c1 or c2 or c3 or r1 or r2 or r3 or d1 or d2 != pWin or cWin:

    clear()
    skip = 0
    choiceComplete = 0
    nC = 0

    print(c1)
    print(c2)
    print(c3)
    print(r1)
    print(r2)
    print(r3)
    print(d1)
    print(d2)

    print(pWin)
    print(cWin)

    while nC <= 8:
        if squares[nC] == 1:
            bV[nC] = "X"
            nC = nC + 1
        elif squares[nC] == 2:
            bV[nC] = "O"
            nC = nC + 1
        elif squares[nC] == 0:
            nC = nC + 1

    board1 = [bV[0], bV[1], bV[2]]
    board2 = [bV[3], bV[4], bV[5]]
    board3 = [bV[6], bV[7], bV[8]]

    print(board1)
    print(board2)
    print(board3)


    uChoice = int(raw_input("Your turn, choose a spot: "))
    '''
    uChoice = test
    '''

    cN = uChoice - 1

    if squares[cN] == 0:
        squares[cN] = 1
    elif squares[cN] == 1 or 2:
        print("Sorry, that square has already been chosen.")
        time.sleep(2)
        skip = 1

    if skip == 0:
        while choiceComplete == 0:
            cChoice = random.randrange(1,10)
            gN = cChoice - 1
            if squares[gN] == 0:
                squares[gN] = 2
                choiceComplete = 1
    s1 = squares[0]
    s2 = squares[1]
    s3 = squares[2]
    s4 = squares[3]
    s5 = squares[4]
    s6 = squares[5]
    s7 = squares[6]
    s8 = squares[7]
    s9 = squares[8]

    c1 = [s1,s4,s7]
    c2 = [s2,s5,s7]
    c3 = [s3,s6,s9]
    r1 = [s1,s2,s3]
    r2 = [s4,s5,s6]
    r3 = [s7,s8,s9]
    d1 = [s1,s5,s9]
    d2 = [s3,s5,s7]

'''
    test = test + 1
'''
if c1 or c2 or c3 or r1 or r2 or r3 or d1 or d2 == pWin:
    print("You Won!")



if c1 or c2 or c3 or r1 or r2 or r3 or d1 or d2 == pWin:
    print("You lost!")

我正在谈论的部分:

while c1 or c2 or c3 or r1 or r2 or r3 or d1 or d2 != pWin or cWin:

P.S。正如你所说,我对编码非常陌生,我是在2天前开始的,所以对我的代码的任何建设性批评都非常感激。

0 个答案:

没有答案