把队伍放在正确的位置 - Python项目

时间:2013-12-13 15:27:12

标签: python inheritance

我正在做一个Python项目,其目标是让一支部队(士兵,升级士兵和坦克)相互攻击。

我为长代码道歉,但是当我快要结束时遇到了问题,希望你能理解:>

该项目的目标是使用类继承和许多其他东西。 我的档案:

PointLine.py:

import math

class Point(object):
    #includes coordiantes - x and y
    def __init__(self, X, Y):
        self._X = X
        self._Y = Y
    def __repr__(self):
        return "X = " + str(self._X) + "Y = " + str(self._Y)
class Line(Point):
    #consists of two points
    def __init__(self, point1,point2):
        if point1 != None and point2 != None:
            self._point1 = point1
            self._point2 = point2
        else:
            myLine = None
    def __repr__(self):
        return "X1 = " + str(point1._X) + " X2 = " + str(point2._X) + " Y1 = " + str(point1._Y) + " Y2 = " + str(point2._Y)
    def arentPointsEqual(self, point1, point2):
        if point1._X != point2._X and point1._Y != point2._Y:
            myLine = Line(point1, point2)
        else:
            print "ERROR ! Two points are equal"
            return False
    def returnLength(self):
        Xs = math.pow((self._point2._X - self._point1._X), 2)
        Ys = math.pow((self._point2._Y - self._point1._Y), 2)
        beforeSqrt = Xs + Ys
        return math.sqrt(beforeSqrt)

Troops.py:

try:
    from PointLine import Point, Line
except ImportError:
    print "One of your libraries hasn't been imported, please try again later"
else:
    class Soldier:
        def __init__(self, p, player):
            self.posX = p._X
            self.posY = p._Y
            self.playerNum = player
        def __repr__(self):
            if self.playerNum == 1:
                return "First's Player soldier"
        else:
                return "Second's player soldier"
        cost = 5
        fireRange = 1
        damage = 2
        health = 10
        instances = 0
        def returnCost(self):
            return self.cost
        def returnDamage(self):
            return self.damage
        def returnHealth(self):
            return self.health
    class UpgradedSoldier:
        def __init__(self, p, player):
            self.posX = p._X
            self.posY = p._Y
            self.playerNum = player
        def __repr__(self):
            if self.playerNum == 1:
                return "First's Player upgraded soldier"
        else:
                return "Second's player upgraded soldier"
        cost = 10
        fireRange = 5
        damage = 5
        health = 15
        instances = 0
        def returnCost(self):
            return self.cost
        def returnDamage(self):
            return self.damage
        def returnHealth(self):
            return self.health
    class Tank:
        def __init__(self, p, player):
            self.posX = p._X
            self.posY = p._Y
            self.playerNum = player
        def __repr__(self):
            if self.playerNum == 1:
        return "First's Player tank"
        else:
                return "Second's player tank"
        cost = 20
        fireRange = 10
        damage = 20
        health = 50
        instances = 0
        def returnCost(self):
            return self.cost
        def returnDamage(self):
            return self.damage
        def returnHealth(self):
            return self.health

Game.py:

try:
    from abc import ABCMeta, abstractmethod
    from PointLine import Point, Line
except RuntimeError:
    print "There was an error importing your libraries, please try again later"
else:
    class Game:
        __metaclass__ = ABCMeta
        def move(self, p):
            self._point._X += p._X
            self._point._Y += p._Y
        def attack(self, p, toAttack = None):
            x = self._point._X
            y = self._point._Y
            p1 = Point(x,y)
            l = Line(p, p1)
            if l.returnLength() > self.fireRange:
                print "You have to move in order to attack, moving you now"
                p1._X += 1
                p1._Y += 1
            else:
                if self.isAttackingBool == True:
                    toStop = raw_input("You are already attacking, do you wish to stop or to continue ? (y/n) : ")
                    if toStop == 'y' or toStop == 'Y':
                        self.isAttackingBool = False
                    elif toStop == 'n' or toStop == 'N':
                        pass
                    else:
                        print "Invalid letter entered, please try again later"
                else: #If currently NOT attacking
                    if toAttack != None:
                        toAttack.health -= self.damage
                        print "The unit you just attacked has " + str(toAttack.health) + " health left"
                    else: #If toAttack is None
                        print "An error occured, please try again later"
        def stopAttack(self):
            self.isAttackingBool = False
        def isAttacking(self):
            return self.isAttackingBool
        @staticmethod
        def returnCost(self):
            pass
        @staticmethod
        def returnDamage(self):
            pass
        @staticmethod
        def returnHealth(self):
            pass

