我正在用python编写一个叫做坦克的游戏,本来应该有50轮,但只有6轮就停止了,有人能告诉我发生了什么吗?它也意味着当用户得分达到10分时停止,因为只有6轮,因此它不可能因此而停止。
下面是我的代码:
#Tanks problem
import random
Grid = [["" for X in range(8)] for Y in range(8)]
PlacePicked=[]
#subroutine to place tanks
def TankPlace():
global Grid
for I in range(0,10,1):
while True:
X=random.randint(0,7)
Y=random.randint(0,7)
if Grid[X][Y]=="T":
pass
else:
Grid[X][Y]="T"
break
#subroutine to output places already picked
def OutputPlacePicked():
global PlacePicked
Places=""
if len(PlacePicked)==0:
pass
else:
for I in range(0,len(PlacePicked),1):
Places=Places+PlacePicked[I]+", "
print("\nSo far you have picked the coordinates of {}".format(Places[:-2]))
#subroutine to check if inputted coordinate is a tank or not
def GameInput(X,Y):
X1=X-1
Y1=Y-1
global Grid
global PlacePicked
while True:
if "({} ,{})".format(X,Y) in PlacePicked:
print("\nYou have already chosen ({}, {})".format(X, Y))
X=int(input("\nWhat is your X coordinate?\n"))
Y=int(input("\nWhat is your Y coordinate?\n"))
elif Grid[X1][Y1]=="T":
print("\nTank Hit!")
PlacePicked.append("({} ,{})".format(X, Y))
tank="True"
return tank
else:
print("\nTank Missed!")
PlacePicked.append("({} ,{})".format(X, Y))
tank="False"
return tank
#maincode
print("Welcome to Tanks!\n")
print("Instructions:\n-Tanks is a game for 1 player.\n-At the start of the game, the computer places 10 tanks on an 8x8 board but does not\nreveal their locations to the player.\n-Each tank occupies one square on the board.\n-The player enters a grid reference, e.g. (1,5) on each turn.\n-The player destroys the tank if the square is occupied by a tank.\n-The player wins if they destroy all the tanks within 50 turns.")
go=0
score=0
while True:
TankPlace()
OutputPlacePicked()
GameInput(int(input("\nWhat is your X coordinate?\n")), int(input("\nWhat is your Y coordinate?\n")))
if GameInput=="True":
score=score+1
elif GameInput=="False":
pass
if score==10:
print("You Win!")
break
else:
pass
if go==50:
print("You Lose")
break
else:
pass
go=go+1有人能告诉我为什么会这样吗?
发布于 2021-10-25 11:39:21
我对你的"main“做了一些修改:
#maincode
print("Welcome to Tanks!\n")
print("""Instructions:
-Tanks is a game for 1 player.
-At the start of the game, the computer places 10 tanks on an 8x8 board but does not
reveal their locations to the player.
-Each tank occupies one square on the board.
-The player enters a grid reference, e.g. (1,5) on each turn.
-The player destroys the tank if the square is occupied by a tank.
-The player wins if they destroy all the tanks within 50 turns.""")
go=0
score=0
TankPlace()
while True:
OutputPlacePicked()
GameInput(int(input("\nWhat is your X coordinate?\n")), int(input("\nWhat is your Y coordinate?\n")))
if GameInput=="True":
score=score+1
elif GameInput=="False":
pass
if score==10:
print("You Win!")
break
else:
pass
if go==50:
print("You Lose")
break
else:
pass
go=go+1请注意,我在循环之前移动了对TankPlace()的调用;我认为每次移动都要重新放置坦克,这不会帮助玩家获胜。我还删除了global tank,因为它没有被使用。
无论如何,在那之后,我可以玩超过6个动作。然而,游戏过早地结束了,因为我猜到了一个9,然后产生了一个异常。对于下一个版本来说,检查用户的输入将是一个真正的改进。
发布于 2021-10-25 11:29:53
我的猜测是GameInput与字符串"True“的比较。
我将允许您查找它,但我担心两个不同类型的比较将降级为bool类型的比较,并且如果int为非零且字符串为非空,则总是成功。
https://stackoverflow.com/questions/69707204
复制相似问题