You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.8 KiB

  1. package de.hsfulda.pmuw.oop;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.Random;
  7. import java.util.Set;
  8. import java.util.stream.Collectors;
  9. import java.util.stream.Stream;
  10. public class CellCreator {
  11. /** one alive to X dead */
  12. private static final int ALIVE_CELLS_RATIO = 30;
  13. private static final int BOARD_SIZE = 500;
  14. public static List<Boolean> aliveList = new Random().ints(0, ALIVE_CELLS_RATIO).limit(BOARD_SIZE * BOARD_SIZE)
  15. .mapToObj(Integer::valueOf).map(i -> Boolean.valueOf(i == 0)).collect(Collectors.toList());
  16. int calculateNeighbourIndex(List<CgolCell> allCells, CgolCell cgolCell, NeighborPosition neighborPosition) {
  17. int row = cgolCell.row;
  18. int column = cgolCell.column;
  19. int size = allCells.size();
  20. int index = neighborPosition.getIndex(row, column, BOARD_SIZE);
  21. int finalIndex = (index + size) % size;
  22. return finalIndex;
  23. }
  24. protected void createCells(Collection<CgolCell> activeCells, boolean isOptimized) {
  25. StateChangeListener<CgolCell> changeListener = getChangeListener(activeCells, isOptimized);
  26. List<CgolCell> allCells = createCells(changeListener);
  27. constructGameField(activeCells, isOptimized, allCells);
  28. System.out.println(String.format("active cells: %7s", activeCells.size()));
  29. System.out.println(String.format("alive cells : %7s", allCells.stream().filter(CgolCell::isAlive).count()));
  30. }
  31. private StateChangeListener<CgolCell> getChangeListener(Collection<CgolCell> activeCells, boolean isOptimized) {
  32. StateChangeListener<CgolCell> changeListener = isOptimized ? nc -> activeCells.add(nc) : nc -> {
  33. ;
  34. };
  35. return changeListener;
  36. }
  37. private List<CgolCell> createCells(StateChangeListener<CgolCell> changeListener) {
  38. Iterator<Boolean> iterator = aliveList.iterator();
  39. List<CgolCell> allCells = new ArrayList<>();
  40. for (int row = 0; row < BOARD_SIZE; row++)
  41. for (int col = 0; col < BOARD_SIZE; col++)
  42. allCells.add(new CgolCell(row, col, iterator.next(), changeListener));
  43. System.out.println(String.format("cells created: %7s", allCells.size()));
  44. return allCells;
  45. }
  46. private void constructGameField(Collection<CgolCell> activeCells, boolean isOptimized, List<CgolCell> allCells) {
  47. for (CgolCell cgolCell : allCells) {
  48. Set<CgolCell> neighbors = Stream.of(NeighborPosition.values())
  49. .map(np -> calculateNeighbourIndex(allCells, cgolCell, np)).map(allCells::get).collect(Collectors.toSet());
  50. cgolCell.setNeighbors(neighbors);
  51. if (cgolCell.isAlive()) {
  52. if (isOptimized) {
  53. activeCells.add(cgolCell);
  54. activeCells.addAll(neighbors);
  55. }
  56. }
  57. }
  58. if (!isOptimized) {
  59. activeCells.addAll(allCells);
  60. }
  61. }
  62. }