Improved Move Action
This commit is contained in:
parent
a2270d5de0
commit
01d855435e
@ -1,22 +1,29 @@
|
||||
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;
|
||||
import java.util.Random;
|
||||
|
||||
public abstract class AbstractAction<point> implements Action {
|
||||
public abstract class AbstractAction implements Action {
|
||||
private final Game game;
|
||||
private final Player player;
|
||||
|
||||
public AbstractAction(Game game){
|
||||
public AbstractAction(Game game, Player player){
|
||||
this.game = game;
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
protected Game getGame() {
|
||||
return game;
|
||||
}
|
||||
|
||||
protected Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
protected Point choseRandomPoint(List<Point> getValidPoint) {
|
||||
Point point = null;
|
||||
switch (getValidPoint.size()) {
|
||||
@ -24,6 +31,7 @@ public abstract class AbstractAction<point> implements Action {
|
||||
break;
|
||||
case 1:
|
||||
point = getValidPoint.get(0);
|
||||
break;
|
||||
default: {
|
||||
Random random = new Random();
|
||||
point = getValidPoint.get(random.nextInt(0, getValidPoint.size()));
|
||||
|
@ -7,8 +7,8 @@ import fr.lnl.game.server.utils.Point;
|
||||
import java.util.List;
|
||||
|
||||
public class DeployShield extends AbstractAction {
|
||||
public DeployShield(Game game){
|
||||
super(game);
|
||||
public DeployShield(Game game, Player player){
|
||||
super(game, player);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -9,8 +9,8 @@ import java.util.List;
|
||||
|
||||
public class DropBomb extends DropObject {
|
||||
|
||||
public DropBomb(Game game){
|
||||
super(game);
|
||||
public DropBomb(Game game, Player player){
|
||||
super(game, player);
|
||||
}
|
||||
|
||||
// voir pour la redondance de code au niveau de DropBomb, DropObject,DropMine
|
||||
|
@ -8,8 +8,8 @@ import fr.lnl.game.server.utils.Point;
|
||||
import java.util.List;
|
||||
|
||||
public class DropMine extends DropObject {
|
||||
public DropMine(Game game){
|
||||
super(game);
|
||||
public DropMine(Game game, Player player){
|
||||
super(game, player);
|
||||
}
|
||||
@Override
|
||||
public void doAction() {
|
||||
|
@ -12,8 +12,8 @@ import java.util.List;
|
||||
|
||||
public abstract class DropObject extends AbstractAction {
|
||||
|
||||
public DropObject(Game game){
|
||||
super(game);
|
||||
public DropObject(Game game, Player player){
|
||||
super(game, player);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -8,34 +8,43 @@ import fr.lnl.game.server.utils.Point;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
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
|
||||
public void doAction() {
|
||||
List<Point> points = getValidPoint();
|
||||
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);
|
||||
getGame().getGrid().getBoard().get(this.point).setA(player);
|
||||
player.setPoint(this.point);
|
||||
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){
|
||||
player.decrementEnergy(player.getClassPlayer().getPenaltyMine());
|
||||
getGame().getGrid().getBoard().get(nextPositon).setB(null);
|
||||
getGame().getGrid().getBoard().get(this.point).setB(null);
|
||||
}
|
||||
if(box instanceof Bomb){
|
||||
player.decrementEnergy(player.getClassPlayer().getPenaltyBomb());
|
||||
getGame().getGrid().getBoard().get(nextPositon).setB(null);
|
||||
getGame().getGrid().getBoard().get(this.point).setB(null);
|
||||
}
|
||||
if(box instanceof EnergyBall){
|
||||
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();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Point> getValidPoint() {
|
||||
List<Point> listMoves = new ArrayList<>();
|
||||
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 deltacolumn = -1; deltacolumn <= 1; deltacolumn++) {
|
||||
if(deltarow == 0 || deltacolumn == 0){
|
||||
@ -63,4 +73,28 @@ 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 NotValidDirectionException extends Exception {
|
||||
|
||||
public NotValidDirectionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
@ -1,18 +1,19 @@
|
||||
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 Nothing extends AbstractAction {
|
||||
|
||||
public Nothing(Game game){
|
||||
super(game);
|
||||
public Nothing(Game game, Player player){
|
||||
super(game, player);
|
||||
}
|
||||
|
||||
public Nothing(){
|
||||
super(null);
|
||||
public Nothing() {
|
||||
super(null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -10,8 +10,8 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Shot extends AbstractAction {
|
||||
public Shot(Game game) {
|
||||
super(game);
|
||||
public Shot(Game game, Player player) {
|
||||
super(game, player);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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.utils.Point;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class AbstractPlayer implements Player {
|
||||
|
||||
private final int id;
|
||||
@ -11,7 +13,7 @@ public abstract class AbstractPlayer implements Player {
|
||||
private int energy;
|
||||
private Weapon weapon;
|
||||
private boolean shieldDeploy;
|
||||
private Action[] actions;
|
||||
private List<Action> actions;
|
||||
private final 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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAlive(){
|
||||
return energy > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEnergy() {
|
||||
return energy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Weapon getWeapon() {
|
||||
return weapon;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isShieldDeploy() {
|
||||
return shieldDeploy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnergy(int energy) {
|
||||
this.energy = energy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setShieldDeploy(boolean shieldDeploy) {
|
||||
this.shieldDeploy = shieldDeploy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setWeapon(Weapon weapon) {
|
||||
this.weapon = weapon;
|
||||
}
|
||||
|
||||
public Action[] getActions() {
|
||||
@Override
|
||||
public List<Action> getActions() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
public void setActions(Action[] actions){
|
||||
@Override
|
||||
public void setActions(List<Action> actions){
|
||||
this.actions = actions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassPlayer getClassPlayer() {
|
||||
return classPlayer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Point getPoint() {
|
||||
return point;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPoint(Point point){
|
||||
this.point = point;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decrementEnergy(int energy){
|
||||
this.energy -= energy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void incrementEnergy(int energy){
|
||||
this.energy += energy;
|
||||
}
|
||||
|
@ -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.utils.Point;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Player {
|
||||
Point getPoint();
|
||||
boolean isAlive();
|
||||
@ -14,8 +16,8 @@ public interface Player {
|
||||
void setEnergy(int energy);
|
||||
void setShieldDeploy(boolean shieldDeploy);
|
||||
void setWeapon(Weapon weapon);
|
||||
Action[] getActions();
|
||||
void setActions(Action[] actions);
|
||||
List<Action> getActions();
|
||||
void setActions(List<Action> actions);
|
||||
Action choseAction();
|
||||
ClassPlayer getClassPlayer();
|
||||
void setPoint(Point point);
|
||||
|
@ -15,14 +15,13 @@ public class RandomComputerPlayer extends AbstractPlayer{
|
||||
@Override
|
||||
public Action choseAction() {
|
||||
Action action = null;
|
||||
switch (getActions().length){
|
||||
switch (getActions().size()){
|
||||
case 0 -> action = new Nothing();
|
||||
case 1 -> action = getActions()[0];
|
||||
case 1 -> action = getActions().get(0);
|
||||
default -> {
|
||||
Random random = new Random();
|
||||
while (action == null || !action.isPossible()) {
|
||||
action = getActions()
|
||||
[random.nextInt(0,getActions().length)];
|
||||
action = getActions().get(random.nextInt(0,getActions().size()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package fr.lnl.game.server;
|
||||
|
||||
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.grid.Grid;
|
||||
import fr.lnl.game.server.utils.Point;
|
||||
@ -27,6 +30,25 @@ public class ActionPlayerTest {
|
||||
@Test
|
||||
public void moveActionTest() {
|
||||
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
|
||||
public void shotActionTest(){
|
||||
System.out.println(grid.toString());
|
||||
Shot shot = new Shot(game);
|
||||
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());
|
||||
|
@ -11,6 +11,10 @@ import fr.lnl.game.server.utils.Point;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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;
|
||||
|
||||
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_EAST, grid.getRow() - 1, grid.getColumn() - 1), grid.getBoard().get(new Point(grid.getRow() - 1, grid.getColumn() - 1)).getB());
|
||||
// test placePlayersBRUT (mocked)
|
||||
assertEquals(grid.getPlayers().get(0), grid.getBoard().get(new Point(1, 1)).getA());
|
||||
assertEquals(grid.getPlayers().get(1), grid.getBoard().get(new Point(14, 14)).getA());
|
||||
assertEquals(grid.getPlayers().get(0), grid.getBoard().get(new Point(7, 7)).getA());
|
||||
assertEquals(grid.getPlayers().get(1), grid.getBoard().get(new Point(7, 8)).getA());
|
||||
// test placeEnergyBallBRUT (mocked)
|
||||
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
|
||||
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()){
|
||||
System.out.println(" Tour du joueur " + game.getCurrentPlayer().getId() + " : " +
|
||||
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());
|
||||
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();
|
||||
System.out.println("Action " + action + " : " + game.getCurrentPlayer().getEnergy() +
|
||||
" points de vies restants");
|
||||
|
Loading…
Reference in New Issue
Block a user