Fix actions and display bugs

This commit is contained in:
Katchan 2021-12-07 15:31:18 +01:00
parent 19dceafb7d
commit f4c3b09078
26 changed files with 105 additions and 43 deletions

View File

@ -18,8 +18,8 @@ public class Cell extends Rectangle {
private static Image PLAYER_IMAGE = new Image("player.png");
private static Image ENERGY_BALL_IMAGE = new Image("energyBall.png");
private static Image BOMB_IMAGE = new Image("bomb.jpg");
private static Image MINE_IMAGE = new Image("mine.webp");
private static Image BOMB_IMAGE = new Image("bomb.png");
private static Image MINE_IMAGE = new Image("mine.png");
private static Image WALL_IMAGE = new Image("wall.jpg");
public Cell(int x, int y){

Binary file not shown.

Before

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.6 KiB

View File

@ -42,9 +42,7 @@ public class Game {
}
public void play() {
if (currentPlayer instanceof ComputerPlayer player) {
selectedAction = player.choseAction();
}
selectedAction = currentPlayer.choseAction();
selectedAction.doAction();
countdownGridElementsUpdate();
nextCurrentPlayer();
@ -52,7 +50,6 @@ public class Game {
if(isOver()) {
gameFinishEvent.updateModel(null);
}
}
private void countdownGridElementsUpdate() {
@ -67,10 +64,10 @@ public class Game {
actions.add(new Move(this, player, direction));
} catch (NotValidDirectionException ignored){}
try {
new DropBomb(this, player, direction);
actions.add(new DropBomb(this, player, direction));
} catch (NotValidDirectionException ignored) {}
try {
new DropMine(this, player, direction);
actions.add(new DropMine(this, player, direction));
} catch (NotValidDirectionException ignored) {}
try {
actions.add(new Shot(this, player, direction));

View File

@ -71,7 +71,7 @@ public class Shot extends AbstractAction {
return null;
for(int i = 0; i < range; i++) {
Point neighbour = new Point(point.getA() + deltaX + (i * deltaX), point.getB() + deltaY + (i * deltaY));
if(game.getGrid().boardPositionIsValid(point)) {
if(game.getGrid().boardPositionIsValid(neighbour)) {
if(game.getGrid().getBoard().get(neighbour).getB() instanceof Wall) {
return null;
}

View File

@ -14,6 +14,7 @@ public abstract class AbstractBuildStrategy implements BuildStrategy{
build();
}
@Override
public void build() {
initGrid();
initPlaceInternWall(WALL_PROBABILITY);

View File

@ -4,6 +4,7 @@ import fr.lnl.game.server.games.grid.Grid;
public interface BuildStrategy {
void build();
void initPlacePlayers();
Grid getGrid();

View File

@ -1,6 +1,5 @@
package fr.lnl.game.server.games.player;
import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.utils.Point;
public abstract class ComputerPlayer extends AbstractPlayer {
@ -8,6 +7,4 @@ public abstract class ComputerPlayer extends AbstractPlayer {
public ComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) {
super(id, point, false, classPlayer);
}
public abstract Action choseAction();
}

View File

@ -1,6 +1,9 @@
package fr.lnl.game.server.games.player;
import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.utils.Maths;
import fr.lnl.game.server.utils.Point;
import java.util.Scanner;
public class HumanPlayer extends AbstractPlayer {
@ -8,4 +11,22 @@ public class HumanPlayer extends AbstractPlayer {
super(id, point,false, classPlayer);
}
@Override
public Action choseAction() {
String error = "Veuillez renseigner une valeur numérique comprise entre 1 et " + getActions().size();
Action action = null;
Scanner scanner = new Scanner(System.in);
do{
System.out.println("Choisissez une action :");
for (int i = 0; i < getActions().size(); i++) {
System.out.println((i + 1) + " : " + getActions().get(i).getClass().getSimpleName());
}
String entry = scanner.next();
int value = Maths.testInteger(entry,scanner,error);
if(value >= 1 && value <= getActions().size()){
action = getActions().get(value - 1);
}
}while(action == null);
return action;
}
}

View File

@ -8,6 +8,8 @@ import java.util.List;
public interface Player {
Action choseAction();
Point getPosition();
boolean isAlive();

View File

@ -0,0 +1,6 @@
package fr.lnl.game.server.utils;
public class Error {
public static final String Entry_Error_Message = "\033[0;31mErreur de saisie\033[0m : ";
}

View File

@ -0,0 +1,40 @@
package fr.lnl.game.server.utils;
import java.util.Scanner;
public class Maths {
public static int testInteger(String entry, Scanner scanner, String error) {
while (!isNumeric(entry)) {
System.out.println(Error.Entry_Error_Message + error);
entry = scanner.next();
}
return Integer.parseInt(entry);
}
public static float testFloat(String entry, Scanner scanner) {
while (!isFloat(entry)) {
System.out.println("\033[0;31mErreur de saisie\033[0m : Veuillez rentrer une valeur correcte !");
entry = scanner.next();
}
return Integer.parseInt(entry);
}
public static boolean isFloat(String strNum) {
try {
float d = Float.parseFloat(strNum);
} catch (NumberFormatException | NullPointerException nfe) {
return false;
}
return true;
}
public static boolean isNumeric(String strNum) {
try {
int d = Integer.parseInt(strNum);
} catch (NumberFormatException | NullPointerException nfe) {
return false;
}
return true;
}
}

View File

@ -4,12 +4,17 @@ import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.action.*;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.elements.Bomb;
import fr.lnl.game.server.games.player.ClassPlayer;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.games.player.RandomComputerPlayer;
import fr.lnl.game.server.utils.Point;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
public class ActionPlayerTest {
private Grid grid;
@ -17,7 +22,9 @@ public class ActionPlayerTest {
@BeforeEach
public void mock() {
Mock mock = new Mock();
List<Player> players = Arrays.asList(new RandomComputerPlayer(1,null, ClassPlayer.DEFAULT),
new RandomComputerPlayer(2,null, ClassPlayer.DEFAULT));
Mock mock = new Mock(players);
this.grid = mock.grid;
this.game = mock.game;
Assertions.assertEquals(game.getPlayers().get(0), game.getCurrentPlayer());

View File

@ -5,12 +5,17 @@ import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.elements.Box;
import fr.lnl.game.server.games.grid.elements.EnergyBall;
import fr.lnl.game.server.games.grid.elements.Wall;
import fr.lnl.game.server.games.player.ClassPlayer;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.games.player.RandomComputerPlayer;
import fr.lnl.game.server.utils.Pair;
import fr.lnl.game.server.utils.Point;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class GridTest {
@ -20,7 +25,9 @@ public class GridTest {
@BeforeEach
public void mock() {
Mock mock = new Mock();
List<Player> players = Arrays.asList(new RandomComputerPlayer(1,null, ClassPlayer.DEFAULT),
new RandomComputerPlayer(2,null, ClassPlayer.DEFAULT));
Mock mock = new Mock(players);
grid = mock.grid;
game = mock.game;
}

View File

@ -21,9 +21,7 @@ public class Mock {
public Game game;
public Grid grid;
public Mock() {
List<Player> players = Arrays.asList(new RandomComputerPlayer(1,null, ClassPlayer.DEFAULT),
new RandomComputerPlayer(2,null, ClassPlayer.DEFAULT));
public Mock(List<Player> players) {
this.buildStrategy = new LockStrategy(new Grid(16,16, players),0.80F, 0.95F);
game = new Game(buildStrategy, players, new GameFinishEvent());
this.grid = buildStrategy.getGrid();

View File

@ -4,10 +4,15 @@ 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.ClassPlayer;
import fr.lnl.game.server.games.player.HumanPlayer;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.games.player.RandomComputerPlayer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class GameTest {
@ -17,39 +22,19 @@ public class GameTest {
@BeforeEach
public void mock() {
Mock mock = new Mock();
List<Player> players = Arrays.asList(new HumanPlayer(1,null, ClassPlayer.DEFAULT),
new HumanPlayer(2,null, ClassPlayer.DEFAULT));
Mock mock = new Mock(players);
grid = mock.grid;
game = mock.game;
}
@Test
public void testPlay(){
System.out.println(game.getGrid());
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();
game.play();
}
System.out.println(game.getGrid().toString());
Player winner = game.getWinner();
System.out.println(winner != null ? ("Le joueur gagnant : " + winner.getId()) : ("Partie nulle, aucun gagnant"));
System.out.println("Le gagnant est " + game.getWinner().toString());
}
}