Емкаа
the worst thing about prison was the dementors.
- Член од
- 14 мај 2008
- Мислења
- 5.001
- Поени од реакции
- 12.599
Еве ја класата каде што се креираат и додаваат бродовите
Тука е методата од класата Grid кај шо се повикува методата addShip
Ми враќа -1 за Battleship.
Код:
public class AddShip implements FactoryAdd {
Ship ship;
@Override
public Ship add(Pozicija p, int s, String type, Grid board) {
boolean isHorizontal = (s == 0);
if(type.equals("Battleship"))
{
try
{
if(!board.niza.contains(ship))
{
ship=new Battleship(board,p,isHorizontal);
System.out.println("DODADOV Battle na pozicija "+board.niza.indexOf(ship));
board.niza.add(ship);
}
}
catch (PositionOccupiedException Exception)
{
System.out.println(String.format("Cannot place %s battleship here, position is occupied \n", (isHorizontal? "horizontal" : "vertical")));
}
catch (PositionExceedsBoardException Exception)
{
System.out.println(String.format("Cannot place %s battleship here, ship will not fit on grid \n", (isHorizontal? "horizontal" : "vertical")));
}
}
if(type.equals("Minesweeper"))
{
try
{
if(!board.niza.contains(ship)){
ship=new Minesweeper(board, p, isHorizontal);
board.niza.add(ship);
System.out.println("DODADOV Mine na pozicija "+board.niza.indexOf(ship));
}
}
catch (PositionOccupiedException Exception)
{
System.out.println(String.format("Cannot place %s Minesweeper here, position is occupied \n", (isHorizontal? "horizontal" : "vertical")));
}
catch (PositionExceedsBoardException Exception)
{
System.out.println(String.format("Cannot place %s Minesweeper here, ship will not fit on grid \n", (isHorizontal? "horizontal" : "vertical")));
}
}
if(type.equals("Air"))
{
try
{
if(!board.niza.contains(ship)){
ship=new AircraftCarrier(board,p, isHorizontal);
board.niza.add(ship);
System.out.println("DODADOV Air na pozicija " +board.niza.indexOf(ship));
}
}
catch (PositionOccupiedException Exception)
{
System.out.println(String.format("Cannot place %s Aircraft Carrier here, position is occupied \n", (isHorizontal? "horizontal" : "vertical")));
}
catch (PositionExceedsBoardException Exception)
{
System.out.println(String.format("Cannot place %s Aircraft Carrier here, ship will not fit on grid \n", (isHorizontal? "horizontal" : "vertical")));
}
}
if(type.equals("Submarine"))
{
try{
if(!board.niza.contains(ship)){
ship=new Submarine(board,p, isHorizontal);
board.niza.add(ship);
System.out.println("DODADOV Sub na pozicija "+board.niza.indexOf(ship));
}
}
catch (PositionOccupiedException Exception)
{
System.out.println(String.format("Cannot place %s submarine here, position is occupied \n", (isHorizontal? "horizontal" : "vertical")));
}
catch (PositionExceedsBoardException Exception)
{
System.out.println(String.format("Cannot place %s submarine here, ship will not fit on grid \n", (isHorizontal? "horizontal" : "vertical")));
}
}
if(type.equals("Destroyer"))
{
try{
if(!board.niza.contains(ship)){
ship= new Destroyer(board,p, isHorizontal);
board.niza.add(ship);
System.out.println("DODADOV Des na pozicija "+board.niza.indexOf(ship));
}
}
catch (PositionOccupiedException Exception)
{
System.out.println(String.format("Cannot place %s destroyer here, position is occupied \n", (isHorizontal? "horizontal" : "vertical")));
}
catch (PositionExceedsBoardException Exception)
{
System.out.println(String.format("Cannot place %s destroyer here, ship will not fit on grid \n", (isHorizontal? "horizontal" : "vertical")));
}
}
return ship;
}
}
Тука е методата од класата Grid кај шо се повикува методата addShip
Код:
public boolean addShip(Pozicija p,int s,String type)
{
AddShip factory=new AddShip();
ship=factory.add(p, s, type, this);
if(niza.size()==5)
{
allShipsPlaced=true;
}
if(checkIsShipPlaced(type))
return true;
return false;
}
Ми враќа -1 за Battleship.