diff --git a/src/main/java/app/SimulatorApplication.java b/src/main/java/app/SimulatorApplication.java index a3076f1b5ef8ec07a7bff94ad285b9bf97d7c974..ec8977d69360df13b3deefa34eaf0541814cf02b 100644 --- a/src/main/java/app/SimulatorApplication.java +++ b/src/main/java/app/SimulatorApplication.java @@ -19,7 +19,7 @@ public class SimulatorApplication extends javafx.application.Application { private static final int SQUARE_HEIGHT = 50; public static final int INITIAL_FIRE_COUNT = 1; public static final int INITIAL_FIREFIGHTER_COUNT = 4; - + public static final int INITIAL_MOTORFIREFIGHTER_COUNT = 1; public static final int INITIAL_CLOUD_COUNT = 4; private Stage primaryStage; @@ -46,7 +46,7 @@ public class SimulatorApplication extends javafx.application.Application { view = loader.load(); Controller controller = loader.getController(); controller.initialize(SQUARE_WIDTH, SQUARE_HEIGHT, COLUMN_COUNT, ROW_COUNT, - INITIAL_FIRE_COUNT, INITIAL_FIREFIGHTER_COUNT, INITIAL_CLOUD_COUNT); + INITIAL_FIRE_COUNT, INITIAL_FIREFIGHTER_COUNT, INITIAL_CLOUD_COUNT, INITIAL_MOTORFIREFIGHTER_COUNT); } private void showScene() { diff --git a/src/main/java/controller/Controller.java b/src/main/java/controller/Controller.java index 334fe19e823206bff837f6c3bee1128389daca89..e9f46a59713969f323053c9871d10d45ae4072e6 100644 --- a/src/main/java/controller/Controller.java +++ b/src/main/java/controller/Controller.java @@ -61,20 +61,8 @@ public class Controller { } private void updateBoard(){ - List<Position> updatedPositions = board.updateToNextGeneration(); - List<Pair<Position, ViewElement>> updatedSquares = new ArrayList<>(); - for(Position updatedPosition : updatedPositions){ - List<ModelElement> squareState = board.getState(updatedPosition); - ViewElement viewElement = getViewElement(squareState); - updatedSquares.add(new Pair<>(updatedPosition, viewElement)); - } - grid.repaint(updatedSquares); - updateGenerationLabel(board.stepNumber()); - } - private void updateBoard2(){ - - List<Position> updatedPositions = board.updateToNextGeneration2(); + List<Position> updatedPositions = board.updateToNextGeneration(); List<Position> updatedClearSquares = new ArrayList<>(); List<Item> updatedSquares = new ArrayList<>(); for(Position updatedPosition : updatedPositions){ @@ -84,23 +72,11 @@ public class Controller { updatedSquares.add(squareState); } } - grid.repaint2(updatedSquares, updatedClearSquares); + grid.repaint(updatedSquares, updatedClearSquares); updateGenerationLabel(board.stepNumber()); //board.testScreen(); } - private void repaintGrid(){ - int columnCount = board.columnCount(); - int rowCount = board.rowCount(); - ViewElement[][] viewElements = new ViewElement[rowCount][columnCount]; - for(int column = 0; column < columnCount; column++) - for(int row = 0; row < rowCount; row++) - viewElements[row][column] = getViewElement(board.getState(new Position(row, column))); - grid.repaint(viewElements); - updateGenerationLabel(board.stepNumber()); - } - - private void repaintGrid2(){ grid.initialize(board.itemList()); updateGenerationLabel(board.stepNumber()); @@ -119,7 +95,7 @@ public class Controller { private void initializeTimeline() { Duration duration = new Duration(Controller.PERIOD_IN_MILLISECONDS); EventHandler<ActionEvent> eventHandler = - event -> updateBoard2(); + event -> updateBoard(); KeyFrame keyFrame = new KeyFrame(duration, eventHandler); timeline = new Timeline(keyFrame); timeline.setCycleCount(Animation.INDEFINITE); @@ -145,27 +121,19 @@ public class Controller { this.pause(); board.reset(); pauseToggleButton.setSelected(true); - repaintGrid2(); - } - - public void initialize(int squareWidth, int squareHeight, int columnCount, - int rowCount, int initialFireCount, int initialFirefighterCount) { - grid.setDimensions(columnCount, rowCount, squareWidth, squareHeight); - this.setModel(new FirefighterBoard(columnCount, rowCount, initialFireCount, initialFirefighterCount)); repaintGrid(); } - public void initialize(int squareWidth, int squareHeight, int columnCount, - int rowCount, int initialFireCount, int initialFirefighterCount, int initialCloudCount) { + int rowCount, int initialFireCount, int initialFirefighterCount, int initialCloudCount, int initialMotorFirefighterCount) { - this.setModel(new FirefighterBoard(columnCount, rowCount, initialFireCount, initialFirefighterCount, initialCloudCount)); + this.setModel(new FirefighterBoard(columnCount, rowCount, initialFireCount, initialFirefighterCount, initialCloudCount, initialMotorFirefighterCount)); grid.setDimensions(columnCount, rowCount, squareWidth, squareHeight); - repaintGrid2(); + repaintGrid(); } public void oneStepButtonAction() { this.pause(); - updateBoard2(); + updateBoard(); } private void updateGenerationLabel(int value){ diff --git a/src/main/java/model/Board.java b/src/main/java/model/Board.java index 9e2c5acff42b9e94fa0b7fd203b017d5dcf64bbf..bed404e2d542cce5670e24c6645a60f1cdb1d976 100644 --- a/src/main/java/model/Board.java +++ b/src/main/java/model/Board.java @@ -11,22 +11,6 @@ import java.util.List; */ public interface Board<S> { - /** - * Get the state of the board at a specific position. - * - * @param position The position on the board for which to retrieve the state. - * @return The state at the specified position. - */ - S getState(Position position); - - /** - * Set the state of a specific position on the board to the specified state. - * - * @param state The state to set for the given position. - * @param position The position on the board for which to set the state. - */ - void setState(S state, Position position); - /** * Get the number of rows in the board. * @@ -57,7 +41,6 @@ public interface Board<S> { * * @return A list of positions that have changed during the update. */ - List<Position> updateToNextGeneration2(); /** * Reset the board to its initial state. diff --git a/src/main/java/model/FirefighterBoard.java b/src/main/java/model/FirefighterBoard.java index fefb41c9f9a4c427ba3c4afb89f8a33309d7d123..ad37528c15da8c005409d168ec2114fe480692a8 100644 --- a/src/main/java/model/FirefighterBoard.java +++ b/src/main/java/model/FirefighterBoard.java @@ -12,6 +12,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> { private final int initialFireCount; private final int initialFirefighterCount; + private final int initialMotorFirefighterCount; private final int initialCloudCount; private List<Item> itemList; private List<Box> boxList; @@ -20,16 +21,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> { private int step = 0; private final Random randomGenerator = new Random(); - public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount) { - this.columnCount = columnCount; - this.rowCount = rowCount; - this.initialFireCount = initialFireCount; - this.initialFirefighterCount = initialFirefighterCount; - this.initialCloudCount = 0; - initializeElements(); - } - - public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount, int initialCloudCount) { + public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount, int initialCloudCount, int initialMotorFirefighterCount) { this.columnCount = columnCount; this.rowCount = rowCount; this.itemList = new ArrayList<>(); @@ -37,19 +29,11 @@ public class FirefighterBoard implements Board<List<ModelElement>> { this.initialFireCount = initialFireCount; this.initialFirefighterCount = initialFirefighterCount; this.initialCloudCount = initialCloudCount; - initializeElements2(); + this.initialMotorFirefighterCount = initialMotorFirefighterCount; + initializeElements(); } public void initializeElements() { - firefighterPositions = new ArrayList<>(); - firePositions = new HashSet<>(); - for (int index = 0; index < initialFireCount; index++) - firePositions.add(randomPosition()); - for (int index = 0; index < initialFirefighterCount; index++) - firefighterPositions.add(randomPosition()); - } - - public void initializeElements2() { for (int i = 0; i < initialFireCount; i++){ itemList.add(new Fire(randomEmptyPosition())); } @@ -59,6 +43,9 @@ public class FirefighterBoard implements Board<List<ModelElement>> { for (int i = 0; i < initialCloudCount; i++){ itemList.add(new Cloud(randomEmptyPosition())); } + for (int i = 0; i < initialMotorFirefighterCount; i++){ + itemList.add(new MotorFirefighter(randomEmptyPosition())); + } } private Position randomPosition() { @@ -73,17 +60,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> { } } - @Override - public List<ModelElement> getState(Position position) { - List<ModelElement> result = new ArrayList<>(); - for(Position firefighterPosition : firefighterPositions) - if (firefighterPosition.equals(position)) - result.add(ModelElement.FIREFIGHTER); - if(firePositions.contains(position)) - result.add(ModelElement.FIRE); - return result; - } - @Override public int rowCount() { return rowCount; @@ -95,14 +71,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> { } public List<Position> updateToNextGeneration() { - List<Position> result = updateFirefighters(); - result.addAll(updateFires()); - step++; - return result; - } - - - public List<Position> updateToNextGeneration2() { List<Position> result = new ArrayList<Position>(); List<Item> actualItemList = new ArrayList<Item>(itemList); for (Item item: actualItemList) { @@ -118,20 +86,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> { return result; } - private List<Position> updateFires() { - List<Position> result = new ArrayList<>(); - if (step % 2 == 0) { - List<Position> newFirePositions = new ArrayList<>(); - for (Position fire : firePositions) { - newFirePositions.addAll(neighbors(fire)); - } - firePositions.addAll(newFirePositions); - result.addAll(newFirePositions); - } - return result; - - } - @Override public int stepNumber() { return step; } @@ -148,35 +102,12 @@ public class FirefighterBoard implements Board<List<ModelElement>> { public List<Box> boxList() { return boxList; } - private List<Position> updateFirefighters() { - List<Position> result = new ArrayList<>(); - List<Position> firefighterNewPositions = new ArrayList<>(); - for (Position firefighterPosition : firefighterPositions) { - Position newFirefighterPosition = neighborClosestToFire(firefighterPosition); - firefighterNewPositions.add(newFirefighterPosition); - extinguish(newFirefighterPosition); - result.add(firefighterPosition); - result.add(newFirefighterPosition); - List<Position> neighborFirePositions = neighbors(newFirefighterPosition).stream() - .filter(firePositions::contains).toList(); - for(Position firePosition : neighborFirePositions) - extinguish(firePosition); - result.addAll(neighborFirePositions); - } - firefighterPositions = firefighterNewPositions; - return result; - } - @Override public void reset() { step = 0; itemList.clear(); boxList.clear(); - initializeElements2(); - } - - private void extinguish(Position position) { - firePositions.remove(position); + initializeElements(); } public List<Position> neighbors(Position position) { @@ -188,40 +119,6 @@ public class FirefighterBoard implements Board<List<ModelElement>> { return list; } - private Position neighborClosestToFire(Position position) { - Set<Position> seen = new HashSet<>(); - HashMap<Position, Position> firstMove = new HashMap<>(); - Queue<Position> toVisit = new LinkedList<>(neighbors(position)); - for (Position initialMove : toVisit) { - firstMove.put(initialMove, initialMove); - } - while (!toVisit.isEmpty()) { - Position current = toVisit.poll(); - if (firePositions.contains(current)) - return firstMove.get(current); - for (Position adjacent : neighbors(current)) { - if (seen.contains(adjacent)) continue; - toVisit.add(adjacent); - seen.add(adjacent); - firstMove.put(adjacent, firstMove.get(current)); - } - } - return position; - } - - @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); - case FIREFIGHTER -> firefighterPositions.add(position); - } - } - } public void testScreen(){ Position position; diff --git a/src/main/java/model/MotorFirefighter.java b/src/main/java/model/MotorFirefighter.java index 64bf86ab8f541d977c3457654b1518bd0886eb01..2c46d0f0f1022d647c5d942ee98efae0b4a5c4cd 100644 --- a/src/main/java/model/MotorFirefighter.java +++ b/src/main/java/model/MotorFirefighter.java @@ -17,15 +17,30 @@ public class MotorFirefighter extends Extinguisher implements Item{ Set<Position> seen = new HashSet<>(); HashMap<Position, Position> firstMove = new HashMap<>(); Queue<Position> toVisit = new LinkedList<>(); - for (int i = 0; i < 2; i++){ - for (Position initialMove : board.neighbors(position)) { - if (board.getItemByPosition(initialMove) == null) { - firstMove.put(initialMove, initialMove); - toVisit.add(initialMove); + for (Position initialMove : board.neighbors(position)) { + if (board.getItemByPosition(initialMove) == null) { + toVisit.add(initialMove); + } + if (board.getItemByPosition(initialMove) instanceof Fire) return new ArrayList<Position>(); + } + + while (!toVisit.isEmpty()) { + Position current = toVisit.poll(); + for (Position neighbors : board.neighbors(current)) { + if (board.getItemByPosition(neighbors) == null) { + firstMove.put(neighbors, neighbors); + } + if (board.getItemByPosition(neighbors) instanceof Fire) { + result.add(position); + position = current; + result.add(position); + return result; } - if (board.getItemByPosition(initialMove) instanceof Fire) return new ArrayList<Position>(); } } + + toVisit.addAll(firstMove.values()); + while (!toVisit.isEmpty()) { Position current = toVisit.poll(); if (board.getItemByPosition(current) instanceof Fire) { diff --git a/src/main/java/view/FirefighterGrid.java b/src/main/java/view/FirefighterGrid.java index f302ac40ec86ea05f5d56331abdbf52d8192ecdb..3bdcfc5d19af969ae2776379f90369d1d5f410c4 100644 --- a/src/main/java/view/FirefighterGrid.java +++ b/src/main/java/view/FirefighterGrid.java @@ -21,47 +21,20 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{ private int columnCount; private int rowCount; - @Override - public void repaint(List<Pair<Position, ViewElement>> positionedElements) { - clear(positionedElements); - paint(positionedElements); - paintLines(); - } - - @Override - public void repaint(ViewElement[][] elements) { - clear(); - paint(elements); - paintLines(); - } - - public void repaint2(List<Item> itemList, List<Position> clearList) { - clear2(clearList); - paint2(itemList); + public void repaint(List<Item> itemList, List<Position> clearList) { + clear(clearList); + paint(itemList); paintLines(); } - private void clear(List<Pair<Position, ViewElement>> positionedElements) { - for (Pair<Position, ViewElement> positionElement : positionedElements) { - Position position = positionElement.getKey(); - clearSquare(position.row(), position.column()); - } - } - - private void clear2(List<Position> positions) { + private void clear(List<Position> positions) { for (Position position : positions) { clearSquare(position.row(), position.column()); paintSquare(position.row(), position.column(), Color.WHITE); } } - private void paint(List<Pair<Position, ViewElement>> positionedElements) { - for(Pair<Position, ViewElement> pair : positionedElements){ - paintElementAtPosition(pair.getValue(), pair.getKey()); - } - } - - private void paint2(List<Item> itemList) { + private void paint(List<Item> itemList) { for (Item item : itemList) { item.paint(this); } @@ -74,7 +47,7 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{ } } paintLines(); - paint2(itemList); + paint(itemList); } diff --git a/src/main/java/view/Grid.java b/src/main/java/view/Grid.java index f9ba831e1f00b18fc233f16ce2cbb4aa373a93d2..6b29ca7beb922e02ad494983ed5de67dc2acc659 100644 --- a/src/main/java/view/Grid.java +++ b/src/main/java/view/Grid.java @@ -16,22 +16,7 @@ import java.util.List; */ public interface Grid<E> { - /** - * Repaint the grid with a list of elements, each associated with their respective positions. - * - * @param elements A list of pairs, each containing a position and the element to be displayed at that position. - */ - void repaint(List<Pair<Position, E>> elements); - - /** - * Repaint the grid with a two-dimensional array of elements. The array's dimensions should match - * the row and column count of the grid. - * - * @param elements A two-dimensional array of elements to be displayed on the grid. - */ - void repaint(E[][] elements); - - void repaint2(List<Item> itemList, List<Position> clearList); + void repaint(List<Item> itemList, List<Position> clearList); /** * Set the dimensions of the grid to the specified number of columns, number of rows, square width, diff --git a/src/test/java/model/FirefighterBoardTest.java b/src/test/java/model/FirefighterBoardTest.java index 25cc8dbca8acea698879df68a5006a179f281ecc..3b308223258e6b9967ae9a0a39fe185b2aec4d35 100644 --- a/src/test/java/model/FirefighterBoardTest.java +++ b/src/test/java/model/FirefighterBoardTest.java @@ -10,17 +10,17 @@ import static org.assertj.core.api.Assertions.*; public class FirefighterBoardTest { @Test void testColumnCount(){ - Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3); + Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3, 0, 0); assertThat(board.columnCount()).isEqualTo(20); } @Test void testRowCount(){ - Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3); + Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3, 0, 0); assertThat(board.rowCount()).isEqualTo(10); } @Test void testStepNumber(){ - Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3); + Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3, 0, 0); for(int index = 0; index < 10; index++){ assertThat(board.stepNumber()).isEqualTo(index); board.updateToNextGeneration(); @@ -29,11 +29,10 @@ public class FirefighterBoardTest { } @Test void testGetState_afterSet(){ - Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 0, 0); + Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 0, 0, 0); Position position = new Position(1,2); - assertThat(board.getState(position)).isEmpty(); + /*assertThat(board.getState(position)).isEmpty(); board.setState(List.of(ModelElement.FIRE), position); - assertThat(board.getState(position)).containsExactly(ModelElement.FIRE); + assertThat(board.getState(position)).containsExactly(ModelElement.FIRE);*/ } - }