Python中简单的战舰游戏实现 [英] Simple Battleships game implementation in Python

查看:28
本文介绍了Python中简单的战舰游戏实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我不知道如何为计算机本身开发另一个带有隐藏空间的板,并测试命中率.同样,我什至不确定我将如何测试我现在拥有的棋盘上的命中率.请注意:玩家转弯功能将迁移到计算机板上,因为您不会攻击自己的船只.这是代码.它可能不是最好的格式(如方法和对象等),但我可以稍后对其进行改进.还有另一种方法可以将所有船只放在一个功能中吗?或者按照我的方式,它是否必须保持这种状态?

Okay I'm not sure how to develop another board with hidden spaces for the computers ships per-se, and have it test for hits. Again I'm not even sure how I'm going to test for hits on the board I have now. Make note: The player turn function will be migrated to the computer board since you wouldn't be attacking your own ships. Here is the code. It may not be the best formatting (as in with Methods and objects and such) but I can polish it up a little later. Also would there be another way to make placing the ships all in one function? Or with the way I have it, will it have to stay that way?

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
"
    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 "
If 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 "
If placing horizontally you can only choose 1-7 for a column."
            elif self.battleshipRow <= 0 or self.battleshipRow > 10:
                print "
 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 "
If 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 "
If placing horizontally you can only choose 1-6 for a column."
            elif self.battleshipRow <= 0 or self.battleshipRow > 10:
                print "
 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 "
If 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 "
If placing horizontally you can only choose 1-8 for a column."
            elif self.battleshipRow <= 0 or self.battleshipRow > 10:
                print "
 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 "
If 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 "
If placing horizontally you can only choose 1-8 for a column."
            elif self.battleshipRow <= 0 or self.battleshipRow > 10:
                print "
 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 "
If 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 "
If placing horizontally you can only choose 1-9 for a column."
            elif self.battleshipRow <= 0 or self.battleshipRow > 10:
                print "
 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()

推荐答案

您需要大量的代码组织.我建议让 Classes 免于任何类型的循环或输入!来自用户的输入内容 &然后将其添加到类实例中,而不是相反.组织您的代码 &向其中添加文档,以便其他人可以帮助您.

You need a lot of code organisation. I would suggest keeping Classes free from any sort of looping or inputs! Input stuff from the user & then add that to the class instance, not the other way round. Organize your code & add documentation to it so that others can help you.

你可以做一些这样的事情

You can do some stuff like this

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

这篇关于Python中简单的战舰游戏实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