Improved actions consistency

This commit is contained in:
Quentin Legot 2021-11-07 15:36:16 +01:00
parent 4b5f9b719d
commit cb4c020fe6
9 changed files with 42 additions and 57 deletions

View File

@ -8,22 +8,15 @@ import java.util.List;
import java.util.Random; import java.util.Random;
public abstract class AbstractAction implements Action { public abstract class AbstractAction implements Action {
private final Game game;
private final Player player; protected final Game game;
protected final Player player;
public AbstractAction(Game game, Player player){ public AbstractAction(Game game, Player player){
this.game = game; this.game = game;
this.player = player; this.player = player;
} }
protected Game getGame() {
return game;
}
protected Player getPlayer() {
return player;
}
protected Point choseRandomPoint(List<Point> getValidPoint) { protected Point choseRandomPoint(List<Point> getValidPoint) {
Point point = null; Point point = null;
switch (getValidPoint.size()) { switch (getValidPoint.size()) {

View File

@ -1,19 +1,18 @@
package fr.lnl.game.server.games.action; package fr.lnl.game.server.games.action;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import java.util.List; import java.util.List;
public class DeployShield extends AbstractAction { public class DeployShield extends AbstractAction {
public DeployShield(Game game, Player player){
super(game, player); public DeployShield(Player player){
super(null, player);
} }
@Override @Override
public void doAction(){ public void doAction(){
Player player = getGame().getCurrentPlayer();
player.setShieldDeploy(true); player.setShieldDeploy(true);
player.decrementEnergy(player.getClassPlayer().getShieldCost()); player.decrementEnergy(player.getClassPlayer().getShieldCost());
} }

View File

@ -13,13 +13,11 @@ public class DropBomb extends DropObject {
super(game, player); super(game, player);
} }
// voir pour la redondance de code au niveau de DropBomb, DropObject,DropMine
@Override @Override
public void doAction() { public void doAction() {
List<Point> points = getValidPoint(); List<Point> points = getValidPoint();
Point point = choseRandomPoint(points); Point point = choseRandomPoint(points);
getGame().getGrid().getBoard().get(point).setB(new Bomb()); game.getGrid().getBoard().get(point).setB(new Bomb());
Player player = getGame().getCurrentPlayer();
player.decrementEnergy(player.getClassPlayer().getBombCost()); player.decrementEnergy(player.getClassPlayer().getBombCost());
} }

View File

@ -8,6 +8,7 @@ import fr.lnl.game.server.utils.Point;
import java.util.List; import java.util.List;
public class DropMine extends DropObject { public class DropMine extends DropObject {
public DropMine(Game game, Player player){ public DropMine(Game game, Player player){
super(game, player); super(game, player);
} }
@ -15,10 +16,8 @@ public class DropMine extends DropObject {
public void doAction() { public void doAction() {
List<Point> points = getValidPoint(); List<Point> points = getValidPoint();
Point point = choseRandomPoint(points); Point point = choseRandomPoint(points);
Mine mine = new Mine(); game.getGrid().getBoard().get(point).setB(new Mine());
getGame().getGrid().getBoard().get(point).setB(mine); game.getCurrentPlayer().decrementEnergy(player.getClassPlayer().getMineCost());
Player player = getGame().getCurrentPlayer();
getGame().getCurrentPlayer().decrementEnergy(player.getClassPlayer().getMineCost());
} }
@Override @Override

View File

@ -16,10 +16,6 @@ public abstract class DropObject extends AbstractAction {
super(game, player); super(game, player);
} }
@Override
public void doAction() {
}
@Override @Override
public boolean isPossible() { public boolean isPossible() {
return !getValidPoint().isEmpty(); return !getValidPoint().isEmpty();
@ -27,11 +23,11 @@ public abstract class DropObject extends AbstractAction {
public List<Point> getValidPoint() { public List<Point> getValidPoint() {
List<Point> listMoves = new ArrayList<>(); List<Point> listMoves = new ArrayList<>();
HashMap<Point, Pair<Player, Box>> board = getGame().getGrid().getBoard(); HashMap<Point, Pair<Player, Box>> board = game.getGrid().getBoard();
Point position = getGame().getCurrentPlayer().getPoint(); Point position = player.getPoint();
for (int row = -1; row <= 1; row++) { for (int row = -1; row <= 1; row++) {
for (int column = -1; column <= 1; column++) { for (int column = -1; column <= 1; column++) {
if(getGame().getGrid().boardPositionIsValid(position.getA(),row,position.getB(),column)){ if(game.getGrid().boardPositionIsValid(position.getA(),row,position.getB(),column)){
Point neighbour = new Point(position.getA() + row, position.getB() + column); Point neighbour = new Point(position.getA() + row, position.getB() + column);
Pair<Player, Box> state = board.get(neighbour); Pair<Player, Box> state = board.get(neighbour);
if(state.getA() == null && state.getB() == null){ if(state.getA() == null && state.getB() == null){

View File

@ -28,14 +28,13 @@ public class Move extends AbstractAction {
@Override @Override
public void doAction() { public void doAction() {
Player player = getGame().getCurrentPlayer(); game.getGrid().getBoard().get(player.getPoint()).setA(null);
getGame().getGrid().getBoard().get(player.getPoint()).setA(null); game.getGrid().getBoard().get(this.point).setA(player);
getGame().getGrid().getBoard().get(this.point).setA(player);
player.setPoint(this.point); player.setPoint(this.point);
player.decrementEnergy(player.getClassPlayer().getMoveCost()); player.decrementEnergy(player.getClassPlayer().getMoveCost());
Box box = getGame().getGrid().getBoard().get(this.point).getB(); Box box = game.getGrid().getBoard().get(this.point).getB();
if(box instanceof InteractiveBox interactiveBox) { if(box instanceof InteractiveBox interactiveBox) {
interactiveBox.interact(getGame().getGrid(), player, this.point); interactiveBox.interact(game.getGrid(), player, this.point);
} }
} }
@ -47,12 +46,12 @@ public class Move extends AbstractAction {
@Override @Override
public List<Point> getValidPoint() { public List<Point> getValidPoint() {
List<Point> listMoves = new ArrayList<>(); List<Point> listMoves = new ArrayList<>();
HashMap<Point, Pair<Player, Box>> board = getGame().getGrid().getBoard(); HashMap<Point, Pair<Player, Box>> board = game.getGrid().getBoard();
Point position = getPlayer().getPoint(); Point position = player.getPoint();
for (int deltarow = -1; deltarow <= 1; deltarow++) { for (int deltarow = -1; deltarow <= 1; deltarow++) {
for (int deltacolumn = -1; deltacolumn <= 1; deltacolumn++) { for (int deltacolumn = -1; deltacolumn <= 1; deltacolumn++) {
if(deltarow == 0 || deltacolumn == 0){ if(deltarow == 0 || deltacolumn == 0){
if(getGame().getGrid().boardPositionIsValid(position.getA(),deltarow,position.getB(),deltacolumn)){ if(game.getGrid().boardPositionIsValid(position.getA(),deltarow,position.getB(),deltacolumn)){
Point neighbour = new Point(position.getA() + deltarow, position.getB() + deltacolumn); Point neighbour = new Point(position.getA() + deltarow, position.getB() + deltacolumn);
Pair<Player, Box> state = board.get(neighbour); Pair<Player, Box> state = board.get(neighbour);
if(state.getA() == null && !(state.getB() instanceof Wall)){ if(state.getA() == null && !(state.getB() instanceof Wall)){

View File

@ -1,17 +1,11 @@
package fr.lnl.game.server.games.action; package fr.lnl.game.server.games.action;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import java.util.List; import java.util.List;
public class Nothing extends AbstractAction { public class Nothing extends AbstractAction {
public Nothing(Game game, Player player){
super(game, player);
}
public Nothing() { public Nothing() {
super(null, null); super(null, null);
} }

View File

@ -10,18 +10,20 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Shot extends AbstractAction { public class Shot extends AbstractAction {
public Shot(Game game, Player player) { public Shot(Game game, Player player) {
super(game, player); super(game, player);
} }
/**
* @deprecated a rewrite -> L'aléatoire ne devrait pas être ici, mais au moment de l'instanciation comme dans {@link Move}
*/
@Deprecated
@Override @Override
public void doAction() { public void doAction() {
List<Point> points = getValidPoint(); player.decrementEnergy(player.getClassPlayer().getShootCost());
Point point = choseRandomPoint(points); game.getGrid().getBoard().get(choseRandomPoint(getValidPoint())).getA()
Player currentPlayer = getGame().getCurrentPlayer(); .decrementEnergy(player.getClassPlayer().getPenaltyShoot());
currentPlayer.decrementEnergy(currentPlayer.getClassPlayer().getShootCost());
Player targetPlayer = getGame().getGrid().getBoard().get(point).getA();
targetPlayer.decrementEnergy(currentPlayer.getClassPlayer().getPenaltyShoot());
} }
@Override @Override
@ -29,11 +31,15 @@ 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 = getGame().getCurrentPlayer().getPoint(); Point position = game.getCurrentPlayer().getPoint();
Weapon weapon = getGame().getCurrentPlayer().getWeapon(); Weapon weapon = game.getCurrentPlayer().getWeapon();
for (int delta = -1; delta <= 1; delta++) { for (int delta = -1; delta <= 1; delta++) {
if(delta != 0){ if(delta != 0){
Point verticalNeibourg = seeNeibourg(position,delta,weapon.getVerticalDistance(),true); Point verticalNeibourg = seeNeibourg(position,delta,weapon.getVerticalDistance(),true);
@ -49,21 +55,22 @@ public class Shot extends AbstractAction {
return listMoves; return listMoves;
} }
@Deprecated(since = "07/11/2021", forRemoval = true)
public Point seeNeibourg(Point point, int delta, int range, boolean isVertical) { public Point seeNeibourg(Point point, int delta, int range, boolean isVertical) {
Point neibourg = null; Point neibourg = null;
if (isVertical) { if (isVertical) {
if (getGame().getGrid().boardVerticalIsValid(point.getA(), delta)) { if (game.getGrid().boardVerticalIsValid(point.getA(), delta)) {
neibourg = new Point(point.getA() + delta, point.getB()); neibourg = new Point(point.getA() + delta, point.getB());
} }
} else { } else {
if (getGame().getGrid().boardHorizontalIsValid(point.getB(), delta)) { if (game.getGrid().boardHorizontalIsValid(point.getB(), delta)) {
neibourg = new Point(point.getA(), point.getB() + delta); neibourg = new Point(point.getA(), point.getB() + delta);
} }
} }
if (getGame().getGrid().getBoard().get(neibourg).getB() instanceof Wall || range + delta < 0) { if (game.getGrid().getBoard().get(neibourg).getB() instanceof Wall || range + delta < 0) {
return null; return null;
} }
if(getGame().getGrid().getBoard().get(neibourg).getA() instanceof Player){ if(game.getGrid().getBoard().get(neibourg).getA() instanceof Player){
return neibourg; return neibourg;
} }
return seeNeibourg(neibourg,delta,range - 1,isVertical); return seeNeibourg(neibourg,delta,range - 1,isVertical);

View File

@ -56,13 +56,13 @@ public class GridTest {
actions.add(new Move(game, player, direction)); actions.add(new Move(game, player, direction));
} catch (NotValidDirectionException ignored){} } catch (NotValidDirectionException ignored){}
} }
actions.addAll(Arrays.asList(new Nothing(game, player), new Shot(game, player), actions.addAll(Arrays.asList(new Nothing(), new Shot(game, player),
new DeployShield(game, player), new DropBomb(game, player), new DropMine(game, player))); new DeployShield(player), new DropBomb(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;
switch (player.getActions().size()){ switch (player.getActions().size()){
case 0 -> action = new Nothing(game, player); case 0 -> action = new Nothing();
case 1 -> action = game.getCurrentPlayer().getActions().get(0); case 1 -> action = game.getCurrentPlayer().getActions().get(0);
default -> { default -> {
Random random = new Random(); Random random = new Random();