我正在为 Discord 服务器制作一个战舰游戏机器人。我还没有实现 Discord 部分,我仍在制定游戏的逻辑。
这是代码:
import numpy as np
import re
waters = np.zeros((10,10),'U2')
headers = ['A','B','C','D','E','F','G','H','I','J']
#PRINTS THE BOARD
for i,header in enumerate(headers):
if i == 0:
print(' ',end='')
print(header + ' ',end='')
if i == len(headers)-1: print()
for x,line in enumerate(waters):
print('%2d'%(x),end='')
for pos in line:
if pos == '':
print(' ##',end='')
else:
print(' '+pos,end='')
print()
board_x_coord = {
"A":0,
"B":1,
"C":2,
"D":3,
"E":4,
"F":5,
"G":6,
"H":7,
"I":8,
"J":9
}
ship_type = [
["CV","Carrier",5],
["BB","Battleship",4],
["CA","Cruiser",3],
["SS","Submarine",3],
["DD","Destroyer",2],
]
def board_coord_to_npcoord(ship,coord):
result = re.findall(r'([a-zA-Z0-9])',coord)
try:
if result[0] in headers:
if int(result[1]) in range(0,9):
if result[2] in headers:
if int(result[3]) in range(0,9):
x_crd_a = board_x_coord.get(result[0])
y_crd_a = int(result[1])
x_crd_b = board_x_coord.get(result[2])
y_crd_b = int(result[3])
print('X: %s , Y: %s , X: %s , Y: %s' %(x_crd_a,y_crd_a,x_crd_b,y_crd_b))
return True
print('Error. Try again.')
except ValueError:
print('Error. Try again.')
return False
for ship in ship_type:
while True:
print('Coordinates for %s (%s): ' %(ship[1],ship[2]),end='')
if board_coord_to_npcoord(ship[0],input()): break
董事会看起来像这样:
A B C D E F G H I J
0 ## ## ## ## ## ## ## ## ## ##
1 ## ## ## ## ## ## ## ## ## ##
2 ## ## ## ## ## ## ## ## ## ##
3 ## ## ## ## ## ## ## ## ## ##
4 ## ## ## ## ## ## ## ## ## ##
5 ## ## ## ## ## ## ## ## ## ##
6 ## ## ## ## ## ## ## ## ## ##
7 ## ## ## ## ## ## ## ## ## ##
8 ## ## ## ## ## ## ## ## ## ##
9 ## ## ## ## ## ## ## ## ## ##
脚本将询问坐标。前任。航母:A0E0,战列舰:A2A5
A B C D E F G H I J
0 CV CV CV CV CV ## ## ## ## ##
1 ## ## ## ## ## ## ## ## ## ##
2 BB ## ## ## ## ## ## ## ## ##
3 BB ## ## ## ## ## ## ## ## ##
4 BB ## ## ## ## ## ## ## ## ##
5 BB ## ## ## ## ## ## ## ## ##
6 ## ## ## ## ## ## ## ## ## ##
7 ## ## ## ## ## ## ## ## ## ##
8 ## ## ## ## ## ## ## ## ## ##
9 ## ## ## ## ## ## ## ## ## ##
如何用相应的船舶类型填充所选元素以及如何检测碰撞?
最佳答案
我做了第一个案例并进行了一些小的更改:
- 改进的正则表达式
- 使用“##”作为面板单元格的默认值
- 打印板变功能
import numpy as np
import re
waters = np.full((10,10), '##','U2')
headers = ['A','B','C','D','E','F','G','H','I','J']
#PRINTS THE BOARD
def printBoard():
for i,header in enumerate(headers):
if i == 0:
print(' ',end='')
print(header + ' ',end='')
if i == len(headers)-1: print()
for x,line in enumerate(waters):
print('%2d'%(x),end='')
[print(' '+pos,end='') for pos in line]
print()
printBoard()
board_x_coord = {
"A":0,
"B":1,
"C":2,
"D":3,
"E":4,
"F":5,
"G":6,
"H":7,
"I":8,
"J":9
}
ship_type = [
["CV","Carrier",5],
["BB","Battleship",4],
["CA","Cruiser",3],
["SS","Submarine",3],
["DD","Destroyer",2],
]
def board_coord_to_npcoord(ship,coord):
try:
result = re.findall(r'([A-J])(\d)([A-J])(\d)',coord)[0]
x_crd_a = board_x_coord.get(result[0])
y_crd_a = int(result[1])
x_crd_b = board_x_coord.get(result[2])
y_crd_b = int(result[3])
vertical = ship[2] == abs(x_crd_b - x_crd_a + 1) and y_crd_a == y_crd_b
horizontal = ship[2] == abs(y_crd_b - y_crd_a + 1) and x_crd_a == x_crd_b
valid = vertical or horizontal
if valid == False:
print('Invalid length')
return False
if vertical:
waters[y_crd_a][x_crd_a:x_crd_b + 1] = ship[0]
print(waters[y_crd_a][x_crd_a:x_crd_b])
printBoard()
print('X: %s , Y: %s , X: %s , Y: %s' %(x_crd_a,y_crd_a,x_crd_b,y_crd_b))
return True
except ValueError:
print('Error. Try again.')
return False
for ship in ship_type:
while True:
print('Coordinates for %s (%s): ' %(ship[1],ship[2]),end='')
if board_coord_to_npcoord(ship,input()): break
关于python - 使用 2 个坐标选择数组中的项目并填充它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58562212/