add javadoc to Game.java, delete InterfaceAction (was useless), improved grid factory calls consistency

This commit is contained in:
Quentin Legot 2021-12-08 21:18:55 +01:00
parent c856a03621
commit f7b6f32ae2
6 changed files with 67 additions and 18 deletions

View File

@ -51,7 +51,7 @@ public class App extends Application {
public static void startGame(ViewLambda lambda) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
InstantiationException, IllegalAccessException {
List<Player> players = parsePlayers();
GridFactoryBuilder builder = LockGridFactoryBuilder.create().energyProbability(0.95F).wallProbability(0.80F).playersList(players).gridDimensions(12, 12);
GridFactoryBuilder builder = LockGridFactoryBuilder.create().energyProbability(0.95F).wallProbability(0.80F).gridDimensions(12, 12);
game = new Game(builder, players);
for (Player player : game.getPlayers()) {
playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player)));

View File

@ -16,32 +16,40 @@ import static java.util.function.Predicate.not;
public class Game {
private final GridFactoryBuilder buildStrategy;
private final Grid grid;
private final List<Player> players;
private Player currentPlayer;
private Action selectedAction = null;
/**
* @param buildStrategy used to build a grid
* @param players a list players in the game
* @throws IllegalArgumentException if number of players given in parameters is too many or inferior to 2
*/
public Game(GridFactoryBuilder buildStrategy, List<Player> players) throws IllegalArgumentException {
this.grid = buildStrategy.build();
this.grid = buildStrategy.playersList(players).build();
if(players.size() < 2)
throw new IllegalArgumentException("The game need 2 or more player to start");
if(players.size() > grid.getNumberNeutralBox()){
throw new IllegalArgumentException("There are too many players for the number of box available");
}
this.buildStrategy = buildStrategy;
this.players = players;
this.currentPlayer = players.get(0);
initGame();
initGame(buildStrategy);
}
public void initGame(){
/**
* Initialize a game by placing players on the grid and by generating current player available actions
* @param buildStrategy builder used to create a grid
*/
public void initGame(GridFactoryBuilder buildStrategy){
buildStrategy.initPlacePlayers();
currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer));
}
/**
*
* Game "main" method, call by a controller after human chose an action or when a computer player play an action
* Method is call everytime an action has been chosen by a human or when aa computer player need to play
* @return true if game is over, false otherwise
*/
public boolean play() {
@ -56,17 +64,28 @@ public class Game {
return isOver();
}
/**
* Remove dead players from the grid
*/
private void gridPlayersUpdate(){
for (Player player: getPlayersNotAlive().toList()) {
getGrid().getBoard().get(player.getPosition()).setA(null);
}
}
/**
* play grid's elements that use a timer like {@link fr.lnl.game.server.games.grid.elements.Bomb} at each game tick
*/
private void countdownGridElementsUpdate() {
List<CountdownBox> countdownBoxes = this.getGrid().getAllCountdownElements();
countdownBoxes.forEach(CountdownBox::update);
}
/**
* Used to list all actions a player can execute at current time
* @param player the player to generate actions
* @return a list of available actions
*/
public List<Action> generateAndGetPlayerActions(Player player) {
List<Action> actions = new ArrayList<>();
for(Direction direction : Direction.values()) {
@ -87,29 +106,52 @@ public class Game {
return actions;
}
/**
*
* @return a list of alive players
*/
public Stream<Player> getPlayersAlive() {
return players.parallelStream().filter(Player::isAlive);
}
/**
* Opposite of {@link Game#getPlayersAlive()}
* @return a list of dead players
*/
public Stream<Player> getPlayersNotAlive() {
return players.parallelStream().filter(not(Player::isAlive));
}
/**
* A game is over if the number of alive players is inferior to 2
* @return true if game is over, false otherwise
*/
public boolean isOver() {
return getPlayersAlive().count() <= 1;
}
/**
*
* @return the winner of the game if exists, null otherwise.<br>
* return the only remaining alive player when it exists, or null if everyone is dead, per example when a bomb kill
* the 2 remaining players
*/
public Player getWinner() {
// On part du principe que isOver est forcément appelé avant d'appeler getWinner
return getPlayersAlive().findFirst().orElse(null);
}
/**
*
* @return the player who is currently playing
*/
public Player getCurrentPlayer() {
return currentPlayer;
}
/**
* Change player to the next available in the list
* Change player to the next available in the list.<br>
* We set its shield deploy state to false.
*/
public void nextCurrentPlayer() {
do {
@ -121,6 +163,9 @@ public class Game {
currentPlayer.setShieldDeploy(false); // on reset son état
}
/**
* @param current_player the new current player
*/
public void setCurrentPlayer(Player current_player) {
this.currentPlayer = current_player;
}
@ -133,10 +178,17 @@ public class Game {
return players;
}
/**
*
* @return action selected by current player if not already executed or last player if already executed
*/
public Action getSelectedAction() {
return selectedAction;
}
/**
* @param selectedAction set the action selected by current player before doing it
*/
public void setSelectedAction(Action selectedAction) {
this.selectedAction = selectedAction;
}

View File

@ -1,8 +0,0 @@
package fr.lnl.game.server.games;
public enum InterfaceAction {
SELECT_ACTION,
DIALOG_TO_CONTINUE_COMPUTER_IA
}

View File

@ -53,7 +53,8 @@ public interface GridFactoryBuilder {
Grid build();
/**
* call when initializing the game, it'll place player depending on the strategy used by its implementation
* call when initializing the game, it'll place player depending on the strategy used by its implementation,
* need to be call after build, call an NullPointerException otherwise
*/
void initPlacePlayers();

View File

@ -0,0 +1,4 @@
/**
* Game package, contains all classes and sub-packages mainly related game works
*/
package fr.lnl.game.server.games;

View File

@ -14,7 +14,7 @@ public class Mock {
public Grid grid;
public Mock(List<Player> players) {
this.buildStrategy = MockGridFactoryBuilder.create().gridDimensions(16, 16).playersList(players).wallProbability(0.80F).energyProbability(0.95F);
this.buildStrategy = MockGridFactoryBuilder.create().gridDimensions(16, 16).wallProbability(0.80F).energyProbability(0.95F);
game = new Game(buildStrategy, players);
this.grid = game.getGrid();
}