“为什么我的定义不更改我的字典?”

时间:2019-10-19 16:56:56

标签: python python-3.x

我正在尝试为2个玩家制作TictacToe游戏。当有人在左下角标记它时会崩溃。而且我不知道为什么

我检查了几次代码,但是找不到问题。

再次播放def play之后,收益仍然不变,而且低左位置始终获胜。

import sys

theBoard = {'T-L': ' ', 'T-M': ' ', 'T-R': ' ',
            'M-L': ' ', 'M-M': ' ', 'M-R': ' ',
            'L-L': ' ', 'L-M': ' ', 'L-R': ' '}

def printBoard(board):
    print('-------')
    print('|' + board['T-L'] + '|' + board['T-M'] + '|' + board['T-R'] + '|')
    print('|' + '-+-+-' + '|')
    print('|' + board['M-L'] + '|' + board['M-M'] + '|' + board['M-R'] + '|')
    print('|' + '-+-+-' + '|')
    print('|' + board['L-L'] + '|' + board['L-M'] + '|' + board['L-R'] + '|')
    print('-------')

def playAgain() :
    print('Do you want to play again?')
    again = 0
    while True:
        print('Press: Y for rematch ; E for exit')
        again = input()
        if again == 'Y':
            turn = 'X'
            theBoard = {'T-L': ' ', 'T-M': ' ', 'T-R': ' ',
            'M-L': ' ', 'M-M': ' ', 'M-R': ' ',
            'L-L': ' ', 'L-M': ' ', 'L-R': ' '}
            break
        if again == 'y':
            turn = 'X'
            theBoard = {'T-L': ' ', 'T-M': ' ', 'T-R': ' ',
            'M-L': ' ', 'M-M': ' ', 'M-R': ' ',
            'L-L': ' ', 'L-M': ' ', 'L-R': ' '}
            break
        elif again == 'E':
            sys.exit()
        elif again == 'e':
            sys.exit()
        else:
            continue


turn = 'X'

while True:
    printBoard(theBoard)
    print('Turn for ' + turn + '. Where do you want to place your ' + turn + '?')
    move=input()
    while True:
        if ' ' in theBoard[move]:
            theBoard[move] = turn
            break
        else:
            print('This spot is taken! try something else')
            print('Where do you want to place your ' + turn + '?')
            move=input()
            continue

    if turn in (theBoard['T-L'] and theBoard['T-M'] and theBoard['T-R']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()

    if turn in (theBoard['M-L'] and theBoard['M-M'] and theBoard['M-R']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()


    if turn in (theBoard['L-L'] and theBoard['L-M'] and theBoard['L-R']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()


    if turn in (theBoard['T-L'] and theBoard['M-M'] and theBoard['L-R']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()


    if turn in (theBoard['L-L'] and theBoard['M-M'] and theBoard['T-R']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()


    if turn in (theBoard['T-L'] and theBoard['M-L'] and theBoard['L-L']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()


    if turn in (theBoard['T-M'] and theBoard['M-M'] and theBoard['L-M']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()


    if turn in (theBoard['T-R'] and theBoard['M-R'] and theBoard['L-R']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()



    if turn == 'X':
        turn = '0'
    else:
        turn = 'X'

1 个答案:

答案 0 :(得分:0)

要测试某个值是否等于其他几个值,应使用比较链接代替and运算符,后者仅以布尔值的形式对这些值执行AND操作。

更改:

if turn in (theBoard['T-L'] and theBoard['M-L'] and theBoard['L-L']):

收件人:

if turn == theBoard['T-L'] == theBoard['M-L'] == theBoard['L-L']:

并对所有其他if条件执行相同操作。