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' ]
}
compileJava {
options.encoding = 'UTF-8'
}
application {
// Define the main class for the application.
mainClassName = 'fr.lnl.game.client.App'

View File

@ -1,4 +1,6 @@
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.Terminal;
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,
InstantiationException, IllegalAccessException {
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()) {
playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player)));
}
@ -56,7 +58,7 @@ public class App extends Application {
| IllegalAccessException e) {
throw new CrashException(e.getMessage(), e);
}
new Thread(() -> game.play());
new Thread(() -> game.play()).start();
updateView();
}
@ -67,8 +69,7 @@ public class App extends Application {
| IllegalAccessException e) {
throw new CrashException(e.getMessage(), e);
}
new Thread(() -> game.play());
updateView();
new Thread(() -> game.play()).start();
}
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'
}
compileJava {
options.encoding = 'UTF-8'
}
application {
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.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.listener.AwakeGame;
import fr.lnl.game.server.listener.ModelListener;
import fr.lnl.game.server.utils.Point;
import java.util.ArrayList;
import java.util.Arrays;
@ -15,28 +18,56 @@ public class Game {
private final Grid grid;
private final List<Player> players;
private final ModelListener viewUpdateEvent;
private final ModelListener gameFinishEvent;
private Player currentPlayer;
private InterfaceAction interfaceAction;
private Action selectedAction = null;
ModelListener awakeEvent;
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)
throw new IllegalArgumentException("The game need 2 or more player to start");
this.players = players;
this.currentPlayer = players.get(0);
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() {
while(!isOver()) {
ModelListener awakeEvent = new AwakeGame(this);
generateAndGetPlayerActions(currentPlayer);
interfaceAction = InterfaceAction.SELECT_ACTION;
waitForInterfaceEvent();
selectedAction.doAction();
awakeEvent = new AwakeGame(this);
currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer));
if(currentPlayer instanceof HumanPlayer) {
interfaceAction = InterfaceAction.SELECT_ACTION;
waitForInterfaceEvent();
selectedAction.doAction();
} else {
ComputerPlayer player = (ComputerPlayer) currentPlayer;
Action action = player.choseAction();
action.doAction();
}
selectedAction = null;
nextCurrentPlayer();
viewUpdateEvent.updateModel(null);
gameFinishEvent.updateModel(null);
}
}
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;
public class RandomComputerPlayer extends AbstractPlayer{
public class RandomComputerPlayer extends ComputerPlayer {
public RandomComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) {
super(id,point,false, classPlayer);
super(id,point, classPlayer);
}
@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.utils.Point;
public class StrategyComputerPlayer extends AbstractPlayer{
public class StrategyComputerPlayer extends ComputerPlayer {
public StrategyComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) {
super(id,point,false, classPlayer);
super(id,point, classPlayer);
}
@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;
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.Grid;
import fr.lnl.game.server.games.grid.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.listener.GameFinishEvent;
import fr.lnl.game.server.utils.Cardinal;
import fr.lnl.game.server.utils.Point;
@ -25,7 +27,7 @@ public class Mock {
placePlayersBRUT();
placeEnergyBallBRUT();
placeInternWallBRUT();
game = new Game(grid, players);
game = new Game(grid, players, new MockViewUpdate(), new GameFinishEvent());
}
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
}
}