Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
FirefighterStarter
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
NDIAYE Ousseynou
FirefighterStarter
Commits
4f32dbe0
Commit
4f32dbe0
authored
8 months ago
by
ousseyn01
Browse files
Options
Downloads
Patches
Plain Diff
FirefighterBoard is DONE
parent
d13c3305
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/model/FirefighterBoard.java
+38
-51
38 additions, 51 deletions
src/main/java/model/FirefighterBoard.java
with
38 additions
and
51 deletions
src/main/java/model/FirefighterBoard.java
+
38
−
51
View file @
4f32dbe0
...
...
@@ -4,16 +4,15 @@ import util.Position;
import
java.util.*
;
public
class
FirefighterBoard
implements
Board
<
List
<
ModelElement
>>
{
private
final
int
columnCount
;
private
final
int
rowCount
;
private
final
int
initialFireCount
;
private
final
int
initialFirefighterCount
;
private
final
TargetStrategy
targetStrategy
=
new
TargetStrategy
();
private
List
<
Position
>
firefighter
Position
s
;
private
Set
<
Position
>
firePositions
;
private
Map
<
Position
,
List
<
Position
>>
neighbors
=
new
HashMap
();
private
final
model
.
TargetStrategy
targetStrategy
=
new
model
.
TargetStrategy
();
private
List
<
Firefighter
>
firefighters
;
private
Fire
fire
;
private
final
Map
<
Position
,
List
<
Position
>>
neighbors
=
new
HashMap
<>
();
private
final
Position
[][]
positions
;
private
int
step
=
0
;
private
final
Random
randomGenerator
=
new
Random
();
...
...
@@ -40,12 +39,13 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
}
public
void
initializeElements
()
{
firefighter
Position
s
=
new
ArrayList
<>();
firePositions
=
new
HashSet
<>();
firefighters
=
new
ArrayList
<>();
Set
<
Position
>
firePositions
=
new
HashSet
<>();
for
(
int
index
=
0
;
index
<
initialFireCount
;
index
++)
firePositions
.
add
(
randomPosition
());
fire
=
new
Fire
(
firePositions
,
neighbors
);
for
(
int
index
=
0
;
index
<
initialFirefighterCount
;
index
++)
firefighter
Positions
.
add
(
randomPosition
());
firefighter
s
.
add
(
new
Firefighter
(
randomPosition
())
)
;
}
private
Position
randomPosition
()
{
...
...
@@ -55,10 +55,10 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
@Override
public
List
<
ModelElement
>
getState
(
Position
position
)
{
List
<
ModelElement
>
result
=
new
ArrayList
<>();
for
(
Position
firefighter
Position
:
firefighter
Position
s
)
if
(
firefighterPosition
.
equals
(
position
))
for
(
Firefighter
firefighter
:
firefighters
)
if
(
firefighter
.
get
Position
()
.
equals
(
position
))
result
.
add
(
ModelElement
.
FIREFIGHTER
);
if
(
firePositions
.
contains
(
position
))
if
(
fire
.
getFire
Positions
()
.
contains
(
position
))
result
.
add
(
ModelElement
.
FIRE
);
return
result
;
}
...
...
@@ -75,23 +75,12 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
public
List
<
Position
>
updateToNextGeneration
()
{
List
<
Position
>
modifiedPositions
=
updateFirefighters
();
modifiedPositions
.
addAll
(
updateFires
());
step
++;
return
modifiedPositions
;
}
private
List
<
Position
>
updateFires
()
{
List
<
Position
>
modifiedPositions
=
new
ArrayList
<>();
if
(
step
%
2
==
0
){
List
<
Position
>
newFirePositions
=
new
ArrayList
<>();
for
(
Position
fire
:
firePositions
)
{
newFirePositions
.
addAll
(
neighbors
.
get
(
fire
));
}
firePositions
.
addAll
(
newFirePositions
);
modifiedPositions
.
addAll
(
newFirePositions
);
modifiedPositions
.
addAll
(
fire
.
spreadFire
());
}
return
modifiedPositions
;
step
++
;
return
modifiedPositions
;
}
@Override
...
...
@@ -101,22 +90,27 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
private
List
<
Position
>
updateFirefighters
()
{
List
<
Position
>
modifiedPosition
=
new
ArrayList
<>();
List
<
Position
>
firefighterNewPositions
=
new
ArrayList
<>();
for
(
Position
firefighterPosition
:
firefighterPositions
)
{
Position
newFirefighterPosition
=
targetStrategy
.
neighborClosestToFire
(
firefighterPosition
,
firePositions
,
neighbors
);
firefighterNewPositions
.
add
(
newFirefighterPosition
);
extinguish
(
newFirefighterPosition
);
modifiedPosition
.
add
(
firefighterPosition
);
modifiedPosition
.
add
(
newFirefighterPosition
);
List
<
Position
>
neighborFirePositions
=
neighbors
.
get
(
newFirefighterPosition
).
stream
()
.
filter
(
firePositions:
:
contains
).
toList
();
for
(
Position
firePosition
:
neighborFirePositions
)
extinguish
(
firePosition
);
modifiedPosition
.
addAll
(
neighborFirePositions
);
}
firefighterPositions
=
firefighterNewPositions
;
for
(
Firefighter
firefighter
:
firefighters
)
{
modifiedPosition
.
add
(
firefighter
.
getPosition
());
Position
newPosition
=
firefighter
.
moveToBestPosition
(
targetStrategy
,
fire
.
getFirePositions
(),
neighbors
);
firefighter
.
setPosition
(
newPosition
);
modifiedPosition
.
add
(
newPosition
);
firefighter
.
extinguish
(
newPosition
,
fire
.
getFirePositions
());
// Éteindre les feux dans les positions voisines
List
<
Position
>
adjacentFires
=
neighbors
.
get
(
newPosition
).
stream
()
.
filter
(
fire
.
getFirePositions
()::
contains
)
.
toList
();
for
(
Position
firePosition
:
adjacentFires
)
{
firefighter
.
extinguish
(
firePosition
,
fire
.
getFirePositions
());
modifiedPosition
.
add
(
firePosition
);
// Ajouter la position comme modifiée
}
}
return
modifiedPosition
;
}
...
...
@@ -126,21 +120,14 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
initializeElements
();
}
private
void
extinguish
(
Position
position
)
{
firePositions
.
remove
(
position
);
}
@Override
public
void
setState
(
List
<
ModelElement
>
state
,
Position
position
)
{
firePositions
.
remove
(
position
);
for
(;
;
)
{
if
(!
firefighterPositions
.
remove
(
position
))
break
;
}
fire
.
getFirePositions
().
remove
(
position
);
firefighters
.
removeIf
(
f
->
f
.
getPosition
().
equals
(
position
));
for
(
ModelElement
element
:
state
)
{
switch
(
element
)
{
case
FIRE
->
firePositions
.
add
(
position
);
case
FIREFIGHTER
->
firefighter
Positions
.
add
(
position
);
case
FIRE
->
fire
.
getFire
Positions
()
.
add
(
position
);
case
FIREFIGHTER
->
firefighter
s
.
add
(
new
Firefighter
(
position
)
)
;
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment