Improved Move Action

This commit is contained in:
Quentin Legot 2021-11-05 17:33:42 +01:00
parent a2270d5de0
commit 01d855435e
14 changed files with 159 additions and 46 deletions

View File

@ -1,22 +1,29 @@
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.player.Player;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
public abstract class AbstractAction<point> implements Action { public abstract class AbstractAction implements Action {
private final Game game; private final Game game;
private final Player player;
public AbstractAction(Game game){ public AbstractAction(Game game, Player player){
this.game = game; this.game = game;
this.player = player;
} }
protected Game getGame() { protected Game getGame() {
return game; 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()) {
@ -24,6 +31,7 @@ public abstract class AbstractAction<point> implements Action {
break; break;
case 1: case 1:
point = getValidPoint.get(0); point = getValidPoint.get(0);
break;
default: { default: {
Random random = new Random(); Random random = new Random();
point = getValidPoint.get(random.nextInt(0, getValidPoint.size())); point = getValidPoint.get(random.nextInt(0, getValidPoint.size()));

View File

@ -7,8 +7,8 @@ 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){ public DeployShield(Game game, Player player){
super(game); super(game, player);
} }
@Override @Override

View File

@ -9,8 +9,8 @@ import java.util.List;
public class DropBomb extends DropObject { public class DropBomb extends DropObject {
public DropBomb(Game game){ public DropBomb(Game game, Player player){
super(game); super(game, player);
} }
// voir pour la redondance de code au niveau de DropBomb, DropObject,DropMine // voir pour la redondance de code au niveau de DropBomb, DropObject,DropMine

View File

@ -8,8 +8,8 @@ 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){ public DropMine(Game game, Player player){
super(game); super(game, player);
} }
@Override @Override
public void doAction() { public void doAction() {

View File

@ -12,8 +12,8 @@ import java.util.List;
public abstract class DropObject extends AbstractAction { public abstract class DropObject extends AbstractAction {
public DropObject(Game game){ public DropObject(Game game, Player player){
super(game); super(game, player);
} }
@Override @Override

View File

@ -8,34 +8,43 @@ import fr.lnl.game.server.utils.Point;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
public class Move extends AbstractAction { public class Move extends AbstractAction {
public Move(Game game) {
super(game); private final Point point;
public Move(Game game, Player player, Direction direction) throws NotValidDirectionException {
super(game, player);
HashSet<Point> points = new HashSet<>(getValidPoint());
Point playerPosition = player.getPoint();
Point newPosition = new Point(playerPosition.getA() + direction.deltaX, playerPosition.getB() + direction.deltaY);
if(!points.contains(newPosition)) {
throw new NotValidDirectionException(direction + " isn't a valid position");
}
this.point = newPosition;
} }
@Override @Override
public void doAction() { public void doAction() {
List<Point> points = getValidPoint();
Point nextPositon = choseRandomPoint(points);
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(this.point).setA(player);
player.setPoint(nextPositon); player.setPoint(this.point);
player.decrementEnergy(player.getClassPlayer().getMoveCost()); player.decrementEnergy(player.getClassPlayer().getMoveCost());
Box box = getGame().getGrid().getBoard().get(nextPositon).getB(); Box box = getGame().getGrid().getBoard().get(this.point).getB();
if (box instanceof Mine){ if (box instanceof Mine){
player.decrementEnergy(player.getClassPlayer().getPenaltyMine()); player.decrementEnergy(player.getClassPlayer().getPenaltyMine());
getGame().getGrid().getBoard().get(nextPositon).setB(null); getGame().getGrid().getBoard().get(this.point).setB(null);
} }
if(box instanceof Bomb){ if(box instanceof Bomb){
player.decrementEnergy(player.getClassPlayer().getPenaltyBomb()); player.decrementEnergy(player.getClassPlayer().getPenaltyBomb());
getGame().getGrid().getBoard().get(nextPositon).setB(null); getGame().getGrid().getBoard().get(this.point).setB(null);
} }
if(box instanceof EnergyBall){ if(box instanceof EnergyBall){
player.incrementEnergy(player.getClassPlayer().getGainEnergy()); player.incrementEnergy(player.getClassPlayer().getGainEnergy());
getGame().getGrid().getBoard().get(nextPositon).setB(null); getGame().getGrid().getBoard().get(this.point).setB(null);
} }
} }
@ -44,10 +53,11 @@ public class Move extends AbstractAction {
return !getValidPoint().isEmpty(); return !getValidPoint().isEmpty();
} }
@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 = getGame().getGrid().getBoard();
Point position = getGame().getCurrentPlayer().getPoint(); Point position = getPlayer().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){
@ -63,4 +73,28 @@ 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 NotValidDirectionException extends Exception {
public NotValidDirectionException(String message) {
super(message);
}
}

View File

@ -1,18 +1,19 @@
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.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){ public Nothing(Game game, Player player){
super(game); super(game, player);
} }
public Nothing() { public Nothing() {
super(null); super(null, null);
} }
@Override @Override

View File

@ -10,8 +10,8 @@ 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) { public Shot(Game game, Player player) {
super(game); super(game, player);
} }
@Override @Override

View File

@ -4,6 +4,8 @@ import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.games.weapon.Weapon; 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.List;
public abstract class AbstractPlayer implements Player { public abstract class AbstractPlayer implements Player {
private final int id; private final int id;
@ -11,7 +13,7 @@ public abstract class AbstractPlayer implements Player {
private int energy; private int energy;
private Weapon weapon; private Weapon weapon;
private boolean shieldDeploy; private boolean shieldDeploy;
private Action[] actions; private List<Action> actions;
private final ClassPlayer classPlayer; private final ClassPlayer classPlayer;
public AbstractPlayer(Integer id, Point point, boolean shieldDeploy, ClassPlayer classPlayer) { public AbstractPlayer(Integer id, Point point, boolean shieldDeploy, ClassPlayer classPlayer) {
@ -23,62 +25,77 @@ public abstract class AbstractPlayer implements Player {
this.point = point; this.point = point;
} }
@Override
public boolean isAlive(){ public boolean isAlive(){
return energy > 0; return energy > 0;
} }
@Override
public int getId() { public int getId() {
return id; return id;
} }
@Override
public int getEnergy() { public int getEnergy() {
return energy; return energy;
} }
@Override
public Weapon getWeapon() { public Weapon getWeapon() {
return weapon; return weapon;
} }
@Override
public boolean isShieldDeploy() { public boolean isShieldDeploy() {
return shieldDeploy; return shieldDeploy;
} }
@Override
public void setEnergy(int energy) { public void setEnergy(int energy) {
this.energy = energy; this.energy = energy;
} }
@Override
public void setShieldDeploy(boolean shieldDeploy) { public void setShieldDeploy(boolean shieldDeploy) {
this.shieldDeploy = shieldDeploy; this.shieldDeploy = shieldDeploy;
} }
@Override
public void setWeapon(Weapon weapon) { public void setWeapon(Weapon weapon) {
this.weapon = weapon; this.weapon = weapon;
} }
public Action[] getActions() { @Override
public List<Action> getActions() {
return actions; return actions;
} }
public void setActions(Action[] actions){ @Override
public void setActions(List<Action> actions){
this.actions = actions; this.actions = actions;
} }
@Override
public ClassPlayer getClassPlayer() { public ClassPlayer getClassPlayer() {
return classPlayer; return classPlayer;
} }
@Override
public Point getPoint() { public Point getPoint() {
return point; return point;
} }
@Override
public void setPoint(Point point){ public void setPoint(Point point){
this.point = point; this.point = point;
} }
@Override
public void decrementEnergy(int energy){ public void decrementEnergy(int energy){
this.energy -= energy; this.energy -= energy;
} }
@Override
public void incrementEnergy(int energy){ public void incrementEnergy(int energy){
this.energy += energy; this.energy += energy;
} }

View File

@ -4,6 +4,8 @@ import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.games.weapon.Weapon; 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.List;
public interface Player { public interface Player {
Point getPoint(); Point getPoint();
boolean isAlive(); boolean isAlive();
@ -14,8 +16,8 @@ public interface Player {
void setEnergy(int energy); void setEnergy(int energy);
void setShieldDeploy(boolean shieldDeploy); void setShieldDeploy(boolean shieldDeploy);
void setWeapon(Weapon weapon); void setWeapon(Weapon weapon);
Action[] getActions(); List<Action> getActions();
void setActions(Action[] actions); void setActions(List<Action> actions);
Action choseAction(); Action choseAction();
ClassPlayer getClassPlayer(); ClassPlayer getClassPlayer();
void setPoint(Point point); void setPoint(Point point);

View File

@ -15,14 +15,13 @@ public class RandomComputerPlayer extends AbstractPlayer{
@Override @Override
public Action choseAction() { public Action choseAction() {
Action action = null; Action action = null;
switch (getActions().length){ switch (getActions().size()){
case 0 -> action = new Nothing(); case 0 -> action = new Nothing();
case 1 -> action = getActions()[0]; case 1 -> action = getActions().get(0);
default -> { default -> {
Random random = new Random(); Random random = new Random();
while (action == null || !action.isPossible()) { while (action == null || !action.isPossible()) {
action = getActions() action = getActions().get(random.nextInt(0,getActions().size()));
[random.nextInt(0,getActions().length)];
} }
} }
} }

View File

@ -1,6 +1,9 @@
package fr.lnl.game.server; package fr.lnl.game.server;
import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.games.action.Move;
import fr.lnl.game.server.games.action.NotValidDirectionException;
import fr.lnl.game.server.games.action.Shot; import fr.lnl.game.server.games.action.Shot;
import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
@ -27,6 +30,25 @@ public class ActionPlayerTest {
@Test @Test
public void moveActionTest() { public void moveActionTest() {
Assertions.assertEquals(game.getPlayers().get(0), game.getCurrentPlayer()); Assertions.assertEquals(game.getPlayers().get(0), game.getCurrentPlayer());
Action move = null;
Point oldPoint = game.getCurrentPlayer().getPoint();
Move.Direction savedDirection = null;
for(Move.Direction direction : Move.Direction.values()) {
try {
move = new Move(game, game.getCurrentPlayer(), direction);
savedDirection = direction;
break;
} catch (NotValidDirectionException ignored) {}
}
Assertions.assertNotEquals(null, move);
assert move != null;
move.doAction();
Point newPoint = game.getCurrentPlayer().getPoint();
Assertions.assertEquals(newPoint,
new Point(oldPoint.getA() + savedDirection.getDeltaX(),
oldPoint.getA() + savedDirection.getDeltaY()
)
);
} }
@ -34,7 +56,7 @@ public class ActionPlayerTest {
@Test @Test
public void shotActionTest(){ public void shotActionTest(){
System.out.println(grid.toString()); System.out.println(grid.toString());
Shot shot = new Shot(game); Shot shot = new Shot(game, game.getCurrentPlayer());
List<Point> points = shot.getValidPoint(); List<Point> points = shot.getValidPoint();
System.out.println(points); System.out.println(points);
System.out.println("Before shot " + game.getPlayers().get(1).getEnergy()); System.out.println("Before shot " + game.getPlayers().get(1).getEnergy());

View File

@ -11,6 +11,10 @@ import fr.lnl.game.server.utils.Point;
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.ArrayList;
import java.util.Arrays;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class GridTest { public class GridTest {
@ -33,25 +37,42 @@ public class GridTest {
assertEquals(new Wall(Cardinal.SOUTH_WEST, grid.getRow() - 1, 0), grid.getBoard().get(new Point(grid.getRow() - 1, 0)).getB()); assertEquals(new Wall(Cardinal.SOUTH_WEST, grid.getRow() - 1, 0), grid.getBoard().get(new Point(grid.getRow() - 1, 0)).getB());
assertEquals(new Wall(Cardinal.SOUTH_EAST, grid.getRow() - 1, grid.getColumn() - 1), grid.getBoard().get(new Point(grid.getRow() - 1, grid.getColumn() - 1)).getB()); assertEquals(new Wall(Cardinal.SOUTH_EAST, grid.getRow() - 1, grid.getColumn() - 1), grid.getBoard().get(new Point(grid.getRow() - 1, grid.getColumn() - 1)).getB());
// test placePlayersBRUT (mocked) // test placePlayersBRUT (mocked)
assertEquals(grid.getPlayers().get(0), grid.getBoard().get(new Point(1, 1)).getA()); assertEquals(grid.getPlayers().get(0), grid.getBoard().get(new Point(7, 7)).getA());
assertEquals(grid.getPlayers().get(1), grid.getBoard().get(new Point(14, 14)).getA()); assertEquals(grid.getPlayers().get(1), grid.getBoard().get(new Point(7, 8)).getA());
// test placeEnergyBallBRUT (mocked) // test placeEnergyBallBRUT (mocked)
assertEquals(new EnergyBall(), grid.getBoard().get(new Point(2, 3)).getB()); assertEquals(new EnergyBall(), grid.getBoard().get(new Point(2, 3)).getB());
assertEquals(new EnergyBall(), grid.getBoard().get(new Point(7, 10)).getB()); assertEquals(new EnergyBall(), grid.getBoard().get(new Point(8, 10)).getB());
} }
@Test @Test
public void testPlay(){ public void testPlay(){
for (Player player: game.getPlayers()) {
player.setActions(new Action[]{new Nothing(game), new Shot(game), new Move(game),
new DeployShield(game), new DropBomb(game), new DropMine(game)});
}
while (!game.isOver()){ while (!game.isOver()){
System.out.println(" Tour du joueur " + game.getCurrentPlayer().getId() + " : " + System.out.println(" Tour du joueur " + game.getCurrentPlayer().getId() + " : " +
game.getCurrentPlayer().getEnergy() + " points de vies restants"); game.getCurrentPlayer().getEnergy() + " points de vies restants");
Player player = game.getCurrentPlayer();
ArrayList<Action> actions = new ArrayList<>();
for(Move.Direction direction : Move.Direction.values()) {
try {
actions.add(new Move(game, player, direction));
} catch (NotValidDirectionException ignored){}
}
actions.addAll(Arrays.asList(new Nothing(game, player), new Shot(game, player),
new DeployShield(game, player), new DropBomb(game, player), new DropMine(game, player)));
player.setActions(actions);
System.out.println(game.getGrid().toString()); System.out.println(game.getGrid().toString());
Action action = game.getCurrentPlayer().choseAction(); Action action = null;
switch (player.getActions().size()){
case 0 -> action = new Nothing(game, player);
case 1 -> action = game.getCurrentPlayer().getActions().get(0);
default -> {
Random random = new Random();
while (action == null || !action.isPossible()) {
action = game.getCurrentPlayer().getActions().get(
random.nextInt(0,game.getCurrentPlayer().getActions().size())
);
}
}
}
action.doAction(); action.doAction();
System.out.println("Action " + action + " : " + game.getCurrentPlayer().getEnergy() + System.out.println("Action " + action + " : " + game.getCurrentPlayer().getEnergy() +
" points de vies restants"); " points de vies restants");