python - python 中的战舰游戏放置问题

标签 python

我正在创建一个带有 10x10 棋盘的战舰游戏,看起来是这样的:

-------------------------------------------------
 0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 |
-------------------------------------------------
10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
-------------------------------------------------
20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
-------------------------------------------------
30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
-------------------------------------------------
40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
-------------------------------------------------
50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 |
-------------------------------------------------
60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 |
-------------------------------------------------
70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 |
-------------------------------------------------
80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 |
-------------------------------------------------
90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 |
-------------------------------------------------

我已经能够用我的代码打印它,但现在我正在尝试编写一个函数来检查选择的位置是否是可以将船放置在棋盘内的位置。

这是我得到的提示,但我真的不知道如何解决这个问题。

如果 choice 是 88,shipDir 是水平的,shipType 是 3,那么 ship 不适合,因为它会占据 88-89-90 的位置,而 90 是下一行的位置(所以 ship 会在板外).

如果 choice 是 88,shipDir 是垂直的,shipType 是 3,那么 ship 也不适合,因为它将占据 88-98-108 的位置,而 108 在棋盘之外。

此函数还检查所选位置是否是板上另一艘船已经占据的位置。

如果一艘船不在船上,并且船的位置被另一艘船占据,函数应返回 False。否则函数应返回 True

有人可以帮忙吗?

最佳答案

您帖子中的评论暗示了您应该做什么。例如,James Thiele 建议为边缘效应制作好位置和坏位置的索引。我喜欢这个主意。一种非常强大的方法是利用 numpy 的广播功能为您进行检查。像这样的方法的优点是能够定义“非传统”船舶,比如形状不仅仅是线性的船舶。

出于教学原因,我将在下面发布完整的解决方案,也就是说,我希望它对您的学习有用。作为家庭作业,请自己编写解决方案的代码——但你可以从下面的答案中获取你能做的。你会注意到我定义了一个“非传统”的 U 形船作为例子。

import numpy as np

# Define the problem
N  = 10
msl = 4 # max_ship_length

# Reserve the boards
BOARD = np.zeros((N,N))
CHECK = np.zeros((N+msl,N+msl))

# Mark the positions outside the board as bad
CHECK[:N,:N] = 1

# Define some ships
battleship  = np.array([[0,1,2,3],[0,0,0,0]])
patrol = np.array([[0,1],[0,0]])
uboat  = np.array([[0,0,1,2,2],[1,0,0,0,1]])
v_idx = [1,0]

def try_place(location, ship, color):
    location = np.reshape(location,(2,1))
    idx = zip(location+ship)
    if CHECK[idx].all() and not BOARD[idx].any():
        BOARD[idx] = color
        return True
    return False

def random_spot(): return np.random.random_integers(0,N-1,2)

# Check some random locations for ships and place them if possible
for _ in xrange(3):
    try_place(random_spot(), patrol, 1)             # Horz. patrol boat
    try_place(random_spot(), battleship, 2)         # Horz. battleship
    try_place(random_spot(), battleship[v_idx], 2)  # Vertical battleship
    try_place(random_spot(), uboat, 3)              # Horz. UBoat

您可以可视化使用 pylab 创建的板

import pylab as plt
plt.matshow(BOARD)
plt.show()

enter image description here

关于python - python 中的战舰游戏放置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10129398/

相关文章:

python - "' 无类型 ' object is not iterable"错误

python - 使用 pandas 从一列字典创建一个热编码

python - 使生成器无限次循环通过容器的最佳实践

python - Python XPATH Selenium 元素没有可见文本结果

长时间运行后python脚本崩溃

python - 在 Spark 中使用带有缺失值的 MLLib

python - 日志文件的实时进度

python - 尝试使用 Python 来区分数字

python - Tensorflow 不显示 "Successfully opened so & so CUDA libraries locally"

python - Numpy Linalg 规范行为异常(错误)