我正在嘗試製作一個tic tac腳趾程序,我將用戶設置爲X並且將計算機設置爲Y.一次獲勝者是 宣稱它是 假設 要開始遊戲再玩。它在第一次循環中完美運行,但是在下面的遊戲中,它將用戶更改爲O並將計算機更改爲O,同時也只有用戶纔有一個回合計算機永遠不會去, 但程序 將計算機註冊爲獲勝因爲電腦是O.如果程序選擇電腦先走, 它會使 第一步,但它不會再次轉向。我認爲循環有問題,但我不知道 在哪裏。需要Python調試建議
import random
winningCombinations=[]
userhistory=[]
secondchance=[]
def drawBoard(board):
# This function prints out the board that it was passed.
# "board" is a list of 10 strings representing the board (ignore index 0)
print('')
print('')
print(' | |')
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
print(' | |')
print('-----------')
print(' | |')
print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
print(' | |')
print('-----------')
print(' | |')
print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
print(' | |')
print('')
print('')
def getComputerMove():
# Here is our algorithm for our Tic Tac Toe AI:
if userhistory in winningCombinations:
#try to beat it
blockingMove = secondchance[len(secondchance)-1]
return blockingMove
else:
#make a rando move
move = random.randint(1, 9)
while theBoard[move] != ' ': #if the move is invalid or occupied
move = random.randint(1, 9)
return move
print('Welcome to Tic Tac Toe!')
#First ask the user if it wants to be X or 0
userin = ''
#while not (userin == 'X' or userin == 'O'):
# userin = raw_input('Do you want to be X or O?').upper()
# the first element in the tuple is the player's letter, the second is the computer's letter.
#if userin == 'X':
# playerLetter, computerLetter = ['X', 'O']
#elif userin =="O":
# computerLetter, playerLetter = ['O', 'X']
numGames = 1 #to keep track of the user history
#computergenerates who gets to go first
if random.randint(0, 1) == 0:
turn = 'computer'
else:
turn = 'player'
print('The ' + turn + ' will go first.')
while True:
# Reset the board
theBoard = [' '] * 10
del userhistory[:]
gameIsPlaying = True
while gameIsPlaying:
playerLetter = 'X'
computerLetter = 'O'
if turn == 'player':
# Player's turn.
drawBoard(theBoard)
umove = int(raw_input('What is your next move? (1-9)'))
while theBoard[umove] != ' ': #if the move is invalid or occupied
umove = int(raw_input('What is your next move? (1-9)'))
theBoard[umove]=playerLetter #places move onto board
userhistory.append(umove) #records users moves
secondchance.append(umove)#records users moves
#Check to see if its a winner
if ((theBoard[7] == playerLetter and theBoard[8] == playerLetter and theBoard[9] == playerLetter) or # across the top
(theBoard[4] == playerLetter and theBoard[5] == playerLetter and theBoard[6] == playerLetter) or # across the middle
(theBoard[1] == playerLetter and theBoard[2] == playerLetter and theBoard[3] == playerLetter) or # across the bottom
(theBoard[7] == playerLetter and theBoard[4] == playerLetter and theBoard[1] == playerLetter) or # down the left side
(theBoard[8] == playerLetter and theBoard[5] == playerLetter and theBoard[2] == playerLetter) or # down the middle
(theBoard[9] == playerLetter and theBoard[6] == playerLetter and theBoard[3] == playerLetter) or # down the right side
(theBoard[7] == playerLetter and theBoard[5] == playerLetter and theBoard[3] == playerLetter) or # diagonal
(theBoard[9] == playerLetter and theBoard[5] == playerLetter and theBoard[1] == playerLetter)):
drawBoard(theBoard)
print('Hooray! You have won the game!')
del userhistory[len(userhistory)-1] #deleting last element to find the combination just before losing
winningCombinations.append(userhistory) #stores the winning combination into another list
numGames+=1
gameIsPlaying = False
else:
empty = ' '
if empty not in theBoard:
print('The game is a tie!')
break
else:
turn = 'computer'
else:
# Computer's turn.
cmove = getComputerMove()
theBoard[cmove] = computerLetter
if ((theBoard[7] == computerLetter and theBoard[8] == computerLetter and theBoard[9] == computerLetter) or # across the top
(theBoard[4] == computerLetter and theBoard[5] == computerLetter and theBoard[6] == computerLetter) or # across the middle
(theBoard[1] == computerLetter and theBoard[2] == computerLetter and theBoard[3] == computerLetter) or # across the bottom
(theBoard[7] == computerLetter and theBoard[4] == computerLetter and theBoard[1] == computerLetter) or # down the left side
(theBoard[8] == computerLetter and theBoard[5] == computerLetter and theBoard[2] == computerLetter) or # down the middle
(theBoard[9] == computerLetter and theBoard[6] == computerLetter and theBoard[3] == computerLetter) or # down the right side
(theBoard[7] == computerLetter and theBoard[5] == computerLetter and theBoard[3] == computerLetter) or # diagonal
(theBoard[9] == computerLetter and theBoard[5] == computerLetter and theBoard[1] == computerLetter)):
drawBoard(theBoard)
print('Aw! You have lost the game, the computer has beaten you!')
gameIsPlaying = False
else:
empty = ' '
if empty not in theBoard:
print('The game is a tie!')
break
else:
turn = 'player'
歡迎來到StackOverflow。請閱讀並遵守幫助文檔中的發佈準則。 [MCVE](http://stackoverflow.com/help/mcve)適用於此處。我們無法爲您提供幫助,直到您發佈最小的完整代碼(簡短並能夠重現錯誤)並準確描述問題。在這種情況下,你省略了很多功能。 – Prune
用戶學習如何使用調試器可以避免SO上的許多問題。這應該是學習python恕我直言的第一步。 Python發行版通常帶有一個名爲_pdb_或_pdb.py_的面向行的調試器。用它來單步執行糟糕的程序部分,你會經常看到問題。通常情況下,回答SO問題的人首先想到了這一點。 – tdelaney
我已經包含了整個程序現在 – user135094