2016-01-19 29 views
0

這是原來的代碼,只有一艘船:我正在Python中的戰艦的比賽,我想添加更多的船隻

#Battleships 
from random import randint 

board = [] 

for x in range(8): 
    board.append(["O "] * 8) 

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

print ("Let's play Battleship!\n\n") 
print_board(board) 

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) 

for turn in range(5): 
    guess_row = int(input("Guess Row:")) 
    guess_col = int(input("Guess Col:")) 
    if guess_row == ship_row and guess_col == ship_col: 
     print ("Congratulations! You sunk my battleship!") 
     break 
    else: 
     if turn==5: 
      print ("Game Over") 

     elif (guess_row < 0 or guess_row > 7) or (guess_col < 0 or guess_col > 7): 
      print ("Oops, that's not even in the ocean.") 
     elif(board[guess_row][guess_col] == "X "): 
      print ("You guessed that one already.") 
     else: 
      print ("You missed my battleship!") 
      board[guess_row][guess_col] = "X " 
     print ("Turn:",turn+1) 
     print_board(board) 

然後我試圖添加更多的船隻,但沒有奏效:

#Battleships 
from random import randint 
board = [] 

for x in range(8): 
    board.append(["O "] * 8) 


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

print ("Let's play Battleship!\n\n") 
print_board(board) 

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) 
import random 
ships = [['row',random_row,'col',random_col]] 
for i in range(9): 
    ships.append(['row',random_row,'col',random_col]) 
random.choice(ships)() #i am lost 




def main(): 

    for turn in range(64): 
     guess_row = int(input("Guess Row:")) 
     guess_col = int(input("Guess Col:")) 

     if guess_row == random_row in ships and guess_col == random_col in ships: 
      print ("Congratulations! You sunk my battleship!") 
      continue 
     else: 
      if turn==65: 
       print ("Game Over") 
       break 

      elif (guess_row < 0 or guess_row > 7) or (guess_col < 0 or guess_col > 7): 
       print ("Oops, that's not even in the ocean.") 
      elif(board[guess_row][guess_col] == "X "): 
       print ("You guessed that one already.") 
      else: 
       print ("You missed my battleship!") 
       board[guess_row][guess_col] = "X " 
      print ("Turn:",turn+1) 
      print_board(board) 

main() 
+1

請確保您的代碼格式正確,它會讓人們更容易理解它,謝謝。歡迎來到StackOverflow! – plamut

+1

歡迎來到StackOverflow - 當你說「不起作用」時,並沒有清楚地描述問題。如果您遇到錯誤,請發佈您獲得的確切回溯。有關更多信息,請參見[本頁](http://stackoverflow.com/help/mcve)。 –

+0

難道你不應該檢查你的(多於一艘的船隻)之間的碰撞,並擴大船外的船舶嗎? – boardrider

回答

0

我會做的是做一個Ship類,然後讓Shiplist秒。像這樣的東西。

class Ship: 
    def __init__(self, col, row): 
     self.col = col 
     self.row = row 
# Say you want 5 ships 
NUMBER_OF_SHIPS = 5 
ships = [] 
for i in range(NUMBER_OF_SHIPS): 
    # random_row, random_col, and board are just like you had them 
    ships.append(Ship(random_col(board), random_row(board))) 

# After you get the guesses... 

for i in ships: 
    if guess_row == i.row and guess_col == i.col: 
     # Player wins... 
     break 
    finally: 
     # Player loses