首页 > > 详细

辅导Java、Java程序讲解、讲解Java程序、Java语言辅导留学生

This class represents the battlefield where the vehicles interact. It contains a 2D array that represents the battlefield where each element contains one
vehicle. There is a second 2D array that contains a string for each element showing the status of each element in the array. Some requirements to keep
things simple 1. This class should be safe. E.g. copy constructors should be used where ever necessary so that you don't have privacy leaks. 2. When a
vehicle is in range of another vehicle it will fire (total anarchy) 3. A vehicle will fire at all vehicles in range on every turn 4. A vehicle will do the max
damage on every round and every shot (very efficient) Mark Distribution is shown in each method. This should represent the relative difficulty
Hint: knock off the easy stuff first.
Author:
G Stewart Von Itzstein
Field Summary
Modifier and Type Field and Description
private unisa.pf.y2017.sp5.assign02.vehicles.Vehicle[][]map
A 2d array representing the entire battlefield Only one vehicle is
present in any one location.
private java.lang.String[][] range
An array of string representing the battlefield.
private int vehicleCount
Number of vehicles in the game
Constructor Summary
Constructor and Description
Battlefield(int noRows, int noCols)
Constructor.
Method Summary
Modifier and Type Method and Description
void addVehicle(unisa.pf.y2017.sp5.assign02.vehicles.Vehicle vehicle,
int row, int col)
Add a vehicle to the battlespace at row,col position.
void attackRound()
This is the main attack round method.
private static boolean containsUpperCase(java.lang.String string)
Convenience method I wrote to check if a vehicle is in range of another one.
Fields
Constructors
All Methods Static Methods Instance Methods Concrete Methods
22/10/17, 3)20 pmBattlefield
Page 2 of 6file:///Users/stewart/Dropbox/Coding/Workspace/UniSA.PF.2017.SP5.Assign02/doc/unisa/pf/y2017/sp5/assign02/game/Battlefield.html
private void drawSquare(int rowLocation, int colLocation, int radius,
char centerSymbol, char rangeSymbol)
Using range array draw a filled in square centered on a position with the given
symbol.
unisa.pf.y2017.sp5.assign02.vehicles.Vehicle[]getArmedVehicles()
Returns a list of all armed vehicles (copies of) that are currently in the battlefield
Marks 3/50
java.lang.String getRangeMap()
Formats the range map as a string to print out.
unisa.pf.y2017.sp5.assign02.vehicles.Tank[]getTanks()
Returns a list of all the tanks (copies of) that are currently in the battlefield Marks
3/50
unisa.pf.y2017.sp5.assign02.vehicles.VehiclegetVehicleAtPosition(int row, int col)
Return (and remove) the vehicle at the specified position.
int getVehicleCount()
Get the number of vehicles still in play
unisa.pf.y2017.sp5.assign02.vehicles.Vehicle[][]getVehicleMap()
Returns a copy of the vehicle map.
void mapRange()
Redraws the map range array to update for the movement and attack rounds.
void movementRound()
If vehicle at the destination then abort movement If movement will be off the
battlefield then on the first turn stop at the last row/col.
void printRangeMap()
Prints the range map to the console Literally is one line of code.
int totalDamagePotentialOfFlyingVehiclesOnField()
Total firepower of flying vehicles on the board.
int totalDamagePotentialOnField()
Total firepower on the board.
int totalHPOnBoard()
return the total health of all live vehicles on the battlefield board.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Field Detail
map
private unisa.pf.y2017.sp5.assign02.vehicles.Vehicle[][] map
A 2d array representing the entire battlefield Only one vehicle is present in any one location.
range
private java.lang.String[][] range
An array of string representing the battlefield. Each element contains either an upper case letter representing the vehicle in that space E.g. F for
fighter. Or a lower letter representing that this element is in range of another vehicles weapons. E.g. f for a fighter that is threatening that element.
Finally, if the vehicle in this cell is threatened by another vehicle the vehicle symbol should be enclosed with [], if the vehicle is unarmed it should
have () around it. E.g. an ambulance would have (+). The () will overwrite the [] so you wouldn't have [(+)]. For example a [T]f means that a Tank
in this cell is threatened by a fighter near by.
vehicleCount
22/10/17, 3)20 pmBattlefield
Page 3 of 6file:///Users/stewart/Dropbox/Coding/Workspace/UniSA.PF.2017.SP5.Assign02/doc/unisa/pf/y2017/sp5/assign02/game/Battlefield.html
private int vehicleCount
Number of vehicles in the game
Constructor Detail
Battlefield
public Battlefield(int noRows,
int noCols)
Constructor. Creates the vehicle map and the range map.
Parameters:
noRows - the number of rows for the battlefield
noCols - the number of columns for the battlefield
Method Detail
getVehicleMap
public unisa.pf.y2017.sp5.assign02.vehicles.Vehicle[][] getVehicleMap()
Returns a copy of the vehicle map. Marks 3/50
Returns:
a copy of the internal vehicle map
getVehicleCount
public int getVehicleCount()
Get the number of vehicles still in play
Returns:
number of vehicles
totalHPOnBoard
public int totalHPOnBoard()
return the total health of all live vehicles on the battlefield board. Marks 2/50
Returns:
total health
getTanks
public unisa.pf.y2017.sp5.assign02.vehicles.Tank[] getTanks()
Returns a list of all the tanks (copies of) that are currently in the battlefield Marks 3/50
Returns:
a list of all the tanks on the battlefield
getArmedVehicles
public unisa.pf.y2017.sp5.assign02.vehicles.Vehicle[] getArmedVehicles()
22/10/17, 3)20 pmBattlefield
Page 4 of 6file:///Users/stewart/Dropbox/Coding/Workspace/UniSA.PF.2017.SP5.Assign02/doc/unisa/pf/y2017/sp5/assign02/game/Battlefield.html
Returns a list of all armed vehicles (copies of) that are currently in the battlefield Marks 3/50
Returns:
An array of all the armed vehicles in the battlefield
totalDamagePotentialOnField
public int totalDamagePotentialOnField()
Total firepower on the board. Add up all the damage potential from all the vehicles that are currently in play (eg not destroyed). Marks 2/50
Returns:
the total amount of damage that can be done.
totalDamagePotentialOfFlyingVehiclesOnField
public int totalDamagePotentialOfFlyingVehiclesOnField()
Total firepower of flying vehicles on the board. Add up all the damage potential from all the vehicles that can fly that are currently in play (e.g. not
destroyed). Marks 3/50
Returns:
the total amount of damage that can be done.
addVehicle
public void addVehicle(unisa.pf.y2017.sp5.assign02.vehicles.Vehicle vehicle,
int row,
int col)
throws java.lang.Exception
Add a vehicle to the battlespace at row,col position. Should throw an exception if there is already a vehicle there or the row and col value puts it
outside the battlefield Marks 3/50
Parameters:
vehicle - the vehicle to insert into the battlefield
row - the row position in the battlefield
col - the col position in the battlefield
Throws:
java.lang.Exception - if row or col is out of range or if the row,col is already occupied
getVehicleAtPosition
public unisa.pf.y2017.sp5.assign02.vehicles.Vehicle getVehicleAtPosition(int row,
int col)
throws java.lang.Exception
Return (and remove) the vehicle at the specified position. Thrown an exception if there is no vehicle at that position. Marks 2/50
Parameters:
row - the row of the vehicle
col - the col of the vehicle
Returns:
the vehicle removed from the battlefield
Throws:
java.lang.Exception - if there is no vehicle at the position
mapRange
public void mapRange()
22/10/17, 3)20 pmBattlefield
Page 5 of 6file:///Users/stewart/Dropbox/Coding/Workspace/UniSA.PF.2017.SP5.Assign02/doc/unisa/pf/y2017/sp5/assign02/game/Battlefield.html
Redraws the map range array to update for the movement and attack rounds. Marks 7/50
drawSquare
private void drawSquare(int rowLocation,
int colLocation,
int radius,
char centerSymbol,
char rangeSymbol)
Using range array draw a filled in square centered on a position with the given symbol. This is a convenience function to help you draw the range
in the mapRange method. Marks 3/50
Parameters:
rowLocation - the center row location
colLocation - the center col location
radius - how many spots to draw out from this point.
centerSymbol - The center symbol (eg for a fighter you would use F)
rangeSymbol - the range symbol (eg for a fighter you would use f)
containsUpperCase
private static boolean containsUpperCase(java.lang.String string)
Convenience method I wrote to check if a vehicle is in range of another one.
Parameters:
string - the string to check for an uppercase letter.
Returns:
true if the string contains an uppercase letter.
getRangeMap
public java.lang.String getRangeMap()
Formats the range map as a string to print out. Should look like the following see class definition for what it means
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0
8 f fb fb fba fba fbat fbat bat t t t t a String representation of the range map
printRangeMap
public void printRangeMap()
Prints the range map to the console Literally is one line of code. Marks 0/50
attackRound
public void attackRound()
This is the main attack round method. In this method all the vehicles will attack any vehicle that is in range of their weapons. You would do this by
going through each element in the array checking what is in range and then reducing the health on those vehicles by the total amount of damage
the weapon does. We are going to use a square for the range to keep it simple so you only have to worry about doing range calculation on rows and
columns. As an example. If your tank is at 10,10 and its cannon have a range of 2. The tank will attack all vehicles from 8,8 to 12,12. Eg x-range,y-
range to x+range,y+range (think of these as your loop indexes. If your vehicle range goes over the beginning or end of the battlefield it will
truncate the range. E.g. if your tank is at 0,0 then the range is 0,0 to 2,2. This is one of the two main (tricky) methods of part 2. So here are some
hints. 1. Loop through the entire board on each board element check if the vehicle is armed 2. Get the range of the weapon from the vehicle 3.
Then using basic arithmatic approach work out how many elements before the current element you have to check. 4. Get the non truncated case
working first its much easier. 5. Draw out a diagram to figure out what your doing. Its nearly impossible unless you try to visualise your solution.
6. Don't attack yourself. Marks 10 (bonus marks)
movementRound
public void movementRound()
If vehicle at the destination then abort movement If movement will be off the battlefield then on the first turn stop at the last row/col. On the
second call to the method when it its sitting in the last col/row turn around and return the way they came. If there is already a vehicle in that spot
its destroyed and removed from the board. hint: 1. If your moving your vehicles around and systematically moving through the 2d array its
possible you will hit the same vehicle again and end up moving it to the end of the range in one movement round add a boolean flag to your vehicle
to indicate that it has already moved and check that before removing a vehicle. Make sure you move by the movement range (except

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!