Can someone please tell me what I am doing wrong with this loop. I want to end the loop when either one of the tanks armour reaches zero. Also if I wanted to add more tanks how can I have the loop end? Thanks

import random

class Tank (object):
    def __init__(self):
        self.xcord = 0
        self.ycord = 0
        self.arm = 10
        self.fire = 1
    def GetX (self):
        return self.xcord
    def GetY (self):
        return self.ycord
    def GetArm (self):
        return self.arm
    def GetFire (self):
        return self.fire
    def SetX (self , move):
        self.xcord = self.xcord + move
    def SetY (self , move):
        self.ycord = self.ycord + move
    def SetArm (self , n):
        self.arm = self.arm + n
    def SetFire (self, n):
        self.fire = self.fire + n

class Die (object):
    def __init__(self):
        self.value = 1

    def roll(self):
        self.value = random.randrange(1,7)
        return self.value

class Direction (object):
    def __init__(self):
        self.value = " "
    def direct(self):
        dir_num =random.randrange(1,5)
        dic={"1":"up","2":"down","3":"right","4":"left"}
        return dic[str(dir_num)]

def dospecial(t):
    if t.xcord == 3 and t.ycord == 3:
        t.SetArm(10)
    elif t.xcord == -3 and t.ycord == -3:
        t.SetFire(1)

def gridmove(direction,t, move):
    g = 10
    if (direction =="up"):
        t.SetY(move)
        if t.ycord>10:
            t.ycord = (2*g - t.ycord)
            print "bounce"
    elif (direction == "down"):
        t.SetY(-move)
        if t.ycord<-10:
            t.ycord = (-2*g) - t.ycord
            print "bounce"
    elif (direction == "right"):
        t.SetX(move)
        if t.xcord>10:
            t.xcord= (2*g - t.xcord)
            print "bounce"
    else:
        t.SetX(-move)
        if t.xcord<-10:
            t.xcord= (-2*g) - t.xcord
            print "bounce"

def main():
    t1 = Tank()
    t2 = Tank()
    i = 1
    g = 10
    print "tank", "number",  "direction", "xValue", "yvalue", "Amour", "firepower"
    while [(t1.arm==0) or (t2.arm==0)]:
    #while i <= 50: 
        d1 = Die()
        dire1 = Direction()
        move1 = d1.roll()
        direction1 = dire1.direct()
        gridmove(direction1, t1, move1)
        dospecial(t1)
        print"\ntank 1","\t", move1,"\t", direction1,"\t ",  t1.xcord,"\t ",    t1.ycord,"\t",    t1.arm,"\t",  t1.fire

        d2 = Die()
        dire2 = Direction()
        move2 = d2.roll()
        direction2 = dire2.direct()
        gridmove(direction2, t2, move2)
        dospecial(t2)
        print"tank 2","\t", move2,"\t", direction2,"\t ",  t2.xcord,"\t ",    t2.ycord,"\t",    t2.arm,"\t",  t2.fire

        if t1.xcord==t2.xcord and t1.ycord==t2.ycord:
            t1.arm -= t2.fire
            t2.arm -= t1.fire
            print"\nBattle happened! tank 1 changed","\t ", "arm: ", t1.arm,"\t",  "fire: ", t1.fire
            print"\n \t tank 2 changed","\t ", "arm: ", t2.arm,"\t",  "fire: ", t2.fire
        #i = i + 1

if __name__ == "__main__":
     main()

Recommended Answers

All 2 Replies

Please, use (code=python)(/code) tags around your code to make it readable...

Change

while [(t1.arm==0) or (t2.arm==0)]:

to

while (t1.arm > 0) or (t2.arm > 0):
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.