2017-08-31 54 views
-2

我試圖讓遊戲採取用戶將有多少船隻等的輸入,並把許多船隻的用戶輸入。我已經把座標放在一個列表中,這就是我如何存儲它們,並檢查它是否是一個命中。但船舶獲得放置在對方,而與此列表的方法,我不知道如何首先檢查他們不重疊,並且所有的第二,改變它,以便他們不是。Python的戰列艦:讓他們有多少船隻要

from random import randint 
    print('Welcome to Battleships for 1 player! Please be careful with entries, as if you get it wrong, you will still lose a go!') 
    print('Good luck!') 
    print('') 
    no_of_goes = int(input("How many goes would you like: ")) 
    size_of_board = int(input("And how big would you like the board to be: ")) 
    if size_of_board > 56: 
     print("That board will be too big to fit on the screen (max 56)") 
     size_of_board = int(input("And choose a more sensible number: ")) 
    no_of_ships = int(input("And finally, how many ships would you like?: ")) 
    board = [] 
    # printing out the board 
    for x in range(size_of_board): 
     board.append(["O"] * size_of_board) 

    def print_board(board): 
     for row in board: 
      print (" ".join(row)) 
    print_board(board) 
    # the lists that will become the locations 
    ship_rows = [] 
    ship_cols = [] 
    # generating random locations 
    def random_row(board): 
      return randint(0, len(board) - 1) 

    def random_col(board): 
      return randint(0, len(board) - 1) 
    # adding locations to lists 
    ships = list(range(no_of_ships)) 
    for i in ships: 
     row = random_row(board) 
     col = random_col(board) 
     ship_rows.append(row) 
     ship_cols.append(col) 
     ## 
     ## And this is my attempt (didn't work) 
     ## 
     for row in ship_cols: 
      if row == ship_cols and col == ship_cols: 
       ship_rows[-1] = random_row(board) 
       ship_cols[-1] = random_col(board)    
    # allowing to see where ships are and logging how many ships have been hit 
    print(ship_rows) 
    print(ship_cols) 
    ship_count = [1] 
    ## I couldn't find a way of ending the game once the ships were sunk, so I stuck in a recursive function (sorry) 
    def printing_stars(): 
     print('You have won!! ' +'*' * 56) 
     printing_stars() 
    for turn in range(no_of_goes): 
     # asking for their guesses 
     print('Turn ' + str(turn + 1) + ' out of ' + str(no_of_goes)) 
     guess_col = int(input("Guess Col:")) - 1 
     guess_row = int(input("Guess Row:")) - 1 
     for item in ship_rows: 
      # If they hit, it gives a '!' 
      if len(ship_count) == no_of_ships and guess_row == item and guess_col == ship_cols[ship_rows.index(item)]: 
       print("You have won!!!! Congratulations") 
       board [guess_row][guess_col] = '!' 
       print_board(board) 
       printing_stars() 

      elif guess_row == item and guess_col == ship_cols[ship_rows.index(item)]: 
       print ("Congratulations! You sunk one of my battleships") 
       board [guess_row][guess_col] = '!' 
       print_board(board) 
       ship_count.append(1) 
       break  
     else: 
      # all misses 
      if (guess_row < 0 or guess_row > size_of_board - 1) or (guess_col < 0 or guess_col > size_of_board - 1): 
       print ("Oops, that's not even in the ocean.") 

      elif board[guess_row][guess_col] == "X" or board[guess_row][guess_col] == "!": 
       print ("You guessed that one already.") 
       turn -= 1 
      else: 
       print ("You missed my battleship!") 
       board[guess_row][guess_col] = "X" 
      print_board(board) 
      if turn == (no_of_goes - 1): 
       print('Game Over') 
       break 

任何想法?將不勝感激:)

+0

這是從codecademy嗎? –

回答

1

一些建議,爲您的代碼:

  • 尺寸:我行j colums =>(我

    你可以播放板與numpy的陣列喜歡這款機型,j)的

  • 0,如果水
  • 1,如果這是由在線播放射水
  • 2如果船unharm
  • 3如果船殼體被擊中

一旦他們不再是2,就意味着有贏家。 有了這個系統,你可以告訴球員,如果他的投籃命中兩次相同的位置,你也可以生成一個顯示屏。

此外,隨機定位船上,你可以使用這個程序:

  • 集船舶大小:假設4個點。
  • 隨機選擇0或1:0至horizo​​ntaly放置船,1放置船verticaly

現在我們來看看如果horizo​​ntaly或verticaly放置。 如果它horizo​​ntaly放置:

  • 選擇0和j-4和0和I 2之間的隨機數。這將是你的船首次發現的座標,這意味着,這個地方轉來2,3點後也。

例有5 * 5板,以及用於水平船舶

00000 
22220 
00000 
00000 
00000 
  • 一個拍攝位置(1,0),我讓你照顧的情況下verticaly,只需要改變隨機數生成指數。

然後該解決方案不會得到上另一船阻止。一個簡單的解決方案,與此循環:

while True: 
    if all ship placed: 
     break 

    pick number 
    Try to place the ship. (with an if statement, check if one of the spot your gonna change is already a 2.) 

該解決方案應該,只要有可能把所有的船在船上工作。如果這是不可能的,你會循環不會結束。此外,如果船的數量在板面尺寸方面不受限制,則可以將它們放置很長時間。

然後,您可以不斷提高方法更復雜的場景。另一種解決方案是在每艘船舶定位之後檢查哪裏仍有放置位置。

+0

非常感謝您的意見!但是如果這將是一場有效的比賽,將2號作爲一艘沒有受到傷害的船將會擊敗猜測它不在哪裏?或者這只是電腦如何看待它,而不是如何在控制檯上顯示 –

+0

當然,你不會那樣表現!對於計算機,您存儲這樣的數據,對於用戶,您創建了一個display_matrix,它將顯示完全相同的內容,但在將0替換爲2時,所以船舶保持隱藏狀態,以便您仍然可以看到您拍攝的位置。 – Mathieu