Improve Shot Action and shotActionTest
This commit is contained in:
parent
8ca2326c89
commit
e2bd1a8bf6
@ -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;
|
||||
}
|
||||
}
|
@ -19,9 +19,9 @@ public class Move extends AbstractAction {
|
||||
|
||||
public Move(Game game, Player player, Direction direction) throws NotValidDirectionException {
|
||||
super(game, player);
|
||||
HashSet<Point> points = new HashSet<>(getValidPoint());
|
||||
List<Point> points = getValidPoint();
|
||||
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)) {
|
||||
throw new NotValidDirectionException(direction + " isn't a valid position");
|
||||
}
|
||||
@ -65,28 +65,4 @@ public class Move extends AbstractAction {
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
package fr.lnl.game.server.games.action;
|
||||
|
||||
public class NoMoreBulletInWeaponException extends Exception {
|
||||
|
||||
public NoMoreBulletInWeaponException() {
|
||||
super();
|
||||
}
|
||||
|
||||
}
|
@ -7,24 +7,44 @@ import fr.lnl.game.server.games.weapon.Weapon;
|
||||
import fr.lnl.game.server.utils.Point;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
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);
|
||||
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
|
||||
public void doAction() {
|
||||
player.decrementEnergy(player.getClassPlayer().getShootCost());
|
||||
game.getGrid().getBoard().get(choseRandomPoint(getValidPoint())).getA()
|
||||
.decrementEnergy(player.getClassPlayer().getPenaltyShoot());
|
||||
int range = direction.isVertical() ? player.getWeapon().getVerticalDistance() : player.getWeapon().getHorizontalDistance();
|
||||
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
|
||||
@ -32,48 +52,36 @@ public class Shot extends AbstractAction {
|
||||
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
|
||||
public List<Point> getValidPoint() {
|
||||
List<Point> listMoves = new ArrayList<>();
|
||||
Point position = game.getCurrentPlayer().getPoint();
|
||||
Weapon weapon = game.getCurrentPlayer().getWeapon();
|
||||
for (int delta = -1; delta <= 1; delta++) {
|
||||
if(delta != 0){
|
||||
Point verticalNeibourg = seeNeibourg(position,delta,weapon.getVerticalDistance(),true);
|
||||
if(verticalNeibourg != null){
|
||||
listMoves.add(verticalNeibourg);
|
||||
}
|
||||
Point horizontalNeibourg = seeNeibourg(position,delta,weapon.getHorizontalDistance(),false);
|
||||
if(horizontalNeibourg != null){
|
||||
listMoves.add(horizontalNeibourg);
|
||||
}
|
||||
}
|
||||
for(Direction direction : Direction.values()) {
|
||||
Point neighbour = seeNeighbour(position, direction.getDeltaX(), direction.getDeltaY(),
|
||||
direction.isVertical() ? weapon.getVerticalDistance() : weapon.getHorizontalDistance());
|
||||
if(neighbour != null)
|
||||
listMoves.add(neighbour);
|
||||
}
|
||||
return listMoves;
|
||||
}
|
||||
|
||||
@Deprecated(since = "07/11/2021", forRemoval = true)
|
||||
public Point seeNeibourg(Point point, int delta, int range, boolean isVertical) {
|
||||
Point neibourg = null;
|
||||
if (isVertical) {
|
||||
if (game.getGrid().boardVerticalIsValid(point.getA(), delta)) {
|
||||
neibourg = new Point(point.getA() + delta, point.getB());
|
||||
}
|
||||
} else {
|
||||
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) {
|
||||
|
||||
public Point seeNeighbour(Point point, int deltaX, int deltaY, int range) {
|
||||
if(range == 0)
|
||||
return null;
|
||||
for(int i = 0; i < range; i++) {
|
||||
Point neighbour = new Point(point.getA() + deltaX + (i * deltaX), point.getB() + deltaY + (i * deltaY));
|
||||
if(game.getGrid().boardPositionIsValid(point)) {
|
||||
if(game.getGrid().getBoard().get(neighbour).getB() instanceof Wall) {
|
||||
return null;
|
||||
}
|
||||
if(game.getGrid().getBoard().get(neibourg).getA() instanceof Player){
|
||||
return neibourg;
|
||||
}
|
||||
return seeNeibourg(neibourg,delta,range - 1,isVertical);
|
||||
if(game.getGrid().getBoard().get(neighbour).getA() instanceof Player) {
|
||||
System.out.println(game.getGrid().getBoard().get(neighbour).getA().getPoint());
|
||||
return neighbour;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -69,7 +69,15 @@ public class Grid {
|
||||
}
|
||||
|
||||
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){
|
||||
|
@ -5,7 +5,7 @@ import fr.lnl.game.server.games.weapon.Weapon;
|
||||
|
||||
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()),
|
||||
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());
|
||||
|
@ -9,8 +9,6 @@ import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ActionPlayerTest {
|
||||
|
||||
private Grid grid;
|
||||
@ -28,16 +26,15 @@ public class ActionPlayerTest {
|
||||
public void moveActionTest() {
|
||||
Action move = null;
|
||||
Point oldPoint = game.getCurrentPlayer().getPoint();
|
||||
Move.Direction savedDirection = null;
|
||||
for(Move.Direction direction : Move.Direction.values()) {
|
||||
Direction savedDirection = null;
|
||||
for(Direction direction : Direction.values()) {
|
||||
try {
|
||||
move = new Move(game, game.getCurrentPlayer(), direction);
|
||||
savedDirection = direction;
|
||||
break;
|
||||
} catch (NotValidDirectionException ignored) {}
|
||||
}
|
||||
Assertions.assertNotEquals(null, move);
|
||||
assert move != null;
|
||||
Assertions.assertNotNull(move);
|
||||
move.doAction();
|
||||
Point newPoint = game.getCurrentPlayer().getPoint();
|
||||
Assertions.assertEquals(newPoint,
|
||||
@ -56,16 +53,22 @@ public class ActionPlayerTest {
|
||||
Assertions.assertTrue(player.isShieldDeploy());
|
||||
}
|
||||
|
||||
// TODO: 10/28/2021 pas un vrai test et marche qu'avec le mock actuel
|
||||
@Test
|
||||
public void shotActionTest(){
|
||||
System.out.println(grid.toString());
|
||||
Shot shot = new Shot(game, game.getCurrentPlayer());
|
||||
List<Point> points = shot.getValidPoint();
|
||||
System.out.println(points);
|
||||
System.out.println("Before shot " + game.getPlayers().get(1).getEnergy());
|
||||
Action shot = null;
|
||||
for(Direction direction : Direction.values()) {
|
||||
try {
|
||||
shot = new Shot(game, game.getCurrentPlayer(), direction);
|
||||
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();
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -51,13 +51,16 @@ public class GridTest {
|
||||
game.getCurrentPlayer().getEnergy() + " points de vies restants");
|
||||
Player player = game.getCurrentPlayer();
|
||||
ArrayList<Action> actions = new ArrayList<>();
|
||||
for(Move.Direction direction : Move.Direction.values()) {
|
||||
for(Direction direction : Direction.values()) {
|
||||
try {
|
||||
actions.add(new Move(game, player, direction));
|
||||
} 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),
|
||||
new DeployShield(player), new DropBomb(game, player), new DropMine(game, player)));
|
||||
actions.addAll(Arrays.asList(new Nothing(), new DeployShield(player), new DropBomb(game, player),
|
||||
new DropMine(game, player)));
|
||||
player.setActions(actions);
|
||||
System.out.println(game.getGrid().toString());
|
||||
Action action = null;
|
||||
|
Loading…
Reference in New Issue
Block a user