Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

- with the code that i currently have. I need to know the best way to create a connection betwee clicked squares and the top

- with the code that i currently have. I need to know the best way to create a connection betwee clicked squares and the top row so that those scuares that are clicked which are touching the top row or another connected square will also show that it is connected and become a blue square instead of a white square.. im currently working on that through the public void open(int i, int j) method i will attach all the code and classes that i currently have

import edu.princeton.cs.algs4.WeightedQuickUnionUF;

public class Percolation { public boolean [][] gridPopulate; int top; public int [][] gridNumberSystem; WeightedQuickUnionUF weightedQuickUnion; private Object[][] check; public Percolation(int N) { boolean testUnion; gridPopulate = new boolean[N][N]; gridNumberSystem = new int[N][N]; weightedQuickUnion = new WeightedQuickUnionUF((N * N)+2); int top = (N * N)+ 1; int x=1; for(int row=0; row

// create NbyN grid, with all sites blocked public void open(int i, int j) { int k =getNumberSystemValue(i, j); gridPopulate[i][j]= true; System.out.println("gridNumberSys" +gridNumberSystem[i][j]); System.out.println("this could be it " + getNumberSystemValue(i, j)); if(isOpen(i,j)) { if (weightedQuickUnion.connected(1, top)) { weightedQuickUnion.union(gridNumberSystem[i][j], top); } // if (gridNumberSystem[i][j] <100 && isopen(i-1,j) )> 0 && isOpen(row -1, col)) // { // weightedQUickUnionUF.union(getFlatgridNum(row, col), getFlatgridNum(row - 1, col)); //check going down will add - 1 // System.out.printf("Row: %d Row -1: %d%n", row, row -1); // }

} // open site (row i, column j) if it is not open already } public boolean isOpen(int i, int j) {

return gridPopulate[i][j]; } public boolean isFull(int i, int j) { int q = getNumberSystemValue(i, j); if (isOpen(i, j)) { return weightedQuickUnion.connected(getNumberSystemValue(i, j), top); } else return false; // is site (row i, column j) full? } public boolean percolates() { return false; } public String numberOfOpenSites() { // TODO Auto-generated method stub return null; } public void connectSquare(int i, int j) { if(isOpen(0,5) == true ) { weightedQuickUnion.union(15, top); } else System.out.println("hell no"); }

public int getNumberSystemValue(int i, int j) { return gridNumberSystem[i][j]; } // public int getRow(int row, int column) // { // int check [][]; // check[row][column]; // return row; // } //

}

--------------------------------------------------------------------------

/****************************************************************************** * Compilation: javac InteractivePercolationVisualizer.java * Execution: java InteractivePercolationVisualizer N * Dependencies: PercolationVisualizer.java Percolation.java * * This program takes the grid size N as a command-line argument. * Then, the user repeatedly clicks sites to open with the mouse. * 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. * ******************************************************************************/

import edu.princeton.cs.algs4.StdDraw; import edu.princeton.cs.algs4.StdOut;

public class InteractivePercolationVisualizer { private static final int DELAY = 20;

public static void main(String[] args) { // N-by-N percolation system (read from command-line, default = 10) int N = 10; if (args.length == 1) N = Integer.parseInt(args[0]);

// turn on animation mode StdDraw.show(0);

// repeatedly open site specified my mouse click and draw resulting system StdOut.println(N);

Percolation perc = new Percolation(N); PercolationVisualizer.draw(perc, N); StdDraw.show(DELAY); while (true) {

// detected mouse click if (StdDraw.mousePressed()) {

// screen coordinates double x = StdDraw.mouseX(); double y = StdDraw.mouseY();

// convert to row i, column j int i = (int) (N - Math.floor(y) - 1); int j = (int) (Math.floor(x));

// open site (i, j) provided it's in bounds if (i >= 0 && i < N && j >= 0 && j < N) { if (!perc.isOpen(i, j)) { StdOut.println(i + " " + j); } perc.open(i, j); }

// draw N-by-N percolation system PercolationVisualizer.draw(perc, N); } StdDraw.show(DELAY); } } }

-------------------------------------------------------------------------------------

/****************************************************************************** * Compilation: javac PercolationVisualizer.java * Execution: java PercolationVisualizer input.txt * Dependencies: Percolation.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 (0, 0) in the upper left-hand corner. * ******************************************************************************/

import java.awt.Font;

import edu.princeton.cs.algs4.In; import edu.princeton.cs.algs4.StdDraw;

public class PercolationVisualizer {

// delay in miliseconds (controls animation speed) private static final int DELAY = 100;

// draw N-by-N percolation system public static void draw(Percolation perc, int N) { StdDraw.clear(); StdDraw.setPenColor(StdDraw.BLACK); StdDraw.setXscale(-.05*N, 1.05*N); StdDraw.setYscale(-.05*N, 1.05*N); // leave a border to write text StdDraw.filledSquare(N/2.0, N/2.0, N/2.0);

// draw N-by-N grid for (int row = 0; row < N; row++) { for (int col = 0; col < N; col++) { if (perc.isFull(row, col)) { StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE); } else if (perc.isOpen(row, col)) { StdDraw.setPenColor(StdDraw.WHITE); } else { StdDraw.setPenColor(StdDraw.BLACK); } StdDraw.filledSquare(col + 0.5, N - row - 0.5, 0.45); } }

// write status text StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 12)); StdDraw.setPenColor(StdDraw.BLACK); StdDraw.text(.25*N, -N*.025, perc.numberOfOpenSites() + " open sites"); if (perc.percolates()) StdDraw.text(.75*N, -N*.025, "percolates"); else StdDraw.text(.75*N, -N*.025, "does not percolate");

}

private static void simulateFromFile(String filename) { In in = new In(filename); int N = in.readInt(); Percolation perc = new Percolation(N);

// turn on animation mode StdDraw.show(0);

// repeatedly read in sites to open and draw resulting system draw(perc, N); StdDraw.show(DELAY); while (!in.isEmpty()) { int i = in.readInt(); int j = in.readInt(); perc.open(i, j); draw(perc, N); StdDraw.show(DELAY); } }

public static void main(String[] args) { String filename = args[0]; simulateFromFile(filename); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

Were all members comfortable brainstorming in front of each other?

Answered: 1 week ago