用Python制作的Tic Tac Toe游戏
这是我第一次在这里发表文章,因为我通常只关注Youtube上的教程或其他内容,但我真的很想自己做一个项目。我用Python制作了一个小的Tic-Tac-Toe游戏,我今天非常自豪,但是有一件事不起作用。获胜的条件。我以前在不同的位置输入了winLoseCondition(),但它不起作用。一旦到达那里,它要么说我输了,要么说我赢了。在没有足够的信息之前,我不知道它是如何决定的。还有一件事,有时电脑会覆盖玩家去过的某个地方,我不知道为什么。我知道我可能做了100件错误的编程这是有一个更简单的方法做它,但有很多喜欢它,但这一个是我的。所以,我在这里真正要问的问题是:我如何修正我的代码,使我的获胜条件在正确满足时,实际上在正确的时间结束游戏,而不是在我把函数放在某个地方的随机时间。另外,作为奖励,为什么我的电脑/随机O放置机器人会覆盖玩家放置X的部分位置?我知道我可以改进O的位置,但那是另一个晚上用Python制作的Tic Tac Toe游戏,python,random,tic-tac-toe,Python,Random,Tic Tac Toe,这是我第一次在这里发表文章,因为我通常只关注Youtube上的教程或其他内容,但我真的很想自己做一个项目。我用Python制作了一个小的Tic-Tac-Toe游戏,我今天非常自豪,但是有一件事不起作用。获胜的条件。我以前在不同的位置输入了winLoseCondition(),但它不起作用。一旦到达那里,它要么说我输了,要么说我赢了。在没有足够的信息之前,我不知道它是如何决定的。还有一件事,有时电脑会覆盖玩家去过的某个地方,我不知道为什么。我知道我可能做了100件错误的编程这是有一个更简单的方法做
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])
有一点提示:你能想出一种比检查一次移动是否与之前的所有移动相比更有效的检查空间是否已被占用的方法吗?请记住,您已经有了电路板的内部表示形式(在
电路板列表中),如果电路板[1]和电路板[2]以及电路板[3]='X',您就不能执行虽然它听起来是合乎逻辑的,它会检查3个变量是否等于X,但它真正做的是检查电路板[1]是否为真(如果值不是0,则为真),如果电路板[2]如果棋盘[1]=“X”和棋盘[2]=“X”和棋盘[3]=“X”:
对于第二个错误,我缺少一些代码,但从我看到的情况来看,你可以检查计算机选择的移动是否与玩家的移动相等,您应该检查阵列板是否包含一个字母,如果它是空的。我还可以给你一些提示:如果int(firstPlayerMove)==1到9:board[1to 9]='x',你应该用一个'board[int(firstPlayerMove)]=x'来替换所有的,这将大大减少你的代码长度(你可以用计算机做同样的事情)有一点提示:你能想出一种比检查一次移动是否与之前的所有移动相比更有效的检查空间是否已被占用的方法吗?请记住,您已经有了电路板的内部表示形式(在电路板列表中),如果电路板[1]和电路板[2]以及电路板[3]='X',您就不能执行虽然它听起来是合乎逻辑的,它会检查3个变量是否等于X,但它真正做的是检查电路板[1]是否为真(如果值不是0,则为真),如果电路板[2]如果棋盘[1]=“X”和棋盘[2]=“X”和棋盘[3]=“X”:
对于第二个错误,我缺少一些代码,但从我看到的情况来看,你可以检查计算机选择的移动是否与玩家的移动相等,您应该检查阵列板是否包含一个字母,如果它是空的。我还可以给你一些提示:如果int(firstPlayerMove)==1到9:board[1to 9]='x',你应该用一个'board[int(firstPlayerMove)]=x'来替换所有的,这将大大减少你的代码长度(你可以用计算机做同样的事情)