Python战舰游戏中的舰船布局问题

2024-04-25 00:54:02 发布

您现在位置: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。在

有人能帮忙吗?在


Tags: 函数代码功能falsetrue游戏棋盘水平
2条回答

你应该发布你如何在内部表示数据,而不仅仅是你打印出来的数据。在

然而,从你的输出中,我想象你有一个线性列表,并使用其中的某种元素来知道它是“包含一艘船”还是“不包含一艘飞船”。在

我们的建议是忘掉它,利用这个机会学习更多面向对象编码的知识,这样你就可以有一个“Board”类来知道它的内容,例如“can_place_ship(self, <coord>, <shipsize>, <shiporientation>)”方法。在

在这里,请尝试以下OO部分的教程: http://www.voidspace.org.uk/python/articles/OOP.shtml(刚从谷歌的第一个结果中选择了一个链接)

你帖子中的评论暗示了你应该做什么。例如,jamesthiele建议为边缘效应建立一个好的和坏的位置的索引。我喜欢这个主意。一个非常强大的方法是利用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创建的电路板

^{pr2}$

enter image description here

相关问题 更多 >