-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPercolation.java
More file actions
179 lines (150 loc) · 4.72 KB
/
Percolation.java
File metadata and controls
179 lines (150 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
*
* Percolation
*
* @author Ana Paula Centeno
* @author Haolin (Daniel) Jin
*/
public class Percolation {
private boolean[][] grid; // gridSize by gridSize grid of sites;
// true = open site, false = closed or blocked site
private WeightedQuickUnionFind wquFind; //
private int gridSize; // gridSize by gridSize is the size of the grid/system
private int gridSquared;
private int virtualTop; // virtual top index on WeightedQuckUnionFind arrays
private int virtualBottom; // virtual bottom index on WeightedQuckUnionFind arrays
/**
* Constructor.
* Initializes all instance variables
*/
public Percolation ( int n ){
gridSize = n;
gridSquared = gridSize * gridSize;
wquFind = new WeightedQuickUnionFind(gridSquared + 2);
grid = new boolean[gridSize][gridSize]; // every site is initialized to closed/blocked
virtualTop = gridSquared;
virtualBottom = gridSquared + 1;
}
/**
* Getter method for GridSize
* @return integer representing the size of the grid.
*/
public int getGridSize () {
return gridSize;
}
/**
* Returns the grid array
* @return grid array
*/
public boolean[][] getGridArray () {
return grid;
}
/**
* Open the site at postion (x,y) on the grid to true and add an edge
* to any open neighbor (left, right, top, bottom) and/or top/bottom virtual sites
* Note: diagonal sites are not neighbors
*
* @param row grid row
* @param col grid column
* @return void
*/
public void openSite (int row, int col) {
// WRITE YOUR CODE HERE
grid[row][col] = true;
if (row != 0 && grid[row-1][col] == true) {
wquFind.union(gridSize*(row-1)+(col) , gridSize*(row)+(col));
}
if (row != gridSize-1 && grid[row+1][col] == true) {
wquFind.union(gridSize*(row+1)+(col) , gridSize*(row)+(col));
}
if(col != 0 && grid[row][col-1] == true) {
wquFind.union(gridSize*(row)+(col-1) , gridSize*(row)+(col));
}
if(col != gridSize-1 && grid[row][col+1] == true) {
wquFind.union(gridSize*(row)+(col+1) , gridSize*(row)+(col));
}
if (row == 0) {
wquFind.union(gridSize*gridSize, col);
}
if (row == gridSize-1) {
wquFind.union(gridSize*gridSize+1, gridSize*(gridSize-1)+col);
}
return;
}
/**
* Check if the system percolates (any top and bottom sites are connected by open sites)
* @return true if system percolates, false otherwise
*/
public boolean percolationCheck () {
// WRITE YOUR CODE HERE
if (wquFind.find(gridSize*gridSize) == wquFind.find(gridSize*gridSize+1)) {
return true;
}
else {
return false;
}
}
/**
* Iterates over the grid array openning every site.
* Starts at [0][0] and moves row wise
* @param probability
* @param seed
*/
public void openAllSites (double probability, long seed) {
// Setting the same seed before generating random numbers ensure that
// the same numbers are generated between different runs
StdRandom.setSeed(seed); // DO NOT remove this line
// WRITE YOUR CODE HERE, DO NOT remove the line above
double b;
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
b = StdRandom.uniform();
if (b <= probability) {
openSite(i,j);
}
}
}
}
/**
* Open up a new window and display the current grid using StdDraw library.
* The output will be colored based on the grid array. Blue for open site, black for closed site.
* @return: void
*/
public void displayGrid () {
double blockSize = 0.9 / gridSize;
double zeroPt = 0.05+(blockSize/2), x = zeroPt, y = zeroPt;
for ( int i = gridSize-1; i >= 0; i-- ) {
x = zeroPt;
for ( int j = 0; j < gridSize; j++) {
if ( grid[i][j] ) {
StdDraw.setPenColor( StdDraw.BOOK_LIGHT_BLUE );
StdDraw.filledSquare( x, y ,blockSize/2);
StdDraw.setPenColor( StdDraw.BLACK);
StdDraw.square( x, y ,blockSize/2);
} else {
StdDraw.filledSquare( x, y ,blockSize/2);
}
x += blockSize;
}
y += blockSize;
}
}
/**
* Main method, for testing only, feel free to change it.
*/
public static void main ( String[] args ) {
double p = 0.47;
Percolation pl = new Percolation(5);
/*
* Setting a seed before generating random numbers ensure that
* the same numbers are generated between runs.
*
* If you would like to reproduce Autolab's output, update
* the seed variable to the value Autolab has used.
*/
long seed = System.currentTimeMillis();
pl.openAllSites(p, seed);
System.out.println("The system percolates: " + pl.percolationCheck());
pl.displayGrid();
}
}