LAB OOP 2009-2010, Jelle Husson: Test 2 (version A)
- When you’re done, tell me. When I’m with you, open up a new email, copy all of your code
in the body of the email.
- Make the subject “Test 2 OOP”
- Send the mail ONLY TO ME. I’ll confirm this by seeing if I received it.
- Leave the room.
- You can write notes on this paper, but you have to hand it in to me when you leave. ASSIGNMENT: MARINES
This exercise models a very simple representation of controlling units in a real-time strategy game like Starcraft. In such a game, a player commands an army of units like soldiers and tanks etc, and controls them on the battlefield to attack the enemy. Let’s assume there’s only one type of unit, a marine.
You’ll be making 2 classes: Marine and Player.
To position him on the battlefield,
a marine has an X and a Y coordinate,
both represented by an int.
Let’s assume these coordinates always are between 0
and 1000. You don’t need to build checks.
Make a constructor that can accept parameters to
set these fields.
Make accessor and mutator methods for these fields. You HAVE to use these if you want to request or change the values. Don’t access the fields directly.
Every player has a name, which is a String.
Every player holds two ArrayLists:
One is called army, which holds all of the player’s marines.
The other one is called selection, and holds the marines currently selected by the player (note that this can be empty, but when it’s not, there are some marines that are both in army and in selection).
Make a simple constructor to initialize the ArrayLists.
Make the following methods:
buildMarine, which creates a new marine at a fixed position and puts it in the army of the player. To balance the gameplay and keep computation cost reasonable, there should be a maximum amount of units that a player can have.
Think about what you need to ensure this.
killMarine, which removes a certain Marine from the army.
makeSelection, which represents the way a player can select a group of marines by dragging a rectangle around them. The rectangle is passed along by specifying the upper left and lower right coordinates (so 4 integers). First the current selection is cleared, then all marines in the army that are within the rectangle are added to the selection. There’s also a maximum amount of units that a selection can contain.
Think about what you need to ensure this. When the selection is already full, no more marines can be added..
deselect, which represents the way a player can deselect a group of marines. This time you don’t do it with a rectangle, but you pass along an ArrayList of marines that you want to remove from the selection.
moveTo, which orders the selected units to move to a certain location (X and Y coords). When the selection is empty, nothing happens. When the selection contains just one unit, that unit moves to the exact spot specified. When the selection contains multiple units, they can’t all stand on the same spot, so the squad distributes itself around it, using some random generated margin.
(In a “real” game, the units would be animated and you would see them gradually walk towards that spot, but here you can make the movement just instantaneous, meaning their old location is just set to their new location).
getClosestMarine Suppose the enemy sends in an enemy unit to attack the player’s
units.That unit will then start firing at the closest marine it sees. Make the method which, given the location of an enemy unit (X and Y coords), returns the Marine object closest to that location (calculate the distance).