fixed the code for exercise 8
fixed the code so that it uses a for each loop instead of an for int i and adjusted the part for validating ship overlaping to use the isCollidingWith method of Battleship
This commit is contained in:
parent
7b70666332
commit
46f75188cb
@@ -17,6 +17,7 @@
|
||||
import java.io.IOException;
|
||||
import java.lang.System.Logger.Level;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static pp.battleship.Resources.lookup;
|
||||
import static pp.battleship.game.client.ClientGameLogic.LOGGER;
|
||||
@@ -264,27 +265,13 @@ private boolean checkMapToLoad(ShipMapDTO dto) {
|
||||
}
|
||||
|
||||
// check if ships overlap
|
||||
int[][] tempMap = new int[mapWidth][mapHeight];
|
||||
for (int[] row : tempMap)
|
||||
Arrays.fill(row, 0);
|
||||
dto.getShips().forEach(s -> {
|
||||
int deltaX = s.getMaxX() - s.getMinX();
|
||||
int deltaY = s.getMaxY() - s.getMinY();
|
||||
int lastX = s.getX();
|
||||
int lastY = s.getY();
|
||||
tempMap[lastX][lastY] += 1;
|
||||
for (int i = 0; i < deltaX - 1; i++)
|
||||
for (int j = 0; j < deltaY - 1; j++) {
|
||||
tempMap[lastX + (deltaX / Math.abs(deltaX))][lastY + (deltaY / Math.abs(deltaY))] += 1;
|
||||
lastX = lastX + (deltaX / Math.abs(deltaX));
|
||||
lastY = lastY + (deltaY / Math.abs(deltaY));
|
||||
}
|
||||
});
|
||||
for (int[] row : tempMap) {
|
||||
for (int cell : row) {
|
||||
if (cell > 1) {
|
||||
LOGGER.log(Level.ERROR, "There are multiple ships on one position");
|
||||
return false;
|
||||
List<Battleship> ships = dto.getShips();
|
||||
for(Battleship ship:ships){
|
||||
for(Battleship compareShip:ships){
|
||||
if(!(ship==compareShip)){
|
||||
if(ship.collidesWith(compareShip)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,8 +164,7 @@ private boolean checkMap(MapMessage msg, int from) {
|
||||
int mapHeight = getPlayerById(from).getMap().getHeight();
|
||||
|
||||
// check if ship is out of bounds
|
||||
for (int i = 0; i < msg.getShips().size(); i++){
|
||||
Battleship battleship = msg.getShips().get(i);
|
||||
for (Battleship battleship : msg.getShips()){
|
||||
if (battleship.getMaxX() >= mapWidth || battleship.getMinX() < 0 || battleship.getMaxY() >= mapHeight || battleship.getMinY() < 0) {
|
||||
LOGGER.log(Level.ERROR, "Ship is out of bounds ({0})", battleship.toString());
|
||||
return false;
|
||||
@@ -173,7 +172,21 @@ private boolean checkMap(MapMessage msg, int from) {
|
||||
}
|
||||
|
||||
// check if ships overlap
|
||||
int[][] tempMap = new int[mapWidth][mapHeight];
|
||||
List<Battleship> ships = msg.getShips();
|
||||
for(Battleship ship:ships){
|
||||
for(Battleship compareShip:ships){
|
||||
if(!(ship==compareShip)){
|
||||
if(ship.collidesWith(compareShip)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*int[][] tempMap = new int[mapWidth][mapHeight];
|
||||
for (int[] row : tempMap)
|
||||
Arrays.fill(row, 0);
|
||||
msg.getShips().forEach(s -> {
|
||||
@@ -196,7 +209,7 @@ private boolean checkMap(MapMessage msg, int from) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user