Link to code: PercolationDFS.java


import princeton.*;

/**
 * Simulate percolation thresholds for a grid-base system using depth-first-search,
 * aka 'flood-fill' techniques for determining if the top of a grid is connected
 * to the bottom of a grid.
 * <P>
 * Modifed from the COS 226 Princeton code for use at Duke. The modifications
 * consist of supporting the <code>IPercolate</code> interface, renaming methods
 * and fields to be more consistent with Java/Duke standards and rewriting code
 * to reflect the DFS/flood-fill techniques used in discussion at Duke.
 * <P>
 * @author Kevin Wayne, wayne@cs.princeton.edu
 * @author Owen Astrachan, ola@cs.duke.edu
 * @author Jeff Forbes, forbes@cs.duke.edu
 */


public class PercolationDFS implements IPercolate {
 
    /**
     * Initialize a grid so that no cells are open.
     * @param n is the size of the simulated (square) grid
     */
    public PercolationDFS(int n) {
        // TODO complete constructor and add necessary instance variables
     }

    /**
     * Draw the current state of the grid using the princeton.StdDraw graphics package.
     */
    public void draw() {
         // TODO complete draw
    }

    /**
     * Find a non-open site and mark it as open. 
     * 
     * The method modifies internal state so that drawing or determining
     * if percolation occurs will/could change after taking a step in
     * the simulation.
     */
    public void step() {
        // TODO complete step
    }


    /**
     * Return true iff the simulated system percolates.
     */
    public boolean percolates() {

        // TODO: run DFS to find all full sites

        // TODO: return true iff any full sites on bottom row
     
        return false;
    }

    /**
     * Private helper method to mark all cells that are open and reachable
     * from (row,col).
     * @param row is the row coordinate of the cell being checked/marked
     * @param col is the col coordinate of the cell being checked/marked
     */
    private void dfs(int row, int col) {
        // TODO: complete dfs
    }

}