Skip to content
Snippets Groups Projects
Commit 28f97b4a authored by HEBBACHE Mohamed's avatar HEBBACHE Mohamed
Browse files

Merge remote-tracking branch 'origin/main'

parents 7f1284a7 de443c0c
No related branches found
No related tags found
No related merge requests found
......@@ -66,7 +66,7 @@ public class Controller {
model.BoardElement[][] boardElements = new model.BoardElement[rowCount][columnCount];
for(int column = 0; column < columnCount; column++)
for(int row = 0; row < rowCount; row++){
if(board.getElementPosition().containsKey(new Position(row,column))){
if(board.getElementPosition().containsKey(new Position(row,column)) && board.getElementPosition().get(new Position(row,column)).size()!=0){
boardElements[row][column] = board.getElementPosition().get(new Position(row,column)).get(0);
}else{
boardElements[row][column]=new EmptyElement();
......
......@@ -81,32 +81,40 @@ public class FireFighter implements ExtinguishFire {
}
}
Position newFirefighterPosition = gameBoard.neighborClosestToFire(position);
if(elementPositionCopie.containsKey(newFirefighterPosition)){
elementPositionCopie.get(newFirefighterPosition).add(new FireFighter(Color.BLUE));
//elementPosition.get(position).remove(this);
if(elementPosition.containsKey(newFirefighterPosition)){
elementPosition.get(newFirefighterPosition).add(this);
}
else{
ArrayList<BoardElement> boardElements = new ArrayList<>();
boardElements.add(new FireFighter(Color.BLUE));
boardElements.add(this);
elementPosition.put(newFirefighterPosition,boardElements);
}
gameBoard.extinguish(newFirefighterPosition);
List<Position> neighborFirePositions = gameBoard.neighbors(newFirefighterPosition).stream()
.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) {
gameBoard.extinguish(firePosition);
}
}
/*private List<Position> updateFirefighters() {
List<Position> result = new ArrayList<>();
List<Position> firefighterNewPositions = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions) {
Position newFirefighterPosition = neighborClosestToFire(firefighterPosition);
firefighterNewPositions.add(newFirefighterPosition);
extinguish(newFirefighterPosition);
result.add(firefighterPosition);
result.add(newFirefighterPosition);
List<Position> neighborFirePositions = neighbors(newFirefighterPosition).stream()
.filter(firePositions::contains).toList();
for(Position firePosition : neighborFirePositions)
extinguish(firePosition);
result.addAll(neighborFirePositions);
}
firefighterPositions = firefighterNewPositions;
return result;
}*/
}
......@@ -41,7 +41,7 @@ public class GameBoard implements Board{
@Override
public void updateToNextGeneration() {
HashMap<Position, ArrayList<BoardElement>> elementPositionCopie = new HashMap<>();
for (Map.Entry<Position, ArrayList<BoardElement>> entry : this.elementPosition.entrySet()){
for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPosition.entrySet()){
elementPositionCopie.put(entry.getKey(),entry.getValue());
}
for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPositionCopie.entrySet()){
......@@ -112,12 +112,16 @@ public class GameBoard implements Board{
}
public Position neighborClosestToFire(Position position) {
HashMap<Position, ArrayList<BoardElement>> elementPositionCopie = new HashMap<>();
for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPosition.entrySet()){
elementPositionCopie.put(entry.getKey(),entry.getValue());
}
FireFinder fireFinder = new FireFinder();
Set<Position> firePositions = new HashSet<>();
Set<Position> seen = new HashSet<>();
HashMap<Position, Position> firstMove = new HashMap<>();
Queue<Position> toVisit = new LinkedList<>(neighbors(position));
for (Map.Entry<Position, ArrayList<BoardElement>> entry : this.elementPosition.entrySet()){
for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPositionCopie.entrySet()){
for(BoardElement element : entry.getValue()){
if (element.accept(fireFinder)){
firePositions.add(entry.getKey());
......@@ -142,11 +146,19 @@ public class GameBoard implements Board{
public void extinguish(Position position) {
FireFinder fireFinder = new FireFinder();
for (BoardElement boardElement : elementPosition.get(position)){
if (boardElement.accept(fireFinder)){
HashMap<Position, ArrayList<BoardElement>> elementPositionCopie = new HashMap<>();
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);
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>{
@Override
public void repaint(List<Pair<Position, model.BoardElement>> positionedElements) {
//clear(positionedElements);
//paint(positionedElements);
clear(positionedElements);
paint(positionedElements);
paintLines();
}
......@@ -34,7 +34,7 @@ public class FirefighterGrid extends Canvas implements Grid<model.BoardElement>{
private void paint(List<Pair<Position, BoardElement>> positionedElements) {
for(Pair<Position, BoardElement> pair : positionedElements){
//paintElementAtPosition(pair.getValue(), pair.getKey());
paintElementAtPosition(pair.getValue(), pair.getKey());
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment