Skip to content
Snippets Groups Projects
Commit 64119975 authored by LABOUREL Arnaud's avatar LABOUREL Arnaud
Browse files

added test for grid

parent c3a3ff1b
No related branches found
No related tags found
No related merge requests found
Showing
with 87 additions and 49 deletions
......@@ -17,9 +17,7 @@ repositories {
}
dependencies {
testRuntimeOnly("org.junit.platform:junit-platform-launcher") {
because("Only needed to run tests in a version of IntelliJ IDEA that bundles older versions")
}
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
testImplementation("org.assertj:assertj-core:3.24.2")
testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
......@@ -30,11 +28,7 @@ test {
}
application {
mainClass.set("firefighter.app.SimulatorMain")
mainClass.set("app.SimulatorMain")
}
tasks {
shadowJar {
exclude("module-info.class")
}
}
package firefighter.app;
package app;
import firefighter.controller.Controller;
import controller.Controller;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
......@@ -11,7 +11,7 @@ import java.io.IOException;
import java.net.URL;
public class SimulatorApplication extends javafx.application.Application {
private static final String VIEW_RESOURCE_PATH = "/firefighter/view/view.fxml";
private static final String VIEW_RESOURCE_PATH = "/view/view.fxml";
private static final String APP_NAME = "Firefighter simulator";
private static final int ROW_COUNT = 20;
private static final int COLUMN_COUNT = 20;
......
package firefighter.app;
package app;
public class SimulatorMain {
public static void main(String[] args){
......
package firefighter.controller;
package controller;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
......@@ -12,12 +12,12 @@ import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.util.Duration;
import javafx.util.Pair;
import firefighter.model.Board;
import firefighter.model.ModelElement;
import firefighter.model.FirefighterBoard;
import firefighter.util.Position;
import firefighter.view.FirefighterGrid;
import firefighter.view.ViewElement;
import model.Board;
import model.ModelElement;
import model.FirefighterBoard;
import util.Position;
import view.Grid;
import view.ViewElement;
import java.util.ArrayList;
import java.util.List;
......@@ -38,7 +38,7 @@ public class Controller {
@FXML
private ToggleButton playToggleButton;
@FXML
private FirefighterGrid grid;
private Grid<ViewElement> grid;
private Timeline timeline;
private Board<List<ModelElement>> board;
......@@ -125,7 +125,7 @@ public class Controller {
public void initialize(int squareWidth, int squareHeight, int columnCount,
int rowCount, int initialFireCount, int initialFirefighterCount) {
grid.initialize(squareWidth, squareHeight, columnCount, rowCount);
grid.setDimensions(columnCount, rowCount, squareWidth, squareHeight);
this.setModel(new FirefighterBoard(columnCount, rowCount, initialFireCount, initialFirefighterCount));
repaintGrid();
}
......
package firefighter.controller;
package controller;
import javafx.collections.ListChangeListener.Change;
import javafx.scene.control.Toggle;
......
package firefighter.model;
package model;
import firefighter.util.Position;
import util.Position;
import java.util.List;
......
package firefighter.model;
package model;
import firefighter.util.Position;
import util.Position;
import java.util.*;
......@@ -143,6 +143,10 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
@Override
public void setState(List<ModelElement> state, Position position) {
firePositions.remove(position);
for (;;) {
if (!firefighterPositions.remove(position)) break;
}
for(ModelElement element : state){
switch (element){
case FIRE -> firePositions.add(position);
......
package firefighter.model;
package model;
public enum ModelElement {
FIREFIGHTER, FIRE
......
......@@ -2,7 +2,7 @@ module firefighter {
requires javafx.controls;
requires javafx.fxml;
requires javafx.graphics;
opens firefighter.controller to javafx.fxml;
exports firefighter.app;
opens firefighter.app to javafx.fxml;
}
\ No newline at end of file
opens controller to javafx.fxml;
exports app;
opens app to javafx.fxml;
}
package firefighter.util;
package util;
public record Position(int row, int column) {
......
package firefighter.view;
package view;
import javafx.scene.canvas.Canvas;
import javafx.scene.paint.Color;
import javafx.util.Pair;
import firefighter.util.Position;
import util.Position;
import java.util.List;
......@@ -35,21 +35,19 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{
paintLines();
}
public int getColumnCount() {
public int columnCount() {
return columnCount;
}
public int getRowCount() {
public int rowCount() {
return rowCount;
}
public FirefighterGrid(){
}
public void initialize(int squareWidth,
int squareHeight,
int columnCount,
int rowCount) {
@Override
public void setDimensions(int columnCount, int rowCount, int squareWidth, int squareHeight) {
this.squareWidth = squareWidth;
this.squareHeight = squareHeight;
this.columnCount = columnCount;
......
package firefighter.view;
package view;
import javafx.util.Pair;
import firefighter.util.Position;
import util.Position;
import java.util.List;
......@@ -27,18 +27,30 @@ public interface Grid<E> {
*/
void repaint(E[][] elements);
/**
* Set the dimensions of the grid to the specified column count, row count, square width, and square height.
* This method adjusts the dimensions of the grid to the given number of columns, number of rows, square width,
* and square height.
*
* @param columnCount The new number of columns in the grid.
* @param rowCount The new number of rows in the grid.
* @param squareWidth The width of each square within the grid.
* @param squareHeight The height of each square within the grid.
*/
void setDimensions(int columnCount, int rowCount, int squareWidth, int squareHeight);
/**
* Get the number of columns in the grid.
*
* @return The number of columns in the grid.
*/
int getColumnCount();
int columnCount();
/**
* Get the number of rows in the grid.
*
* @return The number of rows in the grid.
*/
int getRowCount();
int rowCount();
}
package firefighter.view;
package view;
import javafx.scene.paint.Color;
......
......@@ -3,14 +3,14 @@
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import firefighter.view.FirefighterGrid?>
<?import view.FirefighterGrid?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.Label?>
<HBox styleClass="background" stylesheets="@DarkTheme.css"
xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml"
fx:controller="firefighter.controller.Controller">
fx:controller="controller.Controller">
<VBox>
<Separator maxHeight="-Infinity" maxWidth="-Infinity"
prefHeight="24.0" prefWidth="200.0"/>
......
package firefighter.model;
package model;
import org.junit.jupiter.api.Test;
import util.Position;
import java.util.List;
......@@ -26,4 +27,13 @@ public class FirefighterBoardTest {
}
assertThat(board.stepNumber()).isEqualTo(10);
}
@Test
void testGetState_afterSet(){
Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 0, 0);
Position position = new Position(1,2);
assertThat(board.getState(position)).isEmpty();
board.setState(List.of(ModelElement.FIRE), position);
assertThat(board.getState(position)).containsExactly(ModelElement.FIRE);
}
}
package view;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class FirefighterGridTest {
@Test
void testColumnCount(){
Grid<ViewElement> grid = new FirefighterGrid();
grid.setDimensions(20,10,10,10);
assertThat(grid.columnCount()).isEqualTo(20);
}
@Test
void testRowCount(){
Grid<ViewElement> grid = new FirefighterGrid();
grid.setDimensions(20,10,10,10);
assertThat(grid.rowCount()).isEqualTo(10);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment