Fix game not launching + add supprot to Computer Players (AI) + fix view not updating at each Game#play 's loop

This commit is contained in:
Quentin Legot 2021-11-29 19:22:43 +01:00
parent f73dfc8bb9
commit de8c258322
11 changed files with 109 additions and 15 deletions

View File

@ -21,6 +21,10 @@ javafx {
modules = [ 'javafx.controls', 'javafx.graphics' ] modules = [ 'javafx.controls', 'javafx.graphics' ]
} }
compileJava {
options.encoding = 'UTF-8'
}
application { application {
// Define the main class for the application. // Define the main class for the application.
mainClassName = 'fr.lnl.game.client.App' mainClassName = 'fr.lnl.game.client.App'

View File

@ -1,4 +1,6 @@
package fr.lnl.game.client; package fr.lnl.game.client;
import fr.lnl.game.server.listener.GameFinishEvent;
import fr.lnl.game.client.listener.UpdateViewEvent;
import fr.lnl.game.client.view.AbstractView; import fr.lnl.game.client.view.AbstractView;
import fr.lnl.game.client.view.Terminal; import fr.lnl.game.client.view.Terminal;
import fr.lnl.game.client.view.Window; import fr.lnl.game.client.view.Window;
@ -38,7 +40,7 @@ public class App extends Application {
public static void startGame(ViewLambda lambda) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, public static void startGame(ViewLambda lambda) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
InstantiationException, IllegalAccessException { InstantiationException, IllegalAccessException {
List<Player> players = parsePlayers(); List<Player> players = parsePlayers();
game = new Game(new Grid(12, 12, players), players); game = new Game(new Grid(12, 12, players), players, new UpdateViewEvent(), new GameFinishEvent());
for (Player player : game.getPlayers()) { for (Player player : game.getPlayers()) {
playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player))); playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player)));
} }
@ -56,7 +58,7 @@ public class App extends Application {
| IllegalAccessException e) { | IllegalAccessException e) {
throw new CrashException(e.getMessage(), e); throw new CrashException(e.getMessage(), e);
} }
new Thread(() -> game.play()); new Thread(() -> game.play()).start();
updateView(); updateView();
} }
@ -67,8 +69,7 @@ public class App extends Application {
| IllegalAccessException e) { | IllegalAccessException e) {
throw new CrashException(e.getMessage(), e); throw new CrashException(e.getMessage(), e);
} }
new Thread(() -> game.play()); new Thread(() -> game.play()).start();
updateView();
} }
public static List<Player> parsePlayers() throws IllegalArgumentException, NoSuchMethodException, public static List<Player> parsePlayers() throws IllegalArgumentException, NoSuchMethodException,

View File

@ -0,0 +1,12 @@
package fr.lnl.game.client.listener;
import fr.lnl.game.client.App;
import fr.lnl.game.server.listener.AbstractModelListening;
public class UpdateViewEvent extends AbstractModelListening {
@Override
public void updateModel(Object obj) {
App.updateView();
}
}

View File

@ -12,6 +12,10 @@ dependencies {
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
} }
compileJava {
options.encoding = 'UTF-8'
}
application { application {
mainClassName = 'fr.lnl.game.server.ServerMain' mainClassName = 'fr.lnl.game.server.ServerMain'
} }

View File

