2013-11-24 79 views
-1

好吧我不確定如何爲計算機船舶本身開發隱藏空間的另一塊電路板,並對它進行測試。再次,我甚至不知道我將如何測試我現在的棋盤上的命中。注意:玩家轉向功能將被移植到電腦板上,因爲你不會攻擊自己的船隻。這是代碼。它可能不是最好的格式(如方法和對象等),但稍後可以拋光它。還有另外一種方法可以將船舶全部放在一個功能中嗎?或者以我擁有的方式,它將不得不保持那種方式?Python中的簡易戰列艦遊戲實現

class battleship(object): 

def __init__(self): 
    self.board = [["O"] * 10, ["O"] * 10, ["O"] * 10, ["O"] * 10, ["O"] * 10, ["O"] * 10, ["O"] * 10, ["O"] * 10, ["O"] * 10, ["O"] * 10, ] 
    self.printboard() 
    self.placeAircraftCarrier() 
    self.placeBattleShip() 
    self.placeSubmarine() 
    self.placeDestroyer() 
    self.placePatrolBoat() 
    for i in range(100): 
     self.playerTurn() 


def printboard(self): 
    print "Game Board\n" 
    print "1","2","3","4","5","6","7","8","9","10" 
    for row in self.board: 
     print "|".join(row) 
def placeBattleShip(self): 
    while True: 
     self.vOrHPlacement = input("Would you like to place the Battleship (1) Vertically or (2)Horizontally?:") 
     if self.vOrHPlacement == 1 or self.vOrHPlacement == 2: 
      break 
     else: 
      print "You must press 1 or 2." 
    while True: 
     self.battleshipRow = input("With the head as the starting place what row would you like to place the Battleship (Takes 4 Spaces)?:") 
     self.battleshipCol = input("What column would you like to start the BattleShip at?:") 
     if self.vOrHPlacement == 1: 
      if self.battleshipRow > 7 or self.battleshipRow <= 0: 
       print "\nIf placing vertically you can only choose 1-7 for the row." 
      elif self.battleshipCol <= 0 or self.battleshipCol > 10: 
       print "You must choose 1 - 10 for a column." 
      elif self.board[self.battleshipRow - 1][self.battleshipCol - 1] == "X": 
       print "There is already a ship there." 
      else: 
       self.battleshipRow -= 2 
       self.battleshipCol -= 1 
       for i in range(4): 
        self.board[self.battleshipRow + 1][self.battleshipCol] = "X" 
        self.battleshipRow += 1 
       break 
     elif self.vOrHPlacement == 2: 
      if self.battleshipCol > 7 or self.battleshipCol <= 0: 
       print "\nIf placing horizontally you can only choose 1-7 for a column." 
      elif self.battleshipRow <= 0 or self.battleshipRow > 10: 
       print "\n You must choose 1 - 10 as a row choice." 
      elif self.board[self.battleshipRow - 1][self.battleshipCol - 1] == "X": 
       print "There is already a ship there." 
      else: 
       self.battleshipRow -= 1 
       self.battleshipCol -= 2 
       for i in range(4): 
        self.board[self.battleshipRow][self.battleshipCol + 1] = "X" 
        self.battleshipCol += 1 
       break 
    self.printboard() 
def placeAircraftCarrier(self): 
    while True: 
     self.vOrHPlacement = input("Would you like to place the Aircraft Carrier (1) Vertically or (2)Horizontally?:") 
     if self.vOrHPlacement == 1 or self.vOrHPlacement == 2: 
      break 
     else: 
      print "You must press 1 or 2." 
    while True: 
     self.battleshipRow = input("With the head as the starting place what row would you like to place the Aircraft Carrier (Takes 5 Spaces)?:") 
     self.battleshipCol = input("What column would you like to start the Aircraft Carrier at?:") 
     if self.vOrHPlacement == 1: 
      if self.battleshipRow > 6 or self.battleshipRow <= 0: 
       print "\nIf placing vertically you can only choose 1-6 for the row." 
      elif self.battleshipCol <= 0 or self.battleshipCol > 10: 
       print "You must choose 1 - 10 for a column." 
      elif self.board[self.battleshipRow - 1][self.battleshipCol - 1] == "X": 
       print "There is already a ship there." 
      else: 
       self.battleshipRow -= 2 
       self.battleshipCol -= 1 
       for i in range(5): 
        self.board[self.battleshipRow + 1][self.battleshipCol] = "X" 
        self.battleshipRow += 1 
       break 
     elif self.vOrHPlacement == 2: 
      if self.battleshipCol > 6 or self.battleshipCol <= 0: 
       print "\nIf placing horizontally you can only choose 1-6 for a column." 
      elif self.battleshipRow <= 0 or self.battleshipRow > 10: 
       print "\n You must choose 1 - 10 as a row choice." 
      elif self.board[self.battleshipRow - 1][self.battleshipCol - 1] == "X": 
       print "There is already a ship there." 
      else: 
       self.battleshipRow -= 1 
       self.battleshipCol -= 2 
       for i in range(5): 
        self.board[self.battleshipRow][self.battleshipCol + 1] = "X" 
        self.battleshipCol += 1 
       break 
    self.printboard() 
def placeSubmarine(self): 
    while True: 
     self.vOrHPlacement = input("Would you like to place the Submarine (1) Vertically or (2)Horizontally?:") 
     if self.vOrHPlacement == 1 or self.vOrHPlacement == 2: 
      break 
     else: 
      print "You must press 1 or 2." 
    while True: 
     self.battleshipRow = input("With the head as the starting place what row would you like to place the Submarine (Takes 3 Spaces)?:") 
     self.battleshipCol = input("What column would you like to start the Submarine at?:") 
     if self.vOrHPlacement == 1: 
      if self.battleshipRow > 8 or self.battleshipRow <= 0: 
       print "\nIf placing vertically you can only choose 1-8 for the row." 
      elif self.battleshipCol <= 0 or self.battleshipCol > 10: 
       print "You must choose 1 - 10 for a column." 
      elif self.board[self.battleshipRow - 1][self.battleshipCol - 1] == "X": 
       print "There is already a ship there." 
      else: 
       self.battleshipRow -= 2 
       self.battleshipCol -= 1 
       for i in range(3): 
        self.board[self.battleshipRow + 1][self.battleshipCol] = "X" 
        self.battleshipRow += 1 
       break 
     elif self.vOrHPlacement == 2: 
      if self.battleshipCol > 8 or self.battleshipCol <= 0: 
       print "\nIf placing horizontally you can only choose 1-8 for a column." 
      elif self.battleshipRow <= 0 or self.battleshipRow > 10: 
       print "\n You must choose 1 - 10 as a row choice." 
      elif self.board[self.battleshipRow - 1][self.battleshipCol - 1] == "X": 
       print "There is already a ship there." 
      else: 
       self.battleshipRow -= 1 
       self.battleshipCol -= 2 
       for i in range(3): 
        self.board[self.battleshipRow][self.battleshipCol + 1] = "X" 
        self.battleshipCol += 1 
       break 
    self.printboard() 
def placeDestroyer(self): 
    while True: 
     self.vOrHPlacement = input("Would you like to place the Destroyer (1) Vertically or (2)Horizontally?:") 
     if self.vOrHPlacement == 1 or self.vOrHPlacement == 2: 
      break 
     else: 
      print "You must press 1 or 2." 
    while True: 
     self.battleshipRow = input("With the head as the starting place what row would you like to place the Destroyer (Takes 3 Spaces)?:") 
     self.battleshipCol = input("What column would you like to start the Destroyer at?:") 
     if self.vOrHPlacement == 1: 
      if self.battleshipRow > 8 or self.battleshipRow <= 0: 
       print "\nIf placing vertically you can only choose 1-8 for the row." 
      elif self.battleshipCol <= 0 or self.battleshipCol > 10: 
       print "You must choose 1 - 10 for a column." 
      elif self.board[self.battleshipRow - 1][self.battleshipCol - 1] == "X": 
       print "There is already a ship there." 
      else: 
       self.battleshipRow -= 2 
       self.battleshipCol -= 1 
       for i in range(3): 
        self.board[self.battleshipRow + 1][self.battleshipCol] = "X" 
        self.battleshipRow += 1 
       break 
     elif self.vOrHPlacement == 2: 
      if self.battleshipCol > 8 or self.battleshipCol <= 0: 
       print "\nIf placing horizontally you can only choose 1-8 for a column." 
      elif self.battleshipRow <= 0 or self.battleshipRow > 10: 
       print "\n You must choose 1 - 10 as a row choice." 
      elif self.board[self.battleshipRow - 1][self.battleshipCol - 1] == "X": 
       print "There is already a ship there." 
      else: 
       self.battleshipRow -= 1 
       self.battleshipCol -= 2 
       for i in range(3): 
        self.board[self.battleshipRow][self.battleshipCol + 1] = "X" 
        self.battleshipCol += 1 
       break 
    self.printboard() 
