public void drawCells(Graphics g, Cells cells) {
    Cell[] c = cells.getCells();

    for (int i = 0; i < cells.getFilled(); i++) {
      drawCircle(g, c[i].posX, c[i].posY, c[i].radian, cells.getColor());
    }
  }
Example #2
0
  /**
   * Sets the seencells bit for all cells of a segment
   *
   * @param seg
   */
  private void udpateSeenCellsForSegment(Seg seg) {
    seg.seen = true; // updates the segment

    int sdx = seg.dx / map_unit; // constant, only set once here
    int sdy = seg.dy / map_unit; // constant, only set once here

    // get initial position right on loop variables sx, sy
    int sx = seg.x / map_unit;
    if (sdx < 0) sx--;
    int sy = seg.y / map_unit;
    if (sdy < 0) sy--;

    // define constants to avoid method calls in following loop
    int sdsx = MazeBuilder.getSign(sdx);
    int sdsy = MazeBuilder.getSign(sdy);
    int bit = (sdx != 0) ? Cells.CW_TOP : Cells.CW_LEFT;
    int len = Math.abs(sdx + sdy);
    // true loop variables are (sx,sy),
    for (int i = 0; i != len; i++) {
      // seencells[sx][sy] |= bit;
      seencells.setBitToOne(sx, sy, bit); // updates the cell
      sx += sdsx;
      sy += sdsy;
    }
  }
  /**
   * Call back method for MazeBuilder to communicate newly generated maze as reaction to a call to
   * build()
   *
   * @param root node for traversals, used for the first person perspective
   * @param cells encodes the maze with its walls and border
   * @param dists encodes the solution by providing distances to the exit for each position in the
   *     maze
   * @param startx current position, x coordinate
   * @param starty current position, y coordinate
   */
  public void newMaze(BSPNode root, Cells c, Distance dists, int startx, int starty) {
    if (Cells
        .deepdebugWall) { // for debugging: dump the sequence of all deleted walls to a log file
      // This reveals how the maze was generated
      c.saveLogFile(Cells.deepedebugWallFileName);
    }
    // adjust internal state of maze model
    showMaze = showSolution = solving = false;
    mazecells = c;
    mazedists = dists;
    seencells = new Cells(mazew + 1, mazeh + 1);
    rootnode = root;
    setCurrentDirection(1, 0);
    setCurrentPosition(startx, starty);
    walkStep = 0;
    viewdx = dx << 16;
    viewdy = dy << 16;
    angle = 0;
    mapMode = false;

    cleanViews();

    // Set up the robot and the robot driver.
    setUpRobotDriver();

    // register views for the new maze
    // mazew and mazeh have been set in build() method before mazebuider was called to generate a
    // new maze.
    // reset map_scale in mapdrawer to a value of 18 ///// WAS 10////
    addView(
        new FirstPersonDrawer(
            Constants.VIEW_WIDTH,
            Constants.VIEW_HEIGHT,
            Constants.MAP_UNIT,
            Constants.STEP_SIZE,
            mazecells,
            seencells,
            18,
            mazedists.getDists(),
            mazew,
            mazeh,
            root,
            this));
    // order of registration matters, code executed in order of appearance!
    addView(
        new MapDrawer(
            Constants.VIEW_WIDTH,
            Constants.VIEW_HEIGHT,
            Constants.MAP_UNIT,
            Constants.STEP_SIZE,
            mazecells,
            seencells,
            18,
            mazedists.getDists(),
            mazew,
            mazeh,
            this));
  }
 /**
  * Helper method for walk()
  *
  * @param dir
  * @return true if there is no wall in this direction
  */
 private boolean checkMove(int dir) {
   // obtain appropriate index for direction (CW_BOT, CW_TOP ...)
   // for given direction parameter
   int a = angle / 90;
   if (dir == -1) a = (a + 2) & 3;
   // check if cell has walls in this direction
   // returns true if there are no walls in this direction
   return mazecells.hasMaskedBitsFalse(px, py, Constants.MASKS[a]);
 }
Example #5
0
  public static String toMarkup(Example example, boolean prettyPrint, MarkupPrinter printer) {
    StringBuilder sb = new StringBuilder();
    if (example.hasChild()) sb.append(Cells.toMarkup(example.firstChild(), prettyPrint, printer));
    else sb.append(EMPTY);

    if (example.hasSibling()) {
      sb.append("\n");
      sb.append(toMarkup(example.nextSibling(), prettyPrint, printer));
    }
    return sb.toString();
  }
Example #6
0
  public static Rows parse(String markup) {
    Tables table = new Tables();

    String[] rows = markup.split("\\n");

    for (String cells : rows) {
      if (StringUtil.isBlank(cells)) continue;
      Rows row = (Rows) table.addChild();
      if (!isRowEmpty(cells)) row.addChild(Cells.parse(cells));
    }

    return (Rows) table.firstChild();
  }
 private static long countLiveNeighbors(final Cell cell, final Cells cells) {
   return cell.provideNeighbors().stream().filter((Cell c) -> cells.isAlive(c)).count();
 }
Example #8
0
  @Override
  public Cells nextGen(Cells cells) {
    final int row = cells.getRow();
    final int col = cells.getCol();

    for (int r = 0; r < row; r++) {
      for (int c = 0; c < col; c++) {
        Cell cell = cells.getCell(r, c);
        // Determine alive neighbour size.

        int aliveNeighbourSize = 0;

        // TOP ROW
        int _r = r - 1;
        _r = _r >= 0 ? _r : row - 1;

        int _c = c - 1;
        _c = _c >= 0 ? _c : col - 1;
        if (cells.getCell(_r, _c).isAlive()) {
          aliveNeighbourSize++;
        }

        _c = c;
        if (cells.getCell(_r, _c).isAlive()) {
          aliveNeighbourSize++;
        }

        _c = c + 1;
        _c = _c < col ? _c : 0;
        if (cells.getCell(_r, _c).isAlive()) {
          aliveNeighbourSize++;
        }

        // SAME ROW
        _r = r;

        _c = c - 1;
        _c = _c >= 0 ? _c : col - 1;
        if (cells.getCell(_r, _c).isAlive()) {
          aliveNeighbourSize++;
        }

        _c = c + 1;
        _c = _c < col ? _c : 0;
        if (cells.getCell(_r, _c).isAlive()) {
          aliveNeighbourSize++;
        }

        // BOTTOM ROW
        _r = r + 1;
        _r = _r < row ? _r : 0;

        _c = c - 1;
        _c = _c >= 0 ? _c : col - 1;
        if (cells.getCell(_r, _c).isAlive()) {
          aliveNeighbourSize++;
        }

        _c = c;
        if (cells.getCell(_r, _c).isAlive()) {
          aliveNeighbourSize++;
        }

        _c = c + 1;
        _c = _c < col ? _c : 0;
        if (cells.getCell(_r, _c).isAlive()) {
          aliveNeighbourSize++;
        }

        cell.setAliveNeighbourSize(aliveNeighbourSize);
      }
    }

    // Determine dead or alive.
    Cells result = new Cells(cells.getRow(), cells.getCol());
    for (int r = 0; r < row; r++) {
      for (int c = 0; c < col; c++) {
        final Cell cell = cells.getCell(r, c);
        final int aliveNeighbourSize = cell.getAliveNeighbourSize();
        final boolean alive = cell.isAlive();

        final boolean shouldAlive;
        if (alive) {
          if (aliveNeighbourSize < 2) {
            // Any live cell with fewer than two live neighbours dies, as if caused by
            // under-population.
            shouldAlive = false;
          } else if (aliveNeighbourSize == 2 || aliveNeighbourSize == 3) {
            // Any live cell with two or three live neighbours lives on to the next generation.
            shouldAlive = true;
          } else {
            // Any live cell with more than three live neighbours dies, as if by over-population.
            shouldAlive = false;
          }
        } else {
          if (aliveNeighbourSize == 3) {
            // Any dead cell with exactly three live neighbours becomes a live cell, as if by
            // reproduction.
            shouldAlive = true;
          } else {
            shouldAlive = false;
          }
        }
        result.getCell(r, c).setAlive(shouldAlive);
      }
    }

    return result;
  }