@ -2,9 +2,12 @@ package fr.lnl.game.server.games;
import fr.lnl.game.server.games.action.*; import fr.lnl.game.server.games.action.*;
import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.player.ComputerPlayer;
import fr.lnl.game.server.games.player.HumanPlayer;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.listener.AwakeGame; import fr.lnl.game.server.listener.AwakeGame;
import fr.lnl.game.server.listener.ModelListener; import fr.lnl.game.server.listener.ModelListener;
import fr.lnl.game.server.utils.Point;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -15,28 +18,56 @@ public class Game {
private final Grid grid; private final Grid grid;
private final List<Player> players; private final List<Player> players;
private final ModelListener viewUpdateEvent;
private final ModelListener gameFinishEvent;
private Player currentPlayer; private Player currentPlayer;
private InterfaceAction interfaceAction; private InterfaceAction interfaceAction;
private Action selectedAction = null; private Action selectedAction = null;
ModelListener awakeEvent;
private final Object lock = new Object(); private final Object lock = new Object();
public Game(Grid grid, List<Player> players) throws IllegalArgumentException { public Game(Grid grid, List<Player> players, ModelListener viewUpdate, ModelListener gameFinishEvent) throws IllegalArgumentException {
if(players.size() < 2) if(players.size() < 2)
throw new IllegalArgumentException("The game need 2 or more player to start"); throw new IllegalArgumentException("The game need 2 or more player to start");
this.players = players; this.players = players;
this.currentPlayer = players.get(0); this.currentPlayer = players.get(0);
this.grid = grid; this.grid = grid;
this.viewUpdateEvent = viewUpdate;
this.gameFinishEvent = gameFinishEvent;
placePlayersBRUT();
}
/**
* @deprecated utiliser pour le moment, nécessite une meilleure implémentation pour savoir ou placé les joueurs
*/
@Deprecated
public void placePlayersBRUT(){
grid.getBoard().get(new Point(7,7)).setA(grid.getPlayers().get(0));
grid.getPlayers().get(0).setPoint(new Point(7, 7));
grid.getBoard().get(new Point(7,8)).setA(grid.getPlayers().get(1));
grid.getPlayers().get(1).setPoint(new Point(7, 8));
} }
public void play() { public void play() {
while(!isOver()) { while(!isOver()) {
ModelListener awakeEvent = new AwakeGame(this); awakeEvent = new AwakeGame(this);
generateAndGetPlayerActions(currentPlayer); currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer));
if(currentPlayer instanceof HumanPlayer) {
interfaceAction = InterfaceAction.SELECT_ACTION; interfaceAction = InterfaceAction.SELECT_ACTION;
waitForInterfaceEvent(); waitForInterfaceEvent();
selectedAction.doAction(); selectedAction.doAction();
nextCurrentPlayer(); } else {
ComputerPlayer player = (ComputerPlayer) currentPlayer;
Action action = player.choseAction();
action.doAction();
} }
selectedAction = null;
nextCurrentPlayer();
viewUpdateEvent.updateModel(null);
gameFinishEvent.updateModel(null);
}
} }
private void waitForInterfaceEvent() { private void waitForInterfaceEvent() {

View File

@ -0,0 +1,10 @@
package fr.lnl.game.server.games.player;
import fr.lnl.game.server.utils.Point;
public abstract class ComputerPlayer extends AbstractPlayer {
public ComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) {
super(id, point, false, classPlayer);
}
}

View File

@ -6,10 +6,10 @@ import fr.lnl.game.server.utils.Point;
import java.util.Random; import java.util.Random;
public class RandomComputerPlayer extends AbstractPlayer{ public class RandomComputerPlayer extends ComputerPlayer {
public RandomComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) { public RandomComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) {
super(id,point,false, classPlayer); super(id,point, classPlayer);
} }
@Override @Override

View File

@ -3,10 +3,10 @@ package fr.lnl.game.server.games.player;
import fr.lnl.game.server.games.action.Action; import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
public class StrategyComputerPlayer extends AbstractPlayer{ public class StrategyComputerPlayer extends ComputerPlayer {
public StrategyComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) { public StrategyComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) {
super(id,point,false, classPlayer); super(id,point, classPlayer);
} }
@Override @Override

View File

@ -0,0 +1,20 @@
package fr.lnl.game.server.listener;
import fr.lnl.game.server.listener.AbstractModelListening;
public class GameFinishEvent extends AbstractModelListening {
private final Object lock = new Object();
@Override
public void updateModel(Object obj) {
synchronized (lock) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.exit(0);
}
}

View File

@ -1,12 +1,14 @@
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.MockViewUpdate;
import fr.lnl.game.server.games.grid.EnergyBall; import fr.lnl.game.server.games.grid.EnergyBall;
import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.Wall; import fr.lnl.game.server.games.grid.Wall;
import fr.lnl.game.server.games.player.ClassPlayer; import fr.lnl.game.server.games.player.ClassPlayer;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.games.player.RandomComputerPlayer; import fr.lnl.game.server.games.player.RandomComputerPlayer;
import fr.lnl.game.server.listener.GameFinishEvent;
import fr.lnl.game.server.utils.Cardinal; import fr.lnl.game.server.utils.Cardinal;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
@ -25,7 +27,7 @@ public class Mock {
placePlayersBRUT(); placePlayersBRUT();
placeEnergyBallBRUT(); placeEnergyBallBRUT();
placeInternWallBRUT(); placeInternWallBRUT();
game = new Game(grid, players); game = new Game(grid, players, new MockViewUpdate(), new GameFinishEvent());
} }
public void placePlayersBRUT(){ public void placePlayersBRUT(){

View File

@ -0,0 +1,10 @@
package fr.lnl.game.server.games;
import fr.lnl.game.server.listener.AbstractModelListening;
public class MockViewUpdate extends AbstractModelListening {
@Override
public void updateModel(Object obj) {
// no op
}
}