用Python实现简单的战舰游戏

用Python实现简单的战舰游戏,python,class,python-2.7,implementation,2d-games,Python,Class,Python 2.7,Implementation,2d Games,好吧,我不知道如何开发另一个带有隐藏空间的板,用于计算机本身,并测试命中率。再一次,我甚至不知道我将如何在我现在拥有的板上测试点击率。注意:玩家回合功能将被移植到电脑板上,因为你不会攻击自己的飞船。这是代码。它可能不是最好的格式(如方法和对象等),但我可以稍后再完善它。还有没有另一种方法可以将所有船只放在一个功能中?或者按照我的方式,它必须保持这种方式吗 class battleship(object): def __init__(self): self.board = [["O"]

好吧,我不知道如何开发另一个带有隐藏空间的板,用于计算机本身,并测试命中率。再一次,我甚至不知道我将如何在我现在拥有的板上测试点击率。注意:玩家回合功能将被移植到电脑板上,因为你不会攻击自己的飞船。这是代码。它可能不是最好的格式(如方法和对象等),但我可以稍后再完善它。还有没有另一种方法可以将所有船只放在一个功能中?或者按照我的方式,它必须保持这种方式吗

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()
级战列舰(目标):
定义初始化(自):
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.placesubside()
自我毁灭者()
自助巡逻艇
对于范围(100)内的i:
self.playerTurn()
def打印板(自):
打印“游戏板\n”
打印“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”
对于self.board中的行:
打印“|”。连接(行)
战列舰(自我):
尽管如此:
self.vOrHPlacement=input(“您希望将战列舰(1)垂直放置还是(2)水平放置?:”)
如果self.vOrHPlacement==1或self.vOrHPlacement==2:
打破
其他:
打印“您必须按1或2。”
尽管如此:
self.battleshipRow=输入(“以头部为起始位置,您希望放置战列舰的哪一行(占用4个空格):”)
self.battleshipCol=input(“您希望在哪个列启动战列舰?:”)
如果self.vOrHPlacement==1:
如果self.battleshipRow>7或self.battleshipRow 7或self.battleshipRow 6或self.battleshipRow 8或self.battleshipRow 8或self.battleshipRow 9或self.battleshipRow 10或self.row<0:
打印“您必须选择1到10之间的行坐标。”
如果self.column>10或self.column<0:
打印“必须选择1到10之间的列坐标。”
elif self.board[self.row-1][self.column-1]==“*”:
打印“你已经击中了。”
其他:
self.board[self.row-1][self.column-1]=“*”
打破
self.printboard()
b=战列舰()

您需要大量的代码组织。我建议保持类不受任何循环或输入的影响!从用户输入内容&然后将其添加到类实例中,而不是相反。组织您的代码并向其中添加文档,以便其他人可以帮助您

你可以做这样的事情

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

我在想也许我应该让boards对象和ships对象,但我不确定如何在游戏板中实现它们。基本上,每种放置方法都是完全相同的,只是船的长度有一些细微的变化。使用该功能。谢谢你的建议。就像我说的,我只是一个非常业余的程序员,只在CS的第一学期。我确实需要养成在编写代码时立即评论的习惯。但男人们对这里的东西真的很苛刻。好像有些人把棍子塞进了屁股。对不起,我没说什么私事。我不是说你很严厉,因为当我重读时,我可能在字里行间读到了。但无论如何。非常感谢您对如何设置课程的建议。我不太确定我将如何得到板上完全破坏的部分。我还想知道我将如何检查船是否已经在那里,以及如果他们添加了错误的输入(索引外错误),而循环不在类内,如何返回到输入。如果对象是按现在的方式设置的。我想那张支票一定是在船舱里。对于新手来说,StackOverflow上的人似乎有点苛刻,但相信我,事实并非如此。您不仅需要检查两艘船是否相同,还需要检查它们是否没有共享任何一点。因此,我建议在董事会中创建一个变量,用于跟踪所有占用点(用于此)。董事会的完全销毁方法应跟踪销毁的船舶数量与总船舶数量。当一艘船被毁的时候,就给它打电话。不要因为失败而气馁,这是系统的工作原理。好的问题会上升到最高层&比其他问题更容易被看到。请看看你的理论,游戏板是战列舰的一个子类。呃,也许我需要更多地使用它,但我不确定如何将ship对象输入到游戏板中,它实际上被读取为一个“变量”,或者可能是另一种方式,战舰是游戏板的一个子类。