2014-04-01 64 views
0

我想製作自己的codecademy戰列艦版本。Codecademy戰列艦蟒蛇

這裏是我的代碼,以便far.`

board = [] 

for x in range(0, 5): 
    board.append(["O"] * 5) 

def print_board(board): 
    for row in board: 
     print " ".join(row) 

def random_row(board): 
    return randint(0, len(board) - 1) 

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


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



running = True 
while running: 
    print_board(board) 
    guess_row = int(raw_input("Guess Row:")) - 1 #-1 in order to make rows numbered from 1 to 5 for the user 
    guess_col = int(raw_input("Guess Col:")) - 1 #-1 in order to make cols numbered from 1 to 5 for the user 



    if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4): 
     print "Error: index out of bounds" 

    elif guess_row == ship_row and guess_col == ship_col: 
     print "Congratulations! You sank my battleship!" 
     running = False 
    else: 
     print "You missed my battleship!" 
     board[guess_row][guess_col] = "X"` 

程序工作正常。我只是想製造多種不同長度的船隻,並且有兩個不同的板子,這樣兩個玩家就可以玩並猜對彼此的船隻。但是,我們一次做一件事。

如果我想要有多艘船隻說長度2或3,那麼代表這些船隻的好策略是什麼?我的想法是使用表示船舶在網格上所佔的座標的列表,但每個點都有一個x和一個y座標。那麼長度爲3的船是長度爲3的列表,由3個長度爲2的列表組成,每列長度爲x,y座標?這是我感到困惑的地方。

回答

0

在戰列艦上,每個玩家都有兩個棋盤:一個用來放他的船,另一個用來跟蹤敵人的射擊。

要存儲的船舶,你可以只是把一個代號爲整網:

0 1 1 1 1 0 
0 2 0 0 0 0 
0 2 0 0 0 0 
0 2 0 3 3 0 
0 0 0 0 0 0