Map.py:

try:
    from Game import Game
    from PointLine import Point, Line
    from Troops import Soldier, UpgradedSoldier, Tank
except ImportError:
    print "One of your libraries hasn't been imported, please try again later"
else:
    class Map(Soldier, UpgradedSoldier, Tank):
        moneyStart = 30
        numberOfPlayer = 2
        board = [[0] * 10] * 10
        moneyFirst, moneySecond = moneyStart, moneyStart
        while moneyFirst > 0:
            print "Player 1"
            print "This is your board : "
            for i in range(len(board)):
                print board[i]
            print "You have " + str(moneyFirst) + "$ left"
            if moneyFirst > 20:
                print "You can afford anything"
            elif moneyFirst > 10:
                print "You can afford a regular soldier and an upgraded soldier"
            elif moneyFirst > 5:
                print "You can afford just the regular soldier"
            else:
                print "You can't afford anything, moving on to player 2"
                break
            try:
                whatToBuy = int(raw_input("Please enter what do you want to buy\n1 for a Soldier\n2 for an Upgraded Soldier\n3 for a Tank\n4 to stop buying\nPlease enter your choise: "))
                if whatToBuy < 0 or whatToBuy > 4:
                    print "Invalid number entered, better luck next time!"
                else:
                    if whatToBuy == 1:
                        x = int(raw_input("Please enter the x of your soldier : "))
                        y = int(raw_input("Please enter the y of your soldier : "))
                        p = Point(x,y)
                        if ((x >= 0) and (x < 11) and (y >= 0) and (y < 11)):
                            if board[x][y] == 0:
                                board[x][y] = Soldier(p, 1)
                                moneyFirst -= 5
                            else:
                                print "The point on the board is already taken"
                        else:
                            print "Invalid number entered, better luck next time!"
                    elif whatToBuy == 2:
                        x = int(raw_input("Please enter the x of your soldier : "))
                        y = int(raw_input("Please enter the y of your soldier : "))
                        p = Point(x,y)
                        if ((x >= 0) and (x < 11) and (y >= 0) and (y < 11)):
                            if board[x][y] == 0:
                                board[x][y] = UpgradedSoldier(p,1)
                                moneyFirst -= 10
                            else:
                                print "The point on the board is already taken"
                        else:
                            print "Invalid number entered, better luck next time!"
                    elif whatToBuy == 3:
                        x = int(raw_input("Please enter the x of your soldier : "))
                        y = int(raw_input("Please enter the y of your soldier : "))
                        p = Point(x,y)
                        if ((x >= 0) and (x < 11) and (y >= 0) and (y < 11)):
                            if board[x][y] == 0:
                                board[x][y] = Tank(p,1)
                                moneyFirst -= 20
                            else:
                                print "The point on the board is already taken"
                        else:
                            print "Invalid number entered, better luck next time!"
                    elif whatToBuy == 4:
                        print "Moving to player 2"
                        break
            except ValueError:
                print "Error parsing the data you've entered, better luck next time!"

问题是:当我尝试在棋盘上放置一些部队时(无论哪一个都没关系)它只是将它放到给定X中的整个列表中。

例如:

输入x = 1时,y = 1:

[0, First's Player soldier, 0, 0, 0, 0, 0, 0, 0, 0]
[0, First's Player soldier, 0, 0, 0, 0, 0, 0, 0, 0]
[0, First's Player soldier, 0, 0, 0, 0, 0, 0, 0, 0]
[0, First's Player soldier, 0, 0, 0, 0, 0, 0, 0, 0]
[0, First's Player soldier, 0, 0, 0, 0, 0, 0, 0, 0]
[0, First's Player soldier, 0, 0, 0, 0, 0, 0, 0, 0]
[0, First's Player soldier, 0, 0, 0, 0, 0, 0, 0, 0]
[0, First's Player soldier, 0, 0, 0, 0, 0, 0, 0, 0]
[0, First's Player soldier, 0, 0, 0, 0, 0, 0, 0, 0]
[0, First's Player soldier, 0, 0, 0, 0, 0, 0, 0, 0]

请帮我解决此问题。

Iliya:&gt;

1 个答案:

答案 0 :(得分:1)

由于您在设置电路板时使用了可变对象(列表)的方式,这是一个错误。您的电路板的每一行实际上都是对同一个列表的引用,因此您看到的行为。取代

board = [[0] * 10] * 10

board = [[0 for _ in range(10)] for _ in range(10)]