tictactoe python(无限次打印板)

时间:2019-03-01 19:53:09

标签: python-3.x

theBoard = {'top left': ' ', 'top middle': ' ', 'top right': ' ',
            'center left': ' ', 'center middle': ' ', 'center right': ' ',
            'bottom left': ' ', 'bottom middle': ' ', 'bottom right': ' '}
def printBoard(board):
    print(board['top left'] + '|' + board['top middle'] + '|' + board['top right'])
    print('-+-+-')
    print(board['center left'] + '|' + board['center middle'] + '|' + board['center left'])
    print('-+-+-')
    print(board['bottom left'] + '|' + board['bottom middle'] + '|' + board['bottom right'])
    turn = 'X'
    for i in range (9):
        printBoard(theBoard)
        print('Turn for ' + turn + '.Move on which space?')
        move = input()
        theBoard[move] = turn
        if turn == 'X':
            turn = 'O'
        else:
            turn = 'X'
    printBoard(theBoard)

1 个答案:

答案 0 :(得分:0)

我认为您的缩进错了:

theBoard = {'top left': ' ', 'top middle': ' ', 'top right': ' ',
            'center left': ' ', 'center middle': ' ', 'center right': ' ',
            'bottom left': ' ', 'bottom middle': ' ', 'bottom right': ' '}
def printBoard(board):
    print(board['top left'] + '|' + board['top middle'] + '|' + board['top right'])
    print('-+-+-')
    print(board['center left'] + '|' + board['center middle'] + '|' + board['center left'])
    print('-+-+-')
    print(board['bottom left'] + '|' + board['bottom middle'] + '|' + board['bottom right'])

turn = 'X'
for i in range (9):
    printBoard(theBoard)
    print('Turn for ' + turn + '.Move on which space?')
    move = input()
    theBoard[move] = turn
    if turn == 'X':
        turn = 'O'
    else:
        turn = 'X'
printBoard(theBoard)