• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

general irksome headbanging problem!

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi buddies!

Right, I have a problem. I've made a game with 100 squares, and in each square are units. There can be any number of Cavalry and Infantry. I have an abstract class 'FightingUnit' from which Cavalry and Infactry extend. Infantry have a max strength of 1, whereas Cavalry can have 2.

The problem is this! When there is just one cavalry unit left on a square, the total strength is 2. But if that unit loses just 1 strength, he should be left with 1. The unit does infact lose 1 strength but so does every other cavalry unit on the board belonging to that player!!! Below is my code:


The problem is the line I think! It calls the method setStrength which is declared in the FightingUnit class. But it calls it for every Cavalry unit on the entire board, not just the one losing strength

How can I do it so that just the strength value of that one unit changes and not the entire army. The strength must be set correctly as the method used in the interface to display strenght relies on this.

Hope someone can help

Thanks guys

Mike
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I guess you have a 10*10 grid, you should be storing the position of each of your fighting units. Therefore I would expect a method along the lines of adjustFUHealthAt(int [][]position, int unitType, int adjustment).

HTH
[ November 24, 2004: Message edited by: Nigel Browne ]
 
Mike Smike
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm not quite. The board is a single ArrayList and each item in the list contains an ArrayList of Fighting Units, which can be any number of either Cavalry or Infantry. The strenght for each square is calculated by iterating through the Fighting Unit arraylist, and adding up the strengths. Therefore, the strength of a wounded cavalry unit needs to be set lower than a health cavalry unit.

A calvary unit is constructed like this


I need to somehow change the strength for individual units...

Any ideas??
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like you actually have 1 Cavalry object, and that object is being referenced from each square where the player has Cavalry.

What code do you use to create new Cavalry objects?

Sorry if I'm suggesting something you have already looked into.
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest that at each position on your board you create two queues one for cavalry and one for infantry. As damage is done to your queue of cavalry, for example they take 5 damage points, you would check the health of the cavalry unit at the front of your queue, if for example that unit has only one health point left, it is popped off the front of the queue (it dies) and then the health of the next unit on the queue is checked and so on until all the damage points are allocated and units are popped off the front of the queue or it's health is decreased. This will require a method which allows the player to assign which type of unit to apply damage to.
Also it allows for units to be added to the back of the queue but the last unit to take damage will always receive it first.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Simon Birch:
It sounds like you actually have 1 Cavalry object, and that object is being referenced from each square where the player has Cavalry.



Or another possibility is that the "strength" member in FightingUnit is static.
 
Mike Smike
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I do have 1 Cavalry object that is referenced from each square. I create them using the following method for example


That initialises them onto the board. The FightingUnit constructor is as follows :

So if the strength variable is static, how can I change it for just one unit?

Thanks for all your help guys!
 
Simon Birch
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about the delayed response, I suddenly remembered I had a home to go to!!

Anyway, object variables in Java contain references to objects. So two, or more, variables can point to the same object. Modifying the state of the object using one of the variables will be visible by accessing the object through the other variable.

The above code will print out 23 (assuming the SomeObject class has been defined)

So, your code creates some new objects here


The rest of the code uses those objects, and never creates any new ones, so you end up with lots of references to a single object. As soon as you change the strength through one reference, you can see the change through all the other references.

You need to create 1 Cavalry or FootSoldier object for each 'piece' on the board. Try this


Does that make sense?
 
Mike Smike
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah that makes loads of sense!! Thanks for spelling it out so clearly for me. I still seem to have fundamental misunderstandings with Java :roll:

I'll give it a go now.

Thanks again for speedy response and great advice



Mike
 
reply
    Bookmark Topic Watch Topic
  • New Topic