我正在制作一个程序,让您可以针对我的程序播放 connect 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