Added very basic view support + reworked players' fields in Game.java and Grid.java

This commit is contained in:
Quentin Legot 2021-10-23 19:15:08 +02:00
parent d6f2217554
commit 4e2da3330a
15 changed files with 129 additions and 86 deletions

View File

@ -1,4 +1,12 @@
package fr.lnl.game.client; package fr.lnl.game.client;
import fr.lnl.game.server.games.Game;
public abstract class AbstractView implements View { public abstract class AbstractView implements View {
Game game;
public AbstractView(Game game) {
this.game = game;
}
} }

View File

@ -1,31 +1,56 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package fr.lnl.game.client; 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.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage; 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 class App extends Application {
public static void main(String[] args) { public static void main(String[] args) {
String result = getMessage(); Class<? extends AbstractView> clazz = parseArgs(args);
System.out.println(result); if(clazz.equals(Terminal.class)) {
launch(); 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 @Override
public void start(Stage stage) throws Exception { public void start(Stage stage) throws Exception {
System.out.println("Launching stage"); Game game = startGame();
String javaVersion = System.getProperty("java.version"); Window window = new Window(stage, game);
String javafxVersion = System.getProperty("javafx.version"); // TODO: 23/10/2021 faire une view par joueur
Label l = new Label("Hello, JavaFX version " + javafxVersion + ", running on Java " + javaVersion + "."); window.show();
Scene scene = new Scene(new StackPane(l), 640, 480); }
stage.setScene(scene);
stage.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;
}
} }
} }

View 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() {
}
}

View File

@ -1,4 +1,26 @@
package fr.lnl.game.client; 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 { 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();
}
} }

View File

@ -1,6 +0,0 @@
package fr.lnl.game.client.graphics;
import fr.lnl.game.client.AbstractView;
public class Terminal extends AbstractView {
}

View File

@ -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());
}
}

View File

@ -13,7 +13,7 @@ dependencies {
} }
application { application {
mainClassName = 'fr.lnl.game.server.games.grid.Grid' mainClassName = 'fr.lnl.game.server.ServerMain'
} }
test { test {

View File

@ -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!";
}
}

View File

@ -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.ClassPlayer;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import java.util.Arrays;
import java.util.List;
public class ServerMain { public class ServerMain {
public static void main(String[] args) { public static void main(String[] args) {
Player playerOne = new ComputerPlayer(1,null, ClassPlayer.DEFAULT); List<Player> players = Arrays.asList(new ComputerPlayer(1,null, ClassPlayer.DEFAULT),
Player playerTwo = new ComputerPlayer(2,null, ClassPlayer.DEFAULT); new ComputerPlayer(2,null, ClassPlayer.DEFAULT));
Grid grid = new Grid(16,16, players);
Grid grid = new Grid(16,16,new Player[]{playerOne,playerTwo});
grid.initGrid(); grid.initGrid();
grid.placePlayersBRUT(); grid.placePlayersBRUT();
grid.placeEnergyBallBRUT(); grid.placeEnergyBallBRUT();
grid.placeInternWallBRUT(); grid.placeInternWallBRUT();
Game game = new Game(grid,playerOne,playerTwo); Game game = new Game(grid, players);
game.getGrid().printGrid(); game.getGrid().printGrid();
} }
} }

View File

