@Override
  public void init(GameContainer gameContainer, StateBasedGame stateBasedGame)
      throws SlickException {
    gameContainer.getGraphics().setBackground(Color.white);
    gameContainer.getGraphics().setColor(Color.black);
    gameContainer.setShowFPS(false);
    gameContainer.setAlwaysRender(true);

    Level.init(CELLS, CELLS);

    for (int i = 0; i < cells.length; i++) {
      for (int j = 0; j < cells.length; j++) {
        cells[i][j] = new Cell(i, j, ratio, false);
      }
    }
    this.logger = new Logger();
    this.logger.createNewLogFile();
  }
  @Override
  public void update(GameContainer gameContainer, StateBasedGame stateBasedGame, int delta)
      throws SlickException {

    Input in = gameContainer.getInput();

    if (in.isKeyPressed(Input.KEY_C)) {
      clear();
    }

    gameContainer.setMaximumLogicUpdateInterval(updateInterval);

    if (in.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
      float x1 = in.getMouseX() / ratio;
      float y1 = in.getMouseY() / ratio;
      if (start == null) {
        start = new Cell((int) x1, (int) y1, ratio, false);
        start.start = true;
        cells[start.gridPosX][start.gridPosY].start = true;
        cellStack.add(cells[start.gridPosX][start.gridPosY]);
      } else if (goal == null) {
        goal = new Cell((int) x1, (int) y1, ratio, false);
        cells[goal.gridPosX][goal.gridPosY].goal = true;
      } else {
        changeCellAtPosition((int) x1, (int) y1);
      }

      if (start != null && goal != null) {
        this.wall = cells[(int) x1][(int) y1].wall;
      }
    }

    if (in.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
      if (start != null && goal != null) {
        mousePressing = true;
        float x1 = in.getMouseX() / ratio;
        float y1 = in.getMouseY() / ratio;
        cells[(int) x1][(int) y1].wall = this.wall;
      }
    } else {
      mousePressing = false;
    }

    if (in.isKeyPressed(Input.KEY_ENTER)) {
      if (mazeSolved && displayingMaze) {
        clear();
        createGraph();
        maze = true;
      } else if (!maze) {
        this.search = true;
      }
    }

    if (in.isKeyPressed(Input.KEY_M) && !maze) {
      createGraph();
      maze = true;
      this.logger.initWriter();
    }

    if (in.isKeyPressed(Input.KEY_S)) {
      clear();
      for (int i = 0; i < cells.length; i++) {
        for (int j = 0; j < cells.length; j++) {
          Random random = new Random();
          if (random.nextInt(3) == 0) {
            cells[i][j].wall = true;
          }
        }
      }
      setDefaultStartAndEnd(true);
    }

    if (in.isKeyPressed(Input.KEY_ADD)) {
      if (updateInterval > 1) {
        updateInterval -= 1;
      }
    }
    if (in.isKeyPressed(Input.KEY_SUBTRACT)) {
      if (updateInterval < 20) {
        updateInterval += 1;
      }
    }

    if (in.isKeyPressed(Input.KEY_P)) {
      if (!gameContainer.isPaused()) {
        gameContainer.pause();
      } else {
        gameContainer.resume();
      }
    }

    if (in.isKeyPressed(Input.KEY_D)) {
      this.diagonal = !this.diagonal;
    }

    if (in.isKeyPressed(Input.KEY_ESCAPE)) {
      stateBasedGame.enterState(1);
    }

    if (in.isKeyPressed(Input.KEY_A)) {
      AnalyzeLog.analyzeBacktracks(AnalyzeLog.FIRST);
    }

    if (in.isKeyPressed(Input.KEY_I)) {
      this.createImage();
    }

    if (this.search) {
      if (start == null || goal == null) {
        this.search = false;
      } else {
        if (ButtonStates.DFSState) {
          if (solvingVisited.size() < 1) {
            solvingVisited.add(cells[start.gridPosX][start.gridPosY]);
            cells[start.gridPosX][start.gridPosY].solvingVisited = true;
            currentDFSCell = cells[start.gridPosX][start.gridPosY];
          } else {
            for (int x = 0; x < CELLS; x++) {
              for (int y = 0; y < CELLS; y++) {
                cells[x][y].dfsCheckNeighbors();
              }
            }
            if (currentDFSCell.goal) {
              this.search = false;
              this.drawWay = true;
              this.end = currentDFSCell;
              getPath(false, null);
              if (displayingMaze) {
                mazeSolved = true;
              }
              foundSolution = true;
            } else {
              if (solvingVisited.size() != (CELLS * CELLS)) {
                ArrayList<Cell> currentNeighbors = new ArrayList<>();
                if (currentDFSCell.dfsNorth != null
                    && !currentDFSCell.dfsNorth.solvingVisited
                    && !currentDFSCell.dfsNorth.wall) {
                  currentNeighbors.add(currentDFSCell.dfsNorth);
                } else if (currentDFSCell.dfsEast != null
                    && !currentDFSCell.dfsEast.solvingVisited
                    && !currentDFSCell.dfsEast.wall) {
                  currentNeighbors.add(currentDFSCell.dfsEast);
                } else if (currentDFSCell.dfsSouth != null
                    && !currentDFSCell.dfsSouth.solvingVisited
                    && !currentDFSCell.dfsSouth.wall) {
                  currentNeighbors.add(currentDFSCell.dfsSouth);
                } else if (currentDFSCell.dfsWest != null
                    && !currentDFSCell.dfsWest.solvingVisited
                    && !currentDFSCell.dfsWest.wall) {
                  currentNeighbors.add(currentDFSCell.dfsWest);
                } else {;
                }
                if (currentNeighbors.size() > 0) {
                  Cell neighbor = currentNeighbors.get(0);
                  neighbor.solvingPrevious = currentDFSCell;
                  neighbor.solvingVisited = true;
                  currentDFSCell = neighbor;
                } else {
                  dfsBacktracked.add(currentDFSCell);
                  dfsBacktracked.add(currentDFSCell.solvingPrevious);
                  currentDFSCell = currentDFSCell.solvingPrevious;
                }
              }
            }
          }
        } else if (ButtonStates.AStarState) {

        } else if (ButtonStates.BFSState) {
          if (solvingVisited.size() < 1) {
            solvingVisited.add(cells[start.gridPosX][start.gridPosY]);
            cells[start.gridPosX][start.gridPosY].solvingVisited = true;
            cellStack.add(cells[start.gridPosX][start.gridPosY]);
          } else {
            if (!cellStack.isEmpty()) {
              Cell current = (Cell) cellStack.remove();
              Cell child;
              if (current.goal) {
                this.end = current;
                this.drawWay = true;
                this.search = false;
                getPath(false, null);
                if (displayingMaze) {
                  mazeSolved = true;
                }
                foundSolution = true;
              } else {
                while ((child = getUnvisitedChildCell(current)) != null) {
                  child.solvingVisited = true;
                  child.solvingPrevious = current;
                  solvingVisited.add(child);
                  cellStack.add(child);
                  current.solvingCheckNeighbors(diagonal);
                  getPath(true, child);
                }
              }
            }
          }
        } else {
          try {
            throw new NoSolvingAlgorithmSelectedException();
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
    }
    if (this.maze) {
      if (this.logger.used) {
        this.logger.createNewLogFile();
        this.logger.initWriter();
        this.logger.used = false;
      }
      if (cells.length != 0) {
        logger.writeInLog("Checking neighbors");
        for (int i = 0; i < CELLS; i++) {
          for (int j = 0; j < CELLS; j++) {
            cells[i][j].genCheckNeighbors();
          }
        }

        if (genVisited.size() < 1) {
          genVisited.add(cells[1][1]);
          currentGenCell = cells[1][1];
          currentGenCell.genVisited = true;
          logger.writeInLog("Adding Cell X1 and Y1 to visited Cells");
        }
        if (genVisited.size() != totalCells) {
          ArrayList<Cell> currentNeighbors = new ArrayList<>();
          if (currentGenCell.north != null
              && !currentGenCell.north.genVisited
              && currentGenCell.north.allWallsIntact) {
            currentNeighbors.add(currentGenCell.north);
            logger.writeInLog(
                "Adding north cell to neighbors at: "
                    + currentGenCell.north.gridPosX
                    + "; "
                    + currentGenCell.north.gridPosY);
          }
          if (currentGenCell.east != null
              && !currentGenCell.east.genVisited
              && currentGenCell.east.allWallsIntact) {
            currentNeighbors.add(currentGenCell.east);
            logger.writeInLog(
                "Adding east cell to neighbors at: "
                    + currentGenCell.east.gridPosX
                    + "; "
                    + currentGenCell.east.gridPosY);
          }
          if (currentGenCell.south != null
              && !currentGenCell.south.genVisited
              && currentGenCell.south.allWallsIntact) {
            currentNeighbors.add(currentGenCell.south);
            logger.writeInLog(
                "Adding south cell to neighbors at: "
                    + currentGenCell.south.gridPosX
                    + "; "
                    + currentGenCell.south.gridPosY);
          }
          if (currentGenCell.west != null
              && !currentGenCell.west.genVisited
              && currentGenCell.west.allWallsIntact) {
            currentNeighbors.add(currentGenCell.west);
            logger.writeInLog(
                "Adding west cell to neighbors at: "
                    + currentGenCell.west.gridPosX
                    + "; "
                    + currentGenCell.west.gridPosY);
          }

          if (currentNeighbors.size() > 0) {
            Random random = new Random();
            int randomValue = random.nextInt(currentNeighbors.size());
            logger.writeInLog("Generated random number: " + randomValue);
            Cell neighbor = currentNeighbors.get(randomValue);
            logger.writeInLog("Picked neighbor: " + neighbor.gridPosX + "; " + neighbor.gridPosY);
            neighbor.genVisited = true;

            int wallX = 0;
            int wallY = 0;

            // break wall
            if (neighbor.gridPosX > currentGenCell.gridPosX) {
              wallX = currentGenCell.gridPosX + 1;
            } else if (neighbor.gridPosX < currentGenCell.gridPosX) {
              wallX = currentGenCell.gridPosX - 1;
            } else {
              wallX = currentGenCell.gridPosX;
            }

            if (neighbor.gridPosY > currentGenCell.gridPosY) {
              wallY = currentGenCell.gridPosY + 1;
            } else if (neighbor.gridPosY < currentGenCell.gridPosY) {
              wallY = currentGenCell.gridPosY - 1;
            } else {
              wallY = currentGenCell.gridPosY;
            }

            cells[wallX][wallY].wall = false;
            cells[wallX][wallY].genVisited = true;
            logger.writeInLog("Breaking wall at: " + wallX + "; " + wallY);
            genVisited.add(cells[wallX][wallY]);
            neighbor.genBacktrack = currentGenCell;
            logger.writeInLog(
                "Set current cell to backtrack cell at: "
                    + currentGenCell.gridPosX
                    + "; "
                    + currentGenCell.gridPosY);
            currentGenCell = neighbor;

          } else {
            currentGenCell = currentGenCell.genBacktrack;
            logger.writeInLog(
                "Backtracking at: "
                    + currentGenCell.genBacktrack.gridPosX
                    + "; "
                    + currentGenCell.genBacktrack.gridPosY);
          }
        } else {
          currentGenCell = null;
          this.maze = false;
          setDefaultStartAndEnd(true);
          displayingMaze = true;
          if ((this.search = !this.search) == true) {
            if (this.startSearch == false) {
              this.search = false;
              createImage();
            }
          }
          logger.writeInLog("Done Maze, closing logger.");
          logger.closeWriter();
          if (this.automaticallyGenerateMazes && (generatedMazes < mazesToGenerate)) {
            generatedMazes += 1;
            clear();
            createGraph();
            this.maze = true;
          } else if (this.logger.stepList.size() != 0) {
            this.logger.printStepList();
          } else {;
          }
        }
      }
    }
    char[][] copyOfLevel = new char[CELLS][CELLS];
    for (int x = 0; x < cells.length; x++) {
      for (int y = 0; y < cells.length; y++) {
        if (cells[x][y].wall) {
          copyOfLevel[x][y] = 'W';
        } else if (!cells[x][y].wall) {
          copyOfLevel[x][y] = 'D';
        } else if (cells[x][y].start) {
          copyOfLevel[x][y] = 'S';
        } else if (cells[x][y].goal) {
          copyOfLevel[x][y] = 'G';
        } else {
          copyOfLevel[x][y] = 'D';
        }
      }
    }
    Level.update(copyOfLevel);
  }