Skip to content
Snippets Groups Projects
Commit 59ff154e authored by PHAM Thi ngoc linh's avatar PHAM Thi ngoc linh
Browse files

restart

parent 23813b29
No related branches found
No related tags found
No related merge requests found
Pipeline #24891 failed
......@@ -8,88 +8,6 @@ import java.util.Random;
import java.util.Random;
public class Cloud extends Element implements Extinguish {
// Additional attributes for Cloud
private Random random = new Random();
// Constructor
public Cloud(Position position) {
super(position);
// Initialize cloud-specific attributes
}
@Override
public void extinguish(Element target) {
if (target instanceof Fire) {
// Extinguish the fire
((Fire) target).extinguish();
}
}
@Override
public void update() {
// Move the cloud randomly
moveRandomly();
// Extinguish fires in adjacent positions
extinguishFiresInAdjacentPositions();
}
private void moveRandomly() {
int newX = position.getX() + random.nextInt(3) - 1; // Move left, right, or stay in the same column
int newY = position.getY() + random.nextInt(3) - 1; // Move up, down, or stay in the same row
// Update the cloud's position
position = new Position(newX, newY);
}
private void extinguishFiresInAdjacentPositions() {
// Get adjacent positions
Position[] adjacentPositions = getAdjacentPositions();
// Extinguish fires in adjacent positions
for (Position adjacentPosition : adjacentPositions) {
Element element = getGrid().getElementAt(adjacentPosition);
extinguish(element);
}
}
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;
}
@Override
public List<Position> updatePosition() {
return null;
}
@Override
public List<Position> neighbors() {
return null;
}
@Override
public List<Position> getPosition() {
return null;
}
@Override
public List<Position> setPosition() {
return null;
}
@Override
public void action() {
}
}
......@@ -15,10 +15,10 @@ public abstract class Element {
private List<Position> neighbors(Position position) {}
private Position neighborClosestToFire(Position position) {}
public void setState(List<ModelElement> state, Position position) {}
/*List<Position> updatePosition();
List<Position> neighbors();
List<Position> getPosition();
List<Position> setPosition();*/
private List<Position> updatePosition();
private List<Position> getPosition();
private List<Position> setPosition();
......
......@@ -8,71 +8,7 @@ import java.util.Set;
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;
}
}
......@@ -6,58 +6,6 @@ import java.util.List;
import java.util.Set;
public class Firefighter extends Element implements Extinguish {
// Additional attributes for Firefighter, if any
// Constructor
public Firefighter(Position position) {
super(position);
// Initialize firefighter-specific attributes
}
@Override
public void extinguish(Element target) {
if (target instanceof Fire) {
// Extinguish the fire
((Fire) target).extinguish();
}
}
@Override
public void update() {
// Logic for firefighter's update, if needed
}
// Enhanced move method to check terrain passability
public void move(int deltaX, int deltaY) {
int newX = position.getX() + deltaX;
int newY = position.getY() + deltaY;
// Check if the new position is within the grid boundaries
if (isValidPosition(newX, newY)) {
Element targetElement = getGrid().getElementAt(new Position(newX, newY));
// Check terrain type before moving
if (targetElement instanceof Road) {
// Allow movement on roads
position = new Position(newX, newY);
} else if (targetElement instanceof Mountain) {
// Prevent movement on mountains
System.out.println("Cannot move on mountains!");
} else {
// Default behavior for other terrains
position = new Position(newX, newY);
}
}
}
// Check if a position is valid within the grid boundaries
private boolean isValidPosition(int x, int y) {
return x >= 0 && x < getGrid().getRows() && y >= 0 && y < getGrid().getColumns();
}
@Override
public void action() {
}
}
......@@ -5,48 +5,6 @@ import util.Position;
import java.util.List;
public class Mobile extends Element implements Extinguish {
// Additional attributes for Mobile
private int moveRange;
// Constructor
public Mobile(Position position, int moveRange) {
super(position);
this.moveRange = moveRange;
}
@Override
public void extinguish(Element target) {
if (target instanceof Fire) {
// Extinguish the fire
((Fire) target).extinguish();
}
}
@Override
public void update() {
// Logic for mobile's update, if needed
}
// Method to move the mobile element
public void move(int deltaX, int deltaY) {
int newX = position.getX() + deltaX;
int newY = position.getY() + deltaY;
// Check if the new position is within the grid boundaries
if (isValidPosition(newX, newY)) {
// Move the mobile element
position = new Position(newX, newY);
}
}
// Check if a position is valid within the grid boundaries
private boolean isValidPosition(int x, int y) {
return x >= 0 && x < getGrid().getRows() && y >= 0 && y < getGrid().getColumns();
}
@Override
public void action() {
}
}
......@@ -6,27 +6,6 @@ import java.util.List;
public class Mountain extends Element implements StablePosition {
// Additional attributes for Mountain
private boolean impassable;
// Constructor
public Mountain(Position position, boolean impassable) {
super(position);
this.impassable = impassable;
}
@Override
public void stabilize() {
// Logic for stabilizing mountain
}
@Override
public void update() {
// Logic for mountain's update, if needed
}
// Method to check passability
public boolean isPassable() {
return !impassable;
}
}
......@@ -5,23 +5,6 @@ import util.Position;
import java.util.List;
public class Road extends Element {
// Additional attributes for Road
private boolean passable;
// Constructor
public Road(Position position, boolean passable) {
super(position);
this.passable = passable;
}
@Override
public void update() {
// Logic for road's update, if needed
}
// Method to check passability
public boolean isPassable() {
return passable;
}
}
......@@ -6,32 +6,6 @@ import java.util.List;
public class Rock extends Element implements Stable {
// Additional attributes for Rock
private int propagationDelayCounter;
// Constructor
public Rock(Position position, int propagationDelay) {
super(position);
this.propagationDelayCounter = propagationDelay;
}
@Override
public void stabilize() {
// Logic for stabilizing rock
}
@Override
public void update() {
// Logic for rock's update, if needed
// Check propagation delay
if (propagationDelayCounter > 0) {
propagationDelayCounter--;
}
}
// Method to check propagation
public boolean canPropagate() {
return propagationDelayCounter <= 0;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment