Commit ded8ee31 authored by Avery Johnson's avatar Avery Johnson
Browse files

check

parent 93806421
......@@ -19,20 +19,83 @@ public class FloodGame {
static String toString(FloodGrid grid) {
StringBuilder buf = new StringBuilder();
for (int r = 0; r < grid.edgeSize(); r++) {
for (int c = 0; c < grid.edgeSize(); c++)
for (int c = 0; c < grid.edgeSize(); c++) {
buf.append(grid.getColorAt(r,c));
buf.append(grid.getColorAt(r, c));
}
buf.append('\n');
}
return buf.toString();
return buf.toString();
}
// TODO
static boolean gameOver(FloodGrid grid) {
return false; // placeholder
// Game is over if all elements are same color as origin (0,0)
int c = grid.getColorAt(0, 0);
for(int i = 1; i < grid.numCells(); i++) {
if(grid.getColorAt(i) != c) {
return false;
}
}
return true;
}
// TODO
static void flood(FloodGrid grid, int newColor) {
boolean[][] visited = new boolean[grid.edgeSize()][grid.edgeSize()];
int prevColor = grid.getColorAt(0,0);
flood(grid, visited, 0, 0, prevColor, newColor);
}
/* The first helper for the flood algorithm floods FROM a particular row and
* column, changing any contiguous cells of prevColor into newColor. It
* must consider all four directions, but only proceed if okayToVisit.
*/
private static void flood(
FloodGrid grid,
boolean[][] visited,
int row,
int column,
int prevColor,
int newColor)
{
visited[row][column] = true;
grid.setColorAt(row, column, newColor);
// Down
if(okayToVisit(grid, visited, row+1, column, prevColor)) {
flood(grid, visited, row+1, column, prevColor, newColor);
}
// Right
if(okayToVisit(grid, visited, row, column+1, prevColor)) {
flood(grid, visited, row, column+1, prevColor, newColor);
}
// Up
if(okayToVisit(grid, visited, row-1, column, prevColor)) {
flood(grid, visited, row-1, column, prevColor, newColor);
}
// Left
if(okayToVisit(grid, visited, row, column-1, prevColor)) {
flood(grid, visited, row, column-1, prevColor, newColor);
}
}
/* The last helper for the flood algorithm just determines if it's okay to
* visit a particular coordinate -- it's in-bounds, hasn't been visited yet,
* and matches the existing color.
*/
private static boolean okayToVisit(
FloodGrid grid,
boolean[][] visited,
int row,
int column,
int color)
{
return (row >= 0
&& row < grid.edgeSize()
&& column >= 0
&& column < grid.edgeSize()
&& !visited[row][column]
&& grid.getColorAt(row, column) == color);
}
public static void main(String[] args) throws IOException {
......@@ -59,4 +122,4 @@ public class FloodGame {
}
System.out.println("Puzzle solved.");
}
}
}
\ No newline at end of file
......@@ -7,7 +7,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.android.tools.build:gradle:3.4.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
......
#Tue Jul 09 16:45:17 GMT 2019
#Tue Jul 16 10:05:19 EDT 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment