2016-11-29 30 views
-3

我正在用Python製作戰艦遊戲。如何讓戰列艦船隻比一個空間長?

我想不出如何使船舶長於一個空間。

這裏是我的代碼:

x = 0 
    def game(): 
    while x == 0: 
    from random import randint 
    board = [] 
    row_num = 1 
    hit = 0 
    for i in range(1,11): 
     board.append(10*["O"]) 
     row_num += 1 

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

    print("Let's Play Battleship!") 
    print("Turn 1") 
    print_board(board) 

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

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

    ship1_row = random_row(board) 
    ship1_col = random_col(board) 

    ship2_row = random_row(board) 
    ship2_col = random_col(board) 

    ship3_row = random_row(board) 
    ship3_col = random_col(board) 

    def check_ship2(): 
     if ship2_row == ship1_row and ship2_col == ship1_col: 
      ship2_row = random_row(board) 
      ship2_col = random_col(board) 
      return ship2_row 
      return ship2_col 
      check_ship2() 

    def check_ship3(): 
     if ship3_row == ship1_row and ship3_col == ship1_col: 
      ship3_row = random_row(board) 
      ship3_col = random_col(board) 
      return ship3_row 
      return ship3_col 
      check_ship3() 
     elif ship3_row == ship2_row and ship3_col == ship2_col: 
      ship3_row = random_row(board) 
      ship3_col = random_col(board) 
      return ship3_row 
      return ship3_col 
      check_ship3() 

    def restart(): 
     again = input("Do You want to play again?(Y/N)") 
     if again == "Y" or again == "y": 
      print("\n" * 80) 
      game() 
     elif again == "N" or again == "n": 
      print("Thanks for playing!") 
      exit() 
     else: 
      print("Sorry that wasn't Y or N, try again") 
      restart() 

    for turn in range(1,16): 
     guess_row = int(input("Guess Row with a number 0-9:")) 
     guess_col = int(input("Guess Col with a number 0-9:")) 
     print("\n" * 80) 
     if (guess_row == ship1_row and guess_col == ship1_col) or (guess_row == ship2_row and guess_col == ship2_col) or (guess_row == ship3_row and guess_col == ship3_col): 
      board[guess_row][guess_col] = "S" 
      print("Congratulations! You sank my battleship!") 
      hit += 1    
     else: 
      if (guess_row < 0 or guess_row > 9) or (guess_col < 0 or guess_col > 9): 
       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" 
      if turn == 15: 
       print("Game Over") 
       board[ship1_row][ship1_col] = "S" 
       board[ship2_row][ship2_col] = "S" 
       board[ship3_row][ship3_col] = "S" 
       print_board(board) 
       print("The first ship was at row number " + str(ship1_row) + " and column number " + str(ship1_col)) 
       print("The second ship was at row number " + str(ship2_row) + " and column number " + str(ship2_col)) 
       print("The third ship was at row number " + str(ship3_row) + " and column number " + str(ship3_col)) 
       print(restart()) 
     if hit == 3: 
      print("You sank all of my battleships!") 
      print("You Win!") 
      print_board(board) 
      restart()  
     print ("Turn " + str(turn + 1)) 
     print_board(board) 
    game() 

回答

0

喜看出來!在你的代碼中,你正在做的事情真的是錯誤的:

如果你返回一個值就是它......所以它是沒有意義的,繼續增加的行。請檢查如何將您的船通過該計劃。

return ship3_row 
return ship3_col 
check_ship3() 

當然,選擇在哪裏放船的好選擇是使用一個列表,在那裏存儲船舶的所有點。

如果這個人正在找到一艘船的所有點......那麼這艘船就會被銷燬。 希望這個幫助有點...祝你有美好的一天!

+0

感謝修復和想法,但我對編碼還是有點新,所以我不確定如何將這些船放在列表中。你能告訴我一個例子或者像解釋我怎麼做嗎? – 19jam04

+0

嗨...如果你需要一個正在工作的代碼,然後在這裏使用一個http://code.activestate.com/recipes/578836-the-game-of-battleships-in-python/這是作爲一個工作很多其他的例子,你可以很容易地找到。 –

+0

如果我是一個更好的主意,可以從頭開始,也可以學習使用畫布和圖形部件的TK模塊......這有點難,但它給了你一個編碼的好主意: ) –