2017-08-31 52 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