Skip to content
Snippets Groups Projects
Commit 435918d0 authored by aimennabi's avatar aimennabi
Browse files

tache 1

parent e8ca8aa1
Branches
No related tags found
No related merge requests found
Showing with 113 additions and 15 deletions
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="CompilerConfiguration"> <component name="CompilerConfiguration">
<bytecodeTargetLevel target="16" /> <bytecodeTargetLevel target="18" />
</component> </component>
</project> </project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<component name="FrameworkDetectionExcludesConfiguration"> <component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" /> <file type="web" url="file://$PROJECT_DIR$" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_16" default="true" project-jdk-name="corretto-16" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_18" default="true" project-jdk-name="corretto-16" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>
\ No newline at end of file
...@@ -17,12 +17,12 @@ public class Grid extends Canvas{ ...@@ -17,12 +17,12 @@ public class Grid extends Canvas{
setFocusTraversable(true); setFocusTraversable(true);
setOnMousePressed(this::mousePressed); setOnMousePressed(this::mousePressed);
model = new Model(this); model = new Model(this);
model.initialisation(3,8); model.initialisation(3,1,2,1);
} }
public void restart(MouseEvent mouseEvent){ public void restart(MouseEvent mouseEvent){
model = new Model(this); model = new Model(this);
model.initialisation(3,6); model.initialisation(3,6,0,2);
getGraphicsContext2D().clearRect(0,0,width,height); getGraphicsContext2D().clearRect(0,0,width,height);
repaint(); repaint();
} }
...@@ -51,6 +51,17 @@ public class Grid extends Canvas{ ...@@ -51,6 +51,17 @@ public class Grid extends Canvas{
getGraphicsContext2D().setFill(Color.BLUE); getGraphicsContext2D().setFill(Color.BLUE);
getGraphicsContext2D().fillRect(row*height/rowCount,col*width/colCount,height/rowCount,width/colCount); getGraphicsContext2D().fillRect(row*height/rowCount,col*width/colCount,height/rowCount,width/colCount);
} }
public void paintCC(int row ,int col){
getGraphicsContext2D().setFill(Color.GREEN);
getGraphicsContext2D().fillRect(row*height/rowCount,col*width/colCount,height/rowCount,width/colCount);
}
public void paintMF(int row ,int col){
getGraphicsContext2D().setFill(Color.BLACK);
getGraphicsContext2D().fillRect(row*height/rowCount,col*width/colCount,height/rowCount,width/colCount);
}
public void paintFire(int row, int col) { public void paintFire(int row, int col) {
getGraphicsContext2D().setFill(Color.RED); getGraphicsContext2D().setFill(Color.RED);
......
...@@ -4,9 +4,16 @@ import java.util.*; ...@@ -4,9 +4,16 @@ import java.util.*;
public class Model { public class Model {
Grid grid; Grid grid;
int colCount, rowCount; int colCount, rowCount;
MotoFighter motoFighter = new MotoFighter();
List<Position>linefite=new ArrayList<>();
List<Position> firefighters = new ArrayList<>(); List<Position> firefighters = new ArrayList<>();
List<Position> clouds = new ArrayList<>();
Set<Position> fires = new HashSet<>(); Set<Position> fires = new HashSet<>();
List<Position> ffNewPositions; List<Position> ffNewPositions;
List<Position> cNewPositions;
int step = 0; int step = 0;
public Model(Grid grid) { public Model(Grid grid) {
...@@ -16,11 +23,16 @@ public class Model { ...@@ -16,11 +23,16 @@ public class Model {
} }
public void initialisation(int fireNumber, int fireFighterNumber){ public void initialisation(int fireNumber, int fireFighterNumber,int cloudNumber,int motoFighterNumber){
for(int index=0; index<fireNumber;index++) for(int index=0; index<fireNumber;index++)
fires.add(randomPosition()); fires.add(randomPosition());
for(int index=0; index<fireFighterNumber;index++) for(int index=0; index<fireFighterNumber;index++)
firefighters.add(randomPosition()); firefighters.add(randomPosition());
for(int index=0; index<cloudNumber;index++)
clouds.add(randomPosition());
for(int index=0; index<motoFighterNumber;index++)
linefite.add(randomPosition());
motoFighter.motofighter.add(randomPosition().row());
} }
private Position randomPosition() { private Position randomPosition() {
...@@ -28,22 +40,47 @@ public class Model { ...@@ -28,22 +40,47 @@ public class Model {
} }
public void activation(){ public void activation(){
ffNewPositions = new ArrayList<>(); ffNewPositions = new ArrayList<>();
for(Position ff : firefighters){ for(Position ff : firefighters){
Position newPosition = activateFirefighter(ff); Position newPosition = activateFirefighter(ff);
grid.paint(ff.row,ff.col); grid.paint(ff.row(),ff.col());
grid.paintFF(newPosition.row, newPosition.col); grid.paintFF(newPosition.row(), newPosition.col());
ffNewPositions.add(newPosition); ffNewPositions.add(newPosition);
} }
cNewPositions = new ArrayList<>();
for(Position cc : clouds){
Position newPosition = activeCloud(cc);
grid.paint(cc.row(),cc.col());
grid.paintCC(newPosition.row(), newPosition.col());
cNewPositions.add(newPosition);
}
/* for(Position mF : motofighter){
Position newPosition = motoFighter.activateFighter(mF);
grid.paint(mF.row,mF.col);
grid.paintMF(newPosition.row, newPosition.col);
mfNewPositions.add(newPosition);
}*/
firefighters = ffNewPositions; firefighters = ffNewPositions;
clouds = cNewPositions;
motoFighter.activation();
if(step%2==0){ if(step%2==0){
List<Position> newFires = new ArrayList<>(); List<Position> newFires = new ArrayList<>();
for(Position fire : fires){ for(Position fire : fires){
newFires.addAll(activateFire(fire)); newFires.addAll(activateFire(fire));
} }
for(Position newFire : newFires) for(Position newFire : newFires)
grid.paintFire(newFire.row, newFire.col); grid.paintFire(newFire.row(), newFire.col());
fires.addAll(newFires);} fires.addAll(newFires);}
step++; step++;
...@@ -54,6 +91,26 @@ public class Model { ...@@ -54,6 +91,26 @@ public class Model {
return next(position); return next(position);
} }
private Position activeCloud(Position position){
Position randomPosition=next(position).get((int) (Math.random()*next(position).size()));
List<Position> nextFires = next(randomPosition).stream().filter(fires::contains).toList();
extinguish(randomPosition);
for (Position fire : nextFires)
extinguish(fire);
return randomPosition;
}
/* private Position activateMotofighter(Position position) {
Position randomPosition = tStepTowardFire(position);
//next(position).get((int) (Math.random()*next(position).size()));
List<Position> nextFires = next(randomPosition).stream().filter(fires::contains).toList();
extinguish(randomPosition);
for (Position fire : nextFires)
extinguish(fire);
return randomPosition;
}*/
private Position activateFirefighter(Position position) { private Position activateFirefighter(Position position) {
...@@ -68,17 +125,25 @@ public class Model { ...@@ -68,17 +125,25 @@ public class Model {
private void extinguish(Position position) { private void extinguish(Position position) {
fires.remove(position); fires.remove(position);
grid.paint(position.row, position.col); grid.paint(position.row(), position.col());
} }
private List<Position> next(Position position){ private List<Position> next(Position position){
List<Position> list = new ArrayList<>(); List<Position> list = new ArrayList<>();
if(position.row>0) list.add(new Position(position.row-1, position.col)); if(position.row()>0) list.add(new Position(position.row()-1, position.col()));
if(position.col>0) list.add(new Position(position.row, position.col-1)); if(position.col()>0) list.add(new Position(position.row(), position.col()-1));
if(position.row<rowCount-1) list.add(new Position(position.row+1, position.col)); if(position.row()<rowCount-1) list.add(new Position(position.row()+1, position.col()));
if(position.col<colCount-1) list.add(new Position(position.row, position.col+1)); if(position.col()<colCount-1) list.add(new Position(position.row(), position.col()+1));
return list; return list;
} }
/* private List<Position> nextTwo(Position position){
List<Position> list = new ArrayList<>();
if(position.row>0) list.add(new Position(position.row-2, position.col));
if(position.col>0) list.add(new Position(position.row, position.col-2));
if(position.row<rowCount-1) list.add(new Position(position.row+2, position.col));
if(position.col<colCount-1) list.add(new Position(position.row, position.col+2));
return list;
}*/
private Position aStepTowardFire(Position position){ private Position aStepTowardFire(Position position){
Queue<Position> toVisit = new LinkedList<>(); Queue<Position> toVisit = new LinkedList<>();
...@@ -101,5 +166,26 @@ public class Model { ...@@ -101,5 +166,26 @@ public class Model {
return position; return position;
} }
public record Position(int row, int col){} /* private Position tStepTowardFire(Position position){
Queue<Position> toVisit = new LinkedList<>();
Set<Position> seen = new HashSet<>();
HashMap<Position,Position> firstMove = new HashMap<>();
toVisit.addAll(nextTwo(position));
for(Position initialMove : toVisit)
firstMove.put(initialMove,initialMove);
while(!toVisit.isEmpty()){
Position current = toVisit.poll();
if(fires.contains(current))
return firstMove.get(current);
for(Position adjacent : next(current)){
if(seen.contains(adjacent)) continue;
toVisit.add(adjacent);
seen.add(adjacent);
firstMove.put(adjacent, firstMove.get(current));
}
}
return position;
}*/
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment