Improve Shot Action and shotActionTest

This commit is contained in:
Quentin Legot 2021-11-18 10:58:46 +01:00
parent 8ca2326c89
commit e2bd1a8bf6
8 changed files with 122 additions and 84 deletions

View File

@ -0,0 +1,31 @@
package fr.lnl.game.server.games.action;
public enum Direction {
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;
Direction(int i, int i1, boolean isVertical) {
this.deltaX = i;
this.deltaY = i1;
this.isVertical = isVertical;
}
public int getDeltaX() {
return deltaX;
}
public int getDeltaY() {
return deltaY;
}
public boolean isVertical() {
return isVertical;
}
}

View File

@ -19,9 +19,9 @@ public class Move extends AbstractAction {
public Move(Game game, Player player, Direction direction) throws NotValidDirectionException { public Move(Game game, Player player, Direction direction) throws NotValidDirectionException {
super(game, player); super(game, player);
HashSet<Point> points = new HashSet<>(getValidPoint()); List<Point> points = getValidPoint();
Point playerPosition = player.getPoint(); Point playerPosition = player.getPoint();
Point newPosition = new Point(playerPosition.getA() + direction.deltaX, playerPosition.getB() + direction.deltaY); Point newPosition = new Point(playerPosition.getA() + direction.getDeltaX(), playerPosition.getB() + direction.getDeltaY());
if(!points.contains(newPosition)) { if(!points.contains(newPosition)) {
throw new NotValidDirectionException(direction + " isn't a valid position"); throw new NotValidDirectionException(direction + " isn't a valid position");
} }
@ -65,28 +65,4 @@ public class Move extends AbstractAction {
} }
return listMoves; return listMoves;
} }
public enum Direction {
UP(-1, 0),
DOWN(1, 0),
LEFT(0, -1),
RIGHT(-1, 0);
private final int deltaX;
private final int deltaY;
Direction(int i, int i1) {
this.deltaX = i;
this.deltaY = i1;
}
public int getDeltaX() {
return deltaX;
}
public int getDeltaY() {
return deltaY;
}
}
} }

View File

@ -0,0 +1,9 @@
package fr.lnl.game.server.games.action;
public class NoMoreBulletInWeaponException extends Exception {
public NoMoreBulletInWeaponException() {
super();
}
}

View File

