0

Sorry I just realise that made not sense. I have created a program in python similar to battleships. I have created the class section which were working when I include the direction section in the main and not as a seperate method, However when I move the method into a seperate section and ask the function to be called, I cannot get my method to call into my main function now for the 2 ships to battle Can someone please tell me what I am doing wrong as I cannot get it working and am getting very frustrated....Thank-you

import random

class Tank1 (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 Tank2 (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 determine(directionx,y):
    t1 = Tank1()
    t2 = Tank2()
    g = 10
    if (direction =="up"):
        t.SetY(move)
        if t.ycord>10:
            t.ycord = (2*g - t.ycord)
    elif (direction == "down"):
        t.SetY(-move)
        if t.ycord<-10:
            t.ycord = (-2*g) - t.ycord
    elif (direction == "right"):
        t.SetX(move)
        if t.xcord>10:
            t.xcord= (2*g - t.xcord)
    else:
        t.SetX(-move)
        if t.xcord<-10:
            t.xcord= (-2*g) - t.xcord
    t.xcord,t.ycord()
    print move,"\t", direction,"\t ",  t.xcord,"\t ",    t.ycord,"\t",    t.arm,"\t",  t.fire
    i = i + 1

def main():
    t = Tank()
    i = 1
    g = 10
    print "number",  "direction", "xValue", "yvalue", "Amour", "firepower"
    while i <= 50:
        d = Die()
        dire = Direction()
        move = d.roll()
        direction = dire.direct()
    determine(t.xcord,t.ycord)
    t.xcord,t.ycord = determine(directionx,y)



if __name__ == "__main__":
     main()

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by jrcagle
0

First, please use the [code="Python"] [/code] tags to enclose your code. That will preserve your indentation.

More importantly, your code:

The first thing that strikes me is that your Tank1() and Tank2() classes contain exactly the same code. Therefore, you don't need both. Instead, you can do this:

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

...

def main():
    t1 = Tank()
    t2 = Tank()
...

Now, you have two discrete objects (t1,t2) that follow the same plan (class Tank). I think you were confusing the class with the actual object.

Second, I don't understand the purpose of the Direction class. It seems to me that the use for a direction is to tell the tank to move. But 'move' is a verb -- and therefore should be a method of the Tank() class.

That is, if your object can do action X (like "move"), then X should be a method of the class.

So something like this:

class Tank(object):

   ...

   def movetowards(self, target):
       """move self towards target"""
       if target.GetX() < self.GetX():
            self.SetX(-1)
       elif target.GetX() > self.GetX():
            self.SetX(+1)
       if target.GetY() < self.GetY():
            self.SetY(-1)
       elif target.GetY() > self.GetY():
            self.SetY(+1)

I don't know if that's exactly the behavior you want, but you can modify it to suit your game rules.

Speaking of game rules, I strongly encourage you to put the rules for the game to paper before you code them up. Doing so will help you be clearer about the code.

Jeff

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.