Commit 80be38ab authored by Christopher League's avatar Christopher League
Browse files

Shows numMoves and detects game over

parent 95bc8475
package edu.liu.floodgame;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;
import java.util.Random;
......@@ -27,15 +25,21 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
final int MAX_COLORS = 8; // The number of color buttons supported by UI
final int NUM_COLORS = colors.length < MAX_COLORS? colors.length : MAX_COLORS;
final String COLOR_ARRAY = "COLOR_ARRAY";
final String COLOR_ARRAY_KEY = "COLOR_ARRAY";
final String NUM_MOVES_KEY = "NUM_MOVES";
private GridLayout gridLayout;
private LinearLayout buttonBar;
private TextView numMovesText;
private int numMoves = 0;
private boolean gameOver = false;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flood_grid);
numMovesText = findViewById(R.id.num_moves);
// Set up button bar. It contains MAX_COLORS buttons and MAX_COLORS+1 gaps, so the
// buttons are at odd offsets.
......@@ -60,18 +64,21 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
gridLayout.addView(new GridCellView(this));
}
if(savedInstanceState == null) {
System.out.println("New random grid");
FloodGridOps.randomize(this, new Random(), NUM_COLORS);
gameOver = false;
setNumMoves(0);
} else {
System.out.println("Restoring saved grid");
FloodGridOps.fromArray(this, savedInstanceState.getIntArray(COLOR_ARRAY));
FloodGridOps.fromArray(this, savedInstanceState.getIntArray(COLOR_ARRAY_KEY));
gameOver = FloodGridOps.gameOver(this);
setNumMoves(savedInstanceState.getInt(NUM_MOVES_KEY));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putIntArray(COLOR_ARRAY, FloodGridOps.toArray(this));
outState.putIntArray(COLOR_ARRAY_KEY, FloodGridOps.toArray(this));
outState.putInt(NUM_MOVES_KEY, numMoves);
}
@Override
......@@ -106,7 +113,29 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
cell.setColor(color);
}
public void setNumMoves(int m) {
numMoves = m;
if(gameOver) {
numMovesText.setText(String.format("Solved in %d moves", numMoves));
}
else {
numMovesText.setText(Integer.toString(numMoves));
}
}
public void onClickColor(int color) {
FloodGridOps.flood(this, color);
if(gameOver) {
// Clicking when game already over restarts
FloodGridOps.randomize(this, new Random(), NUM_COLORS);
gameOver = false;
setNumMoves(0);
}
else {
FloodGridOps.flood(this, color);
if (FloodGridOps.gameOver(this)) {
gameOver = true;
}
setNumMoves(numMoves + 1);
}
}
}
<vector android:height="24dp" android:viewportHeight="11.358956"
android:viewportWidth="11.358956" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillAlpha="1" android:fillColor="#7071ff"
android:pathData="M2.4054,0L8.9535,0A2.4054,2.4054 0,0 1,11.359 2.4054L11.359,8.9535A2.4054,2.4054 0,0 1,8.9535 11.359L2.4054,11.359A2.4054,2.4054 0,0 1,0 8.9535L0,2.4054A2.4054,2.4054 0,0 1,2.4054 0z"
android:strokeAlpha="1" android:strokeColor="#0001e1"
android:strokeLineCap="round" android:strokeLineJoin="miter" android:strokeWidth="0"/>
<path android:fillAlpha="1" android:fillColor="#ffc470"
android:pathData="M5.8799,5.3454m-2.2718,0a2.2718,2.2718 0,1 1,4.5436 0a2.2718,2.2718 0,1 1,-4.5436 0"
android:strokeAlpha="1" android:strokeColor="#0001e1"
android:strokeLineCap="round" android:strokeLineJoin="miter" android:strokeWidth="0"/>
</vector>
......@@ -175,4 +175,22 @@
</LinearLayout>
<TextView
android:id="@+id/num_moves"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="numMoves"
android:textSize="30sp"
app:fontFamily="sans-serif-black"
app:layout_constraintBottom_toTopOf="@+id/buttonBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/grid"
app:layout_constraintVertical_bias="0.341" />
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
Markdown is supported
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