Commit 15c50471 authored by Christopher League's avatar Christopher League
Browse files

Glyphs for connected colors working well!

parent e5325a70
...@@ -69,6 +69,8 @@ public class GridActivity extends AppCompatActivity implements FloodGrid { ...@@ -69,6 +69,8 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
gameOver = FloodGridOps.gameOver(this); gameOver = FloodGridOps.gameOver(this);
setNumMoves(savedInstanceState.getInt(NUM_MOVES_KEY)); setNumMoves(savedInstanceState.getInt(NUM_MOVES_KEY));
} }
recordNeighborsThroughout();
} }
@Override @Override
...@@ -93,6 +95,49 @@ public class GridActivity extends AppCompatActivity implements FloodGrid { ...@@ -93,6 +95,49 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
return getColorAt(row * edgeSize() + column); return getColorAt(row * edgeSize() + column);
} }
// Uses -1 for off-the-board
public int safeGetColorAt(int row, int column) {
if(row < 0
|| row >= edgeSize()
|| column < 0
|| column >= edgeSize())
return -1;
return getColorAt(row, column);
}
void recordNeighborsThroughout() {
for(int r = 0; r < edgeSize(); r++) {
for(int c = 0; c < edgeSize(); c++) {
recordNeighborsAt(r, c);
}
}
}
public void recordNeighborsAt(int row, int column) {
int index = row * edgeSize() + column;
GridCellView cell = (GridCellView) gridLayout.getChildAt(index);
int color = cell.color;
boolean west = color == safeGetColorAt(row, column-1);
boolean northwest = color == safeGetColorAt(row-1, column-1);
boolean north = color == safeGetColorAt(row-1, column);
boolean northeast = color == safeGetColorAt(row-1, column+1);
boolean east = color == safeGetColorAt(row, column+1);
boolean southeast = color == safeGetColorAt(row+1, column+1);
boolean south = color == safeGetColorAt(row+1, column);
boolean southwest = color == safeGetColorAt(row+1, column-1);
cell.setNeighbors(
// NW
GridCellView.corner(west, northwest, north),
// NE
GridCellView.corner(east, northeast, north),
// SE
GridCellView.corner(east, southeast, south),
// SW
GridCellView.corner(west, southwest, south));
}
@Override @Override
public int getColorAt(int index) { public int getColorAt(int index) {
GridCellView cell = (GridCellView) gridLayout.getChildAt(index); GridCellView cell = (GridCellView) gridLayout.getChildAt(index);
...@@ -138,5 +183,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid { ...@@ -138,5 +183,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
} }
setNumMoves(numMoves + 1); setNumMoves(numMoves + 1);
} }
recordNeighborsThroughout();
} }
} }
package edu.liu.floodgame; package edu.liu.floodgame;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v7.widget.GridLayout; import android.support.v7.widget.GridLayout;
import android.view.View; import android.view.View;
...@@ -17,7 +19,108 @@ public class GridCellView extends View { ...@@ -17,7 +19,108 @@ public class GridCellView extends View {
void setColor(int color) { void setColor(int color) {
this.color = color; this.color = color;
//GridActivity activity = (GridActivity) getContext();
//setBackgroundColor(activity.colorFromPalette(color));
invalidate();
}
enum Quadrant {
NW(0), NE(1), SE(2), SW(3);
final int value;
Quadrant(int value) {
this.value = value;
}
}
enum Corner {
NO_NO, NO_YES, YES_NO, YES_NO_YES, YES_YES_YES
}
static Corner corner (boolean adjacent1, boolean diagonal, boolean adjacent2) {
if(adjacent1) {
if(!adjacent2) return Corner.YES_NO;
else if(diagonal) return Corner.YES_YES_YES;
else return Corner.YES_NO_YES;
}
else if(adjacent2) return Corner.NO_YES;
else return Corner.NO_NO;
}
private Paint paint = new Paint();
private Corner[] neighbors = new Corner[] { Corner.NO_NO, Corner.NO_NO,Corner.NO_NO,Corner.NO_NO};
void setNeighbors(Corner nw, Corner ne, Corner se, Corner sw) {
boolean changed = false;
if(neighbors[0] != nw) {
neighbors[0] = nw;
changed = true;
}
if(neighbors[1] != ne) {
neighbors[1] = ne;
changed = true;
}
if(neighbors[2] != se) {
neighbors[2] = se;
changed = true;
}
if(neighbors[3] != sw) {
neighbors[3] = sw;
changed = true;
}
if(changed) invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
GridActivity activity = (GridActivity) getContext(); GridActivity activity = (GridActivity) getContext();
setBackgroundColor(activity.colorFromPalette(color)); paint.setStyle(Paint.Style.FILL);
paint.setColor(activity.colorFromPalette(color));
final float z = getWidth();
final float c = z * 0.5f;
final float m = z * 0.1f;
for(Quadrant q : Quadrant.values()) {
System.out.print(" " + q.value + "=" + q + ":" + neighbors[q.value]);
switch(neighbors[q.value]) {
case NO_NO:
drawRect(canvas, q, m, m, c, c, paint); // corner (nn)
break;
case NO_YES:
drawRect(canvas, q, m, 0, c, c, paint); // vert (ny)
break;
case YES_NO:
drawRect(canvas, q, 0, m, c, c, paint); // horiz (yn)
break;
case YES_YES_YES:
drawRect(canvas, q, 0, 0, c, c, paint); // full (yyy)
break;
case YES_NO_YES:
drawRect(canvas, q, m, 0, c, c, paint); // vert
drawRect(canvas, q, 0, m, c, c, paint); // horiz
break;
}
}
System.out.println();
}
void drawRect(Canvas canvas, Quadrant quadrant, float left, float top, float right, float bottom, Paint paint) {
// Maybe flip horizontal coordinates
float tmp;
switch(quadrant) {
case NE: case SE:
tmp = left;
left = right;
right = getWidth() - tmp;
}
// Maybe flip vertical coordinates
switch(quadrant) {
case SW: case SE:
tmp = top;
top = bottom;
bottom = getHeight() - tmp;
}
// Now draw
canvas.drawRect(left, top, right, bottom, paint);
} }
} }
...@@ -5,7 +5,6 @@ import android.content.res.TypedArray; ...@@ -5,7 +5,6 @@ import android.content.res.TypedArray;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.hardware.camera2.params.BlackLevelPattern;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
......
...@@ -22,6 +22,13 @@ ...@@ -22,6 +22,13 @@
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="\u00A92019 Christopher League" />
<Space <Space
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<dimen name="default_gap">4dp</dimen> <dimen name="default_gap">2dp</dimen>
<dimen name="color_button">30dp</dimen> <dimen name="color_button">30dp</dimen>
<dimen name="radio_sep">12dp</dimen> <dimen name="radio_sep">12dp</dimen>
</resources> </resources>
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