diff --git a/src/main/java/model/Cloud.java b/src/main/java/model/Cloud.java index d269ca2f5fb2dfbd1febe5e7962588bb68cfac19..cf7482a639ac602e27af2b384cd41793e9903a31 100644 --- a/src/main/java/model/Cloud.java +++ b/src/main/java/model/Cloud.java @@ -5,14 +5,86 @@ import util.Position; import java.util.List; import java.util.Random; -public class Cloud implements Extinguish{ - private final Random randomGenerator = new Random(); - private Position randomPosition() { - return new Position(randomGenerator.nextInt(rowCount), randomGenerator.nextInt(columnCount)); +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; + } } + diff --git a/src/main/java/model/Element.java b/src/main/java/model/Element.java index c6f37a309f8e3d36d5e576f2050300c8b01c8038..30e5ac9dd6322a0c04d09b4702d973886392b926 100644 --- a/src/main/java/model/Element.java +++ b/src/main/java/model/Element.java @@ -6,18 +6,35 @@ import java.util.List; import java.util.Random; public abstract class Element { - public void setPosition(){ + protected Position position; // Attribute to represent the current position + protected Grid grid; // Attribute to represent the reference to the grid + // Constructor + public Element(Position position, Grid grid) { + this.position = position; + this.grid = grid; } - public List<Position> getPosition(){ - return null; + // Abstract method for updating the state of the element + public abstract void update(); + + // Getters and Setters for Position + public Position getPosition() { + return position; } - public List<util.Position> updateToNextGeneration() { - return null; + + public void setPosition(Position position) { + this.position = position; } - private List<util.Position> neighbors(util.Position position){ - return null; + + // Getters and Setters for Grid Reference + public Grid getGrid() { + return grid; + } + + public void setGrid(Grid grid) { + this.grid = grid; } - private final Random randomGenerator = new Random(); } + + diff --git a/src/main/java/model/FireFighter.java b/src/main/java/model/FireFighter.java index ff00762354d3721058ecbb67c76ae8143912fc5c..7d3c7c1dbd4f8cec4def8a7f34eca73e908880ab 100644 --- a/src/main/java/model/FireFighter.java +++ b/src/main/java/model/FireFighter.java @@ -5,16 +5,54 @@ import util.Position; import java.util.List; import java.util.Set; -public class FireFighter implements Extinguish{ - private final int initialFirefighterCount; - private List<Position> firefighterPositions; - private Set<Position> firePositions; - private Position randomPosition() { - return new Position(randomGenerator.nextInt(rowCount), randomGenerator.nextInt(columnCount)); +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 List<Position> updatePosition() { - return null; + 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(); } } + diff --git a/src/main/java/model/Grid.java b/src/main/java/model/Grid.java index 649fa28723ff376d19d337fd5fea678d8204f7e5..d40aad3993d494e6b7061e4624f14b166735097a 100644 --- a/src/main/java/model/Grid.java +++ b/src/main/java/model/Grid.java @@ -1,22 +1,55 @@ package model; +import util.Position; + import java.util.Random; +import java.util.ArrayList; +import java.util.List; + public class Grid { - private final int columnCount; - private final int rowCount; - private int[][] grid ; - public Grid(int row,int column){ - this.columnCount = column; - this.rowCount = row; - grid = new int[rowCount][columnCount]; + private int rows; + private int columns; + private Element[][] elements; + + // Constructor + public Grid(int rows, int columns) { + this.rows = rows; + this.columns = columns; + this.elements = new Element[rows][columns]; } - public State getState(Position position){ - return null; + + // Method to add an element to the grid at a specific position + public void addElement(Element element, Position position) { + elements[position.getX()][position.getY()] = element; + element.setGrid(this); + element.setPosition(position); } - //ham de lay trang thai cua tung vi tri ban dau - //ham update vi tri sau khi da dap lua - //ham dap lua:xet vi tri lua;vi tri dia ly, quyet dinh phuong thuc dap lua + // Method to get the element at a specific position on the grid + public Element getElementAt(Position position) { + return elements[position.getX()][position.getY()]; + } + // Method to update all elements in the grid + public void update() { + for (int i = 0; i < rows; i++) { + for (int j = 0; j < columns; j++) { + Element element = elements[i][j]; + if (element != null) { + element.update(); + } + } + } + } + + // Getters for grid size + public int getRows() { + return rows; + } + + public int getColumns() { + return columns; + } } + diff --git a/src/main/java/model/Mobile.java b/src/main/java/model/Mobile.java index f08435ecb5a5b11a6dd8e2aa5f29ec0974abe8b8..8c5b5210b87c98e193a41aebea963a94412ff3e9 100644 --- a/src/main/java/model/Mobile.java +++ b/src/main/java/model/Mobile.java @@ -4,9 +4,44 @@ import util.Position; import java.util.List; -public class Mobile implements Extinguish{ +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 List<Position> updatePosition() { - return null; + 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(); } } + diff --git a/src/main/java/model/Moutain.java b/src/main/java/model/Moutain.java index 3fbb0f168fd6314d615fc04bd5f106fb35c5bf20..9d91a3072a924b5cfd8932888118f2764cde129f 100644 --- a/src/main/java/model/Moutain.java +++ b/src/main/java/model/Moutain.java @@ -4,9 +4,29 @@ import util.Position; import java.util.List; -public class Moutain implements StablePosition{ +public class Mountain extends Element implements Stable { + // Additional attributes for Mountain + private boolean impassable; + + // Constructor + public Mountain(Position position, boolean impassable) { + super(position); + this.impassable = impassable; + } + @Override - public List<Position> stablePosition() { - return null; + 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; } } + diff --git a/src/main/java/model/Road.java b/src/main/java/model/Road.java index 9a28c25f72dfc67a1e02eddca83f1eba151e4b34..736d9833637b408176ed9aaf69acb0894da1038f 100644 --- a/src/main/java/model/Road.java +++ b/src/main/java/model/Road.java @@ -4,9 +4,24 @@ import util.Position; import java.util.List; -public class Road implements StablePosition{ +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 List<Position> stablePosition() { - return null; + public void update() { + // Logic for road's update, if needed + } + + // Method to check passability + public boolean isPassable() { + return passable; } } + diff --git a/src/main/java/model/Rocks.java b/src/main/java/model/Rocks.java index 875427169b18a7dc011c690854204a7ccedc6333..f8aa3ce5bc07d69e4f3af57a016279e90f3e0c64 100644 --- a/src/main/java/model/Rocks.java +++ b/src/main/java/model/Rocks.java @@ -4,9 +4,34 @@ import util.Position; import java.util.List; -public class Rocks implements StablePosition{ +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 List<Position> stablePosition() { - return null; + 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; } } +