Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Christopher League
FloodGame
Commits
8cd27942
Commit
8cd27942
authored
Jul 08, 2019
by
Christopher League
Browse files
Undo button works, except doesn't signify disabled
parent
7ad90241
Changes
3
Hide whitespace changes
Inline
Side-by-side
app/src/main/java/edu/liu/floodgame/GridActivity.java
View file @
8cd27942
...
...
@@ -7,10 +7,12 @@ import android.support.annotation.Nullable;
import
android.support.v7.app.AppCompatActivity
;
import
android.support.v7.widget.GridLayout
;
import
android.view.View
;
import
android.widget.ImageButton
;
import
android.widget.LinearLayout
;
import
android.widget.TextView
;
import
java.util.Random
;
import
java.util.Stack
;
public
class
GridActivity
extends
AppCompatActivity
implements
FloodGrid
{
...
...
@@ -24,15 +26,18 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
private
GridLayout
gridLayout
;
private
LinearLayout
buttonBar
;
private
TextView
numMovesText
;
private
ImageButton
undoButton
;
private
int
numColors
;
private
int
numMoves
=
0
;
private
boolean
gameOver
=
false
;
private
Stack
<
int
[]>
history
=
new
Stack
<>();
@Override
protected
void
onCreate
(
@Nullable
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
setContentView
(
R
.
layout
.
activity_flood_grid
);
numMovesText
=
findViewById
(
R
.
id
.
num_moves
);
undoButton
=
findViewById
(
R
.
id
.
undoButton
);
// Grab palette
Intent
intent
=
getIntent
();
...
...
@@ -69,7 +74,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
gameOver
=
FloodGridOps
.
gameOver
(
this
);
setNumMoves
(
savedInstanceState
.
getInt
(
NUM_MOVES_KEY
));
}
undoButton
.
setEnabled
(
history
.
size
()
>
0
);
recordNeighborsThroughout
();
}
...
...
@@ -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
)
{
if
(
gameOver
)
{
// Clicking when game already over restarts
FloodGridOps
.
randomize
(
this
,
new
Random
(),
numColors
);
gameOver
=
false
;
setNumMoves
(
0
);
history
.
clear
();
undoButton
.
setEnabled
(
false
);
}
else
{
history
.
push
(
FloodGridOps
.
toArray
(
this
));
FloodGridOps
.
flood
(
this
,
color
);
if
(
FloodGridOps
.
gameOver
(
this
))
{
gameOver
=
true
;
}
setNumMoves
(
numMoves
+
1
);
}
undoButton
.
setEnabled
(
history
.
size
()
>
0
);
recordNeighborsThroughout
();
}
...
...
app/src/main/res/layout-land/activity_flood_grid.xml
View file @
8cd27942
...
...
@@ -87,4 +87,21 @@
app:layout_constraintStart_toEndOf=
"@+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>
\ No newline at end of file
app/src/main/res/layout/activity_flood_grid.xml
View file @
8cd27942
...
...
@@ -85,4 +85,21 @@
app:layout_constraintTop_toBottomOf=
"@+id/grid"
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>
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment