Unknown's avatar

About Bruce Ryan

https://about.me/bruce.ryan

bug-hunt!

When Mood Music
2012-04-17 22:25:00 frustrated The Robots – Kraftwerk

This morning I traced the ‘not working when recalled from saved state’ to the code that that calculates where the ogre is. (This is used to decide whether there should be a battle by counting the number of enemies at the ogre’s location. If there are none, no battle ensues. If there is only 1 enemy, there is a battle which the ogre wins and the enemy is removed. If there is more than 1 enemy, the enemies win, the ogre is removed and the game ends.)

During a brand-new game, the ogre-location method works fine. In a recalled-from-disk game, it doesn’t. The code in question (and rather questionable code) is in two pieces, one for the x-coordinate and one for the y-coordinate. Here’s the x-coordinate code:

public int ogreXcoord() {
    //Declare variable – it needs to be initialised, so use a silly number
    int ogreXCoordinate = 2000;

    //Code
    for (Creature tempCreature : creaturesInSwamp) {
        if (tempCreature.getType() == OGRE ) {
ogreXCoordinate = tempCreature.getxCoord();
} //end if
} //end for-each

    return ogreXCoordinate;
} //end ogreXcoord

The green bits are just comments to help humans read the code. The pink bits are reserved words, i.e words that have special meanings to Java and the blue bit is an example of an instance variable, i.e. a variable that occurs in every instance of the Swamp class.

The code translates as.
This method, named ‘ogreXcoord’ may be called from outside the Swamp class. It will return an integer.
Create an integer variable (which only exists within this method) called ‘ogreXCoordinate’ and sets its value to 2000.
One-by-one, put each Creature in this Swamp’s list of Creatures into a variable called tempCreature.
Then, for each version of tempCreature, if it is an ogre, then set
‘ogreXCoordinate’ to the value of the tempCreature’s x-coordinate.
When you have run through all the creatures and thus definitely have the required xCoordinate, pass this back to the code that called this method.

Why so complex? Only the ogre knows where it is. Each time it moves, it calculates new coordinates for itself and moves to these coordinates. (All Creatures do this, and don’t tell anyone where they are until asked.) So the program doesn’t know where the ogre is until it can ask him. And it can’t ask him where he is until it has found him – and it can only find him by looking through all the creatures in the Swamp and asking them in turn ‘Are you an ogre?’ (I guess this could be shortened by stopping asking once the ogre has been found. And he’s likely to be the first creature in the ‘CreaturesInSwamp’ list because he’s the first creature to be created once the swamp has been created. However, I don’t know that this is guaranteed.)

Anyway, I can show this works when the game is working – the ogreXCoordinate is always right. But in a recalled-from-disk game, this fails – the ogreXCoordinate is always returned as 2000. So the for-each loop is either not working or being ignored.

Bah!!!!!!

Bah humbug

When Mood Music
2012-04-16 17:43:00 confused Save The Day – Diss-cuss

Wierdness: so long as I start a new game each time, the game plays out as it should. However, if I recall a saved game from disk, the creatures appear not to battle and are effectively immortal.

This may or may not be connected with the moveAllCreatures() method in Swamp.class appearing to call moveCreature() in Creature.class at least twice per Creature.

If it were always twice, I’d be a little less confused.

Statistics

When Mood Music
2012-04-16 14:25:00 ground state, i.e. prevaricating Robber Rock – Yabby U

My lecturer tells me ‘All debugging code should be removed. The only exception is when a program can be run in debug mode as per SD1 coursework.’

I’ve saved a copy with the extra methods and commented-out pieces to track events elsewhere, then looked at what’s left after removing these undesirables.

 

Version lines ‘words’ characters characters that are spaces
complete code
340
1834
15,021
2654
comment lines removed,
i.e. just code lines
217
986
8345
1260
%
64
54
55
47
Hence comment lines
123
848
6676
1394
%
36
46
45
43
inline comments (e.g. //end method) removed,
i.e. pure code
217
873
7517
1148
%
64
45
50
43

So I’ve written 217 lines of code for this class of which around 50% is substantive – the rest is spaces and comments to allow humans to read it.

Programming coursework entries

When Mood Music
2012-04-16 12:29:00 sore Rino’s Prayer – Leftfield

With thanks to ggreig for alerting me to this…

It turns out I was using an image format that’s unsuitable for most browsers, so you may have been seeing ‘broken-link’ images or bank spaces instead of screenshots.

I’ve now converted the screenshots to PNG format – this seems to work in all the browsers I have for MacOSX Lion, Ubuntu Linux and Windows XP.

Some more progress

When Mood Music
2012-04-15 22:31:00 content Repeat (Stars And Stripes) – Manic Street Preachers

I’ve got further today than I dared hope: getting the battle code roughly working was my minimum aim. It’s working OK and so I’ve been able to do more. Here’s a checklist:

The remaining tasks are

  • ensuring the ogre doesn’t start at (0,0) That took 3 extra lines, one of which was merely ‘}’
  • reducing the number of times the map is drawn Now just at the very beginning of the game (to show the solitary ogre), then once per turn, then once at the end of the game.
  • creating an interface which is completely independent of the game
  • enabling saving of the current game and later restarting it
  • writing comprehensive JUnit tests for each method (excluding the constructors) defined on the class Swamp
  • writing a report on
    • how I created the swamp
    • where and why I used polymorphic programming
    • my programming style (i.e. showing where and why you have used things like interfaces, enumerator, exceptions etc) There are none of these in the current code, although I suspect I should use an enumerator for the types of enemy.
    • how my swamp is extensible
    • any problems encountered during the coding of my game. Hence these blog entries!

Success!

When Mood Music
2012-04-15 19:24:00 pleased Agog in the Ether – Ozric Tentacles

The basic game does what it should:

Swampy bloodbath!

  1. It creates a swamp creating an ogre in a random position.
  2. An enemy may enter.
  3. All creatures in the swamp move to a proximate location.
  4. Steps 2 and 3 repeat …

    until one or more enemies enter the ogre’s current location.

    • If there is only enemy in the ogre’s current location, it is killed and this happy event is reported.
      Then steps 2 and 3 begin to be repeated.
    • If there are two or more enemies in the creature’s location, the ogre is killed and this sad event is reported.

 

The remaining tasks are

  • ensuring the ogre doesn’t start at (0,0)
  • reducing the number of times the map is drawn
  • creating an interface which is completely independent of the game
  • enabling saving of the current game and later restarting it
  • writing comprehensive JUnit tests for each method (excluding the constructors) defined on the class Swamp
  • writing a report on
    • how I created the swamp
    • where and why I used polymorphic programming
    • my programming style (i.e. showing where and why you have used things like interfaces, enumerator, exceptions etc) There are none of these in the current code, although I suspect I should use an enumerator for the types of enemy.
    • how my swamp is extensible
    • any problems encountered during the coding of my game. Hence these blog entries!

I need to demonstrate my program and submit my report next Tuesday, so I better crack on.

One refinement I could make is to not use both type (‘ogre’ or ‘enemy’) and subtype of enemy (‘donkey’, ‘snake’ and ‘parrot’). I think these could be collapsed into just one type. Then I’d only need one non-abstract class of creature. But this would remove all vestiges of polymorphic programming from my code…

Bah! again

When Mood Music
2012-04-15 17:54:00 bitchy Again and Again – Phil Kieran

I think I have it: it seems I was still trying to take a step while examining the list of creatures in a different place. Back to the full game to find out.