Reworked dropBomb and DropMine + reordered tests
This commit is contained in:
parent
360a84c403
commit
b702a4030f
@ -1,8 +1,11 @@
|
||||
package fr.lnl.game.server.games;
|
||||
|
||||
import fr.lnl.game.server.games.action.*;
|
||||
import fr.lnl.game.server.games.grid.Grid;
|
||||
import fr.lnl.game.server.games.player.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Game {
|
||||
@ -23,6 +26,26 @@ public class Game {
|
||||
|
||||
}
|
||||
|
||||
protected List<Action> generateAndGetPlayerActions(Player player) {
|
||||
List<Action> actions = new ArrayList<>();
|
||||
for(Direction direction : Direction.values()) {
|
||||
try {
|
||||
actions.add(new Move(this, player, direction));
|
||||
} catch (NotValidDirectionException ignored){}
|
||||
try {
|
||||
new DropBomb(this, player, direction);
|
||||
} catch (NotValidDirectionException ignored) {}
|
||||
try {
|
||||
new DropMine(this, player, direction);
|
||||
} catch (NotValidDirectionException ignored) {}
|
||||
try {
|
||||
actions.add(new Shot(this, player, direction));
|
||||
} catch (NotValidDirectionException | NoMoreBulletInWeaponException ignored) {}
|
||||
}
|
||||
actions.addAll(Arrays.asList(new Nothing(), new DeployShield(player)));
|
||||
return actions;
|
||||
}
|
||||
|
||||
public boolean isOver() {
|
||||
return players.parallelStream().filter(player -> !player.isAlive()).count() == 1;
|
||||
}
|
||||
|
@ -3,24 +3,15 @@ package fr.lnl.game.server.games.action;
|
||||
import fr.lnl.game.server.games.Game;
|
||||
import fr.lnl.game.server.games.grid.Bomb;
|
||||
import fr.lnl.game.server.games.player.Player;
|
||||
import fr.lnl.game.server.utils.Point;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DropBomb extends DropObject {
|
||||
|
||||
public DropBomb(Game game, Player player){
|
||||
super(game, player);
|
||||
public DropBomb(Game game, Player player, Direction direction) throws NotValidDirectionException {
|
||||
super(game, player, direction);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated même principe que {@link Shot#doAction()}
|
||||
*/
|
||||
@Deprecated
|
||||
@Override
|
||||
public void doAction() {
|
||||
List<Point> points = getValidPoint();
|
||||
Point point = choseRandomPoint(points);
|
||||
game.getGrid().getBoard().get(point).setB(new Bomb());
|
||||
player.decrementEnergy(player.getClassPlayer().getBombCost());
|
||||
}
|
||||
@ -30,10 +21,5 @@ public class DropBomb extends DropObject {
|
||||
return super.isPossible();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Point> getValidPoint() {
|
||||
return super.getValidPoint();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -9,8 +9,9 @@ import java.util.List;
|
||||
|
||||
public class DropMine extends DropObject {
|
||||
|
||||
public DropMine(Game game, Player player){
|
||||
super(game, player);
|
||||
public DropMine(Game game, Player player, Direction direction) throws NotValidDirectionException {
|
||||
super(game, player, direction);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -19,20 +20,8 @@ public class DropMine extends DropObject {
|
||||
@Deprecated
|
||||
@Override
|
||||
public void doAction() {
|
||||
List<Point> points = getValidPoint();
|
||||
Point point = choseRandomPoint(points);
|
||||
game.getGrid().getBoard().get(point).setB(new Mine());
|
||||
game.getCurrentPlayer().decrementEnergy(player.getClassPlayer().getMineCost());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPossible() {
|
||||
return super.isPossible();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Point> getValidPoint() {
|
||||
return super.getValidPoint();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,8 +12,17 @@ import java.util.List;
|
||||
|
||||
public abstract class DropObject extends AbstractAction {
|
||||
|
||||
public DropObject(Game game, Player player){
|
||||
protected final Point point;
|
||||
|
||||
public DropObject(Game game, Player player, Direction direction) throws NotValidDirectionException {
|
||||
super(game, player);
|
||||
List<Point> points = getValidPoint();
|
||||
Point playerPosition = player.getPoint();
|
||||
Point dropDirection = new Point(playerPosition.getA() + direction.getDeltaX(), playerPosition.getB() + direction.getDeltaY());
|
||||
if(!points.contains(dropDirection)) {
|
||||
throw new NotValidDirectionException(direction + " isn't a valid position");
|
||||
}
|
||||
this.point = dropDirection;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -2,6 +2,7 @@ package fr.lnl.game.server;
|
||||
|
||||
import fr.lnl.game.server.games.Game;
|
||||
import fr.lnl.game.server.games.action.*;
|
||||
import fr.lnl.game.server.games.grid.Bomb;
|
||||
import fr.lnl.game.server.games.grid.Grid;
|
||||
import fr.lnl.game.server.games.player.Player;
|
||||
import fr.lnl.game.server.utils.Point;
|
||||
@ -71,4 +72,22 @@ public class ActionPlayerTest {
|
||||
Assertions.assertEquals(currentEnergyOtherPlayer - otherPlayer.getClassPlayer().getPenaltyShoot(), otherPlayer.getEnergy());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dropBombActionTest() {
|
||||
Player player = game.getCurrentPlayer();
|
||||
Action action = null;
|
||||
Direction savedDirection = null;
|
||||
for(Direction direction : Direction.values()) {
|
||||
try {
|
||||
action = new DropBomb(game, game.getCurrentPlayer(), direction);
|
||||
savedDirection = direction;
|
||||
break;
|
||||
} catch (NotValidDirectionException ignored) {}
|
||||
}
|
||||
Assertions.assertNotNull(action);
|
||||
action.doAction();
|
||||
Point bombPosition = new Point(player.getPoint().getA() + savedDirection.getDeltaX(), player.getPoint().getB() + savedDirection.getDeltaY());
|
||||
Assertions.assertTrue(game.getGrid().getBoard().get(bombPosition).getB() instanceof Bomb);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -44,46 +44,4 @@ public class GridTest {
|
||||
assertEquals(new EnergyBall(), grid.getBoard().get(new Point(8, 10)).getB());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlay(){
|
||||
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(Direction direction : Direction.values()) {
|
||||
try {
|
||||
actions.add(new Move(game, player, direction));
|
||||
} catch (NotValidDirectionException ignored){}
|
||||
try {
|
||||
actions.add(new Shot(game, player, direction));
|
||||
} catch (NotValidDirectionException | NoMoreBulletInWeaponException ignored) {}
|
||||
}
|
||||
actions.addAll(Arrays.asList(new Nothing(), new DeployShield(player), new DropBomb(game, player),
|
||||
new DropMine(game, player)));
|
||||
player.setActions(actions);
|
||||
System.out.println(game.getGrid().toString());
|
||||
Action action = null;
|
||||
switch (player.getActions().size()){
|
||||
case 0 -> action = new Nothing();
|
||||
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");
|
||||
game.nextCurrentPlayer();
|
||||
}
|
||||
System.out.println(game.getGrid().toString());
|
||||
Player winner = game.getWinner();
|
||||
System.out.println(winner != null ? ("Le joueur gagnant : " + winner.getId()) : ("Partie nulle, aucun gagnant"));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,8 +15,8 @@ import java.util.List;
|
||||
|
||||
public class Mock {
|
||||
|
||||
Grid grid;
|
||||
Game game;
|
||||
public Grid grid;
|
||||
public Game game;
|
||||
|
||||
public Mock() {
|
||||
List<Player> players = Arrays.asList(new RandomComputerPlayer(1,null, ClassPlayer.DEFAULT),
|
||||
|
55
server/src/test/java/fr/lnl/game/server/games/GameTest.java
Normal file
55
server/src/test/java/fr/lnl/game/server/games/GameTest.java
Normal file
@ -0,0 +1,55 @@
|
||||
package fr.lnl.game.server.games;
|
||||
|
||||
import fr.lnl.game.server.Mock;
|
||||
import fr.lnl.game.server.games.action.Action;
|
||||
import fr.lnl.game.server.games.action.Nothing;
|
||||
import fr.lnl.game.server.games.grid.Grid;
|
||||
import fr.lnl.game.server.games.player.Player;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class GameTest {
|
||||
|
||||
private Grid grid;
|
||||
private Game game;
|
||||
|
||||
@BeforeEach
|
||||
public void mock() {
|
||||
Mock mock = new Mock();
|
||||
grid = mock.grid;
|
||||
game = mock.game;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlay(){
|
||||
while (!game.isOver()){
|
||||
System.out.println(" Tour du joueur " + game.getCurrentPlayer().getId() + " : " +
|
||||
game.getCurrentPlayer().getEnergy() + " points de vies restants");
|
||||
Player player = game.getCurrentPlayer();
|
||||
player.setActions(game.generateAndGetPlayerActions(player));
|
||||
System.out.println(game.getGrid().toString());
|
||||
Action action = null;
|
||||
switch (player.getActions().size()){
|
||||
case 0 -> action = new Nothing();
|
||||
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");
|
||||
game.nextCurrentPlayer();
|
||||
}
|
||||
System.out.println(game.getGrid().toString());
|
||||
Player winner = game.getWinner();
|
||||
System.out.println(winner != null ? ("Le joueur gagnant : " + winner.getId()) : ("Partie nulle, aucun gagnant"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user