我如何在不使用一堆 if 语句的情况下在战舰顾问中创建第二个攻击输入?

How would I Create a second attack input in a battleship adviser without using a bunch of if statements?

将我的代码更改为 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]))

好的 - 这确实不是答案,但我想放入一些代码片段并使用比评论允许的更多的字符。

我建议重新考虑您的方法。

您说您希望能够改变网格大小,但是您的函数 As() 明确硬连接到 5 行长度。

第一步 - 尝试将问题分解为一个或多个引擎,这些引擎根据您对数据和一组数据的一般假设工作。

例如,要允许最大 10 x 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()

现在,google 获取输入或阅读的字母在 col 列表中的位置的方法:How to get the position of a character in Python?

我将其称为 x

像以前一样将提取的行值转换为 int 并减去一个,如 python 列表中第一个索引为零。

我将把这个值称为 y

所以如果我输入 'D7' 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 时该怎么办,以及如何相应地重组输出文本。

将向上、向下、向右和向左的计算构建到一个函数中,同时适当地检查索引,然后您就可以根据要尝试处理的容器的大小来处理更复杂的建议了击中并最终存储已知的击中位置,因此您不建议在以前被击中的地方射击。

希望对您有所帮助。

==> 根据您编辑的问题 - 这有帮助吗:

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]))