Last active
December 21, 2015 16:09
-
-
Save ellotheth/6331848 to your computer and use it in GitHub Desktop.
The PercolationVisualizer class from Coursera's Algorithms I course, modified for text-only output. It uses ANSI escape codes for colors and screen clearing. Laggy demo: http://www.playterm.org/r/MjAxMy0wOC90dHlyZWNvcmQtMTM3NzU1MDgyNXw4MHgyNA==
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************************************************** | |
* Compilation: javac PercolationVisualizer.java | |
* Execution: java PercolationVisualizer input.txt | |
* Dependencies: Percolation.java StdDraw.java In.java | |
* | |
* This program takes the name of a file as a command-line argument. | |
* From that file, it | |
* | |
* - Reads the grid size N of the percolation system. | |
* - Creates an N-by-N grid of sites (intially all blocked) | |
* - Reads in a sequence of sites (row i, column j) to open. | |
* | |
* After each site is opened, it draws full sites in light blue, | |
* open sites (that aren't full) in white, and blocked sites in black, | |
* with with site (1, 1) in the upper left-hand corner. | |
* | |
****************************************************************************/ | |
import java.awt.Font; | |
public class PercolationVisualizer { | |
// delay in miliseconds (controls animation speed) | |
private static final int DELAY = 100; | |
// sleep, since we're not using 'draw' | |
private static void sleep(int n) { | |
try { | |
Thread.sleep(n); | |
} catch(InterruptedException ex) {} | |
} | |
// draw N-by-N percolation system | |
public static String draw(Percolation perc, int N) { | |
//StdDraw.clear(); | |
//StdDraw.setPenColor(StdDraw.BLACK); | |
//StdDraw.setXscale(0, N); | |
//StdDraw.setYscale(0, N); | |
//StdDraw.filledSquare(N/2.0, N/2.0, N/2.0); | |
String grid = "\n"; | |
// draw N-by-N grid | |
int opened = 0; | |
String status; | |
for (int row = 1; row <= N; row++) { | |
for (int col = 1; col <= N; col++) { | |
if (perc.isFull(row, col)) { | |
//StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE); | |
status = "\u001B[34;1mF\u001B[0m"; | |
opened++; | |
} | |
else if (perc.isOpen(row, col)) { | |
//StdDraw.setPenColor(StdDraw.WHITE); | |
status = "O"; | |
opened++; | |
} | |
else { | |
//StdDraw.setPenColor(StdDraw.BLACK); | |
status = "\u001B[30;1mX\u001B[0m"; | |
} | |
//StdDraw.filledSquare(col - 0.5, N - row + 0.5, 0.45); | |
grid += "|" + status; | |
} | |
grid += "|\n"; | |
} | |
// write status text | |
//StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 12)); | |
//StdDraw.setPenColor(StdDraw.BLACK); | |
//StdDraw.text(.25*N, -N*.025, opened + " open sites"); | |
if (perc.percolates()) return grid += "percolates"; //StdDraw.text(.75*N, -N*.025, "percolates"); | |
else return grid += "does not percolate"; //StdDraw.text(.75*N, -N*.025, "does not percolate"); | |
} | |
public static void main(String[] args) { | |
In in = new In(args[0]); // input file | |
int N = in.readInt(); // N-by-N percolation system | |
// turn on animation mode | |
//StdDraw.show(0); | |
// repeatedly read in sites to open and draw resulting system | |
Percolation perc = new Percolation(N); | |
String grid = draw(perc, N); | |
//StdDraw.show(DELAY); | |
StdOut.println(grid); | |
sleep(DELAY); | |
while (!in.isEmpty()) { | |
StdOut.print(((char) 27) + "[2J"); | |
int i = in.readInt(); | |
int j = in.readInt(); | |
perc.open(i, j); | |
grid = draw(perc, N); | |
StdOut.println(grid); | |
//StdDraw.show(DELAY); | |
sleep(DELAY); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment