2012-04-12 78 views
1

我创建战舰游戏板是10×10,看起来这样的:战舰游戏蟒蛇船安置问题

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

我已经能够用我的代码打印这一点,但现在我想编写一个函数来检查选择是否可以放置在船上的位置。

这是一个暗示,我已经给了,但我从字面上不知道如何解决这个问题。

如果选择是88,shipDir是水平的,shipType是3,那么船舶不适合,因为它将采取位置88-89-90和90是下一行中的位置(并且因此船舶将不在板)。

如果选择是88,shipDir是垂直的,shipType是3,那么出货也不合适,因为它将采取88-98-108和108的位置。

此功能还可以检查选定的位置是否已被船上其他船舶占用的位置。

如果船舶在船外,并且如果船上有另一艘船,则功能应返回False。否则,函数应返回True

任何人都可以帮忙吗?

+10

我感觉到一些创造了板 “你尝试过什么?”在空中 – icecrime 2012-04-12 18:18:47

+0

是完全诚实的,我不知道从哪里开始 – user1329880 2012-04-12 18:23:25

+0

这是功课吗? – GWW 2012-04-12 18:23:58

回答

1

你应该发布你如何在内部表示数据,而不仅仅是你打印出来的内容。但是,从你的输出中,我想你有一个线性列表,并使用某种元素来知道它是否“包含船只”或“不包含船只”。

建议是忘掉它,并采取机会学习更多关于面向对象的编码 - 以便您可以拥有一个知道其内容的“Board”类和一个“can_place_ship(self, <coord>, <shipsize>, <shiporientation>)”方法,例如。

这里,尝试本教程为OO部分: http://www.voidspace.org.uk/python/articles/OOP.shtml你应该做的事情(只是拿起从谷歌的第一个结果中的链接)在您的文章提示

2

的意见。例如,詹姆斯泰勒建议制作边缘效应好坏位置的索引。我喜欢这个想法。一个非常强大的方法是利用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