@ -7,24 +7,44 @@ import fr.lnl.game.server.games.weapon.Weapon;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
public class Shot extends AbstractAction { public class Shot extends AbstractAction {
public Shot(Game game, Player player) { private final Point point;
private final Direction direction;
public Shot(Game game, Player player, Direction direction) throws NoMoreBulletInWeaponException, NotValidDirectionException {
super(game, player); super(game, player);
if(player.getWeapon().getBullet() == 0) {
throw new NoMoreBulletInWeaponException();
}
List<Point> points = getValidPoint();
Point playerPosition = player.getPoint();
Point shotDirection = new Point(playerPosition.getA() + direction.getDeltaX(), playerPosition.getB() + direction.getDeltaY());
if(!points.contains(shotDirection)) {
throw new NotValidDirectionException(direction + " isn't a valid position");
}
this.point = shotDirection;
this.direction = direction;
} }
/**
* @deprecated a rewrite -> L'aléatoire ne devrait pas être ici, mais au moment de l'instanciation par exemple
* comme dans {@link Move}
*/
@Deprecated
@Override @Override
public void doAction() { public void doAction() {
player.decrementEnergy(player.getClassPlayer().getShootCost()); player.decrementEnergy(player.getClassPlayer().getShootCost());
game.getGrid().getBoard().get(choseRandomPoint(getValidPoint())).getA() int range = direction.isVertical() ? player.getWeapon().getVerticalDistance() : player.getWeapon().getHorizontalDistance();
.decrementEnergy(player.getClassPlayer().getPenaltyShoot()); for(int i=0; i < range; i++) {
Point point = new Point(this.point.getA() + (i * direction.getDeltaX()),
this.point.getB() + (i * direction.getDeltaY()));
Player player = game.getGrid().getBoard().get(point).getA();
if(player != null) {
player.decrementEnergy(player.getClassPlayer().getPenaltyShoot());
System.out.println("Not null: " + point);
} else {
System.out.println("null:" + point);
}
}
} }
@Override @Override
@ -32,48 +52,36 @@ public class Shot extends AbstractAction {
return !getValidPoint().isEmpty(); return !getValidPoint().isEmpty();
} }
/**
* @deprecated voir {@link Shot#doAction()}, surement renommé en isValidPoint(Point): bool après rework
*/
@Deprecated(forRemoval = true, since = "07/11/2021")
@Override @Override
public List<Point> getValidPoint() { public List<Point> getValidPoint() {
List<Point> listMoves = new ArrayList<>(); List<Point> listMoves = new ArrayList<>();
Point position = game.getCurrentPlayer().getPoint(); Point position = game.getCurrentPlayer().getPoint();
Weapon weapon = game.getCurrentPlayer().getWeapon(); Weapon weapon = game.getCurrentPlayer().getWeapon();
for (int delta = -1; delta <= 1; delta++) { for(Direction direction : Direction.values()) {
if(delta != 0){ Point neighbour = seeNeighbour(position, direction.getDeltaX(), direction.getDeltaY(),
Point verticalNeibourg = seeNeibourg(position,delta,weapon.getVerticalDistance(),true); direction.isVertical() ? weapon.getVerticalDistance() : weapon.getHorizontalDistance());
if(verticalNeibourg != null){ if(neighbour != null)
listMoves.add(verticalNeibourg); listMoves.add(neighbour);
}
Point horizontalNeibourg = seeNeibourg(position,delta,weapon.getHorizontalDistance(),false);
if(horizontalNeibourg != null){
listMoves.add(horizontalNeibourg);
}
}
} }
return listMoves; return listMoves;
} }
@Deprecated(since = "07/11/2021", forRemoval = true)
public Point seeNeibourg(Point point, int delta, int range, boolean isVertical) { public Point seeNeighbour(Point point, int deltaX, int deltaY, int range) {
Point neibourg = null; if(range == 0)
if (isVertical) { return null;
if (game.getGrid().boardVerticalIsValid(point.getA(), delta)) { for(int i = 0; i < range; i++) {
neibourg = new Point(point.getA() + delta, point.getB()); Point neighbour = new Point(point.getA() + deltaX + (i * deltaX), point.getB() + deltaY + (i * deltaY));
} if(game.getGrid().boardPositionIsValid(point)) {
} else { if(game.getGrid().getBoard().get(neighbour).getB() instanceof Wall) {
if (game.getGrid().boardHorizontalIsValid(point.getB(), delta)) {
neibourg = new Point(point.getA(), point.getB() + delta);
}
}
if (game.getGrid().getBoard().get(neibourg).getB() instanceof Wall || range + delta < 0) {
return null; return null;
} }
if(game.getGrid().getBoard().get(neibourg).getA() instanceof Player){ if(game.getGrid().getBoard().get(neighbour).getA() instanceof Player) {
return neibourg; System.out.println(game.getGrid().getBoard().get(neighbour).getA().getPoint());
return neighbour;
} }
return seeNeibourg(neibourg,delta,range - 1,isVertical); }
}
return null;
} }
} }

View File

