Skip to content
Snippets Groups Projects
Commit 3fe65dbf authored by LATIOUI's avatar LATIOUI
Browse files

Merge remote-tracking branch 'origin/main'

# Conflicts:
#	.gradle/8.4/checksums/checksums.lock
#	.gradle/8.4/dependencies-accessors/dependencies-accessors.lock
#	.gradle/8.4/executionHistory/executionHistory.bin
#	.gradle/8.4/executionHistory/executionHistory.lock
#	.gradle/8.4/fileHashes/fileHashes.bin
#	.gradle/8.4/fileHashes/fileHashes.lock
#	.gradle/8.4/fileHashes/resourceHashesCache.bin
#	.gradle/buildOutputCleanup/buildOutputCleanup.lock
#	.gradle/buildOutputCleanup/cache.properties
#	.gradle/buildOutputCleanup/outputFiles.bin
#	.gradle/file-system.probe
#	build/classes/java/main/controller/Controller.class
#	build/classes/java/main/model/ExtinguishFire/ExtinguishFire.class
#	build/classes/java/main/model/ExtinguishFire/FireFighter.class
#	build/classes/java/main/model/Flammable/Fire.class
#	build/classes/java/main/model/Flammable/Flammable.class
#	build/classes/java/main/model/GameBoard.class
#	build/classes/java/main/view/FirefighterGrid.class
#	build/tmp/compileJava/previous-compilation-data.bin
parents ae60e36b 9a547962
No related branches found
No related tags found
No related merge requests found
...@@ -6,10 +6,9 @@ import util.Position; ...@@ -6,10 +6,9 @@ import util.Position;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
public interface ExtinguishFire extends BoardElement { public interface ExtinguishFire extends BoardElement {
void update(GameBoard gameBoard , Position position); void update(GameBoard gameBoard , Position position, HashMap<Position, ArrayList<BoardElement>> newElementPosition, ArrayList<Position> extinguishPosition);
} }
...@@ -66,14 +66,10 @@ public class FireFighter implements ExtinguishFire { ...@@ -66,14 +66,10 @@ public class FireFighter implements ExtinguishFire {
} }
@Override @Override
public void update(GameBoard gameBoard, Position position) { public void update(GameBoard gameBoard, Position position, HashMap<Position, ArrayList<BoardElement>> newElementPosition, ArrayList<Position> extinguishPosition) {
HashMap<Position, ArrayList<BoardElement>> elementPositionCopie = new HashMap<>();
for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPosition.entrySet()){
elementPositionCopie.put(entry.getKey(),entry.getValue());
}
List<Position> firePositions = new ArrayList<>(); List<Position> firePositions = new ArrayList<>();
for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPositionCopie.entrySet()) { for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPosition.entrySet()) {
for (BoardElement element : entry.getValue()) { for (BoardElement element : entry.getValue()) {
if (element.accept(new FireFinder())) { if (element.accept(new FireFinder())) {
firePositions.add(entry.getKey()); firePositions.add(entry.getKey());
...@@ -81,32 +77,22 @@ public class FireFighter implements ExtinguishFire { ...@@ -81,32 +77,22 @@ public class FireFighter implements ExtinguishFire {
} }
} }
Position newFirefighterPosition = gameBoard.neighborClosestToFire(position); Position newFirefighterPosition = gameBoard.neighborClosestToFire(position);
if(elementPositionCopie.containsKey(newFirefighterPosition)){ if(newElementPosition.containsKey(newFirefighterPosition)){
elementPositionCopie.get(newFirefighterPosition).add(new FireFighter(Color.BLUE)); newElementPosition.get(newFirefighterPosition).add(this);
} }
else{ else{
ArrayList<BoardElement> boardElements = new ArrayList<>(); ArrayList<BoardElement> boardElements = new ArrayList<>();
boardElements.add(new FireFighter(Color.BLUE)); boardElements.add(this);
elementPosition.put(newFirefighterPosition,boardElements); newElementPosition.put(newFirefighterPosition,boardElements);
} }
gameBoard.extinguish(newFirefighterPosition); extinguishPosition.add(newFirefighterPosition);
List<Position> neighborFirePositions = gameBoard.neighbors(newFirefighterPosition).stream() List<Position> neighborFirePositions = gameBoard.neighbors(newFirefighterPosition).stream()
.filter(firePositions::contains).toList(); .filter(firePositions::contains).toList();
for(Position newposition : neighborFirePositions){
if(elementPositionCopie.containsKey(newposition)){
elementPosition.get(newposition).add(new FireFighter(Color.BLUE));
}
else{
ArrayList<BoardElement> boardElements = new ArrayList<>();
boardElements.add(new FireFighter(Color.BLUE));
elementPosition.put(newposition,boardElements);
}
}
for(Position firePosition : neighborFirePositions) { for(Position firePosition : neighborFirePositions) {
gameBoard.extinguish(firePosition); extinguishPosition.add(firePosition);
} }
} }
} }
...@@ -64,19 +64,29 @@ public class Fire implements Flammable{ ...@@ -64,19 +64,29 @@ public class Fire implements Flammable{
} }
@Override // TODO: 15/11/2023 @Override // TODO: 15/11/2023
public void update(GameBoard gameBoard , Position position) { public void update(GameBoard gameBoard , Position position,HashMap<Position, ArrayList<BoardElement>> newElementPosition, ArrayList<Position> extinguishPosition) {
if(extinguishPosition.contains(position)){
return ;
}
if (gameBoard.stepNumber() % 2 == 0) { if (gameBoard.stepNumber() % 2 == 0) {
List<Position> newFirePositions = new ArrayList<>(); List<Position> newPositions = new ArrayList<>();
newFirePositions.addAll(gameBoard.neighbors(position)); newPositions.addAll(gameBoard.neighbors(position));
for(Position newPosition : newFirePositions){ if(newElementPosition.containsKey(position))
newElementPosition.get(position).add(this);
else {
newElementPosition.put(position,(new ArrayList<>()));
newElementPosition.get(position).add(this);
}
for(Position newPosition : newPositions){
//if(extinguishPosition.contains(newPosition))
//continue;
if(GameBoard.elementPosition.containsKey(newPosition)) { if(GameBoard.elementPosition.containsKey(newPosition)) {
for(BoardElement boardElement : GameBoard.elementPosition.get(newPosition)){ for(BoardElement boardElement : GameBoard.elementPosition.get(newPosition)){
if(boardElement.accept(new FireFinder()) && boardElement.accept(new CrossRoad()) && boardElement.accept(new CrossMountain())){ if(boardElement.accept(new FireFinder()) && boardElement.accept(new CrossRoad()) && boardElement.accept(new CrossMountain())){
break; break;
} }
else if(!boardElement.accept( new FireFinder())&& !boardElement.accept(new CrossRoad()) && !boardElement.accept(new CrossMountain())){ else if(!boardElement.accept( new FireFinder())&& !boardElement.accept(new CrossRoad()) && !boardElement.accept(new CrossMountain())){
GameBoard.elementPosition.get(newPosition).add(this); newElementPosition.get(newPosition).add(this);
} }
} }
...@@ -84,11 +94,18 @@ public class Fire implements Flammable{ ...@@ -84,11 +94,18 @@ public class Fire implements Flammable{
else{ else{
ArrayList<BoardElement> boardElements = new ArrayList<>(); ArrayList<BoardElement> boardElements = new ArrayList<>();
boardElements.add(this); boardElements.add(this);
GameBoard.elementPosition.put(newPosition,boardElements); newElementPosition.put(newPosition,boardElements);
System.out.println(GameBoard.elementPosition.get(position).get(0).getColor()); System.out.println(GameBoard.elementPosition.get(position).get(0).getColor());
} }
} }
}else{
if(newElementPosition.containsKey(position))
newElementPosition.get(position).add(this);
else {
newElementPosition.put(position,(new ArrayList<>()));
newElementPosition.get(position).add(this);
}
} }
} }
......
...@@ -10,5 +10,5 @@ import java.util.List; ...@@ -10,5 +10,5 @@ import java.util.List;
public interface Flammable extends BoardElement{ public interface Flammable extends BoardElement{
void update(GameBoard gameBoard, Position position); void update(GameBoard gameBoard, Position position,HashMap<Position, ArrayList<BoardElement>> newElementPosition, ArrayList<Position> extinguishPosition);
} }
...@@ -40,29 +40,35 @@ public class GameBoard implements Board{ ...@@ -40,29 +40,35 @@ public class GameBoard implements Board{
} }
@Override @Override
public void updateToNextGeneration() { public void updateToNextGeneration() {
HashMap<Position, ArrayList<BoardElement>> elementPositionCopie = new HashMap<>(); HashMap<Position, ArrayList<BoardElement>> newElementPosition = new HashMap<>();
for (Map.Entry<Position, ArrayList<BoardElement>> entry : this.elementPosition.entrySet()){ ArrayList<Position> extinguishPosition = new ArrayList<>();
elementPositionCopie.put(entry.getKey(),entry.getValue()); for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPosition.entrySet()){
}
for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPositionCopie.entrySet()){
for(BoardElement element : entry.getValue()){ for(BoardElement element : entry.getValue()){
if (!element.accept(new FireFinder())){ if (!element.accept(new FireFinder())){
ExtinguishFire element1 = (ExtinguishFire) element; ExtinguishFire element1 = (ExtinguishFire) element;
element1.update(this,entry.getKey()); element1.update(this,entry.getKey(),newElementPosition,extinguishPosition);
} }
} }
} }
/*for (Map.Entry<Position, ArrayList<BoardElement>> entryCopie : elementPositionCopie.entrySet()){ for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPosition.entrySet()){
for(BoardElement element : entryCopie.getValue()){ for(BoardElement element : entry.getValue()){
if (element.accept(new FireFinder())){ if (element.accept(new FireFinder())){
Flammable element1 = (Flammable) element; Flammable element1 = (Flammable) element;
element1.update(this,entryCopie.getKey()); element1.update(this,entry.getKey(),newElementPosition,extinguishPosition);
} }
} }
}*/ }
elementPosition.clear();
extinguishPosition.clear();
for (Map.Entry<Position, ArrayList<BoardElement>> entry : newElementPosition.entrySet()){
if(elementPosition.containsKey(entry.getKey())){
elementPosition.get(entry.getKey()).addAll(entry.getValue());
}else{
elementPosition.put(entry.getKey(),entry.getValue());
}
}
step++; step++;
...@@ -112,12 +118,13 @@ public class GameBoard implements Board{ ...@@ -112,12 +118,13 @@ public class GameBoard implements Board{
} }
public Position neighborClosestToFire(Position position) { public Position neighborClosestToFire(Position position) {
FireFinder fireFinder = new FireFinder(); FireFinder fireFinder = new FireFinder();
Set<Position> firePositions = new HashSet<>(); Set<Position> firePositions = new HashSet<>();
Set<Position> seen = new HashSet<>(); Set<Position> seen = new HashSet<>();
HashMap<Position, Position> firstMove = new HashMap<>(); HashMap<Position, Position> firstMove = new HashMap<>();
Queue<Position> toVisit = new LinkedList<>(neighbors(position)); Queue<Position> toVisit = new LinkedList<>(neighbors(position));
for (Map.Entry<Position, ArrayList<BoardElement>> entry : this.elementPosition.entrySet()){ for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPosition.entrySet()){
for(BoardElement element : entry.getValue()){ for(BoardElement element : entry.getValue()){
if (element.accept(fireFinder)){ if (element.accept(fireFinder)){
firePositions.add(entry.getKey()); firePositions.add(entry.getKey());
...@@ -140,14 +147,22 @@ public class GameBoard implements Board{ ...@@ -140,14 +147,22 @@ public class GameBoard implements Board{
return position; return position;
} }
public void extinguish(Position position) { /*public void extinguish(Position position) {
FireFinder fireFinder = new FireFinder(); FireFinder fireFinder = new FireFinder();
for (BoardElement boardElement : elementPosition.get(position)){ HashMap<Position, ArrayList<BoardElement>> elementPositionCopie = new HashMap<>();
if (boardElement.accept(fireFinder)){ for (Map.Entry<Position, ArrayList<BoardElement>> entry : this.elementPosition.entrySet()){
elementPositionCopie.put(entry.getKey(),entry.getValue());
}
for (BoardElement boardElement : elementPositionCopie.get(position)){
if (boardElement.accept(fireFinder) && elementPosition.get(position).size()!=0){
this.elementPosition.get(position).remove(boardElement); this.elementPosition.get(position).remove(boardElement);
break; break;
} }
if (boardElement.accept(fireFinder) && elementPosition.get(position).size()==0){
this.elementPosition.get(position).remove(boardElement);
this.elementPosition.remove(position);
} }
} }
}*/
} }
...@@ -20,8 +20,8 @@ public class FirefighterGrid extends Canvas implements Grid<model.BoardElement>{ ...@@ -20,8 +20,8 @@ public class FirefighterGrid extends Canvas implements Grid<model.BoardElement>{
@Override @Override
public void repaint(List<Pair<Position, model.BoardElement>> positionedElements) { public void repaint(List<Pair<Position, model.BoardElement>> positionedElements) {
//clear(positionedElements); clear(positionedElements);
//paint(positionedElements); paint(positionedElements);
paintLines(); paintLines();
} }
...@@ -34,7 +34,7 @@ public class FirefighterGrid extends Canvas implements Grid<model.BoardElement>{ ...@@ -34,7 +34,7 @@ public class FirefighterGrid extends Canvas implements Grid<model.BoardElement>{
private void paint(List<Pair<Position, BoardElement>> positionedElements) { private void paint(List<Pair<Position, BoardElement>> positionedElements) {
for(Pair<Position, BoardElement> pair : positionedElements){ for(Pair<Position, BoardElement> pair : positionedElements){
//paintElementAtPosition(pair.getValue(), pair.getKey()); paintElementAtPosition(pair.getValue(), pair.getKey());
} }
} }
......
...@@ -7,33 +7,33 @@ import java.util.List; ...@@ -7,33 +7,33 @@ import java.util.List;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
/*public class FirefighterBoardTest { public class FirefighterBoardTest {/*
@Test @Test
void testColumnCount(){ void testColumnCount(){
Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3); Board board = new GameBoard(20, 10, 1, 3);
assertThat(board.columnCount()).isEqualTo(20); assertThat(board.columnCount()).isEqualTo(20);
} }
@Test @Test
void testRowCount(){ void testRowCount(){
Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3); Board<List board = new GameBoard(20, 10, 1, 3);
assertThat(board.rowCount()).isEqualTo(10); assertThat(board.rowCount()).isEqualTo(10);
} }
@Test @Test
void testStepNumber(){ void testStepNumber(){
Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3); Board board = new GameBoard(20, 10, 1, 3);
for(int index = 0; index < 10; index++){ for(int index = 0; index < 10; index++){
assertThat(board.stepNumber()).isEqualTo(index); assertThat(board.stepNumber()).isEqualTo(index);
board.updateToNextGeneration(); board.updateToNextGeneration();
} }
assertThat(board.stepNumber()).isEqualTo(10); assertThat(board.stepNumber()).isEqualTo(10);
} }
@Test /*@Test
void testGetState_afterSet(){ void testGetState_afterSet(){
Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 0, 0); Board<List<BoardElement>> board = new GameBoard(20, 10, 0, 0);
Position position = new Position(1,2); Position position = new Position(1,2);
assertThat(board.getState(position)).isEmpty(); assertThat(board.getState(position)).isEmpty();
board.setState(List.of(ModelElement.FIRE), position); board.setState(List.of(BoardElement.FIRE), position);
assertThat(board.getState(position)).containsExactly(ModelElement.FIRE); assertThat(board.getState(position)).containsExactly(ModelElement.FIRE);
}
}*/ }*/
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment