Commit 516e5bf0 authored by Christopher League's avatar Christopher League
Browse files

Improve layout, require grids to be square

parent 9eb5cbe0
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="" vcs="Git" />
</component> </component>
</project> </project>
\ No newline at end of file
package edu.liu.floodgame; package edu.liu.floodgame;
public interface FloodGrid { public interface FloodGrid {
int numRows(); int edgeSize();
int numColumns(); int numCells(); // Should always be edgeSize squared
int numCells(); // Should always be NUM_ROWS*columns
int getColorAt(int row, int column); int getColorAt(int row, int column);
int getColorAt(int index); int getColorAt(int index);
......
...@@ -4,27 +4,22 @@ public class FloodGridArray2D implements FloodGrid { ...@@ -4,27 +4,22 @@ public class FloodGridArray2D implements FloodGrid {
private int[][] grid; private int[][] grid;
FloodGridArray2D(int numRows, int numColumns) { FloodGridArray2D(int edgeSize) {
if(numRows * numColumns < 3) { if(edgeSize < 2) {
throw new IllegalArgumentException("numRows*numColumns too small"); throw new IllegalArgumentException("edgeSize too small");
} }
assert numRows > 0; grid = new int[edgeSize][edgeSize];
grid = new int[numRows][numColumns];
} }
@Override @Override
public int numRows() { public int edgeSize() {
return grid.length; return grid.length;
} }
@Override
public int numColumns() {
return grid[0].length;
}
@Override @Override
public int numCells() { public int numCells() {
return numRows() * numColumns();
return grid.length * grid.length;
} }
@Override @Override
...@@ -34,7 +29,7 @@ public class FloodGridArray2D implements FloodGrid { ...@@ -34,7 +29,7 @@ public class FloodGridArray2D implements FloodGrid {
@Override @Override
public int getColorAt(int index) { public int getColorAt(int index) {
return getColorAt(index / numColumns(), index % numColumns()); return getColorAt(index / edgeSize(), index % edgeSize());
} }
@Override @Override
...@@ -44,6 +39,6 @@ public class FloodGridArray2D implements FloodGrid { ...@@ -44,6 +39,6 @@ public class FloodGridArray2D implements FloodGrid {
@Override @Override
public void setColorAt(int index, int color) { public void setColorAt(int index, int color) {
setColorAt(index / numColumns(), index % numColumns(), color); setColorAt(index / edgeSize(), index % edgeSize(), color);
} }
} }
...@@ -18,8 +18,8 @@ public class FloodGridOps { ...@@ -18,8 +18,8 @@ public class FloodGridOps {
static String toString(FloodGrid grid) { static String toString(FloodGrid grid) {
StringBuilder buf = new StringBuilder(); StringBuilder buf = new StringBuilder();
for(int row = 0; row < grid.numRows(); row++) { for(int row = 0; row < grid.edgeSize(); row++) {
for(int col = 0; col < grid.numColumns(); col++) { for(int col = 0; col < grid.edgeSize(); col++) {
buf.append(grid.getColorAt(row, col)); buf.append(grid.getColorAt(row, col));
} }
buf.append('\n'); buf.append('\n');
...@@ -59,9 +59,9 @@ public class FloodGridOps { ...@@ -59,9 +59,9 @@ public class FloodGridOps {
int color) int color)
{ {
return (row >= 0 return (row >= 0
&& row < grid.numRows() && row < grid.edgeSize()
&& column >= 0 && column >= 0
&& column < grid.numColumns() && column < grid.edgeSize()
&& !alreadyVisited[row][column] && !alreadyVisited[row][column]
&& grid.getColorAt(row, column) == color); && grid.getColorAt(row, column) == color);
} }
...@@ -95,14 +95,15 @@ public class FloodGridOps { ...@@ -95,14 +95,15 @@ public class FloodGridOps {
} }
static void flood(FloodGrid grid, int newColor) { static void flood(FloodGrid grid, int newColor) {
flood(grid, new boolean[grid.numRows()][grid.numColumns()], 0, 0, boolean[][] alreadyVisited = new boolean[grid.edgeSize()][grid.edgeSize()];
grid.getColorAt(0,0), newColor); int previousColor = grid.getColorAt(0,0);
flood(grid, alreadyVisited, 0, 0, previousColor, newColor);
} }
// Console version of the game! // Console version of the game!
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
final int numColors = 4; final int numColors = 4;
FloodGrid grid = new FloodGridArray2D(5, 7); FloodGrid grid = new FloodGridArray2D(6);
randomize(grid, new Random(), numColors); randomize(grid, new Random(), numColors);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print(toString(grid)); System.out.print(toString(grid));
......
...@@ -46,7 +46,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid { ...@@ -46,7 +46,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
final int NUM_CELLS = NUM_ROWS * NUM_COLUMNS; final int NUM_CELLS = NUM_ROWS * NUM_COLUMNS;
gridLayout.setColumnCount(NUM_COLUMNS); gridLayout.setColumnCount(NUM_COLUMNS);
for(int i = 0; i < NUM_CELLS; i++) { for(int i = 0; i < NUM_CELLS; i++) {
gridLayout.addView(new GridCellView(this, 100)); gridLayout.addView(new GridCellView(this));
} }
if(savedInstanceState == null) { if(savedInstanceState == null) {
System.out.println("New random grid"); System.out.println("New random grid");
...@@ -64,12 +64,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid { ...@@ -64,12 +64,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
} }
@Override @Override
public int numRows() { public int edgeSize() {
return numCells() / numColumns();
}
@Override
public int numColumns() {
return gridLayout.getColumnCount(); return gridLayout.getColumnCount();
} }
...@@ -80,7 +75,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid { ...@@ -80,7 +75,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
@Override @Override
public int getColorAt(int row, int column) { public int getColorAt(int row, int column) {
return getColorAt(row * numColumns() + column); return getColorAt(row * edgeSize() + column);
} }
@Override @Override
...@@ -91,7 +86,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid { ...@@ -91,7 +86,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
@Override @Override
public void setColorAt(int row, int column, int color) { public void setColorAt(int row, int column, int color) {
setColorAt(row * numColumns() + column, color); setColorAt(row * edgeSize() + column, color);
} }
@Override @Override
......
package edu.liu.floodgame; package edu.liu.floodgame;
import android.content.Context;
import android.support.v7.widget.GridLayout; import android.support.v7.widget.GridLayout;
import android.view.View; import android.view.View;
...@@ -8,10 +7,11 @@ public class GridCellView extends View { ...@@ -8,10 +7,11 @@ public class GridCellView extends View {
int color; int color;
GridCellView(GridActivity activity, int size) { GridCellView(GridActivity activity) {
super(activity); super(activity);
GridLayout.LayoutParams lp = new GridLayout.LayoutParams(); GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
lp.width = lp.height = size; lp.rowSpec = lp.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f);
lp.width = lp.height = 0;
setLayoutParams(lp); setLayoutParams(lp);
} }
......
...@@ -7,37 +7,49 @@ ...@@ -7,37 +7,49 @@
<android.support.v7.widget.GridLayout <android.support.v7.widget.GridLayout
android:id="@+id/grid" android:id="@+id/grid"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="0dp"
android:padding="10dp"
app:columnCount="2" app:columnCount="2"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<View <View
android:id="@+id/sample1" android:id="@+id/sample1"
android:layout_width="50dp" android:layout_width="0dp"
android:layout_height="50dp" android:layout_height="0dp"
android:background="@color/colorPrimary" /> android:background="@color/colorPrimary"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<View <View
android:id="@+id/sample2" android:id="@+id/sample2"
android:layout_width="50dp" android:layout_width="0dp"
android:layout_height="50dp" android:layout_height="0dp"
android:background="@color/colorAccent" /> android:background="@color/colorAccent"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<View <View
android:id="@+id/sample3" android:id="@+id/sample3"
android:layout_width="50dp" android:layout_width="0dp"
android:layout_height="50dp" android:layout_height="0dp"
android:background="@color/colorPrimaryDark" /> android:background="@color/colorPrimaryDark"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<View <View
android:id="@+id/sample4" android:id="@+id/sample4"
android:layout_width="50dp" android:layout_width="0dp"
android:layout_height="50dp" android:layout_height="0dp"
android:background="@color/colorPrimary" /> android:background="@color/colorPrimary"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
</android.support.v7.widget.GridLayout> </android.support.v7.widget.GridLayout>
<LinearLayout <LinearLayout
......
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