将我的代码更改为R.Sharp建议的一切都完好无损但我仍然无法找到教程或任何有关如何创建第二次攻击的内容
Rank= input("What is your rank?")
Name= input("What is your name " + Rank + "?")
grid= input("What would you like the length of your grid to be " + Rank + " " + Name + "?")
Attack1=input("Where do you want to attack first " + Rank + " " + Name + "?\n" +"(Please print with capital letters)")
grid= int(grid)
#Removing the Number from the Letter
RemoveNum = Attack1[0].upper()
#Removing the letter from the coordinates
RemoveLetter = Attack1[1].upper()
RemoveLett=int(RemoveLetter)
row = "1 2 3 4 5 6 7 8 9 10".split()
col = "A B C D E F G H I J".split()
x=col.index(RemoveNum)
y=row.index(RemoveLetter)
possibles=[] # blank list to store suggestions in
if x > 0 :
possibles.append(col[x-1] + row[y]) # left
if x < grid-1:
possibles.append(col[x+1] + row[y]) # right
if y > 0 :
possibles.append(col[x] + row[y-1]) # up
if y < grid-1:
possibles.append(col[x] + row[y+1]) # down
# Construct string of all possibles except the last
poss_list = ','.join(possibles[:len(possibles)-1])
if possibles > grid:
pass
#Printing First Suggestions
print("You could try {0:s} and {1:s}".format(poss_list,possibles[-1]))
答案 0 :(得分:0)
我建议重新思考你的方法。
您说您希望能够改变网格大小,但是您的函数As()
明确地硬连接到行长度为5。
第一步 - 尝试将问题分解为一个引擎或引擎,它们可以处理关于数据和一组数据的一般假设。
例如,要允许最多10到10的网格,请定义2个索引列表:
row = "1 2 3 4 5 6 7 8 9 10".split()
col = "A B C D E F G H I J".split()
现在,谷歌找到一个方法来获取列表中的位置,输入或读取的字母:How to get the position of a character in Python?
我将其称为x
将您提取的行值转换为前一个int,并在python列表中减去一个,第一个索引为零。
我将此值称为y
所以,如果我有输入&#39; D7&#39; x为3,y为6。
x=3
y=6
因为网格是常规的,所以你知道你给出的那个位置(x,y)的4个位置是:
Left = (col[x-1] + row[y])
Right = (col[x+1] + row[y])
Up = (col[x] + row[y-1])
Down = (col[x] + row[y+1])
所以你需要做的就是获得4分,所有你需要做的是:
print("you could try {0:s}, {1:s}, {2:s} and {3:s}".format(Left, Right, Up, Down))
给出:
you could try C7, E7, D6 and D8
您现在需要考虑的是如何处理索引超出网格的时间。即,如果x或y为零,或9,以及如何相应地重构输出文本,该怎么办。
将Up,Down,Right和Left的计算构建到一个函数中,同时适当检查索引,然后您就可以根据您所使用的容器的大小来处理更复杂的建议试图击中并最终存储已知的命中位置,这样你就不会建议在以前被击中的某个地方进行射击。
我希望有所帮助。
Rank= input("What is your rank?")
Name= input("What is your name " + Rank + "?")
grid= input("What would you like the length of your grid to be " + Rank + " " + Name + "?")
Attack1=input("Where do you want to attack first "
+ Rank + " " + Name + "?\n" +
"(Please print with capital letters)")
#Removing the Number from the Letter
RemoveNum = Attack1[0].upper()
#Removing the letter from the coordinates
RemoveLetter = Attack1[1].upper()
row = "1 2 3 4 5 6 7 8 9 10".split()
col = "A B C D E F G H I J".split()
x=col.index(RemoveNum)
y=row.index(RemoveLetter)
possibles=[] # blank list to store suggestions in
if x > 0 :
possibles.append(col[x-1] + row[y]) # left
if x < grid-1:
possibles.append(col[x+1] + row[y]) # right
if y > 0 :
possibles.append(col[x] + row[y-1]) # up
if y < grid-1:
possibles.append(col[x] + row[y+1]) # down
# Construct string of all possibles except the last
poss_list = ','.join(possibles[:len(possibles)-1])
#Printing First Suggestions
print("You could try {0:s} and {1:s}".format(poss_list,possibles[-1]))