Игра Tic Tac Toe, созданная в условиях Win Python - PullRequest
1 голос
/ 30 сентября 2019

Это мой первый пост здесь, так как обычно я просто следую инструкциям на Youtube или чем-то еще, но я действительно хотел сделать проект в основном сам. Я сделал небольшую игру Tic Tac Toe, которой я очень гордился сегодня в Python, но есть одна вещь, которая не работает. Условия выигрыша. Я помещал в winLoseCondition () в разных местах раньше, но это не работает. Как только он попадает туда, он либо говорит, что проиграл, либо выиграл. Я не знаю, как он решит, пока не будет достаточно информации. Еще одна вещь, иногда компьютер перезаписывает одно из мест, куда ушел плеер, и я не знаю почему. Я знаю, что, вероятно, сделал 100 вещей неправильно, программируя это, и есть более простой способ сделать это, но есть много подобных, но этот мой. Итак, вопрос, который я действительно задаю здесь: как мне исправить мой код, чтобы мои условия победы, при правильном выполнении, действительно заканчивали игру в правильное время, а не в случайное время, когда я размещаю функцию где-нибудь. Кроме того, в качестве бонуса, почему мой компьютер / случайный бот, размещающий O, перезаписывает часть места, где игрок размещает свои X? Я знаю, что мог бы улучшить то, что помещает О, но это на другую ночь.

import random
import sys
winCondition = False
def winLoseCondition():
    if board[1] and board[2] and board[3] == 'X':
        print('You win!')
        sys.exit()
    if board[1] and board[2] and board[3] == 'O':
        print('You lose...')
        sys.exit()
    if board[1] and board[5] and board[9] == 'X':
        print('You win!')
        sys.exit()
    if board[1] and board[5] and board[9] == 'O':
        print('You lose...')
        sys.exit()
    if board[3] and board[5] and board[7] == 'X':
        print('You win!')
        sys.exit()
    if board[3] and board[5] and board[7] == 'O':
        print('You lose...')
        sys.exit()
    if board[1] and board[4] and board[7] == 'X':
        print('You win!')
        sys.exit()
    if board[1] and board[4] and board[7] == 'O':
        print('You lose...')
        sys.exit()
    if board[2] and board[5] and board[8] == 'X':
        print('You win!')
        sys.exit()
    if board[2] and board[5] and board[8] == 'O':
        print('You lose...')
        sys.exit()
    if board[3] and board[6] and board[9] == 'O':
        print('You lose...')
        sys.exit()
    if board[3] and board[6] and board[9] == 'X':
        print('You win!')
        sys.exit()
    if board[4] and board[5] and board[6] == 'O':
        print('You lose...')
        sys.exit()
    if board[4] and board[5] and board[6] == 'X':
        print('You win!')
        sys.exit()
    if board[7] and board[8] and board[9] == 'X':
        print('You win!')
        sys.exit()
    if board[7] and board[8] and board[9] == 'O':
        print('You lose...')
        sys.exit()

