Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
Firefighter template
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
PHAM Thi ngoc linh
Firefighter template
Commits
db66faa5
Commit
db66faa5
authored
1 year ago
by
PHAM Thi ngoc linh
Browse files
Options
Downloads
Patches
Plain Diff
upgrade
parent
cd8a2246
No related branches found
No related tags found
No related merge requests found
Pipeline
#24838
failed
1 year ago
Stage: build
Stage: test
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/model/Fire.java
+69
-6
69 additions, 6 deletions
src/main/java/model/Fire.java
with
69 additions
and
6 deletions
src/main/java/model/Fire.java
+
69
−
6
View file @
db66faa5
...
...
@@ -5,11 +5,74 @@ import util.Position;
import
java.util.Random
;
import
java.util.Set
;
public
class
Fire
{
private
final
int
initialFireCount
;
private
Set
<
Position
>
firePositions
;
private
final
Random
randomGenerator
=
new
Random
();
private
Position
randomPosition
()
{
return
new
Position
(
randomGenerator
.
nextInt
(
rowCount
),
randomGenerator
.
nextInt
(
columnCount
));
import
java.util.Random
;
public
class
Fire
extends
Element
implements
State
{
private
boolean
onFire
;
private
int
propagationDelay
;
// Number of turns before the fire can spread
private
int
propagationCounter
;
// Counter to track the remaining turns for propagation
private
Random
random
=
new
Random
();
// Constructor
public
Fire
(
Position
position
,
int
propagationDelay
)
{
super
(
position
);
this
.
onFire
=
true
;
this
.
propagationDelay
=
propagationDelay
;
this
.
propagationCounter
=
propagationDelay
;
}
// Implementing State interface method
@Override
public
boolean
isOnFire
()
{
return
onFire
;
}
// Implementing Element class method
@Override
public
void
update
()
{
if
(
canSpread
())
{
spreadFire
();
}
}
// Method to check if the fire can spread
private
boolean
canSpread
()
{
return
onFire
&&
propagationCounter
<=
0
;
}
// Method to spread the fire randomly to adjacent positions
private
void
spreadFire
()
{
Position
[]
adjacentPositions
=
getAdjacentPositions
();
if
(
adjacentPositions
.
length
>
0
)
{
int
randomIndex
=
random
.
nextInt
(
adjacentPositions
.
length
);
Position
randomAdjacentPosition
=
adjacentPositions
[
randomIndex
];
Element
element
=
getGrid
().
getElementAt
(
randomAdjacentPosition
);
// Check if the adjacent position is not occupied by another fire
if
(
element
==
null
||
!(
element
instanceof
Fire
))
{
// Spread the fire to the adjacent position
getGrid
().
addElement
(
new
Fire
(
randomAdjacentPosition
,
propagationDelay
),
randomAdjacentPosition
);
}
}
// Reset the propagation counter
propagationCounter
=
propagationDelay
;
}
// Method to get adjacent positions
private
Position
[]
getAdjacentPositions
()
{
int
x
=
position
.
getX
();
int
y
=
position
.
getY
();
// Define adjacent positions
Position
[]
adjacentPositions
=
{
new
Position
(
x
-
1
,
y
-
1
),
new
Position
(
x
-
1
,
y
),
new
Position
(
x
-
1
,
y
+
1
),
new
Position
(
x
,
y
-
1
),
new
Position
(
x
,
y
+
1
),
new
Position
(
x
+
1
,
y
-
1
),
new
Position
(
x
+
1
,
y
),
new
Position
(
x
+
1
,
y
+
1
)
};
return
adjacentPositions
;
}
}
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