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