def placePatrolBoat(self): 
    while True: 
     self.vOrHPlacement = input("Would you like to place the Patrol Boat (1) Vertically or (2)Horizontally?:") 
     if self.vOrHPlacement == 1 or self.vOrHPlacement == 2: 
      break 
     else: 
      print "You must press 1 or 2." 
    while True: 
     self.battleshipRow = input("With the head as the starting place what row would you like to place the Patrol Boat (Takes 2 Spaces)?:") 
     self.battleshipCol = input("What column would you like to start the Patrol Boat at?:") 
     if self.vOrHPlacement == 1: 
      if self.battleshipRow > 9 or self.battleshipRow <= 0: 
       print "\nIf placing vertically you can only choose 1-9 for the row." 
      elif self.battleshipCol <= 0 or self.battleshipCol > 10: 
       print "You must choose 1 - 10 for a column." 
      elif self.board[self.battleshipRow - 1][self.battleshipCol - 1] == "X": 
       print "There is already a ship there." 
      else: 
       self.battleshipRow -= 2 
       self.battleshipCol -= 1 
       for i in range(2): 
        self.board[self.battleshipRow + 1][self.battleshipCol] = "X" 
        self.battleshipRow += 1 
       break 
     elif self.vOrHPlacement == 2: 
      if self.battleshipCol > 9 or self.battleshipCol <= 0: 
       print "\nIf placing horizontally you can only choose 1-9 for a column." 
      elif self.battleshipRow <= 0 or self.battleshipRow > 10: 
       print "\n You must choose 1 - 10 as a row choice." 
      elif self.board[self.battleshipRow - 1][self.battleshipCol - 1] == "X": 
       print "There is already a ship there." 
      else: 
       self.battleshipRow -= 1 
       self.battleshipCol -= 2 
       for i in range(2): 
        self.board[self.battleshipRow][self.battleshipCol + 1] = "X" 
        self.battleshipCol += 1 
       break 
    self.printboard() 
def playerTurn(self): 
    while True: 
     self.row = input("What row coordinate would you like to hit?:") 
     self.column = input("What column coordinate would you like to hit?") 
     if self.row > 10 or self.row < 0: 
      print "You must pick a row coordinate between 1 and 10." 
     elif self.column > 10 or self.column < 0: 
      print "You must pick a column coordinate between 1 and 10." 
     elif self.board[self.row - 1][self.column - 1] == "*": 
      print "You have already hit there." 
     else: 
      self.board[self.row - 1][self.column - 1] = "*" 
      break 
    self.printboard() 

b = battleship() 
+0

我想也許我應該讓板對象和船隻對象,但我不知道我怎麼能實現它們進入遊戲板。基本上每個安置方法都是一樣的,只需稍稍改變一下船長就可以了。 –

+0

使用[編輯](http://stackoverflow.com/posts/20179953/edit)功能。 –

回答

0

您需要大量的代碼組織。我建議保持類不受任何循環或輸入的影響!輸入用戶&的內容,然後將其添加到類實例中,而不是相反。組織您的代碼&添加文檔,以便其他人可以幫助您。

你可以做一些這樣的東西

class BattleShip: 
    """ Ship object container""" 

    def __init__(self, position_x, position_y, size): 
     """ Necessary variables for the ship go here """ 
     self.position_x = position_x 
     self.position_y = position_y 
     self.size = size 

    def contains(self, position_x, position_y): 
     """ Returns true if supplied point lies inside this ship """ 
     # code 

    def destroy(self, position_x, position_y): 
     """ Destroys the ship's point supplied if it contains it """ 
     assert self.contains(position_x, position_y) 
     # Now update the gameboard 
     # code 

    def isCompletelyDestroyed(self): 
     """ Returns True if ship is completely destoryed else False """ 
     # code 


class GameBoard: 
    """ The container for the ships """ 
    def __init__(self, size): 
     """Initialize clean GameBoard depending on size, etc """ 
     self.occupied = "+" # representation for ships 
     self.destroyed = 'x' # representation for destroyed area 
     self.clear = '-' # representation for clear water 

    def printBoard(self): 
     """ Print the current gameboard state """ 

    def update(self, ship): 
     """ Updates the gameboard according to updated ship """ 


# Now do the mainloop 
while game_not_over: 
    # run game 
+0

謝謝你的建議。就像我說的,我只是CS第一學期的一名非常業餘的程序員。在編寫代碼時,我確實需要立即養成評論的習慣。但是人們對這裏的東西非常苛刻。看起來好像有些人用棍棒推動了他們的屁股。對不起,只是咆哮沒有個人。我並不是說你很苛刻,因爲當我重讀時,我可能會在兩行之間閱讀。但無論如何。非常感謝您提供關於如何設置課程的建議。我不太清楚我將如何獲得董事會完全摧毀的部分。 –

+0

也Idk我將檢查如果船已經存在或沒有,以及如果他們添加一個錯誤的輸入(超出索引錯誤),而沒有在類內循環如何循環回輸入。如果物體按照現在的方式設置。我認爲檢查必須在船級中。或者在其中包含的方法.. –

+0

對於新手來說,它可能*似乎*像StackOverflow上的人有點苛刻,但相信我,情況並非如此。 您不僅需要檢查2艘船是否相同,而且它們也不共享任何點。所以,我建議在開發板上創建一個跟蹤所有佔用點的變量(使用['sets'](http://docs.python.org/2/library/stdtypes.html#set)) 該委員會的徹底毀滅方法應該跟蹤相對於整艘船毀壞的船隻數量。在船毀壞時調用它。 –