Example #1
0
class ConfigTest(unittest.TestCase):
    """
    TestCase for Config.py and some random methods of Config.py
    """

    def setUp(self):
        """
        sets self.config up
        """
        self.config = Config()

    def testRead(self):
        """
        Tests if self.config is filled
        """
        self.assertTrue(self.config is not None)

    def testStaticClass(self):
        """
        tests if self.config.config is the same instance of
        ConfigParser.SafeConfigParser() as Config().config
        """
        self.assertEqual(self.config.config, Config().config)

    def testgetStartEnergy(self):
        """
        Tests if config.__getStartEnergy__ returns the correct StartEnergy
        and the correct Type "int"
        """
        self.assertTrue(self.config.__getStartEnergy__() == 30)
        self.assertEqual(type(self.config.__getStartEnergy__()), int)

    def testgetMovingCosts(self):
        """
        tests if config.__getMovingCosts__() returns the right value
        """
        self.assertEqual(self.config.__getMovingCosts__(7), 2)
        self.assertEqual(type(self.config.__getMovingCosts__(7)), int)
        self.assertTrue(self.config.__getMovingCosts__(6) == 3)

    def testgetMovingCostsException(self):
        """
Example #2
0
    def move(self, worldMap):
        """
        @brief does all essential operations for a beast object to move  
        
        calls 'bewege' on client beast, calculates moving-costs and finally 
        calls 'makeMove' in WorldMap to check of fights occur or food gets eaten and
        update position on worldMap
        """
        self.hidden = False
        whitelistedMoves = (0, 2, 4, 6, 7, 8, 10, 11, 12, 13, 14, 16, 17, 18, 20, 22, 24, '?')
        
        #necessary to prevent move-methods to be called of beasts killen in this ROUND
        if not self.alive:
            #send ten rounds before to beast when it's dead
            self.beast.bewege(';;' + worldMap.getStateTenRoundsBeforeAsString()+';')
            return
            
        destination = self.beast.bewege(str(self.energy) + ';' + worldMap.getEnvironment(self) + ';' + worldMap.getStateTenRoundsBeforeAsString()+';')
                
        if self.sprintCooldown > 0:
            self.sprintCooldown -= 1

        if destination != '?':
            try:
                destination = int(destination)
                if destination not in whitelistedMoves:
                    destination = 12 #beast needs to stay at current position
            except Exception: # if destination contains string/character
                destination = 12
        else:
            self.hidden = True
            
        # destinations where you can sprint
        if destination in (0, 2, 4, 10, 14, 20, 22, 24):
            if self.sprintCooldown == 0:
                self.sprintCooldown = 4
            else:    
                destination = 12 #can't sprint --> stays at old position

        if destination not in (12,'?'):
            destRow = destination / 5 - 2
            destCol = destination % 5 - 2  
            
            #Calculation the new cords incl. wrap around
            newX = worldMap.getWrappedCoordinates(self.x + (destCol), self.y + (destRow))['x']
            newY = worldMap.getWrappedCoordinates(self.x + (destCol), self.y + (destRow))['y']   
           
            #update Position in Worldmap if beast does not stay or hide
            worldMap.makeMove(self.x, self.y, newX, newY)
            self.x = newX
            self.y = newY

        #subtracts the energycosts if it has energy left 
        if self.energy > 0:
            self.energy -= Config.__getMovingCosts__(destination)
        
        # checks if a beast starved and delete it from the WorldMap
        if self.alive == True and self.energy <= 0:
            self.setBeastAsDead()
            worldMap.deleteBeastFromWorld(self.x, self.y)
                
        #Beast Analytics
        if self.beastAnalytics:
            if destination not in whitelistedMoves:
                self.beastAnalytics.madeNotAllowedMove(self.name)
            elif destination in (0, 4, 20, 24):
                self.beastAnalytics.madeDiagonalSprintMove(self.name)
            elif destination in (10, 14):
                self.beastAnalytics.madeHorizontalSprintMove(self.name)
            elif destination in (2, 22):
                self.beastAnalytics.madeVerticalSprintMove(self.name)
            elif destination in (11, 13):
                self.beastAnalytics.madeHorizontalMove(self.name)
            elif destination in (7, 17):
                self.beastAnalytics.madeVerticalMove(self.name)
            elif destination in (6, 8, 16, 18):
                self.beastAnalytics.madeDiagonalMove(self.name)
            elif destination == '?':
                self.beastAnalytics.hide(self.name)