我目前正在CLI中制作“火柴棍”游戏,它是Player vs AI。几乎所有的东西都可以正常工作,只是“ AI”选择了先前已取下的棍子。
这是代码:
class CPyramide(object):
def __init__(self, lines):
self.pir = []
for i in range(lines):
sticks = ["|" for j in range(i+1)]
self.pir.append(sticks)
def __str__(self):
o = ""
for i, L in enumerate(self.pir):
spaces = (len(self.pir) - i) * " "
o += spaces
o += " ".join(L)
o += "\n"
return o
def stickDelete(self, line, n):
self.pir[line] = self.pir[line][n:]
try:
lines = int(sys.argv[1])
sticks = int(sys.argv[2])
cpir = CPyramide(lines)
print(cpir)
while True:
print("Your turn:")
inputLine = input("Line: ")
inputSticks = input("Matches: ")
if int(inputSticks) > sticks:
print("Error: you cannot remove more than",sticks,"matches per turn")
continue
else:
print("Player removed",inputSticks,"match(es) from line",inputLine)
cpir.stickDelete(int(inputLine) - 1,int(inputSticks))
print(cpir)
print("AI's turn...")
aiSticks = randint(1,sticks)
aiLines = randint(1,lines)
print("AI removed",aiSticks,"match(es) from line",aiLines)
cpir.stickDelete(aiLines - 1,aiSticks)
print(cpir)
我一直在努力使它检查包含[|]的每个数组,并可能将其删除,但我不知道该怎么做。
有没有办法使cpir.stickDelete函数检查数组中是否包含[|]并可能将其删除但随机删除?因为每次我在玩AI时,它都会选择以前已经删除的东西。
是否有办法检查每个数组,并可能检查它是否包含[|]并以随机方式将其删除?
感谢阅读。
答案 0 :(得分:0)
尝试一下:
if "|" in my_list:
my_list.remove("|")