Skip to content
Snippets Groups Projects
Commit de443c0c authored by 1380's avatar 1380
Browse files

Some changes

parent a0080968
No related branches found
No related tags found
No related merge requests found
Pipeline #24394 passed
...@@ -66,7 +66,7 @@ public class Controller { ...@@ -66,7 +66,7 @@ public class Controller {
model.BoardElement[][] boardElements = new model.BoardElement[rowCount][columnCount]; model.BoardElement[][] boardElements = new model.BoardElement[rowCount][columnCount];
for(int column = 0; column < columnCount; column++) for(int column = 0; column < columnCount; column++)
for(int row = 0; row < rowCount; row++){ 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); boardElements[row][column] = board.getElementPosition().get(new Position(row,column)).get(0);
}else{ }else{
boardElements[row][column]=new EmptyElement(); boardElements[row][column]=new EmptyElement();
......
...@@ -81,32 +81,40 @@ public class FireFighter implements ExtinguishFire { ...@@ -81,32 +81,40 @@ public class FireFighter implements ExtinguishFire {
} }
} }
Position newFirefighterPosition = gameBoard.neighborClosestToFire(position); Position newFirefighterPosition = gameBoard.neighborClosestToFire(position);
if(elementPositionCopie.containsKey(newFirefighterPosition)){ //elementPosition.get(position).remove(this);
elementPositionCopie.get(newFirefighterPosition).add(new FireFighter(Color.BLUE)); if(elementPosition.containsKey(newFirefighterPosition)){
elementPosition.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); elementPosition.put(newFirefighterPosition,boardElements);
} }
gameBoard.extinguish(newFirefighterPosition); gameBoard.extinguish(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); 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{ ...@@ -41,7 +41,7 @@ public class GameBoard implements Board{
@Override @Override
public void updateToNextGeneration() { public void updateToNextGeneration() {
HashMap<Position, ArrayList<BoardElement>> elementPositionCopie = new HashMap<>(); 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()); elementPositionCopie.put(entry.getKey(),entry.getValue());
} }
for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPositionCopie.entrySet()){ for (Map.Entry<Position, ArrayList<BoardElement>> entry : elementPositionCopie.entrySet()){
...@@ -112,12 +112,16 @@ public class GameBoard implements Board{ ...@@ -112,12 +112,16 @@ public class GameBoard implements Board{
} }
public Position neighborClosestToFire(Position position) { 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(); 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 : elementPositionCopie.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());
...@@ -142,11 +146,19 @@ public class GameBoard implements Board{ ...@@ -142,11 +146,19 @@ public class GameBoard implements Board{
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());
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment