PIbd21.LyovushkinaA.A.Conta.../ShipGenericDop.java

58 lines
1.8 KiB
Java
Raw Permalink Normal View History

2023-10-24 01:06:14 +04:00
import java.util.Random;
2023-10-24 13:02:54 +04:00
import java.util.ArrayList;
2023-10-24 01:06:14 +04:00
public class ShipGenericDop<T extends EntityShip, U extends IDecksDrawing> {
2023-10-24 13:02:54 +04:00
private ArrayList<T> Ships;
private ArrayList<U> Decks;
2023-10-24 01:06:14 +04:00
private int countShips;
private int max_countShips;
private int countDecks;
private int max_countDecks;
private Random rand;
private int _pictureWidth;
private int _pictureHeight;
public ShipGenericDop(int _max_countShips, int _max_countDecks, int pictureWidth, int pictureHeight){
max_countShips = _max_countShips;
max_countDecks = _max_countDecks;
2023-10-24 13:02:54 +04:00
Ships = new ArrayList<T>(max_countShips);
Decks = new ArrayList<U>(max_countDecks);
2023-10-24 01:06:14 +04:00
countShips = 0;
countDecks = 0;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
rand = new Random();
}
2023-11-03 23:38:13 +04:00
public boolean Add(T ship){
2023-10-24 01:06:14 +04:00
if(ship == null){
return false;
}
if (countShips > max_countShips){
return false;
}
2023-10-24 13:02:54 +04:00
Ships.add(countShips++, ship);
2023-10-24 01:06:14 +04:00
return true;
}
2023-11-03 23:38:13 +04:00
public boolean Add(U deck){
2023-10-24 01:06:14 +04:00
if(deck == null){
return false;
}
if (countDecks > max_countDecks){
return false;
}
2023-10-24 13:02:54 +04:00
Decks.add(countDecks++, deck);
2023-10-24 01:06:14 +04:00
return true;
}
public DrawingShip getObjectRandomShip(){
if(countShips == 0 || countDecks == 0){
return null;
}
int i = rand.nextInt(countShips);
int j = rand.nextInt(countDecks);
2023-10-24 13:02:54 +04:00
if(Ships.get(i) instanceof EntityContainerShip){
return new DrawingContainerShip((EntityContainerShip)Ships.get(i), (IDecksDrawing)Decks.get(j), _pictureWidth, _pictureHeight);
2023-10-24 01:06:14 +04:00
}
else{
2023-10-24 13:02:54 +04:00
return new DrawingShip((EntityShip)Ships.get(i),_pictureWidth, _pictureHeight);
2023-10-24 01:06:14 +04:00
}
}
}