PIbd-22_Bondarenko_M.S._War.../ShipsMix.java

57 lines
1.5 KiB
Java
Raw Normal View History

2022-12-15 22:28:43 +04:00
import java.util.Random;
public class ShipsMix<T extends EntityWarmlyShip, U extends IDrawningDeck>{
public T[] _ships;
public U[] _decks;
int shipsCount = 0;
int deckCount = 0;
String indexX;
String indexY;
public ShipsMix(int countE,int countI){
_ships = (T[]) new EntityWarmlyShip[countE];
_decks = (U[]) new IDrawningDeck[countI];
}
public int Insert(T ship){
if(shipsCount < _ships.length){
_ships[shipsCount] = ship;
++shipsCount;
return shipsCount - 1;
}
return -1;
}
public int Insert(U deck){
if(deckCount < _decks.length){
_decks[deckCount] = deck;
++deckCount;
return deckCount - 1;
}
return -1;
}
public void SetIndexs(int ind1, int ind2)
{
indexX = Integer.toString(ind1);
indexY = Integer.toString(ind2);
}
public DrawingShip MakeShipMix(){
Random random = new Random();
int indEnt = 0;
int indIllum = 0;
if(shipsCount - 1 != 0 && deckCount - 1 != 0){
indEnt = random.nextInt(0,shipsCount - 1);
indIllum = random.nextInt(0, deckCount - 1);
}
T plane = (T)_ships[indEnt];
U illum = (U)_decks[indIllum];
SetIndexs(indEnt,indIllum);
if(plane instanceof EntityMotorShip){
return new DrawningMotorShip(plane,illum);
}
return new DrawingShip(plane,illum);
}
}