From 785e28e6a99f38c212c92a1c5edc792c0320290f Mon Sep 17 00:00:00 2001 From: Katchan Date: Thu, 28 Oct 2021 23:17:12 +0200 Subject: [PATCH] Implement all action function --- .../server/games/action/AbstractAction.java | 21 ++++++- .../lnl/game/server/games/action/Action.java | 5 ++ .../server/games/action/DeployShield.java | 8 +++ .../game/server/games/action/DropBomb.java | 3 +- .../game/server/games/action/DropMine.java | 6 +- .../game/server/games/action/DropObject.java | 9 +-- .../fr/lnl/game/server/games/action/Move.java | 41 +++++++------ .../lnl/game/server/games/action/Nothing.java | 8 +++ .../fr/lnl/game/server/games/action/Shot.java | 59 ++++++++++++++++++- 9 files changed, 131 insertions(+), 29 deletions(-) diff --git a/server/src/main/java/fr/lnl/game/server/games/action/AbstractAction.java b/server/src/main/java/fr/lnl/game/server/games/action/AbstractAction.java index eb3d74d..c415fc9 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/AbstractAction.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/AbstractAction.java @@ -1,8 +1,12 @@ package fr.lnl.game.server.games.action; import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.utils.Point; -public abstract class AbstractAction implements Action { +import java.util.List; +import java.util.Random; + +public abstract class AbstractAction implements Action { private final Game game; public AbstractAction(Game game){ @@ -12,4 +16,19 @@ public abstract class AbstractAction implements Action { protected Game getGame() { return game; } + + protected Point choseRandomPoint(List getValidPoint) { + Point point = null; + switch (getValidPoint.size()) { + case 0: + break; + case 1: + point = getValidPoint.get(0); + default: { + Random random = new Random(); + point = getValidPoint.get(random.nextInt(0, getValidPoint.size())); + } + } + return point; + } } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Action.java b/server/src/main/java/fr/lnl/game/server/games/action/Action.java index 910921a..4b136e5 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/Action.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/Action.java @@ -1,8 +1,13 @@ package fr.lnl.game.server.games.action; +import fr.lnl.game.server.utils.Point; + +import java.util.List; + public interface Action { void doAction(); boolean isPossible(); + List getValidPoint(); } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DeployShield.java b/server/src/main/java/fr/lnl/game/server/games/action/DeployShield.java index 7424b83..dba643d 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DeployShield.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DeployShield.java @@ -2,6 +2,9 @@ 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 java.util.List; public class DeployShield extends AbstractAction { public DeployShield(Game game){ @@ -20,6 +23,11 @@ public class DeployShield extends AbstractAction { return true; } + @Override + public List getValidPoint() { + return null; + } + } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java b/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java index 62e0638..715d3e1 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java @@ -18,8 +18,7 @@ public class DropBomb extends DropObject { @Override public void doAction() { List points = getValidPoint(); - Random random = new Random(); - Point point = points.get(random.nextInt(0,points.size()-1)); + Point point = choseRandomPoint(points); getGame().getGrid().getBoard().get(point).setB(new Bomb()); Player player = getGame().getCurrentPlayer(); player.decrementEnergy(player.getClassPlayer().getBombCost()); diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java b/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java index 85d473e..6937270 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java @@ -15,11 +15,11 @@ public class DropMine extends DropObject { @Override public void doAction() { List points = getValidPoint(); - Random random = new Random(); - Point point = points.get(random.nextInt(0,points.size()-1)); + Point point = choseRandomPoint(points); Mine mine = new Mine(); getGame().getGrid().getBoard().get(point).setB(mine); - getGame().getCurrentPlayer().decrementEnergy(getGame().getCurrentPlayer().getClassPlayer().getMineCost()); + Player player = getGame().getCurrentPlayer(); + getGame().getCurrentPlayer().decrementEnergy(player.getClassPlayer().getMineCost()); } @Override diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java b/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java index 7284eaa..9d626ce 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java @@ -8,6 +8,7 @@ import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Pair; import fr.lnl.game.server.utils.Point; +import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -24,19 +25,19 @@ public abstract class DropObject extends AbstractAction { @Override public boolean isPossible() { - return getValidPoint().isEmpty(); + return !getValidPoint().isEmpty(); } public List getValidPoint() { - List listMoves = new LinkedList<>(); + List listMoves = new ArrayList<>(); HashMap> board = getGame().getGrid().getBoard(); Point position = getGame().getCurrentPlayer().getPoint(); for (int row = -1; row <= 1; row++) { for (int column = -1; column <= 1; column++) { - if(Grid.caseisValid(position.getA(),row,position.getB(),column)){ + if(getGame().getGrid().boardPositionIsValid(position.getA(),row,position.getB(),column)){ Point neighbour = new Point(position.getA() + row, position.getB() + column); Pair state = board.get(neighbour); - if(state.getA() == null || state.getB() instanceof Wall){ + if(state.getA() == null && state.getB() == null){ listMoves.add(neighbour); } } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Move.java b/server/src/main/java/fr/lnl/game/server/games/action/Move.java index 0eb36fe..275948e 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/Move.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/Move.java @@ -1,17 +1,12 @@ package fr.lnl.game.server.games.action; import fr.lnl.game.server.games.Game; -import fr.lnl.game.server.games.grid.Box; -import fr.lnl.game.server.games.grid.Grid; -import fr.lnl.game.server.games.grid.Wall; +import fr.lnl.game.server.games.grid.*; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Pair; import fr.lnl.game.server.utils.Point; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Random; +import java.util.*; public class Move extends AbstractAction { public Move(Game game) { @@ -21,12 +16,25 @@ public class Move extends AbstractAction { @Override public void doAction() { List points = getValidPoint(); - Random random = new Random(); - Point nextPositon = points.get(random.nextInt(0,points.size() - 1)); + Point nextPositon = choseRandomPoint(points); Player player = getGame().getCurrentPlayer(); getGame().getGrid().getBoard().get(player.getPoint()).setA(null); getGame().getGrid().getBoard().get(nextPositon).setA(player); + player.setPoint(nextPositon); player.decrementEnergy(player.getClassPlayer().getMoveCost()); + Box box = getGame().getGrid().getBoard().get(nextPositon).getB(); + if (box instanceof Mine){ + player.decrementEnergy(player.getClassPlayer().getPenaltyMine()); + getGame().getGrid().getBoard().get(nextPositon).setB(null); + } + if(box instanceof Bomb){ + player.decrementEnergy(player.getClassPlayer().getPenaltyBomb()); + getGame().getGrid().getBoard().get(nextPositon).setB(null); + } + if(box instanceof EnergyBall){ + player.incrementEnergy(player.getClassPlayer().getGainEnergy()); + getGame().getGrid().getBoard().get(nextPositon).setB(null); + } } @Override @@ -35,16 +43,16 @@ public class Move extends AbstractAction { } public List getValidPoint() { - List listMoves = new LinkedList<>(); + List listMoves = new ArrayList<>(); HashMap> board = getGame().getGrid().getBoard(); Point position = getGame().getCurrentPlayer().getPoint(); - for (int row = -1; row <= 1; row++) { - for (int column = -1; column <= 1; column++) { - if(row == position.getA() + row || column == position.getB() + column){ - if(Grid.caseisValid(position.getA(),row,position.getB(),column)){ - Point neighbour = new Point(position.getA() + row, position.getB() + column); + for (int deltarow = -1; deltarow <= 1; deltarow++) { + for (int deltacolumn = -1; deltacolumn <= 1; deltacolumn++) { + if(deltarow == 0 || deltacolumn == 0){ + if(getGame().getGrid().boardPositionIsValid(position.getA(),deltarow,position.getB(),deltacolumn)){ + Point neighbour = new Point(position.getA() + deltarow, position.getB() + deltacolumn); Pair state = board.get(neighbour); - if(state.getA() == null || state.getB() instanceof Wall){ + if(state.getA() == null && !(state.getB() instanceof Wall)){ listMoves.add(neighbour); } } @@ -53,5 +61,4 @@ public class Move extends AbstractAction { } return listMoves; } - } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Nothing.java b/server/src/main/java/fr/lnl/game/server/games/action/Nothing.java index 2b71ed0..3b2d350 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/Nothing.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/Nothing.java @@ -1,6 +1,9 @@ package fr.lnl.game.server.games.action; import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.utils.Point; + +import java.util.List; public class Nothing extends AbstractAction { @@ -15,4 +18,9 @@ public class Nothing extends AbstractAction { public boolean isPossible() { return true; } + + @Override + public List getValidPoint() { + return null; + } } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Shot.java b/server/src/main/java/fr/lnl/game/server/games/action/Shot.java index dca771b..5ceab86 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/Shot.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/Shot.java @@ -1,6 +1,16 @@ package fr.lnl.game.server.games.action; import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.games.grid.Box; +import fr.lnl.game.server.games.grid.Wall; +import fr.lnl.game.server.games.player.Player; +import fr.lnl.game.server.games.weapon.Weapon; +import fr.lnl.game.server.utils.Pair; +import fr.lnl.game.server.utils.Point; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; public class Shot extends AbstractAction { public Shot(Game game) { @@ -9,11 +19,56 @@ public class Shot extends AbstractAction { @Override public void doAction() { - + List points = getValidPoint(); + Point point = choseRandomPoint(points); + Player currentPlayer = getGame().getCurrentPlayer(); + currentPlayer.decrementEnergy(currentPlayer.getClassPlayer().getShootCost()); + Player targetPlayer = getGame().getGrid().getBoard().get(point).getA(); + targetPlayer.decrementEnergy(currentPlayer.getClassPlayer().getPenaltyShoot()); } @Override public boolean isPossible() { - return false; + return !getValidPoint().isEmpty(); + } + + @Override + public List getValidPoint() { + List listMoves = new ArrayList<>(); + Point position = getGame().getCurrentPlayer().getPoint(); + Weapon weapon = getGame().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); + } + } + } + return listMoves; + } + + public Point seeNeibourg(Point point, int delta, int range, boolean isVertical) { + Point neibourg = null; + if (isVertical) { + if (getGame().getGrid().boardVerticalIsValid(point.getA(), delta)) { + neibourg = new Point(point.getA() + delta, point.getB()); + } + } else { + if (getGame().getGrid().boardHorizontalIsValid(point.getB(), delta)) { + neibourg = new Point(point.getA(), point.getB() + delta); + } + } + if (getGame().getGrid().getBoard().get(neibourg).getB() instanceof Wall || range + delta < 0) { + return null; + } + if(getGame().getGrid().getBoard().get(neibourg).getA() instanceof Player){ + return neibourg; + } + return seeNeibourg(neibourg,delta,range - 1,isVertical); } }