001package algs15.perc;
002import stdlib.*;
003import java.awt.Font;
004
005/* **************************************************************************
006 *  Compilation:  javac PercolationVisualizer.java
007 *  Execution:    java PercolationVisualizer input.txt
008 *  Dependencies: Percolation.java StdDraw.java In.java
009 *
010 *  This program takes the name of a file as a command-line argument.
011 *  From that file, it
012 *
013 *    - Reads the grid size N of the percolation system.
014 *    - Creates an N-by-N grid of sites (intially all blocked)
015 *    - Reads in a sequence of sites (row i, column j) to open.
016 *
017 *  After each site is opened, it draws full sites in light blue,
018 *  open sites (that aren't full) in white, and blocked sites in black,
019 *  with with site (0, 0) in the upper left-hand corner.
020 *
021 ****************************************************************************/
022public class PercolationVisualizer {
023        // delay in miliseconds (controls animation speed)
024        private static int delay = 100;
025
026        // draw N-by-N percolation system
027        public static void draw(Percolation perc, int N) {
028                StdDraw.clear();
029                StdDraw.setPenColor(StdDraw.BLACK);
030                StdDraw.setXscale(0, N);
031                StdDraw.setYscale(0, N);
032                StdDraw.filledSquare(N/2.0, N/2.0, N/2.0);
033
034                // draw N-by-N grid
035                int opened = 0;
036                for (int row = 0; row < N; row++) {
037                        for (int col = 0; col < N; col++) {
038                                if (perc.isFull(row, col)) {
039                                        StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE);
040                                        opened++;
041                                }
042                                else if (perc.isOpen(row, col)) {
043                                        StdDraw.setPenColor(StdDraw.WHITE);
044                                        opened++;
045                                }
046                                else
047                                        StdDraw.setPenColor(StdDraw.BLACK);
048                                StdDraw.filledSquare(col + 0.5, N - row - 0.5, 0.45);
049                        }
050                }
051
052                // write status text
053                StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 12));
054                StdDraw.setPenColor(StdDraw.BLACK);
055                StdDraw.text(.25*N, -N*.025, opened + " open sites");
056                if (perc.percolates()) StdDraw.text(.75*N, -N*.025, "percolates");
057                else                   StdDraw.text(.75*N, -N*.025, "does not percolate");
058
059        }
060
061        // file input
062        private static void simulateFromFile(String filename) {
063                In in = new In(filename);
064                int N = in.readInt();
065                Percolation perc = new Percolation(N);
066
067                // turn on animation mode
068                StdDraw.show(0);
069
070                // repeatedly read in sites to open and draw resulting system
071                PercolationVisualizer.draw(perc, N);
072                StdDraw.show(delay);
073                while (!in.isEmpty()) {
074                        int i = in.readInt();
075                        int j = in.readInt();
076                        perc.open(i, j);
077                        PercolationVisualizer.draw(perc, N);
078                        StdDraw.show(delay);
079                        //if (i==0 && !perc.isFull (i, j)) StdOut.format("yikes: %d %d\n", i, j);
080                }
081                PercolationVisualizer.draw(perc, N);
082                StdDraw.show(delay);
083        }
084
085        // random input
086        private static void simulateFromRandom(int N) {
087                // repeatedly generate sites at random and draw resulting system
088                Percolation perc = new Percolation(N);
089
090                // turn on animation mode
091                StdDraw.show(0);
092
093                // repeatedly open sites until system percolates and draw resulting system
094                PercolationVisualizer.draw(perc, N);
095                StdDraw.show(delay);
096                while (!perc.percolates()) {
097                        int i = StdRandom.uniform(N);
098                        int j = StdRandom.uniform(N);
099                        if (!perc.isOpen(i, j)) {
100                                perc.open(i, j);
101                                PercolationVisualizer.draw(perc, N);
102                                StdDraw.show(delay);
103                                //if (i==0 && !perc.isFull (i, j)) StdOut.format("yikes: %d %d\n", i, j);
104                        }
105                }
106        }
107
108        public static void main(String[] args) {
109                //delay = 500; args = new String[] { "src/algs15/perc/perc-input2.txt" };
110                //delay = 500; args = new String[] { "src/algs15/perc/perc-input2-no.txt" };
111                //delay = 500; args = new String[] { "src/algs15/perc/perc-input3.txt" };
112                //delay = 500; args = new String[] { "src/algs15/perc/perc-input4.txt" };
113                //delay = 200; args = new String[] { "src/algs15/perc/perc-input5.txt" };
114                //delay = 100; args = new String[] { "src/algs15/perc/perc-input6.txt" };
115                //delay = 100; args = new String[] { "src/algs15/perc/perc-input7.txt" };
116                //delay = 100; args = new String[] { "src/algs15/perc/perc-input8.txt" };
117                //delay = 100; args = new String[] { "src/algs15/perc/perc-input8-no.txt" };
118                //delay = 100; args = new String[] { "src/algs15/perc/perc-input10.txt" };
119                //delay = 100; args = new String[] { "src/algs15/perc/perc-input10-no.txt" };
120                //delay = 100; args = new String[] { "src/algs15/perc/perc-input20.txt" };
121                //delay = 0;   args = new String[] { "src/algs15/perc/perc-input50.txt" };
122                //delay = 0;   args = new String[] { "src/algs15/perc/perc-greeting57.txt" };
123                //delay = 100; args = new String[] { "src/algs15/perc/perc-snake13.txt" };
124                //delay = 0;   args = new String[] { "src/algs15/perc/perc-snake101.txt" };
125                //delay = 10;  args = new String[] { "src/algs15/perc/perc-heart25.txt" };
126                delay = 100; args = new String[] { "10" };
127
128                if (args.length >= 2) throw new RuntimeException("Command line argument should be a filename or integer");
129                else if (args.length == 0) simulateFromRandom(10);
130                else {
131                        try {
132                                int N = Integer.parseInt(args[0]);
133                                simulateFromRandom(N);
134                        }
135                        catch (NumberFormatException e) {
136                                String filename = args[0];
137                                simulateFromFile(filename);
138                        }
139                }
140        }
141}