围绕两个列表之间的确定坐标进行迭代
Iterations around definite coordinates between two lists
经典战舰游戏。
我正在尝试制作一个 space(看起来更低 'o'),我们无法放置另一艘船。
我们有一个 5*5 的“O”数组:
board = [['O','O','O','O','O'],['O','O','O','O','O'],['O','O','O','O','O'],
['O','O','O','O','O'],['O','O','O','O','O']]
我已经放置了两格飞船并且知道它的坐标。
坐标是:
[(row, column),(row, column)]
[(0,0),(1,0)]
Board = [['S','O','O','O','O'],['S','O','O','O','O'],['O','O','O','O','O'],
['O','O','O','O','O'],['O','O','O','O','O']]
现在我必须找到两个坐标周围的所有 'O'-es 并将其更改为 'o'。如果有 'S' 不要更改它。
def mark_nearby_cells(board): # function for mar
"""
function for marking cells nearby to currently placed ship 0 -> o
"""
ships_coordinates = []
for i, row in enumerate(board):
for j, ship in enumerate(row):
if ship == "S":
ship_coordinate = [row, column]
ships_coordinates.append(ship_coordinate)
for coordinate in ships_coordinates:
#how to avoid simply iteration coordinate by adding and subtracting "1
if ships_coordinates[ships_coordinates[0][0] - 1][ships_coordinates[0][1]] != 'S':
....
....
....
return board
为了简单起见,我编写了一个附加函数来显示面板并将您的“mark_nearby_cells”函数分成两部分。
import numpy as np
def print_board(board):
"""Displays the board nicely."""
print("\n".join("".join(row) for row in board))
print()
# Create the board.
X = 5
Y = 5
board = [['O' for _ in range(X)] for _ in range(Y)]
print_board(board)
# OOOOO
# OOOOO
# OOOOO
# OOOOO
# OOOOO
# Place a ship.
for x, y in ((0, 0), (0, 1)):
board[x][y] = "S"
print_board(board)
# SSOOO
# OOOOO
# OOOOO
# OOOOO
# OOOOO
def detect_ships(board):
"""Returns a list of all the cells coordinates containing "S"."""
ships_coordinates = []
for x, row in enumerate(board):
for y, cell in enumerate(row):
if cell == "S":
ships_coordinates.append((x, y))
return ships_coordinates
def mark_nearby_cells(board):
"""Puts a lower "o" in the cells near a ship."""
for (x, y) in ships_coordinates:
for dx in (-1, +1):
for dy in (-1, +1):
xdx = np.clip(0, x+dx, X-1) # We don't want the coordinates to loop around the
ydy = np.clip(0, y+dy, Y-1) # board so we trim them to [0; X] and [0; Y].
cell = board[xdx][ydy]
if cell == "O":
board[xdx][ydy] = "o"
return board
ships_coordinates = detect_ships(board)
board = mark_nearby_cells(board)
print_board(board)
# SSoOO
# oooOO
# OOOOO
# OOOOO
# OOOOO
经典战舰游戏。 我正在尝试制作一个 space(看起来更低 'o'),我们无法放置另一艘船。
我们有一个 5*5 的“O”数组:
board = [['O','O','O','O','O'],['O','O','O','O','O'],['O','O','O','O','O'],
['O','O','O','O','O'],['O','O','O','O','O']]
我已经放置了两格飞船并且知道它的坐标。
坐标是:
[(row, column),(row, column)]
[(0,0),(1,0)]
Board = [['S','O','O','O','O'],['S','O','O','O','O'],['O','O','O','O','O'],
['O','O','O','O','O'],['O','O','O','O','O']]
现在我必须找到两个坐标周围的所有 'O'-es 并将其更改为 'o'。如果有 'S' 不要更改它。
def mark_nearby_cells(board): # function for mar
"""
function for marking cells nearby to currently placed ship 0 -> o
"""
ships_coordinates = []
for i, row in enumerate(board):
for j, ship in enumerate(row):
if ship == "S":
ship_coordinate = [row, column]
ships_coordinates.append(ship_coordinate)
for coordinate in ships_coordinates:
#how to avoid simply iteration coordinate by adding and subtracting "1
if ships_coordinates[ships_coordinates[0][0] - 1][ships_coordinates[0][1]] != 'S':
....
....
....
return board
为了简单起见,我编写了一个附加函数来显示面板并将您的“mark_nearby_cells”函数分成两部分。
import numpy as np
def print_board(board):
"""Displays the board nicely."""
print("\n".join("".join(row) for row in board))
print()
# Create the board.
X = 5
Y = 5
board = [['O' for _ in range(X)] for _ in range(Y)]
print_board(board)
# OOOOO
# OOOOO
# OOOOO
# OOOOO
# OOOOO
# Place a ship.
for x, y in ((0, 0), (0, 1)):
board[x][y] = "S"
print_board(board)
# SSOOO
# OOOOO
# OOOOO
# OOOOO
# OOOOO
def detect_ships(board):
"""Returns a list of all the cells coordinates containing "S"."""
ships_coordinates = []
for x, row in enumerate(board):
for y, cell in enumerate(row):
if cell == "S":
ships_coordinates.append((x, y))
return ships_coordinates
def mark_nearby_cells(board):
"""Puts a lower "o" in the cells near a ship."""
for (x, y) in ships_coordinates:
for dx in (-1, +1):
for dy in (-1, +1):
xdx = np.clip(0, x+dx, X-1) # We don't want the coordinates to loop around the
ydy = np.clip(0, y+dy, Y-1) # board so we trim them to [0; X] and [0; Y].
cell = board[xdx][ydy]
if cell == "O":
board[xdx][ydy] = "o"
return board
ships_coordinates = detect_ships(board)
board = mark_nearby_cells(board)
print_board(board)
# SSoOO
# oooOO
# OOOOO
# OOOOO
# OOOOO