Skip to content

Commit de85178

Browse files
committed
visualizer
1 parent 571bf07 commit de85178

File tree

2 files changed

+107
-10
lines changed

2 files changed

+107
-10
lines changed

src/Percolation.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22
import edu.princeton.cs.algs4.QuickFindUF;
33

44
public class Percolation {
5-
private int MaxN;
6-
private int[][] Matrix;
5+
private int MaxN, P, Q;
6+
private QuickFindUF UFarray;
77

88

99
public Percolation(int n) {
10-
System.out.println("Percolation constructor " + n);
10+
System.out.println("Percolation constructor (x" + n + ")");
1111
MaxN = n;
12-
Matrix = new int[MaxN][MaxN];
12+
P = MaxN * MaxN + 1;
13+
Q = MaxN * MaxN + 2;
14+
UFarray = new QuickFindUF(MaxN * MaxN + 2);
1315

16+
System.out.println(UFarray.count());
17+
UFarray.union(5,6);
18+
UFarray.union(5,7);
19+
System.out.println(UFarray.count());
20+
21+
System.out.println(UFarray.find(5));
22+
System.out.println(UFarray.connected(5,6));
23+
System.out.println(UFarray.connected(5,8));
1424

1525
int row, col, c = 0; // TODO: c - temp
1626

@@ -26,15 +36,17 @@ public Percolation(int n) {
2636

2737
}
2838

29-
public void open(int row, int col) { Matrix[row][col] = 1; }
30-
public boolean isOpen(int row, int col) { return (boolean)(Matrix[row][col] >= 1); } // is site (row, col) open?
31-
public boolean isFull(int row, int col) { return (boolean)(Matrix[row][col] == 2); } // is site (row, col) full?
39+
public void open(int row, int col) { }
40+
41+
public boolean isOpen(int row, int col) { return true; }
42+
43+
public boolean isFull(int row, int col) { return true; }
3244

3345
public int numberOfOpenSites() {
3446
int count = 0;
3547
for (int row = 0; row < MaxN; row++) {
3648
for (int col = 0; col < MaxN; col++) {
37-
if (Matrix[row][col] >= 1) count++;
49+
// if (Matrix[row][col] >= 1) count++;
3850
}
3951
}
4052
return count;
@@ -51,7 +63,7 @@ public boolean percolates() {
5163
private void visualizeMatrix() {
5264
for (int row = 0; row < MaxN; row++) {
5365
for (int col = 0; col < MaxN; col++) {
54-
System.out.print(Matrix[row][col]);
66+
// System.out.print(Matrix[row][col]);
5567
}
5668
System.out.print("\n");
5769
}
@@ -63,7 +75,7 @@ public static void main(String[] args) {
6375
Percolation p = new Percolation(6);
6476
p.visualizeMatrix();
6577

66-
System.out.println("open sites: " + p.numberOfOpenSites());
78+
// System.out.println("open sites: " + p.numberOfOpenSites());
6779
}
6880

6981
}

src/PercolationVisualizer.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/******************************************************************************
2+
* Compilation: javac PercolationVisualizer.java
3+
* Execution: java PercolationVisualizer input.txt
4+
* Dependencies: Percolation.java
5+
*
6+
* This program takes the name of a file as a command-line argument.
7+
* From that file, it
8+
*
9+
* - Reads the grid size n of the percolation system.
10+
* - Creates an n-by-n grid of sites (intially all blocked)
11+
* - Reads in a sequence of sites (row i, column j) to open.
12+
*
13+
* After each site is opened, it draws full sites in light blue,
14+
* open sites (that aren't full) in white, and blocked sites in black,
15+
* with with site (1, 1) in the upper left-hand corner.
16+
*
17+
******************************************************************************/
18+
19+
import java.awt.Font;
20+
21+
import edu.princeton.cs.algs4.In;
22+
import edu.princeton.cs.algs4.StdDraw;
23+
24+
public class PercolationVisualizer {
25+
26+
// delay in miliseconds (controls animation speed)
27+
private static final int DELAY = 100;
28+
29+
// draw n-by-n percolation system
30+
public static void draw(Percolation perc, int n) {
31+
StdDraw.clear();
32+
StdDraw.setPenColor(StdDraw.BLACK);
33+
StdDraw.setXscale(-0.05*n, 1.05*n);
34+
StdDraw.setYscale(-0.05*n, 1.05*n); // leave a border to write text
35+
StdDraw.filledSquare(n/2.0, n/2.0, n/2.0);
36+
37+
// draw n-by-n grid
38+
int opened = 0;
39+
for (int row = 1; row <= n; row++) {
40+
for (int col = 1; col <= n; col++) {
41+
if (perc.isFull(row, col)) {
42+
StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE);
43+
opened++;
44+
}
45+
else if (perc.isOpen(row, col)) {
46+
StdDraw.setPenColor(StdDraw.WHITE);
47+
opened++;
48+
}
49+
else
50+
StdDraw.setPenColor(StdDraw.BLACK);
51+
StdDraw.filledSquare(col - 0.5, n - row + 0.5, 0.45);
52+
}
53+
}
54+
55+
// write status text
56+
StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 12));
57+
StdDraw.setPenColor(StdDraw.BLACK);
58+
StdDraw.text(0.25*n, -0.025*n, opened + " open sites");
59+
if (perc.percolates()) StdDraw.text(0.75*n, -0.025*n, "percolates");
60+
else StdDraw.text(0.75*n, -0.025*n, "does not percolate");
61+
62+
}
63+
64+
public static void main(String[] args) {
65+
In in = new In(args[0]); // input file
66+
int n = in.readInt(); // n-by-n percolation system
67+
68+
// turn on animation mode
69+
StdDraw.enableDoubleBuffering();
70+
71+
// repeatedly read in sites to open and draw resulting system
72+
Percolation perc = new Percolation(n);
73+
draw(perc, n);
74+
StdDraw.show();
75+
StdDraw.pause(DELAY);
76+
while (!in.isEmpty()) {
77+
int i = in.readInt();
78+
int j = in.readInt();
79+
perc.open(i, j);
80+
draw(perc, n);
81+
StdDraw.show();
82+
StdDraw.pause(DELAY);
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)