Commit 8cd27942 authored by Christopher League's avatar Christopher League
Browse files

Undo button works, except doesn't signify disabled

parent 7ad90241
...@@ -7,10 +7,12 @@ import android.support.annotation.Nullable; ...@@ -7,10 +7,12 @@ import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayout; import android.support.v7.widget.GridLayout;
import android.view.View; import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import java.util.Random; import java.util.Random;
import java.util.Stack;
public class GridActivity extends AppCompatActivity implements FloodGrid { public class GridActivity extends AppCompatActivity implements FloodGrid {
...@@ -24,15 +26,18 @@ public class GridActivity extends AppCompatActivity implements FloodGrid { ...@@ -24,15 +26,18 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
private GridLayout gridLayout; private GridLayout gridLayout;
private LinearLayout buttonBar; private LinearLayout buttonBar;
private TextView numMovesText; private TextView numMovesText;
private ImageButton undoButton;
private int numColors; private int numColors;
private int numMoves = 0; private int numMoves = 0;
private boolean gameOver = false; private boolean gameOver = false;
private Stack<int[]> history = new Stack<>();
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flood_grid); setContentView(R.layout.activity_flood_grid);
numMovesText = findViewById(R.id.num_moves); numMovesText = findViewById(R.id.num_moves);
undoButton = findViewById(R.id.undoButton);
// Grab palette // Grab palette
Intent intent = getIntent(); Intent intent = getIntent();
...@@ -69,7 +74,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid { ...@@ -69,7 +74,7 @@ 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));
} }
undoButton.setEnabled(history.size() > 0);
recordNeighborsThroughout(); recordNeighborsThroughout();
} }
...@@ -169,20 +174,32 @@ public class GridActivity extends AppCompatActivity implements FloodGrid { ...@@ -169,20 +174,32 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
} }
} }
public void onClickUndo(View v) {
FloodGridOps.fromArray(this, history.pop());
gameOver = false;
setNumMoves(numMoves-1);
undoButton.setEnabled(history.size() > 0);
recordNeighborsThroughout();
}
public void onClickColor(int color) { public void onClickColor(int color) {
if(gameOver) { if(gameOver) {
// Clicking when game already over restarts // Clicking when game already over restarts
FloodGridOps.randomize(this, new Random(), numColors); FloodGridOps.randomize(this, new Random(), numColors);
gameOver = false; gameOver = false;
setNumMoves(0); setNumMoves(0);
history.clear();
undoButton.setEnabled(false);
} }
else { else {
history.push(FloodGridOps.toArray(this));
FloodGridOps.flood(this, color); FloodGridOps.flood(this, color);
if (FloodGridOps.gameOver(this)) { if (FloodGridOps.gameOver(this)) {
gameOver = true; gameOver = true;
} }
setNumMoves(numMoves + 1); setNumMoves(numMoves + 1);
} }
undoButton.setEnabled(history.size() > 0);
recordNeighborsThroughout(); recordNeighborsThroughout();
} }
......
...@@ -87,4 +87,21 @@ ...@@ -87,4 +87,21 @@
app:layout_constraintStart_toEndOf="@+id/grid" app:layout_constraintStart_toEndOf="@+id/grid"
app:layout_constraintTop_toBottomOf="@+id/grid" /> app:layout_constraintTop_toBottomOf="@+id/grid" />
<ImageButton
android:id="@+id/undoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:onClick="onClickUndo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/buttonBar"
app:layout_constraintStart_toEndOf="@+id/grid"
app:layout_constraintTop_toBottomOf="@+id/num_moves"
app:srcCompat="@android:drawable/ic_menu_revert" />
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>
\ No newline at end of file
...@@ -85,4 +85,21 @@ ...@@ -85,4 +85,21 @@
app:layout_constraintTop_toBottomOf="@+id/grid" app:layout_constraintTop_toBottomOf="@+id/grid"
app:layout_constraintVertical_bias="0.341" /> app:layout_constraintVertical_bias="0.341" />
<ImageButton
android:id="@+id/undoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:onClick="onClickUndo"
app:layout_constraintBottom_toTopOf="@+id/buttonBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/num_moves"
app:srcCompat="@android:drawable/ic_menu_revert" />
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>
\ No newline at end of file
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