2015-10-27 162 views
0

我正在尝试制作一个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' 
+0

欢迎来到StackOverflow。请阅读并遵守帮助文档中的发布准则。 [MCVE](http://stackoverflow.com/help/mcve)适用于此处。我们无法为您提供帮助,直到您发布最小的完整代码(简短并能够重现错误)并准确描述问题。在这种情况下,你省略了很多功能。 – Prune

+0

用户学习如何使用调试器可以避免SO上的许多问题。这应该是学习python恕我直言的第一步。 Python发行版通常带有一个名为_pdb_或_pdb.py_的面向行的调试器。用它来单步执行糟糕的程序部分,你会经常看到问题。通常情况下,回答SO问题的人首先想到了这一点。 – tdelaney

+0

我已经包含了整个程序现在 – user135094

回答

3

由于您要求提供调试建议,我会在该级别进行回答。

当你有一个生病的病人,问问它在哪里伤害。打印命令是一种钝器,但却是有效的武器。在您的代码的关键点,坚持在一个声明,如

print "CHECKPOINT A", "turn =", turn 

换句话说,跟踪逻辑流和有用的值。这些语句在块(循环或函数)的顶部或底部特别有用。

接下来,当你有一段很长的代码时,考虑将一些逻辑移到一个函数中。在这个程序中,你检测胜利的代码将是一个很好的候选者:你有8行相同的代码,你可以用函数调用来隔离和替换。

我预计这两个将足以追查问题;他们几乎处理了我所有的失败。我没有重复长期以来的建议,而是一个接一个地向我推荐博客文章how to debug以获得进一步的建议。

+0

我已经尝试过你的检查点的建议,一切仍然看起来不错。我不知道问题是什么 – user135094

+0

我已经编辑了上面的代码来包含我的整个程序 – user135094