while winCondition == False:
    board = {1: ' ',2: ' ',3: ' ',
                 4: ' ',5: ' ', 6: ' ',
                 7: ' ', 8: ' ', 9: ' '}
    print('We are going to play Tic Tac Toe')
    firstPlayerMove = input('What is your first move? (Input a number 1:9. 1 is the top left box. 9 is the bottom right box.)')
    #Player's First Move
    if int(firstPlayerMove) == 1:
        board[1] = 'X'
    if int(firstPlayerMove) == 2:
        board[2] = 'X'
    if int(firstPlayerMove) == 3:
        board[3] = 'X'
    if int(firstPlayerMove) == 4:
        board[4] = 'X'
    if int(firstPlayerMove) == 5:
        board[5] = 'X'
    if int(firstPlayerMove) == 6:
        board[6] = 'X'
    if int(firstPlayerMove) == 7:
        board[7] = 'X'
    if int(firstPlayerMove) == 8:
        board[8] = 'X'
    if int(firstPlayerMove) == 9:
        board[9] = 'X'
    #Computer's First Move:
    firstComputerMove = random.randint(1,9)
    while firstComputerMove == int(firstPlayerMove):
        firstComputerMove = random.randint(1,9)
    while firstComputerMove != int(firstPlayerMove):

        if firstComputerMove == 1:
            board[1] = 'O'
            break
        if firstComputerMove == 2:
            board[2] = 'O'
            break
        if firstComputerMove == 3:
            board[3] = 'O'
            break
        if firstComputerMove == 4:
            board[4] = 'O'
            break
        if firstComputerMove == 5:
            board[5] = 'O'
            break
        if firstComputerMove == 6:
            board[6] = 'O'
            break
        if firstComputerMove == 7:
            board[7] = 'O'
            break
        if firstComputerMove == 8:
            board[8] = 'O'
            break
        if firstComputerMove == 9:
            board[9] = 'O'
            break
    #Player's Second Move
    print('This is what the board looks like:')
    print(board[1] +  '|' + board[2] + '|' + board[3])
    print('_ _ _')
    print(board[4] +  '|' + board[5] + '|' + board[6])
    print('_ _ _')
    print(board[7] +  '|' + board[8] + '|' + board[9])
    secondPlayerMove = input('What is your second move? ')
    while int(secondPlayerMove) == int(firstPlayerMove):
        secondPlayerMove = input('That space is already occupied. Please input a different number ')
    while int(secondPlayerMove) == firstComputerMove:
        secondPlayerMove = input('That space is already occupied. Please input a different number ')
    while int(secondPlayerMove) != int(firstPlayerMove) or firstComputerMove:

        if int(secondPlayerMove) == 1:
            board[1] = 'X'
            break
        if int(secondPlayerMove) == 2:
            board[2] = 'X'
            break
        if int(secondPlayerMove) == 3:
            board[3] = 'X'
            break
        if int(secondPlayerMove) == 4:
            board[4] = 'X'
            break
        if int(secondPlayerMove) == 5:
            board[5] = 'X'
            break
        if int(secondPlayerMove) == 6:
            board[6] = 'X'
            break
        if int(secondPlayerMove) == 7:
            board[7] = 'X'
            break
        if int(secondPlayerMove) == 8:
            board[8] = 'X'
            break
        if int(secondPlayerMove) == 9:
            board[9] = 'X'
            break
    #Computer's Second Move
    secondComputerMove = random.randint(1,9)
    while int(secondComputerMove) == int(firstPlayerMove):
        secondComputerMove = random.randint(1,9)
    while int(secondComputerMove) == firstComputerMove:
        secondComputerMove = random.randint(1,9)
    while int(secondComputerMove) == int(secondPlayerMove):
        secondComputerMove = random.randint(1,9)
    while int(secondComputerMove) != int(firstPlayerMove) or firstComputerMove or int(secondPlayerMove):

        if int(secondComputerMove) == 1:
            board[1] = 'O'
            break
        if int(secondComputerMove) == 2:
            board[2] = 'O'
            break
        if int(secondComputerMove) == 3:
            board[3] = 'O'
            break
        if int(secondComputerMove) == 4:
            board[4] = 'O'
            break
        if int(secondComputerMove) == 5:
            board[5] = 'O'
            break
        if int(secondComputerMove) == 6:
            board[6] = 'O'
            break
        if int(secondComputerMove) == 7:
            board[7] = 'O'
            break
        if int(secondComputerMove) == 8:
            board[8] = 'O'
            break
        if int(secondComputerMove) == 9:
            board[9] = 'O'
            break
    #Player's Third Move
    print('This is what the board looks like:')
    print(board[1] +  '|' + board[2] + '|' + board[3])
    print('_ _ _')
    print(board[4] +  '|' + board[5] + '|' + board[6])
    print('_ _ _')
    print(board[7] +  '|' + board[8] + '|' + board[9])
    thirdPlayerMove = input('Please say where you want to move.')
    while int(thirdPlayerMove) == int(firstPlayerMove):
        thirdPlayerMove = input('Sorry, that space is taken, please try again.')
    while int(thirdPlayerMove) == firstComputerMove:
        thirdPlayerMove = input('Sorry, that space is taken, please try again.')
    while int(thirdPlayerMove) == int(secondPlayerMove):
        thirdPlayerMove = input('Sorry, that space is taken, please try again.')
    while int(thirdPlayerMove) == secondComputerMove:
        thirdPlayerMove = input('Sorry, that space is taken, please try again.')
    while int(thirdPlayerMove) != int(firstPlayerMove) or firstComputerMove or int(secondPlayerMove) or secondComputerMove:

        if int(thirdPlayerMove) == 1:
            board[1] = 'X'
            break
        if int(thirdPlayerMove) == 2:
            board[2] = 'X'
            break
        if int(thirdPlayerMove) == 3:
            board[3] = 'X'
            break
        if int(thirdPlayerMove) == 4:
            board[4] = 'X'
            break
        if int(thirdPlayerMove) == 5:
            board[5] = 'X'
            break
        if int(thirdPlayerMove) == 6:
            board[6] = 'X'
            break
        if int(thirdPlayerMove) == 7:
            board[7] = 'X'
            break
        if int(thirdPlayerMove) == 8:
            board[8] = 'X'
            break
        if int(thirdPlayerMove) == 9:
            board[9] = 'X'
            break
    #Computer's Third Move
    thirdComputerMove = random.randint(1,9)
    while int(thirdComputerMove) == int(firstPlayerMove):
        thirdComputerMove = random.randint(1,9)
    while int(thirdComputerMove) == firstComputerMove:
        thirdComputerMove = random.randint(1,9)
    while int(thirdComputerMove) == int(secondPlayerMove):
        thirdComputerMove = random.randint(1,9)
    while int(thirdComputerMove) == secondComputerMove:
        thirdComputerMove = random.randint(1,9)
    while int(thirdComputerMove) == int(thirdPlayerMove):
        thirdComputerMove = random.randint(1,9)
    while int(thirdComputerMove) != int(firstPlayerMove) or firstComputerMove or int(secondPlayerMove) or secondComputerMove or int(thirdPlayerMove):

        if int(thirdComputerMove) == 1:
            board[1] = 'O'
            break
        if int(thirdComputerMove) == 2:
            board[2] = 'O'
            break
        if int(thirdComputerMove) == 3:
            board[3] = 'O'
            break
        if int(thirdComputerMove) == 4:
            board[4] = 'O'
            break
        if int(thirdComputerMove) == 5:
            board[5] = 'O'
            break
        if int(thirdComputerMove) == 6:
            board[6] = 'O'
            break
        if int(thirdComputerMove) == 7:
            board[7] = 'O'
            break
        if int(thirdComputerMove) == 8:
            board[8] = 'O'
            break
        if int(thirdComputerMove) == 9:
            board[9] = 'O'
            break
    #Player's Fourth Move
    print('This is what the board looks like:')
    print(board[1] +  '|' + board[2] + '|' + board[3])
    print('_ _ _')
    print(board[4] +  '|' + board[5] + '|' + board[6])
    print('_ _ _')
    print(board[7] +  '|' + board[8] + '|' + board[9])
    fourthPlayerMove = input('Where do you want to move?')
    while int(fourthPlayerMove) == int(firstPlayerMove):
        fourthPlayerMove = input('Sorry, that space is taken, please try again.')
    while int(fourthPlayerMove) == firstComputerMove:
        fourthPlayerMove = input('Sorry, that space is taken, please try again.')
    while int(fourthPlayerMove) == int(secondPlayerMove):
        fourthPlayerMove = input('Sorry, that space is taken, please try again.')
    while int(fourthPlayerMove) == secondComputerMove:
        fourthPlayerMove = input('Sorry, that space is taken, please try again.')
    while int(fourthPlayerMove) == int(thirdPlayerMove):
        fourthPlayerMove = input('Sorry, that space is taken, please try again.')
    while int(fourthPlayerMove) == thirdComputerMove:
        fourthPlayerMove = input('Sorry, that space is taken, please try again.')
    while int(fourthPlayerMove) != int(firstPlayerMove) or firstComputerMove or int(secondPlayerMove) or secondComputerMove or int(thirdPlayerMove) or thirdComputerMove:

        if int(fourthPlayerMove) == 1:
            board[1] = 'X'
            break
        if int(fourthPlayerMove) == 2:
            board[2] = 'X'
            break
        if int(fourthPlayerMove) == 3:
            board[3] = 'X'
            break
        if int(fourthPlayerMove) == 4:
            board[4] = 'X'
            break
        if int(fourthPlayerMove) == 5:
            board[5] = 'X'
            break
        if int(fourthPlayerMove) == 6:
            board[6] = 'X'
            break
        if int(fourthPlayerMove) == 7:
            board[7] = 'X'
            break
        if int(fourthPlayerMove) == 8:
            board[8] = 'X'
            break
        if int(fourthPlayerMove) == 9:
            board[9] = 'X'
            break
    #Computer's Fourth Move
    fourthComputerMove = random.randint(1,9)
    while int(fourthComputerMove) == int(firstPlayerMove):
        fourthComputerMove = random.randint(1,9)
    while int(fourthComputerMove) == firstComputerMove:
        fourthComputerMove = random.randint(1,9)
    while int(fourthComputerMove) == int(secondPlayerMove):
        fourthComputerMove = random.randint(1,9)
    while int(fourthComputerMove) == secondComputerMove:
        fourthComputerMove = random.randint(1,9)
    while int(fourthComputerMove) == int(thirdPlayerMove):
        fourthComputerMove = random.randint(1,9)
    while int(fourthComputerMove) == thirdComputerMove:
        fourthComputerMove = random.randint(1,9)
    while int(fourthComputerMove) == int(fourthPlayerMove):
        fourthComputerMove = random.randint(1,9)
    while int(fourthComputerMove) != int(firstPlayerMove) or firstComputerMove or int(secondPlayerMove) or secondComputerMove or int(thirdPlayerMove) or thirdComputerMove or int(fourthPlayerMove):

        if int(fourthComputerMove) == 1:
            board[1] = 'O'
            break
        if int(fourthComputerMove) == 2:
            board[2] = 'O'
            break
        if int(fourthComputerMove) == 3:
            board[3] = 'O'
            break
        if int(fourthComputerMove) == 4:
            board[4] = 'O'
            break
        if int(fourthComputerMove) == 5:
            board[5] = 'O'
            break
        if int(fourthComputerMove) == 6:
            board[6] = 'O'
            break
        if int(fourthComputerMove) == 7:
            board[7] = 'O'
            break
        if int(fourthComputerMove) == 8:
            board[8] = 'O'
            break
        if int(fourthComputerMove) == 9:
            board[9] = 'O'
            break
    #Player's Last Move
    print('This is what the board looks like:')
    print(board[1] +  '|' + board[2] + '|' + board[3])
    print('_ _ _')
    print(board[4] +  '|' + board[5] + '|' + board[6])
    print('_ _ _')
    print(board[7] +  '|' + board[8] + '|' + board[9])
    print('There is only one last spot.')
    if board[1] == ' ':
        print('You had to move to spot 1')
        board[1] = 'X'
    if board[2] == ' ':
        print('You had to move to spot 2')
        board[2] = 'X'
    if board[3] == ' ':
        print('You had to move to spot 3')
        board[3] == 'X'
    if board[4] == ' ':
        print('You had to move to spot 4')
        board[4] = 'X'
    if board[5] == ' ':
        print('You had to move to spot 5')
        board[5] = 'X'
    if board[6] == ' ':
        print('You had to move to spot 6')
        board[6] = 'X'
    if board[7] == ' ':
        print('You had to move to spot 7')
        board[7] = 'X'
    if board[8] == ' ':
        print('You had to move to spot 8')
        board[8] = 'X'
    if board[9] == ' ':
        print('You had to move to spot 9')
        board[9] = 'X'
    #Final Board
    print('The final board looks like:')
    print(board[1] +  '|' + board[2] + '|' + board[3])
    print('_ _ _')
    print(board[4] +  '|' + board[5] + '|' + board[6])
    print('_ _ _')
    print(board[7] +  '|' + board[8] + '|' + board[9])
    winCondition = True

print('The final board looks like:')
print(board[1] +  '|' + board[2] + '|' + board[3])
print('_ _ _')
print(board[4] +  '|' + board[5] + '|' + board[6])
print('_ _ _')
print(board[7] +  '|' + board[8] + '|' + board[9])

...