簡體   English   中英

為什么我在自己的值列表中附加了值,但我的scoreList還是空的? 代碼嘗試最大化列表時的運行時

[英]Why is my scoreList empty even though I've appended values to it? Runtime when code tries to max list

我正在制作一個程序,允許您對我的程序進行連接4。 這是外殼中發生的所有事情:

b = board(7,6) 
aiPlayer = player("X", 3)
b.playGameWith(aiPlayer)

| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
---------------
 0 1 2 3 4 5 6  

O's choice: 3

Traceback (most recent call last):
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 1, in <module>
    # Used internally for debug sandbox under external interpreter
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 184, in playGameWith
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 225, in nextMove
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 214, in scoresFor
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 214, in scoresFor
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 220, in scoresFor
ValueError: max() arg is an empty sequence

b是我在board類中在shell中創建的對象。 aiPlayer是播放器對象。

以下是相關代碼:

class board: # i have other methods too but didn't want to clutter
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.data = []

        for row in range(self.height):
            boardRow = []
            for col in range (self.width):
                boardRow += [' ']
            self.data += [boardRow]

    def __repr__(self):     
        s = ''
        for row in range( self.height ):
            s += '|'
            for col in range ( self.width ):
                s += self.data[row][col] + '|'
            s += '\n' 

        i = -1
        for col in range( (self.width * 2) + 1 ):
            s += "-"
        s+= '\n'
        s+= " "
        for col in range( self.width ):
            i += 1
            s += str(i) + " "       
        return s

     def playGameWith(self, aiPlayer):
        winner = False
        if aiPlayer.ox == "X" or aiPlayer.ox == "x":
            firstMove = "O"
        else:
            firstMove = "X"
        while(winner == False):
            print "\n", self, "\n"

            if firstMove == "X":
                humanMove = int(raw_input("X's choice: "))  # get user input for column choice
                while(self.allowsMove(humanMove) == False): # ensure the user is selecting a valid column
                    humanMove = int(raw_input("\n" + "Invalid 'X' move, please try again: "))
                self.addMove(humanMove, "X")
                if (self.winsFor("X") == True):
                    message = "X wins -- Congratulations!"
                    break

                elif (self.isFull() == True):
                    message = "Looks like we got ourselves a tie."
                    break                   

            else:
                humanMove = int(raw_input("O's choice: "))
                while(self.allowsMove(humanMove) == False): # ensure the user is selecting a valid column
                    humanMove = int(raw_input("\n" + "Invalid 'O' move, please try again: "))
                self.addMove(humanMove, "O")  
                if (self.winsFor("O") == True):
                    message = "O wins -- Congratulations!"
                    break 

                elif (self.isFull() == True):
                    message = "Looks like we got ourselves a tie."
                    break     

            AI_Move = aiPlayer.nextMove(b) # here's where problems start. line 184
            self.addMove(AI_Move, aiPlayer.ox)

class player:
    def __init__(self, ox, ply):
        self.ox = ox
        self.ply = ply

    def scoresFor(self, b, ox, ply):
        scoreList = []
        opScoreList = []
        for col in range(b.width):
            if b.allowsMove(col):
                b.addMove(col, ox)

                if b.winsFor(ox) == True:
                    scoreList.append(100)

                elif ply == 1:
                    scoreList.append(50)

                else:
                    if ox == "X" or ox == "x":
                        ox = "O"
                    else:
                        ox = "X"
                    opScoreList = self.scoresFor(b, ox, ply-1) # line 214

                b.delMove(col)
            else:
                scoreList.append(-1) 
        scoreList.append(100 - max(opScoreList)) # line 220
        return scoreList

    def nextMove(self, board):
        moveList = self.scoresFor(b, self.ox, self.ply) # line 225
        bestMove = moveList.index((max(moveList)))
        return bestMove
self.scoresFor(b, ox, ply-1)

要么

self.scoresFor(b, self.ox, self.ply)

返回一個空列表 嘗試在代碼中放入一些跟蹤輸出,然后找出原因。 沒有更多的上下文是不可能說的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM