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

upgrade

parent d914b949
No related branches found
No related tags found
No related merge requests found
Pipeline #24834 failed
......@@ -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;
}
}
......@@ -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;
}
private final Random randomGenerator = new Random();
public void setGrid(Grid grid) {
this.grid = grid;
}
}
......@@ -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 List<Position> updatePosition() {
return null;
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();
}
}
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];
}
// 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);
}
// 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();
}
}
}
public State getState(Position position){
return null;
}
//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
// Getters for grid size
public int getRows() {
return rows;
}
public int getColumns() {
return columns;
}
}
......@@ -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();
}
}
......@@ -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;
}
}
......@@ -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;
}
}
......@@ -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 List<Position> stablePosition() {
return null;
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