This is my first ever post on here, as usually I just follow tutorials on Youtube or whatever, but I really wanted to do a project mainly by myself. I made a little Tic Tac Toe game I was pretty proud of today in Python, but there is one thing that doesn't work. The win conditions. I put in the winLoseCondition() in different spots before, but it doesn't work. Once it gets to there, it either says I lose or win. I don't know how it decides before there is enough information. One other thing, sometimes the computer overwrites one of the places the player has gone, and I don't know why. I know I probably did 100 things wrong programming this and there is a way easier way of doing it, but there are many like it but this one is mine. So, the question I really am asking here is: How do I fix my code so that my win conditions, when fulfilled correctly, actually end the game at the correct time, and not at a random time when I place the function somewhere. Also, as a bonus, why is my computer/random O placing bot overwriting some of where the player places their X's? I know I could improve the thing that places the O's, but that is for another night.
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])
Aucun commentaire:
Enregistrer un commentaire