fix DropBomb, DropMine and Shot not using the correct Direction implementation, delete debug messages

This commit is contained in:
Quentin Legot 2021-12-09 19:55:16 +01:00
parent af5cbf80c1
commit 80b737ad5f
9 changed files with 42 additions and 43 deletions

View File

@ -143,7 +143,6 @@ public class App extends Application {
default -> throw new IllegalArgumentException("Unknown argument: " + str);
}
}
System.out.println("oui");
if(playerClass != null)
playerList.add(createNewPlayer(playerClass,
classPlayer != null ? classPlayer : ClassPlayer.DEFAULT, playerList.size())

View File

@ -98,6 +98,12 @@ public class Game {
try {
actions.add(new Move(this, player, direction));
} catch (NotValidDirectionException ignored){}
try {
actions.add(new Shot(this, player, direction));
} catch (NotValidDirectionException | NoMoreBulletInWeaponException ignored) {}
}
for(Direction8Axis direction : Direction8Axis.values()) {
try {
actions.add(new DropBomb(this, player, direction));
} catch (NotValidDirectionException ignored) {}
@ -105,11 +111,6 @@ public class Game {
actions.add(new DropMine(this, player, direction));
} catch (NotValidDirectionException ignored) {}
}
for(Direction8Axis direction : Direction8Axis.values()) {
try {
actions.add(new Shot(this, player, direction));
} catch (NotValidDirectionException | NoMoreBulletInWeaponException ignored) {}
}
actions.addAll(Arrays.asList(new Nothing(), new DeployShield(player)));
return actions;
}

View File

@ -5,17 +5,19 @@ package fr.lnl.game.server.games.action;
*/
public enum Direction4Axis implements Direction {
UP(-1, 0),
DOWN(1, 0),
LEFT(0, -1),
RIGHT(0, 1);
UP(-1, 0, true),
DOWN(1, 0, true),
LEFT(0, -1, false),
RIGHT(0, 1, false);
private final int deltaX;
private final int deltaY;
private final boolean isVertical;
Direction4Axis(int i, int i1) {
Direction4Axis(int i, int i1, boolean isVertical) {
this.deltaX = i;
this.deltaY = i1;
this.isVertical = isVertical;
}
@Override
@ -27,4 +29,13 @@ public enum Direction4Axis implements Direction {
public int getDeltaY() {
return deltaY;
}
/**
* Used by {@link Shot} to know if the direction is in vertical direction cause shot action can have a different
* distance depending on the direction
* @return true if the direction is vertical
*/
public boolean isVertical() {
return isVertical;
}
}

View File

@ -2,23 +2,21 @@ package fr.lnl.game.server.games.action;
public enum Direction8Axis implements Direction {
UP(-1, 0, true),
DOWN(1, 0, true),
LEFT(0, -1, false),
RIGHT(0, 1, false),
UP_LEFT(-1, -1, true),
UP_RIGHT(-1, 1, true),
DOWN_RIGHT(1, 1, true),
DOWN_LEFT(1, -1, true);
UP(-1, 0),
DOWN(1, 0),
LEFT(0, -1),
RIGHT(0, 1),
UP_LEFT(-1, -1),
UP_RIGHT(-1, 1),
DOWN_RIGHT(1, 1),
DOWN_LEFT(1, -1);
private final int deltaX;
private final int deltaY;
private final boolean isVertical;
Direction8Axis(int i, int i1, boolean isVertical) {
Direction8Axis(int i, int i1) {
this.deltaX = i;
this.deltaY = i1;
this.isVertical = isVertical;
}
@Override
@ -31,14 +29,5 @@ public enum Direction8Axis implements Direction {
return deltaY;
}
/**
* Used by {@link Shot} to know if the direction is in vertical direction cause shot action can have a different
* distance depending of the direction
* @return true if the direction is vertical
*/
public boolean isVertical() {
return isVertical;
}
}

View File

@ -9,7 +9,7 @@ import fr.lnl.game.server.games.player.Player;
*/
public class DropBomb extends DropObject {
public DropBomb(Game game, Player player, Direction4Axis direction) throws NotValidDirectionException {
public DropBomb(Game game, Player player, Direction8Axis direction) throws NotValidDirectionException {
super(game, player, direction);
}

View File

@ -9,7 +9,7 @@ import fr.lnl.game.server.games.player.Player;
*/
public class DropMine extends DropObject {
public DropMine(Game game, Player player, Direction4Axis direction) throws NotValidDirectionException {
public DropMine(Game game, Player player, Direction8Axis direction) throws NotValidDirectionException {
super(game, player, direction);
}

View File

@ -16,14 +16,14 @@ import java.util.List;
public abstract class DropObject extends AbstractAction {
protected final Point point;
private final Direction4Axis direction;
private final Direction8Axis direction;
/**
* @param player basically current player
* @param direction chosen direction
* @throws NotValidDirectionException throw when the chosen direction is invalid
*/
public DropObject(Game game, Player player, Direction4Axis direction) throws NotValidDirectionException {
public DropObject(Game game, Player player, Direction8Axis direction) throws NotValidDirectionException {
super(game, player);
List<Point> points = getValidPoint();
Point playerPosition = player.getPosition();
@ -67,7 +67,7 @@ public abstract class DropObject extends AbstractAction {
return listMoves;
}
public Direction4Axis getDirection() {
public Direction8Axis getDirection() {
return direction;
}

View File

@ -12,9 +12,9 @@ import java.util.List;
public class Shot extends AbstractAction {
private final Point point;
private final Direction8Axis direction;
private final Direction4Axis direction;
public Shot(Game game, Player player, Direction8Axis direction) throws NoMoreBulletInWeaponException, NotValidDirectionException {
public Shot(Game game, Player player, Direction4Axis direction) throws NoMoreBulletInWeaponException, NotValidDirectionException {
super(game, player);
if(player.getWeapon().getBullet() == 0) {
throw new NoMoreBulletInWeaponException();
@ -75,7 +75,7 @@ public class Shot extends AbstractAction {
List<Point> listMoves = new ArrayList<>();
Point position = game.getCurrentPlayer().getPosition();
Weapon weapon = game.getCurrentPlayer().getWeapon();
for(Direction8Axis direction : Direction8Axis.values()) {
for(Direction4Axis direction : Direction4Axis.values()) {
Point neighbour = seeNeighbour(position, direction.getDeltaX(), direction.getDeltaY(),
direction.isVertical() ? weapon.getVerticalDistance() : weapon.getHorizontalDistance());
if(neighbour != null)
@ -102,7 +102,6 @@ public class Shot extends AbstractAction {
return null;
}
if(game.getGrid().getBoard().get(neighbour).getA() instanceof Player) {
System.out.println(game.getGrid().getBoard().get(neighbour).getA().getPosition());
return neighbour;
}
}

View File

@ -65,7 +65,7 @@ public class ActionPlayerTest {
@Test
public void shotActionTest(){
Action shot = null;
for(Direction8Axis direction : Direction8Axis.values()) {
for(Direction4Axis direction : Direction4Axis.values()) {
try {
shot = new Shot(game, game.getCurrentPlayer(), direction);
break;
@ -84,8 +84,8 @@ public class ActionPlayerTest {
public void dropBombActionTest() {
Player player = game.getCurrentPlayer();
Action action = null;
Direction4Axis savedDirection = null;
for(Direction4Axis direction : Direction4Axis.values()) {
Direction8Axis savedDirection = null;
for(Direction8Axis direction : Direction8Axis.values()) {
try {
action = new DropBomb(game, game.getCurrentPlayer(), direction);
savedDirection = direction;