2015-04-15 70 views
0
def random_row(board): 
    return randrange(0, len(board)-1) 

def random_col(board): 
    return randrange(0, len(board[0])-1) 

ship_row = random_row(board) 
ship_col = random_col(board) 
print ship_row 
print ship_col 

這是codeacademy的我的戰艦遊戲代碼。我如何獲得一個隨機索引範圍,而不是隻有一個數字的ship_row和ship_col?我希望我的船有多個索引,因爲猜測一個數字很難,戰列艦有多個瓦片對嗎?謝謝,我希望我的問題很清楚。如何獲得隨機指數範圍來創建戰艦? (Python)

+1

戰列艦上圖 –

回答

1

算法將是這樣的......

如果是,你需要得到4點在一排,你可以有兩種可能性的5×5板。你可以在棋盤上選一個位置,然後倒數4來獲得戰列艦的空間,或者選擇位置然後倒數。我會做這個問題的方法是做一個隨機選擇,看看你是否想上下。然後你可以做兩個更多的隨機選擇來獲得x和y。你只需要確定一切都在基於板子大小的範圍內。

+0

行認爲我有2維板,讓我們說這是5×5和我想爲那些隨機獲得4塊瓷磚(必須是一條線)。我怎麼做? –

+0

我編輯了我的答案,給出了可以使用的算法@VinceParulan –

+0

最好的方法是首先選擇方向(水平或垂直),然後選擇左側或頂部瓷磚(不能在電路板上的任何位置......) –

0

這似乎是一件很有趣的事情,但我通常只是試着將你推向正確的方向,今天我決定給你一些東西,讓它們能夠放置船隻。

這不是最佳的,但對此應用程序很好。

處理安打,沉船被留作練習:

import collections 
import random 

DIRECTIONS = ['up', 'down', 'left', 'right'] 
Ship = collections.namedtuple("Ship", ("name", "size", "count")) 
Board = collections.namedtuple("Board", ("w", "h", "map")) 

ships = [Ship("battle", 4, 1), Ship('submarine', 3, 1), Ship('destroyer', 3, 1), Ship("patrol", 2, 1), Ship("carrier", 5, 1)] 

def create_board(w, h): 
    return Board(w, h, [['_'] * w for _ in range(h)]) 


def place_ships(board, ships): 
    for ship in ships: 
     for _ in range (ship.count): 
      place_ship(board, ship) 


def place_ship(board, ship): 
    while True: 
     _x = random.randint(0, board.w - 1) 
     _y = random.randint(0, board.h - 1) 
     random.shuffle(DIRECTIONS) 
     for direction in DIRECTIONS: 
      if maybe_place(board, _x, _y, direction, ship): 
       return 


def maybe_place(board, x, y, direction, ship): 
    if direction == "right": 
     right = min(board.w - 1, x + ship.size) 
     left = right - ship.size 
     slots = [(x, i) for i in range(left, right)] 
    elif direction == "left": 
     left = max(0, x - ship.size) 
     right = left + ship.size 
     slots = [(x, i) for i in range(left, right)] 
    elif direction == "down": 
     bottom = min(board.h - 1, y + ship.size) 
     top = bottom - ship.size 
     slots = [(i, y) for i in range(top, bottom)] 
    elif direction == "up": 
     top = max(0, y - ship.size) 
     bottom = top + ship.size 
     slots = [(i, y) for i in range(top, bottom)] 

    if all([board.map[x][y] == '_' for x, y in slots]): 
     for x, y in slots: 
      board.map[x][y] = ship.name[0] 
     return True 
    return False 


def print_board(board): 
    for row in board.map: 
     for x in row: 
      print x, 
     print 


if __name__ == "__main__": 
    board = create_board(10, 10) 
    place_ships(board, ships) 
    print_board(board) 

輸出的例子

_ _ c _ _ _ _ _ _ _ 
_ _ c _ _ _ _ _ _ _ 
_ _ c _ _ _ _ _ _ _ 
_ _ c _ _ _ _ _ _ _ 
_ _ c _ p p _ _ _ _ 
_ _ _ _ _ b _ _ _ _ 
_ _ _ _ _ b _ _ _ _ 
_ _ _ _ _ b s s s _ 
_ _ _ _ _ b _ _ _ _ 
_ _ _ _ _ _ d d d _ 


_ _ _ c b _ _ _ _ _ 
_ _ _ c b _ _ _ _ _ 
_ _ _ c b _ _ _ _ _ 
_ _ _ c b _ _ _ _ _ 
_ _ _ c _ _ _ _ _ _ 
_ _ _ _ _ _ _ _ d _ 
_ _ _ _ _ _ p p d _ 
_ _ _ _ _ _ _ _ d _ 
_ _ _ _ _ _ _ _ _ _ 
_ _ _ _ _ _ s s s _ 


_ _ _ _ _ c _ _ _ _ 
_ _ _ _ _ c _ _ _ _ 
_ _ d d d c _ _ _ _ 
_ _ _ _ _ c _ _ _ _ 
_ _ _ _ _ c _ _ _ _ 
_ _ _ _ _ b b b b _ 
_ _ _ _ _ _ p s _ _ 
_ _ _ _ _ _ p s _ _ 
_ _ _ _ _ _ _ s _ _ 
_ _ _ _ _ _ _ _ _ _