diff --git a/src/main/java/app/SimulatorApplication.java b/src/main/java/app/SimulatorApplication.java index 179d224778e79e32f5b657dbdb29fab36f0dc3c3..7b20109cc6272fc8bd2f996d24042dc9ad05c7a5 100644 --- a/src/main/java/app/SimulatorApplication.java +++ b/src/main/java/app/SimulatorApplication.java @@ -18,7 +18,9 @@ public class SimulatorApplication extends javafx.application.Application { private static final int SQUARE_WIDTH = 50; private static final int SQUARE_HEIGHT = 50; public static final int INITIAL_FIRE_COUNT = 3; - public static final int INITIAL_FIREFIGHTER_COUNT = 6; + public static final int INITIAL_FIREFIGHTER_COUNT = 0; + + public static final int INITIAL_CLOUD_COUNT = 3; private Stage primaryStage; private Parent view; @@ -44,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_FIRE_COUNT, INITIAL_FIREFIGHTER_COUNT, INITIAL_CLOUD_COUNT); } private void showScene() { diff --git a/src/main/java/controller/Controller.java b/src/main/java/controller/Controller.java index 8f765e69cb3d5128f11c4d0e7ae99eebfca53886..ba7d444e1a5a5dd931de271f492e460d4165dbeb 100644 --- a/src/main/java/controller/Controller.java +++ b/src/main/java/controller/Controller.java @@ -21,6 +21,7 @@ import view.Grid; import view.ViewElement; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import static java.util.Objects.requireNonNull; @@ -72,13 +73,18 @@ public class Controller { } private void updateBoard2(){ + board.testScreen(); List<Position> updatedPositions = board.updateToNextGeneration2(); + List<Position> updatedClearSquares = new ArrayList<>(); List<Item> updatedSquares = new ArrayList<>(); for(Position updatedPosition : updatedPositions){ Item squareState = board.getItemByPosition(updatedPosition); - updatedSquares.add(squareState); + updatedClearSquares.add(updatedPosition); + if (squareState != null){ + updatedSquares.add(squareState); + } } - grid.repaint(updatedSquares); + grid.repaint2(updatedSquares, updatedClearSquares); updateGenerationLabel(board.stepNumber()); } @@ -93,6 +99,12 @@ public class Controller { updateGenerationLabel(board.stepNumber()); } + private void repaintGrid2(){ + + grid.initialize(board.itemList()); + updateGenerationLabel(board.stepNumber()); + } + private ViewElement getViewElement(List<ModelElement> squareState) { if(squareState.contains(ModelElement.FIREFIGHTER)){ return ViewElement.FIREFIGHTER; @@ -106,7 +118,7 @@ public class Controller { private void initializeTimeline() { Duration duration = new Duration(Controller.PERIOD_IN_MILLISECONDS); EventHandler<ActionEvent> eventHandler = - event -> updateBoard(); + event -> updateBoard2(); KeyFrame keyFrame = new KeyFrame(duration, eventHandler); timeline = new Timeline(keyFrame); timeline.setCycleCount(Animation.INDEFINITE); @@ -132,7 +144,7 @@ public class Controller { this.pause(); board.reset(); pauseToggleButton.setSelected(true); - repaintGrid(); + repaintGrid2(); } public void initialize(int squareWidth, int squareHeight, int columnCount, @@ -142,9 +154,17 @@ public class Controller { repaintGrid(); } + public void initialize(int squareWidth, int squareHeight, int columnCount, + int rowCount, int initialFireCount, int initialFirefighterCount, int initialCloudCount) { + + this.setModel(new FirefighterBoard(columnCount, rowCount, initialFireCount, initialFirefighterCount, initialCloudCount)); + grid.setDimensions(columnCount, rowCount, squareWidth, squareHeight); + repaintGrid2(); + } + public void oneStepButtonAction() { this.pause(); - updateBoard(); + updateBoard2(); } private void updateGenerationLabel(int value){ diff --git a/src/main/java/model/Board.java b/src/main/java/model/Board.java index 46b92b8bc1d1a35d831eca3ee8e83961fe823684..9e2c5acff42b9e94fa0b7fd203b017d5dcf64bbf 100644 --- a/src/main/java/model/Board.java +++ b/src/main/java/model/Board.java @@ -71,6 +71,9 @@ public interface Board<S> { */ int stepNumber(); + List<Item> itemList(); + Item getItemByPosition(Position position); + void testScreen(); } diff --git a/src/main/java/model/Cloud.java b/src/main/java/model/Cloud.java index bc42652baba0a7c93a234e04d86c9297aaa3649c..a4b18d8d9ef28d0157c2fe104f4b4b5fddcdfc40 100644 --- a/src/main/java/model/Cloud.java +++ b/src/main/java/model/Cloud.java @@ -1,5 +1,6 @@ package model; +import javafx.scene.paint.Color; import util.Position; import java.util.ArrayList; @@ -9,6 +10,7 @@ import java.util.Random; public class Cloud extends Extinguisher implements Item { public Cloud(Position position) { super(position); + color = Color.GREY; } @@ -29,4 +31,9 @@ public class Cloud extends Extinguisher implements Item { result.add(position); return result; } + + public String toString(){ + return "Nuage position : [" + position.row() + ", " + position.column()+ "]"; + } + } diff --git a/src/main/java/model/Extinguisher.java b/src/main/java/model/Extinguisher.java index 87a996cb52bcef679d47cff8f69514c88d4d86d6..af8c779ce504fac84653ac85a24d8f31ac8797dd 100644 --- a/src/main/java/model/Extinguisher.java +++ b/src/main/java/model/Extinguisher.java @@ -1,12 +1,16 @@ package model; +import javafx.scene.paint.Color; import util.Position; +import view.FirefighterGrid; import java.util.ArrayList; import java.util.List; public abstract class Extinguisher { + protected Position position; + protected Color color; public Extinguisher(Position position){ this.position = position; @@ -41,4 +45,11 @@ public abstract class Extinguisher { } abstract List<Position> move(FirefighterBoard board); + + public void paint(FirefighterGrid grid){ + grid.paintCircle(position.row(), position.column(), color); + } + + public abstract String toString(); + } diff --git a/src/main/java/model/Fire.java b/src/main/java/model/Fire.java index f2e8db671a8c8f17d99e5e4aa59f31aeefb27b45..c6789998b9feabc235065a69b516d6ed2b6fc875 100644 --- a/src/main/java/model/Fire.java +++ b/src/main/java/model/Fire.java @@ -1,6 +1,8 @@ package model; +import javafx.scene.paint.Color; import util.Position; +import view.FirefighterGrid; import java.util.ArrayList; import java.util.List; @@ -8,9 +10,11 @@ import java.util.List; public class Fire implements Item{ private final Position position; + private final Color color; public Fire(Position position) { this.position = position; + this.color = Color.RED; } public List<Position> update(FirefighterBoard board) { @@ -22,9 +26,17 @@ public class Fire implements Item{ result.add(neighborposition); } } - return neighborslist; + return result; } public Position position() { return position; } + + public void paint(FirefighterGrid grid){ + grid.paintTriangle(position.row(), position.column(), color); + } + + public String toString(){ + return "Feu position : [" + position.row() + ", " + position.column()+ "]"; + } } diff --git a/src/main/java/model/Firefighter.java b/src/main/java/model/Firefighter.java index dc18e07fa8c8e12f13a682b1e2c56ea8541c8ae1..906e3eedd6e8415975fa0c6cfe00b3e9cb5643b3 100644 --- a/src/main/java/model/Firefighter.java +++ b/src/main/java/model/Firefighter.java @@ -1,12 +1,15 @@ package model; +import javafx.scene.paint.Color; import util.Position; +import java.util.List; import java.util.*; public class Firefighter extends Extinguisher implements Item{ public Firefighter(Position position) { super(position); + this.color = Color.BLUE; } public List<Position> move(FirefighterBoard board) { @@ -38,4 +41,9 @@ public class Firefighter extends Extinguisher implements Item{ } return null; } + + public String toString(){ + return "Pompier position : [" + position.row() + ", " + position.column()+ "]"; + } + } diff --git a/src/main/java/model/FirefighterBoard.java b/src/main/java/model/FirefighterBoard.java index 7ff12de03d2aff326eb81ab13fa36835449a092e..235d833c1aa3f0ff274fa9fe1585dd074d397700 100644 --- a/src/main/java/model/FirefighterBoard.java +++ b/src/main/java/model/FirefighterBoard.java @@ -11,6 +11,8 @@ public class FirefighterBoard implements Board<List<ModelElement>> { private final int initialFireCount; private final int initialFirefighterCount; + + private final int initialCloudCount; private List<Item> itemList; private List<Box> boxList; private List<Position> firefighterPositions; @@ -23,9 +25,21 @@ public class FirefighterBoard implements Board<List<ModelElement>> { 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) { + this.columnCount = columnCount; + this.rowCount = rowCount; + this.itemList = new ArrayList<>(); + this.boxList = new ArrayList<>(); + this.initialFireCount = initialFireCount; + this.initialFirefighterCount = initialFirefighterCount; + this.initialCloudCount = initialCloudCount; + initializeElements2(); + } + public void initializeElements() { firefighterPositions = new ArrayList<>(); firePositions = new HashSet<>(); @@ -35,10 +49,30 @@ public class FirefighterBoard implements Board<List<ModelElement>> { firefighterPositions.add(randomPosition()); } + public void initializeElements2() { + for (int i = 0; i < initialFireCount; i++){ + itemList.add(new Fire(randomEmptyPosition())); + } + for (int i = 0; i < initialFirefighterCount; i++){ + itemList.add(new Firefighter(randomEmptyPosition())); + } + for (int i = 0; i < initialCloudCount; i++){ + itemList.add(new Cloud(randomEmptyPosition())); + } + } + private Position randomPosition() { return new Position(randomGenerator.nextInt(rowCount), randomGenerator.nextInt(columnCount)); } + private Position randomEmptyPosition() { + Position result; + for(;;){ + result = new Position(randomGenerator.nextInt(rowCount), randomGenerator.nextInt(columnCount)); + if (getItemByPosition(result) == null) return result; + } + } + @Override public List<ModelElement> getState(Position position) { List<ModelElement> result = new ArrayList<>(); @@ -70,11 +104,13 @@ public class FirefighterBoard implements Board<List<ModelElement>> { public List<Position> updateToNextGeneration2() { List<Position> result = new ArrayList<Position>(); - for (Item item: itemList) { + List<Item> actualItemList = new ArrayList<Item>(itemList); + for (Item item: actualItemList) { if (!(item instanceof Fire)) result.addAll(item.update(this)); } - if (step%2 == 0){ - for (Item item: itemList) { + actualItemList = new ArrayList<Item>(itemList); + if (step % 2 == 1){ + for (Item item: actualItemList) { if (item instanceof Fire) result.addAll(item.update(this)); } } @@ -102,8 +138,8 @@ public class FirefighterBoard implements Board<List<ModelElement>> { public List<Item> itemList() { return itemList; } public Item getItemByPosition(Position position) { - for (Item item: itemList) { - if (item.position() == position){ + for (Item item : itemList) { + if (item.position().equals(position)) { return item; } } @@ -134,7 +170,9 @@ public class FirefighterBoard implements Board<List<ModelElement>> { @Override public void reset() { step = 0; - initializeElements(); + itemList.clear(); + boxList.clear(); + initializeElements2(); } private void extinguish(Position position) { @@ -184,4 +222,24 @@ public class FirefighterBoard implements Board<List<ModelElement>> { } } } + public void testScreen(){ + Position position; + for (int row = 0; row < rowCount; row++){ + for (int column = 0; column < columnCount; column++){ + + position = new Position(row,column); + if (getItemByPosition(position) == null){ + System.out.print("□ "); + } else if (getItemByPosition(position) instanceof Fire) { + System.out.print("f "); + } else if (getItemByPosition(position) instanceof Cloud) { + System.out.print("c "); + } else if (getItemByPosition(position) instanceof Firefighter) { + System.out.print("F "); + } + } + System.out.println(); + } + System.out.println(); + } } \ No newline at end of file diff --git a/src/main/java/model/Item.java b/src/main/java/model/Item.java index c0dab144aa4335e57a48d8c69fbeb48363b9e3c7..f4c82a3e1e67d8eb51144bcc4074e3106a8909d0 100644 --- a/src/main/java/model/Item.java +++ b/src/main/java/model/Item.java @@ -1,6 +1,8 @@ package model; import util.Position; +import view.FirefighterGrid; + import java.util.ArrayList; import java.util.List; @@ -9,4 +11,8 @@ public interface Item { List<Position> update(FirefighterBoard board); Position position(); + + void paint(FirefighterGrid grid); + + String toString(); } diff --git a/src/main/java/view/FirefighterGrid.java b/src/main/java/view/FirefighterGrid.java index c1b2274637f93e4a2482faa9d2a5559211b31dc6..28bbedd8e0e10cdcb8fe9412d07e720541baaaec 100644 --- a/src/main/java/view/FirefighterGrid.java +++ b/src/main/java/view/FirefighterGrid.java @@ -3,8 +3,12 @@ package view; import javafx.scene.canvas.Canvas; import javafx.scene.paint.Color; import javafx.util.Pair; +import model.Fire; +import model.Item; import util.Position; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; public class FirefighterGrid extends Canvas implements Grid<ViewElement>{ @@ -24,6 +28,19 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{ paintLines(); } + @Override + public void repaint(ViewElement[][] elements) { + clear(); + paint(elements); + paintLines(); + } + + public void repaint2(List<Item> itemList, List<Position> clearList) { + clear2(clearList); + paint2(itemList); + paintLines(); + } + private void clear(List<Pair<Position, ViewElement>> positionedElements) { for (Pair<Position, ViewElement> positionElement : positionedElements) { Position position = positionElement.getKey(); @@ -31,19 +48,37 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{ } } + private void clear2(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()); } } - @Override - public void repaint(ViewElement[][] elements) { - clear(); - paint(elements); + private void paint2(List<Item> itemList) { + for (Item item : itemList) { + item.paint(this); + } + } + + public void initialize(List<Item> itemList) { + for (int row = 0; row < rowCount; row++){ + for (int column = 0; column < columnCount; column++){ + paintSquare(row, column, Color.WHITE); + } + } paintLines(); + paint2(itemList); } + + private void clear() { getGraphicsContext2D().clearRect(0,0,getWidth(), getHeight()); } @@ -91,6 +126,18 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{ getGraphicsContext2D().fillRect(row*squareHeight,column*squareWidth,squareHeight,squareWidth); } + public void paintTriangle(int row, int column, Color color){ + getGraphicsContext2D().setFill(color); + getGraphicsContext2D().fillPolygon(new double[]{row*squareHeight , row*squareHeight + squareHeight/2, (row+1)*squareHeight}, + new double[]{(column+1)*squareWidth , column*squareHeight, (column+1)*squareHeight}, + 3); + } + + public void paintCircle(int row, int column, Color color){ + getGraphicsContext2D().setFill(color); + getGraphicsContext2D().fillOval(row*squareHeight, column*squareWidth, squareHeight, squareWidth); + } + private void clearSquare(int row, int column){ getGraphicsContext2D().clearRect(row*squareHeight,column*squareWidth,squareHeight,squareWidth); } diff --git a/src/main/java/view/Grid.java b/src/main/java/view/Grid.java index b95d59f622a86b41f2a41261b8b27aaf2e911dfb..f9ba831e1f00b18fc233f16ce2cbb4aa373a93d2 100644 --- a/src/main/java/view/Grid.java +++ b/src/main/java/view/Grid.java @@ -1,8 +1,12 @@ package view; import javafx.util.Pair; +import model.Item; import util.Position; +import model.Board; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; /** @@ -27,6 +31,8 @@ public interface Grid<E> { */ void repaint(E[][] elements); + void repaint2(List<Item> itemList, List<Position> clearList); + /** * Set the dimensions of the grid to the specified number of columns, number of rows, square width, * and square height. @@ -51,5 +57,7 @@ public interface Grid<E> { * @return The number of rows in the grid. */ int rowCount(); + + void initialize(List<Item> itemList); }