@ -3,37 +3,32 @@ package fr.lnl.game.server.games;
import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import java.util.List;
public class Game { public class Game {
Grid grid; Grid grid;
Player player_One;
Player player_Two;
Player current_player; Player current_player;
Player[] players; List<Player> players;
public Game(Grid grid, Player player_One, Player player_Two){ public Game(Grid grid, List<Player> players) {
this.player_One = player_One; if(players.size() < 2)
this.player_Two = player_Two; throw new IllegalArgumentException("The game need 2 or more player to start");
this.current_player = player_One; this.players = players;
this.current_player = players.get(0);
this.grid = grid; this.grid = grid;
players = new Player[]{player_One, player_Two};
} }
public void play(){ public void play(){
} }
public boolean isOver(){ public boolean isOver() {
return players.length == 1; return players.parallelStream().filter(player -> !player.isAlive()).count() == 1;
} }
public Player getWinner(){ public Player getWinner(){
// Quentin: simple avis: appel de isOver pas forcément nécessaire, puisqu'on appelera surement getWinner après return players.parallelStream().filter(player -> !player.isAlive()).findFirst().orElseThrow(ArrayIndexOutOfBoundsException::new);
// un appel a isOver retournant true
if(isOver()){
return players[0];
}
return null;
} }
public Player getCurrentPlayer() { public Player getCurrentPlayer() {
@ -44,7 +39,7 @@ public class Game {
return grid; return grid;
} }
public Player[] getPlayers() { public List<Player> getPlayers() {
return players; return players;
} }
} }

View File

@ -6,15 +6,16 @@ import fr.lnl.game.server.utils.Pair;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
public class Grid { public class Grid {
private HashMap<Point, Pair<Player, Box>> board; private HashMap<Point, Pair<Player, Box>> board;
private int row; private int row;
private int column; 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.row = row;
this.column = column; this.column = column;
this.players = players; this.players = players;
@ -50,8 +51,8 @@ public class Grid {
} }
public void placePlayersBRUT(){ public void placePlayersBRUT(){
board.get(new Point(1,1)).setA(players[0]); board.get(new Point(1,1)).setA(players.get(0));
board.get(new Point(14,14)).setA(players[1]); board.get(new Point(14,14)).setA(players.get(1));
} }
public void placeEnergyBallBRUT(){ public void placeEnergyBallBRUT(){
@ -128,7 +129,7 @@ public class Grid {
System.out.println(this); System.out.println(this);
} }
public Player[] getPlayers() { public List<Player> getPlayers() {
return players; return players;
} }

View File

@ -1,3 +1,7 @@
module server { module server {
exports fr.lnl.game.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;
} }

View File

@ -22,7 +22,7 @@ public class ActionPlayerTest {
// en checkant son résultat // en checkant son résultat
@Test @Test
public void moveActionTest() { public void moveActionTest() {
Assertions.assertEquals(game.getPlayers()[0], game.getCurrentPlayer()); Assertions.assertEquals(game.getPlayers().get(0), game.getCurrentPlayer());
} }
} }

View File

@ -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_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()); 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) // test placePlayersBRUT (mocked)
assertEquals(grid.getPlayers()[0], grid.getBoard().get(new Point(1, 1)).getA()); assertEquals(grid.getPlayers().get(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(1), grid.getBoard().get(new Point(14, 14)).getA());
// test placeEnergyBallBRUT (mocked) // test placeEnergyBallBRUT (mocked)
assertEquals(new EnergyBall(), grid.getBoard().get(new Point(2, 3)).getB()); assertEquals(new EnergyBall(), grid.getBoard().get(new Point(2, 3)).getB());
assertEquals(new EnergyBall(), grid.getBoard().get(new Point(7, 10)).getB()); assertEquals(new EnergyBall(), grid.getBoard().get(new Point(7, 10)).getB());

View File

@ -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.Cardinal;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import java.util.Arrays;
import java.util.List;
public class Mock { public class Mock {
Grid grid; Grid grid;
Game game; Game game;
public Mock() { public Mock() {
Player playerOne = new ComputerPlayer(1,null, ClassPlayer.DEFAULT); List<Player> players = Arrays.asList(new ComputerPlayer(1,null, ClassPlayer.DEFAULT),
Player playerTwo = new ComputerPlayer(2,null, ClassPlayer.DEFAULT); new ComputerPlayer(2,null, ClassPlayer.DEFAULT));
this.grid = new Grid(16,16,new Player[]{playerOne,playerTwo}); this.grid = new Grid(16,16, players);
grid.initGrid(); grid.initGrid();
placePlayersBRUT(); placePlayersBRUT();
placeEnergyBallBRUT(); placeEnergyBallBRUT();
placeInternWallBRUT(); placeInternWallBRUT();
game = new Game(grid,playerOne,playerTwo); game = new Game(grid, players);
} }
public void placePlayersBRUT(){ public void placePlayersBRUT(){
grid.getBoard().get(new Point(1,1)).setA(grid.getPlayers()[0]); grid.getBoard().get(new Point(1,1)).setA(grid.getPlayers().get(0));
grid.getBoard().get(new Point(14,14)).setA(grid.getPlayers()[1]); grid.getBoard().get(new Point(14,14)).setA(grid.getPlayers().get(1));
} }
public void placeEnergyBallBRUT(){ public void placeEnergyBallBRUT(){