Implement all action function

This commit is contained in:
Katchan 2021-10-28 23:17:12 +02:00
parent 70439266e9
commit 785e28e6a9
9 changed files with 131 additions and 29 deletions

View File

@ -1,8 +1,12 @@
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.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<point> implements Action {
private final Game game; private final Game game;
public AbstractAction(Game game){ public AbstractAction(Game game){
@ -12,4 +16,19 @@ public abstract class AbstractAction implements Action {
protected Game getGame() { protected Game getGame() {
return game; return game;
} }
protected Point choseRandomPoint(List<Point> 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;
}
} }

View File

@ -1,8 +1,13 @@
package fr.lnl.game.server.games.action; package fr.lnl.game.server.games.action;
import fr.lnl.game.server.utils.Point;
import java.util.List;
public interface Action { public interface Action {
void doAction(); void doAction();
boolean isPossible(); boolean isPossible();
List<Point> getValidPoint();
} }

View File

@ -2,6 +2,9 @@ package fr.lnl.game.server.games.action;
import fr.lnl.game.server.games.Game; 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 java.util.List;
public class DeployShield extends AbstractAction { public class DeployShield extends AbstractAction {
public DeployShield(Game game){ public DeployShield(Game game){
@ -20,6 +23,11 @@ public class DeployShield extends AbstractAction {
return true; return true;
} }
@Override
public List<Point> getValidPoint() {
return null;
}
} }

View File

@ -18,8 +18,7 @@ public class DropBomb extends DropObject {
@Override @Override
public void doAction() { public void doAction() {
List<Point> points = getValidPoint(); List<Point> points = getValidPoint();
Random random = new Random(); Point point = choseRandomPoint(points);
Point point = points.get(random.nextInt(0,points.size()-1));
getGame().getGrid().getBoard().get(point).setB(new Bomb()); getGame().getGrid().getBoard().get(point).setB(new Bomb());
Player player = getGame().getCurrentPlayer(); Player player = getGame().getCurrentPlayer();
player.decrementEnergy(player.getClassPlayer().getBombCost()); player.decrementEnergy(player.getClassPlayer().getBombCost());

View File

@ -15,11 +15,11 @@ public class DropMine extends DropObject {
@Override @Override
public void doAction() { public void doAction() {
List<Point> points = getValidPoint(); List<Point> points = getValidPoint();
Random random = new Random(); Point point = choseRandomPoint(points);
Point point = points.get(random.nextInt(0,points.size()-1));
Mine mine = new Mine(); Mine mine = new Mine();
getGame().getGrid().getBoard().get(point).setB(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 @Override

View File

@ -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.Pair;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -24,19 +25,19 @@ public abstract class DropObject extends AbstractAction {
@Override @Override
public boolean isPossible() { public boolean isPossible() {
return getValidPoint().isEmpty(); return !getValidPoint().isEmpty();
} }
public List<Point> getValidPoint() { public List<Point> getValidPoint() {
List<Point> listMoves = new LinkedList<>(); List<Point> listMoves = new ArrayList<>();
HashMap<Point, Pair<Player, Box>> board = getGame().getGrid().getBoard(); HashMap<Point, Pair<Player, Box>> board = getGame().getGrid().getBoard();
Point position = getGame().getCurrentPlayer().getPoint(); Point position = getGame().getCurrentPlayer().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(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); 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() instanceof Wall){ if(state.getA() == null && state.getB() == null){
listMoves.add(neighbour); listMoves.add(neighbour);
} }
} }

View File

@ -1,17 +1,12 @@
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.Game;
import fr.lnl.game.server.games.grid.Box; import fr.lnl.game.server.games.grid.*;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.Wall;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Pair; import fr.lnl.game.server.utils.Pair;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import java.util.HashMap; import java.util.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class Move extends AbstractAction { public class Move extends AbstractAction {
public Move(Game game) { public Move(Game game) {
@ -21,12 +16,25 @@ public class Move extends AbstractAction {
@Override @Override
public void doAction() { public void doAction() {
List<Point> points = getValidPoint(); List<Point> points = getValidPoint();
Random random = new Random(); Point nextPositon = choseRandomPoint(points);
Point nextPositon = points.get(random.nextInt(0,points.size() - 1));
Player player = getGame().getCurrentPlayer(); Player player = getGame().getCurrentPlayer();
getGame().getGrid().getBoard().get(player.getPoint()).setA(null); getGame().getGrid().getBoard().get(player.getPoint()).setA(null);
getGame().getGrid().getBoard().get(nextPositon).setA(player); getGame().getGrid().getBoard().get(nextPositon).setA(player);
player.setPoint(nextPositon);
player.decrementEnergy(player.getClassPlayer().getMoveCost()); 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 @Override
@ -35,16 +43,16 @@ public class Move extends AbstractAction {
} }
public List<Point> getValidPoint() { public List<Point> getValidPoint() {
List<Point> listMoves = new LinkedList<>(); List<Point> listMoves = new ArrayList<>();
HashMap<Point, Pair<Player, Box>> board = getGame().getGrid().getBoard(); HashMap<Point, Pair<Player, Box>> board = getGame().getGrid().getBoard();
Point position = getGame().getCurrentPlayer().getPoint(); Point position = getGame().getCurrentPlayer().getPoint();
for (int row = -1; row <= 1; row++) { for (int deltarow = -1; deltarow <= 1; deltarow++) {
for (int column = -1; column <= 1; column++) { for (int deltacolumn = -1; deltacolumn <= 1; deltacolumn++) {
if(row == position.getA() + row || column == position.getB() + column){ if(deltarow == 0 || deltacolumn == 0){
if(Grid.caseisValid(position.getA(),row,position.getB(),column)){ if(getGame().getGrid().boardPositionIsValid(position.getA(),deltarow,position.getB(),deltacolumn)){
Point neighbour = new Point(position.getA() + row, position.getB() + column); 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)){
listMoves.add(neighbour); listMoves.add(neighbour);
} }
} }
@ -53,5 +61,4 @@ public class Move extends AbstractAction {
} }
return listMoves; return listMoves;
} }
} }

View File

@ -1,6 +1,9 @@
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.Game;
import fr.lnl.game.server.utils.Point;
import java.util.List;
public class Nothing extends AbstractAction { public class Nothing extends AbstractAction {
@ -15,4 +18,9 @@ public class Nothing extends AbstractAction {
public boolean isPossible() { public boolean isPossible() {
return true; return true;
} }
@Override
public List<Point> getValidPoint() {
return null;
}
} }

View File

@ -1,6 +1,16 @@
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.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 class Shot extends AbstractAction {
public Shot(Game game) { public Shot(Game game) {
@ -9,11 +19,56 @@ public class Shot extends AbstractAction {
@Override @Override
public void doAction() { public void doAction() {
List<Point> 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 @Override
public boolean isPossible() { public boolean isPossible() {
return false; return !getValidPoint().isEmpty();
}
@Override
public List<Point> getValidPoint() {
List<Point> 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);
} }
} }