Added very basic view support + reworked players' fields in Game.java and Grid.java
This commit is contained in:
parent
d6f2217554
commit
4e2da3330a
@ -1,4 +1,12 @@
|
||||
package fr.lnl.game.client;
|
||||
|
||||
import fr.lnl.game.server.games.Game;
|
||||
|
||||
public abstract class AbstractView implements View {
|
||||
|
||||
Game game;
|
||||
|
||||
public AbstractView(Game game) {
|
||||
this.game = game;
|
||||
}
|
||||
}
|
||||
|
@ -1,31 +1,56 @@
|
||||
/*
|
||||
* This Java source file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
package fr.lnl.game.client;
|
||||
import fr.lnl.game.server.games.Game;
|
||||
import fr.lnl.game.server.games.grid.Grid;
|
||||
import fr.lnl.game.server.games.player.ClassPlayer;
|
||||
import fr.lnl.game.server.games.player.ComputerPlayer;
|
||||
import fr.lnl.game.server.games.player.Player;
|
||||
import javafx.application.Application;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.layout.StackPane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import static fr.lnl.game.server.MessageUtils.getMessage;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class App extends Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String result = getMessage();
|
||||
System.out.println(result);
|
||||
Class<? extends AbstractView> clazz = parseArgs(args);
|
||||
if(clazz.equals(Terminal.class)) {
|
||||
launchTerminal();
|
||||
} else {
|
||||
launch();
|
||||
}
|
||||
}
|
||||
|
||||
public static Game startGame() {
|
||||
List<Player> players = Arrays.asList(new ComputerPlayer(1, null, ClassPlayer.DEFAULT),
|
||||
new ComputerPlayer(1, null, ClassPlayer.DEFAULT));
|
||||
return new Game(new Grid(12, 12, players), players);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) throws Exception {
|
||||
System.out.println("Launching stage");
|
||||
String javaVersion = System.getProperty("java.version");
|
||||
String javafxVersion = System.getProperty("javafx.version");
|
||||
Label l = new Label("Hello, JavaFX version " + javafxVersion + ", running on Java " + javaVersion + ".");
|
||||
Scene scene = new Scene(new StackPane(l), 640, 480);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
Game game = startGame();
|
||||
Window window = new Window(stage, game);
|
||||
// TODO: 23/10/2021 faire une view par joueur
|
||||
window.show();
|
||||
}
|
||||
|
||||
public static void launchTerminal() {
|
||||
Game game = startGame();
|
||||
Terminal terminal = new Terminal(game);
|
||||
// TODO: 23/10/2021 faire une view par joueur
|
||||
}
|
||||
|
||||
// TODO: 23/10/2021 nécessite un rework -> faire une view par joueur
|
||||
public static Class<? extends AbstractView> parseArgs(String[] args) {
|
||||
if(args.length >= 1) {
|
||||
if(args[0].equalsIgnoreCase("terminal")) {
|
||||
return Terminal.class;
|
||||
} else {
|
||||
return Window.class;
|
||||
}
|
||||
} else {
|
||||
return Window.class;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
15
client/src/main/java/fr/lnl/game/client/Terminal.java
Normal file
15
client/src/main/java/fr/lnl/game/client/Terminal.java
Normal file
@ -0,0 +1,15 @@
|
||||
package fr.lnl.game.client;
|
||||
|
||||
import fr.lnl.game.server.games.Game;
|
||||
|
||||
public class Terminal extends AbstractView {
|
||||
|
||||
public Terminal(Game game) {
|
||||
super(game);
|
||||
}
|
||||
|
||||
public void displayGrid() {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,26 @@
|
||||
package fr.lnl.game.client;
|
||||
|
||||
import fr.lnl.game.server.games.Game;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.layout.StackPane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
public class Window extends AbstractView {
|
||||
|
||||
private final Stage stage;
|
||||
|
||||
public Window(Stage stage, Game game) {
|
||||
super(game);
|
||||
this.stage = stage;
|
||||
}
|
||||
|
||||
public void show() {
|
||||
String javaVersion = System.getProperty("java.version");
|
||||
String javafxVersion = System.getProperty("javafx.version");
|
||||
Label l = new Label("Hello, JavaFX version " + javafxVersion + ", running on Java " + javaVersion + ".");
|
||||
Scene scene = new Scene(new StackPane(l), 640, 480);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +0,0 @@
|
||||
package fr.lnl.game.client.graphics;
|
||||
|
||||
import fr.lnl.game.client.AbstractView;
|
||||
|
||||
public class Terminal extends AbstractView {
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
/*
|
||||
* This Java source file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
package fr.lnl.game.client;
|
||||
|
||||
import fr.lnl.game.server.MessageUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class MessageUtilsTest {
|
||||
@Test
|
||||
public void testGetMessage() {
|
||||
assertEquals("Hello World!", MessageUtils.getMessage());
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@ dependencies {
|
||||
}
|
||||
|
||||
application {
|
||||
mainClassName = 'fr.lnl.game.server.games.grid.Grid'
|
||||
mainClassName = 'fr.lnl.game.server.ServerMain'
|
||||
}
|
||||
|
||||
test {
|
||||
|
@ -1,10 +0,0 @@
|
||||
/*
|
||||
* This Java source file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
package fr.lnl.game.server;
|
||||
|
||||
public class MessageUtils {
|
||||
public static String getMessage() {
|
||||
return "Hello World!";
|
||||
}
|
||||
}
|
@ -6,18 +6,20 @@ import fr.lnl.game.server.games.player.ComputerPlayer;
|
||||
import fr.lnl.game.server.games.player.ClassPlayer;
|
||||
import fr.lnl.game.server.games.player.Player;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ServerMain {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Player playerOne = new ComputerPlayer(1,null, ClassPlayer.DEFAULT);
|
||||
Player playerTwo = new ComputerPlayer(2,null, ClassPlayer.DEFAULT);
|
||||
|
||||
Grid grid = new Grid(16,16,new Player[]{playerOne,playerTwo});
|
||||
List<Player> players = Arrays.asList(new ComputerPlayer(1,null, ClassPlayer.DEFAULT),
|
||||
new ComputerPlayer(2,null, ClassPlayer.DEFAULT));
|
||||
Grid grid = new Grid(16,16, players);
|
||||
grid.initGrid();
|
||||
grid.placePlayersBRUT();
|
||||
grid.placeEnergyBallBRUT();
|
||||
grid.placeInternWallBRUT();
|
||||
Game game = new Game(grid,playerOne,playerTwo);
|
||||
Game game = new Game(grid, players);
|
||||
game.getGrid().printGrid();
|
||||
}
|
||||
}
|
||||
|
@ -3,20 +3,20 @@ package fr.lnl.game.server.games;
|
||||
import fr.lnl.game.server.games.grid.Grid;
|
||||
import fr.lnl.game.server.games.player.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Game {
|
||||
|
||||
Grid grid;
|
||||
Player player_One;
|
||||
Player player_Two;
|
||||
Player current_player;
|
||||
Player[] players;
|
||||
List<Player> players;
|
||||
|
||||
public Game(Grid grid, Player player_One, Player player_Two){
|
||||
this.player_One = player_One;
|
||||
this.player_Two = player_Two;
|
||||
this.current_player = player_One;
|
||||
public Game(Grid grid, List<Player> players) {
|
||||
if(players.size() < 2)
|
||||
throw new IllegalArgumentException("The game need 2 or more player to start");
|
||||
this.players = players;
|
||||
this.current_player = players.get(0);
|
||||
this.grid = grid;
|
||||
players = new Player[]{player_One, player_Two};
|
||||
}
|
||||
|
||||
public void play(){
|
||||
@ -24,16 +24,11 @@ public class Game {
|
||||
}
|
||||
|
||||
public boolean isOver() {
|
||||
return players.length == 1;
|
||||
return players.parallelStream().filter(player -> !player.isAlive()).count() == 1;
|
||||
}
|
||||
|
||||
public Player getWinner(){
|
||||
// Quentin: simple avis: appel de isOver pas forcément nécessaire, puisqu'on appelera surement getWinner après
|
||||
// un appel a isOver retournant true
|
||||
if(isOver()){
|
||||
return players[0];
|
||||
}
|
||||
return null;
|
||||
return players.parallelStream().filter(player -> !player.isAlive()).findFirst().orElseThrow(ArrayIndexOutOfBoundsException::new);
|
||||
}
|
||||
|
||||
public Player getCurrentPlayer() {
|
||||
@ -44,7 +39,7 @@ public class Game {
|
||||
return grid;
|
||||
}
|
||||
|
||||
public Player[] getPlayers() {
|
||||
public List<Player> getPlayers() {
|
||||
return players;
|
||||
}
|
||||
}
|
@ -6,15 +6,16 @@ import fr.lnl.game.server.utils.Pair;
|
||||
import fr.lnl.game.server.utils.Point;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class Grid {
|
||||
private HashMap<Point, Pair<Player, Box>> board;
|
||||
private int row;
|
||||
private int column;
|
||||
private Player[] players;
|
||||
private List<Player> players;
|
||||
|
||||
public Grid(int row, int column, Player[] players){
|
||||
public Grid(int row, int column, List<Player> players) {
|
||||
this.row = row;
|
||||
this.column = column;
|
||||
this.players = players;
|
||||
@ -50,8 +51,8 @@ public class Grid {
|
||||
}
|
||||
|
||||
public void placePlayersBRUT(){
|
||||
board.get(new Point(1,1)).setA(players[0]);
|
||||
board.get(new Point(14,14)).setA(players[1]);
|
||||
board.get(new Point(1,1)).setA(players.get(0));
|
||||
board.get(new Point(14,14)).setA(players.get(1));
|
||||
}
|
||||
|
||||
public void placeEnergyBallBRUT(){
|
||||
@ -128,7 +129,7 @@ public class Grid {
|
||||
System.out.println(this);
|
||||
}
|
||||
|
||||
public Player[] getPlayers() {
|
||||
public List<Player> getPlayers() {
|
||||
return players;
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,7 @@
|
||||
module server {
|
||||
exports fr.lnl.game.server;
|
||||
exports fr.lnl.game.server.games;
|
||||
exports fr.lnl.game.server.games.player;
|
||||
exports fr.lnl.game.server.games.grid;
|
||||
exports fr.lnl.game.server.utils;
|
||||
}
|
@ -22,7 +22,7 @@ public class ActionPlayerTest {
|
||||
// en checkant son résultat
|
||||
@Test
|
||||
public void moveActionTest() {
|
||||
Assertions.assertEquals(game.getPlayers()[0], game.getCurrentPlayer());
|
||||
Assertions.assertEquals(game.getPlayers().get(0), game.getCurrentPlayer());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -28,8 +28,8 @@ public class GridTest {
|
||||
assertEquals(new Wall(Cardinal.SOUTH_WEST, grid.getRow() - 1, 0), grid.getBoard().get(new Point(grid.getRow() - 1, 0)).getB());
|
||||
assertEquals(new Wall(Cardinal.SOUTH_EAST, grid.getRow() - 1, grid.getColumn() - 1), grid.getBoard().get(new Point(grid.getRow() - 1, grid.getColumn() - 1)).getB());
|
||||
// test placePlayersBRUT (mocked)
|
||||
assertEquals(grid.getPlayers()[0], grid.getBoard().get(new Point(1, 1)).getA());
|
||||
assertEquals(grid.getPlayers()[1], grid.getBoard().get(new Point(14, 14)).getA());
|
||||
assertEquals(grid.getPlayers().get(0), grid.getBoard().get(new Point(1, 1)).getA());
|
||||
assertEquals(grid.getPlayers().get(1), grid.getBoard().get(new Point(14, 14)).getA());
|
||||
// test placeEnergyBallBRUT (mocked)
|
||||
assertEquals(new EnergyBall(), grid.getBoard().get(new Point(2, 3)).getB());
|
||||
assertEquals(new EnergyBall(), grid.getBoard().get(new Point(7, 10)).getB());
|
||||
|
@ -10,25 +10,28 @@ import fr.lnl.game.server.games.player.Player;
|
||||
import fr.lnl.game.server.utils.Cardinal;
|
||||
import fr.lnl.game.server.utils.Point;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Mock {
|
||||
|
||||
Grid grid;
|
||||
Game game;
|
||||
|
||||
public Mock() {
|
||||
Player playerOne = new ComputerPlayer(1,null, ClassPlayer.DEFAULT);
|
||||
Player playerTwo = new ComputerPlayer(2,null, ClassPlayer.DEFAULT);
|
||||
this.grid = new Grid(16,16,new Player[]{playerOne,playerTwo});
|
||||
List<Player> players = Arrays.asList(new ComputerPlayer(1,null, ClassPlayer.DEFAULT),
|
||||
new ComputerPlayer(2,null, ClassPlayer.DEFAULT));
|
||||
this.grid = new Grid(16,16, players);
|
||||
grid.initGrid();
|
||||
placePlayersBRUT();
|
||||
placeEnergyBallBRUT();
|
||||
placeInternWallBRUT();
|
||||
game = new Game(grid,playerOne,playerTwo);
|
||||
game = new Game(grid, players);
|
||||
}
|
||||
|
||||
public void placePlayersBRUT(){
|
||||
grid.getBoard().get(new Point(1,1)).setA(grid.getPlayers()[0]);
|
||||
grid.getBoard().get(new Point(14,14)).setA(grid.getPlayers()[1]);
|
||||
grid.getBoard().get(new Point(1,1)).setA(grid.getPlayers().get(0));
|
||||
grid.getBoard().get(new Point(14,14)).setA(grid.getPlayers().get(1));
|
||||
}
|
||||
|
||||
public void placeEnergyBallBRUT(){
|
||||
|
Loading…
Reference in New Issue
Block a user