@ -69,7 +69,15 @@ public class Grid {
} }
public boolean boardPositionIsValid(int row, int deltaRow, int column, int deltaColumn){ public boolean boardPositionIsValid(int row, int deltaRow, int column, int deltaColumn){
return row + deltaRow >= 0 && row + deltaRow < this.row && column + deltaColumn >= 0 && column + deltaColumn < this.column; return boardPositionIsValid(row + deltaRow, column + deltaColumn);
}
public boolean boardPositionIsValid(int row, int column) {
return row >= 0 && column >= 0 && row < this.row && column < this.column;
}
public boolean boardPositionIsValid(Point point) {
return boardPositionIsValid(point.getA(), point.getB());
} }
public boolean boardHorizontalIsValid(int column, int deltaColumn){ public boolean boardHorizontalIsValid(int column, int deltaColumn){

View File

@ -5,7 +5,7 @@ import fr.lnl.game.server.games.weapon.Weapon;
public enum ClassPlayer { public enum ClassPlayer {
DEFAULT(800, 25, 40, 30, 40, 10, 80, 20, 20, 15, new Firearm()), DEFAULT(800, 25, 20, 30, 40, 10, 80, 40, 20, 15, new Firearm()),
TANK(1000, 20, 20, 17, 23, 13, 80, 27, 30, 22, new Firearm()), TANK(1000, 20, 20, 17, 23, 13, 80, 27, 30, 22, new Firearm()),
DPS(800, 25, 16, 15, 20, 10, 80, 40, 40, 30, new Firearm()), DPS(800, 25, 16, 15, 20, 10, 80, 40, 40, 30, new Firearm()),
SUPPORT(600, 25, 20, 11, 15, 7, 80, 45, 45, 35, new Firearm()); SUPPORT(600, 25, 20, 11, 15, 7, 80, 45, 45, 35, new Firearm());

View File

@ -9,8 +9,6 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.List;
public class ActionPlayerTest { public class ActionPlayerTest {
private Grid grid; private Grid grid;
@ -28,16 +26,15 @@ public class ActionPlayerTest {
public void moveActionTest() { public void moveActionTest() {
Action move = null; Action move = null;
Point oldPoint = game.getCurrentPlayer().getPoint(); Point oldPoint = game.getCurrentPlayer().getPoint();
Move.Direction savedDirection = null; Direction savedDirection = null;
for(Move.Direction direction : Move.Direction.values()) { for(Direction direction : Direction.values()) {
try { try {
move = new Move(game, game.getCurrentPlayer(), direction); move = new Move(game, game.getCurrentPlayer(), direction);
savedDirection = direction; savedDirection = direction;
break; break;
} catch (NotValidDirectionException ignored) {} } catch (NotValidDirectionException ignored) {}
} }
Assertions.assertNotEquals(null, move); Assertions.assertNotNull(move);
assert move != null;
move.doAction(); move.doAction();
Point newPoint = game.getCurrentPlayer().getPoint(); Point newPoint = game.getCurrentPlayer().getPoint();
Assertions.assertEquals(newPoint, Assertions.assertEquals(newPoint,
@ -56,16 +53,22 @@ public class ActionPlayerTest {
Assertions.assertTrue(player.isShieldDeploy()); Assertions.assertTrue(player.isShieldDeploy());
} }
// TODO: 10/28/2021 pas un vrai test et marche qu'avec le mock actuel
@Test @Test
public void shotActionTest(){ public void shotActionTest(){
System.out.println(grid.toString()); Action shot = null;
Shot shot = new Shot(game, game.getCurrentPlayer()); for(Direction direction : Direction.values()) {
List<Point> points = shot.getValidPoint(); try {
System.out.println(points); shot = new Shot(game, game.getCurrentPlayer(), direction);
System.out.println("Before shot " + game.getPlayers().get(1).getEnergy()); break;
} catch (NoMoreBulletInWeaponException | NotValidDirectionException ignored) {}
}
Assertions.assertNotNull(shot);
Player otherPlayer = game.getPlayers().get(1);
int currentEnergyOtherPlayer = otherPlayer.getEnergy();
int currentEnergyCurrentPlayer = game.getCurrentPlayer().getEnergy();
shot.doAction(); shot.doAction();
System.out.println("After shot " + game.getPlayers().get(1).getEnergy()); Assertions.assertEquals(currentEnergyCurrentPlayer - game.getCurrentPlayer().getClassPlayer().getShootCost(), game.getCurrentPlayer().getEnergy());
Assertions.assertEquals(currentEnergyOtherPlayer - otherPlayer.getClassPlayer().getPenaltyShoot(), otherPlayer.getEnergy());
} }
} }

View File

@ -51,13 +51,16 @@ public class GridTest {
game.getCurrentPlayer().getEnergy() + " points de vies restants"); game.getCurrentPlayer().getEnergy() + " points de vies restants");
Player player = game.getCurrentPlayer(); Player player = game.getCurrentPlayer();
ArrayList<Action> actions = new ArrayList<>(); ArrayList<Action> actions = new ArrayList<>();
for(Move.Direction direction : Move.Direction.values()) { for(Direction direction : Direction.values()) {
try { try {
actions.add(new Move(game, player, direction)); actions.add(new Move(game, player, direction));
} catch (NotValidDirectionException ignored){} } catch (NotValidDirectionException ignored){}
try {
actions.add(new Shot(game, player, direction));
} catch (NotValidDirectionException | NoMoreBulletInWeaponException ignored) {}
} }
actions.addAll(Arrays.asList(new Nothing(), new Shot(game, player), actions.addAll(Arrays.asList(new Nothing(), new DeployShield(player), new DropBomb(game, player),
new DeployShield(player), new DropBomb(game, player), new DropMine(game, player))); new DropMine(game, player)));
player.setActions(actions); player.setActions(actions);
System.out.println(game.getGrid().toString()); System.out.println(game.getGrid().toString());
Action action = null; Action action = null;