I am stuck on the parts of the first code that have <> in place of actual code and having trouble getting testing done at the end of the first code, the testing of the functions that I cannot get to work have being quoted out in Python.
from random import randint
class Tank(object):
"""Tank stores all the methods to modify the properties of the tanks"""
def __init__(self, name="", armour=10, firepower=5):
"""Constructor - sets default values for properties"""
self.x = 0
self.y = 0
self.armour = armour
self.firepower = firepower
self.name = name
def __str__(self):
"""Print the tanks properties"""
return "xpos: " + str(self.x) + " ypos: " + str(self.y) + " armour: " + str(self.armour) + " firepower: " + str(self.firepower)
def getArmour(self):
"""Return armour value"""
return self.armour
def getFirepower(self):
"""Return firepower value"""
return self.firepower
def getLocation(self):
"""Return the location as a tuple"""
return self.x, self.y
def getName(self):
"""Return name"""
return self.name
def getX(self):
"""Return the X coordinate"""
return self.x
def getY(self):
"""Return the Y coordinate"""
return self.y
def increaseArmour(self, amount):
"""Increase armour value by the amount"""
self.armour += amount
def increaseFirepower(self, amount):
"""Increase firepower value by the amount"""
self.firepower += amount
def isAt(self, loc):
"""Check if tank is at location loc, return True or False accordingly"""
return self.getLocation() == loc
def randomMove(self, gridmax):
diceRoll = random.randint(1,7)
directionNumber = random.randrange(1,5)
directions = {1:"up", 2: "down", 3: "left", 4: "right"}
theDirection = directions[directionNumber]
self.move(theDirection, diceRoll, gridmax)
def move(self, direction, amount, gridmax):
"""Moves tanks keeping it inside the grid maximum passed in"""
if direction == "up":
<>
elif direction == "down":
<>
elif direction == "left":
<>
elif direction == "right":
<>
break
def reduceArmour(self, amount):
"""Decrease the armour value by the amount"""
self.armour -= amount
if self.armour < 1:
print ":(", self.name, "is gone :("
def reduceFirepower(self, amount):
"""Decrease the firepower value by the amount"""
self.firepower -= amount
def setArmour(self, a):
"""Set armour value"""
self.armour = a
def setFirepower(self, f):
"""Set firepower value"""
self.firepower = f
def setX(self, x):
"""Set x coordinate"""
self.x = x
def setY(self, y):
"""Set y coordinate"""
self.y = y
#############
## Testing ##
#############
#set Variables for Tank
name = "test"
armour = 20
firepower = 10
amount = 5
print "Testing __init__ .....",
testTank = Tank(name, armour, firepower)
print "Success"
print "Testing __str__ ....."
tankStr = testTank
print "Success"
print "printing tank info: "
print tankStr
print "Testing getArmour .....",
if testTank.getArmour() == armour: #We can test expected outcome
print "Success"
else:
print "Failed"
print "Testing getFirepower .....",
if testTank.getFirepower() == firepower:
print "Success"
else:
print "Failed"
print "Testing getLocation .....",
if testTank.getLocation() == (0,0):
print "Success"
else:
print "Failed"
print "Testing getName .....",
if testTank.getName() == name:
print "Success"
else:
print "Failed"
print "Testing getX .....",
if testTank.getX() == 0:
print "Success"
else:
print "Failed"
print "Testing getY .....",
if testTank.getY() == 0:
print "Success"
else:
print "Failed"
print "Testing increaseArmour .....",
testTank.increaseArmour(amount)
armour += amount #increase expected result
if testTank.getArmour() == armour:
print "Success"
else:
print "Failed"
print "Testing increaseFirepower .....",
testTank.increaseFirepower(amount)
firepower += amount #increase expected result
if testTank.getFirepower() == firepower:
print "Success"
else:
print "Failed"
print "Testing isAt .....",
if testTank.isAt((0,0)):
print "Success"
else:
print "Failed"
print "Testing move .....",
##testTank.move("up", diceroll, gridmax)
##if testTank.move == move:
## print "Success"
##else:
## print "Failed"
print "Testing reduceArmour .....",
##if testTank.reduceArmour() == :
## print "Success"
##else:
## print "Failed"
print "Testing reduceFirepower .....",
#if testTank.reduceFirepower() == :
# print "Success"
#else:
# print "Failed:
print "Testing setArmour .....",
##if testTank.setArmour() == armour:
## print "Success"
##else:
## print "Failed"
print "Testing setX .....",
##if testTank.setX() == x:
## print "Success"
##else:
## print "Failed"
print "Testing setY .....",
##if testTank.setY() == y:
## print "Success"
##else:
## print "Failed"
from random import randint
class Grid(object):
""" Grid is a playing field for tanks. It stores the size and the special squares """
def __init__(self, maxx = 4, bonusA = 2, bonusF = 4, bonusAmt = 5):
""" Constructor - set size and special squares """
self.max = maxx
# set special squares
self.bonusSquares = []
tempLocations = []
# set bonus armour squares
if bonusA:
for x in xrange(bonusA):
loc = randint(-self.max, self.max), randint(-self.max, self.max)
while loc in tempLocations:
loc = randint(-self.max, self.max), randint(-self.max, self.max)
square = loc, "A", bonusAmt
self.bonusSquares.append(square)
tempLocations.append(loc)
# set bonus firepower squares
if bonusF:
for x in xrange(bonusF):
loc = randint(-self.max, self.max), randint(-self.max, self.max)
while loc in tempLocations:
loc = randint(-self.max, self.max), randint(-self.max, self.max)
square = loc, "F", bonusAmt
self.bonusSquares.append(square)
tempLocations.append(loc)
def __str__(self):
squaresInfo = ""
for square in self.bonusSquares:
squaresInfo += "+" + str(square[2])
if square[1] == "F":
squaresInfo += " firepower at "
else:
squaresInfo += " armour at "
squaresInfo += str(square[0]) + ", "
string = "Maximum dimension: " + str(self.max) + " (width & height are " + str(self.max * 2) \
+ ")\nBonus Squares: " + squaresInfo
return string[:-2]
def getMax(self):
return self.max
def getSquare(self, loc):
""" return a single square item given its location """
if self.getLocation == loc:
return bonusA, bonusF, bonusAmt
def landOnBonus(self, loc):
""" Bonus has been landed on so remove it from list, checked by location """
for square in self.bonusSquares:
if square[0] == loc:
self.bonusSquares.remove(square)
def getBonusLocations(self):
""" Return a list of just the locations of the bonus squares """
locations = []
for square in self.bonusSquares:
locations.append(square[0])
return locations
from grid import Grid
from tank import Tank
import random
t1 = Tank("Meanie", 12, 7)
t2 = Tank("CP1200", 80, 5)
t3 = Tank("Wimpy", 1, 2)
tanks = [t1, t2, t3]
playingDimensions = input("Max dimension for playing field: ")
noBonusArmourSquares = input("Number of bonus armour squares: ")
noBonusFirepowerSquares = input("Number of bonus firepower squares: ")
amountBonus = input("Amount of bonus: ")
widthHeight = playingDimensions * 2 + 1
print "The setup:"
print "Maximum dimension:", playingDimensions, "(width and height are " + str(widthHeight) + ")"
print "Bonus squares:", "+" + str(amountBonus) + " armour at",
print "+" + str(amountBonus) + " firepower at"
print "Let's ""Play..."""
#Grid.getBonusLocations()
def deadOrAlive(self):
if self.armour < 1:
print ":(", self.name, "is gone :("
tanks.remove(self)
def moveTanks():
for i in range(len(tanks)):
diceRoll.append(theDice.roll())
theDirection.append(theDir.randomDir())
if theDirection[i] == "up":
tanks[i].set_y(diceRoll[i])
elif theDirection[i] == "down":
tanks[i].set._y(-diceRoll[i])
elif theDirection[i] == "right":
tanks[i].set_x(diceRoll[i])
elif theDirection[i] == "left":
tanks[i].set_x(-diceRoll[i])
def determineCoords():
for i in range(len(tanks)):
if tanks[i].getx() > gridSize:
tanks[i].x = 2 * gridSize - tanks[i].x
elif tanks[i].getx() < -gridSize:
tanks[i].x = -2 * gridSize - tanks[i].x
elif tanks[i].getY() > gridSize:
tanks[i].y = 2 * gridSize - tanks[i].y
elif tanks[i].getY() < - gridSize:
tanks[i].y = -2 * gridSize - tanks[i].y
def doSpecials():
for i in range(len(tanks)):
if tanks[i].getx() == Grid.bonusSquares() and tanks[i].get.y() == Grid.bonusSquares():
tanks[i].set_a(10)
if tanks[i].getx() == Grid.bonusSquares() and tanks[i].get.y() == Grid.bonusSquares():
tanks[i].set.f
def doBattle():
isBattle = 0
theCoords = []
for i in range(len(tanks)):
theCoords.append([tanks[i].x, tanks[i].y])
i = 0
x = 0
battleLocation = []
tanksInvolved = []
for tank in tanks:
pos = i
theCoords[pos] = ""
i += 1
for coord in theCoords:
if tank.Coords() == coord:
battleLocation.append(tank.Coords())
Tank.setArmour(-tanks[x].f)
x += 1
if x == len(tanks):
x = 0
theCoords[pos] = tank.Coords()
break
#Remove duplicates from battle location list
fighting = []
while battleLocation != []:
item = battleLocation.pop(0)
if item not in fighting:
fighting.append(item)
for cord in fighting:
print "- Battle! -", self.name, self.name, "are fighting"
return isBattle
def gameOver():
if len(tanks) <= 1:
print "After", rounds, "rounds, and", noBattles, "battles, the winner is...\n", tanks
def main():
noBattles = 0
#diceRoll = Die()
#theDirections = Direction()
while len(tanks) >= 2:
moveTanks()
determineCoords()
doSpecials()
doBattle()
battle = theBattle()
noBattles += 1
deadCheck()
gameOver()
gridSize = playingDimensions
main()