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
95bc8475
Commit
95bc8475
authored
Jul 05, 2019
by
Christopher League
Browse files
Handles variations in number of colors better
parent
516e5bf0
Changes
7
Hide whitespace changes
Inline
Side-by-side
app/build.gradle
View file @
95bc8475
...
@@ -10,6 +10,7 @@ android {
...
@@ -10,6 +10,7 @@ android {
versionCode
1
versionCode
1
versionName
"1.0"
versionName
"1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
vectorDrawables
.
useSupportLibrary
true
}
}
buildTypes
{
buildTypes
{
release
{
release
{
...
...
app/src/main/java/edu/liu/floodgame/ColorButton.java
View file @
95bc8475
package
edu.liu.floodgame
;
package
edu.liu.floodgame
;
import
android.content.Context
;
import
android.graphics.Canvas
;
import
android.graphics.Paint
;
import
android.util.AttributeSet
;
import
android.view.View
;
import
android.view.View
;
import
android.widget.LinearLayout
;
import
android.widget.LinearLayout
;
...
@@ -7,18 +11,26 @@ public class ColorButton extends View implements View.OnClickListener {
...
@@ -7,18 +11,26 @@ public class ColorButton extends View implements View.OnClickListener {
int
color
;
int
color
;
ColorButton
(
GridActivity
activity
,
int
size
,
int
color
)
{
ColorButton
(
Context
context
,
AttributeSet
attrs
)
{
super
(
activity
);
super
(
context
,
attrs
);
LinearLayout
.
LayoutParams
lp
=
new
LinearLayout
.
LayoutParams
(
size
,
size
);
setLayoutParams
(
lp
);
this
.
color
=
color
;
setBackgroundColor
(
activity
.
colors
[
color
]);
setOnClickListener
(
this
);
setOnClickListener
(
this
);
}
}
@Override
@Override
public
void
onClick
(
View
view
)
{
public
void
onClick
(
View
view
)
{
System
.
out
.
println
(
"You clicked color "
+
color
);
System
.
out
.
println
(
"You clicked color "
+
color
);
((
GridActivity
)
getContext
()).
onClickColor
(
color
);
GridActivity
activity
=
(
GridActivity
)
getContext
();
activity
.
onClickColor
(
color
);
}
@Override
protected
void
onDraw
(
Canvas
canvas
)
{
super
.
onDraw
(
canvas
);
GridActivity
activity
=
(
GridActivity
)
getContext
();
int
half
=
canvas
.
getHeight
()
/
2
;
Paint
paint
=
new
Paint
();
paint
.
setStyle
(
Paint
.
Style
.
FILL
);
paint
.
setColor
(
activity
.
colors
[
color
]);
canvas
.
drawCircle
(
half
,
half
,
half
,
paint
);
}
}
}
}
app/src/main/java/edu/liu/floodgame/GridActivity.java
View file @
95bc8475
package
edu.liu.floodgame
;
package
edu.liu.floodgame
;
import
android.graphics.Color
;
import
android.os.Bundle
;
import
android.os.Bundle
;
import
android.support.annotation.Nullable
;
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.ViewGroup
;
import
android.widget.LinearLayout
;
import
android.widget.LinearLayout
;
import
android.widget.Space
;
import
java.util.Random
;
import
java.util.Random
;
...
@@ -14,14 +18,15 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
...
@@ -14,14 +18,15 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
0xFFfbb4ae
,
0xFFfbb4ae
,
0xFFb3cde3
,
0xFFb3cde3
,
0xFFccebc5
,
0xFFccebc5
,
0xFFdecbe4
,
0xFFdecbe4
0xFFfed9a6
,
//
0xFFfed9a6,
0xFFffffcc
,
//
0xFFffffcc,
0xFFe5d8bd
,
//
0xFFe5d8bd,
0xFFfddaec
//
0xFFfddaec
};
};
final
int
NUM_COLORS
=
colors
.
length
;
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
=
"COLOR_ARRAY"
;
private
GridLayout
gridLayout
;
private
GridLayout
gridLayout
;
...
@@ -31,21 +36,27 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
...
@@ -31,21 +36,27 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
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
);
// Set up button bar
// Set up button bar. It contains MAX_COLORS buttons and MAX_COLORS+1 gaps, so the
// buttons are at odd offsets.
buttonBar
=
findViewById
(
R
.
id
.
buttonBar
);
buttonBar
=
findViewById
(
R
.
id
.
buttonBar
);
buttonBar
.
removeAllViews
();
int
buttonIdx
=
0
;
for
(
int
i
=
0
;
i
<
NUM_COLORS
;
i
++)
{
for
(
;
buttonIdx
<
NUM_COLORS
;
buttonIdx
++)
{
buttonBar
.
addView
(
new
ColorButton
(
this
,
100
,
i
));
ColorButton
button
=
(
ColorButton
)
buttonBar
.
getChildAt
(
2
*
buttonIdx
+
1
);
button
.
color
=
buttonIdx
;
}
// Remove any subsequent buttons and gaps
for
(
;
buttonIdx
<
MAX_COLORS
;
buttonIdx
++)
{
buttonBar
.
getChildAt
(
2
*
buttonIdx
).
setVisibility
(
View
.
GONE
);
buttonBar
.
getChildAt
(
2
*
buttonIdx
+
1
).
setVisibility
(
View
.
GONE
);
}
}
// Set up grid
// Set up grid
gridLayout
=
findViewById
(
R
.
id
.
grid
);
gridLayout
=
findViewById
(
R
.
id
.
grid
);
gridLayout
.
removeAllViews
();
gridLayout
.
removeAllViews
();
final
int
NUM_ROWS
=
10
;
final
int
EDGE_SIZE
=
5
;
final
int
NUM_COLUMNS
=
8
;
gridLayout
.
setColumnCount
(
EDGE_SIZE
);
final
int
NUM_CELLS
=
NUM_ROWS
*
NUM_COLUMNS
;
for
(
int
i
=
0
;
i
<
EDGE_SIZE
*
EDGE_SIZE
;
i
++)
{
gridLayout
.
setColumnCount
(
NUM_COLUMNS
);
for
(
int
i
=
0
;
i
<
NUM_CELLS
;
i
++)
{
gridLayout
.
addView
(
new
GridCellView
(
this
));
gridLayout
.
addView
(
new
GridCellView
(
this
));
}
}
if
(
savedInstanceState
==
null
)
{
if
(
savedInstanceState
==
null
)
{
...
...
app/src/main/java/edu/liu/floodgame/GridCellView.java
View file @
95bc8475
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
;
...
...
app/src/main/res/drawable/ic_drawing.xml
0 → 100644
View file @
95bc8475
<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>
app/src/main/res/layout/activity_flood_grid.xml
View file @
95bc8475
...
@@ -54,37 +54,125 @@
...
@@ -54,37 +54,125 @@
<LinearLayout
<LinearLayout
android:id=
"@+id/buttonBar"
android:id=
"@+id/buttonBar"
android:layout_width=
"
0dp
"
android:layout_width=
"
match_parent
"
android:layout_height=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_marginBottom=
"8dp"
android:layout_marginBottom=
"@dimen/default_gap"
android:background=
"#ccc"
android:orientation=
"horizontal"
android:orientation=
"horizontal"
android:padding=
"10dp"
android:padding=
"@dimen/default_gap"
android:paddingRight=
"0dp"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintStart_toStartOf=
"parent"
>
app:layout_constraintTop_toBottomOf=
"@+id/grid"
>
<Space
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
/>
<edu.liu.floodgame.ColorButton
android:layout_width=
"@dimen/color_button"
android:layout_height=
"@dimen/color_button"
android:layout_marginStart=
"@dimen/default_gap"
android:layout_marginLeft=
"@dimen/default_gap"
android:layout_marginEnd=
"@dimen/default_gap"
android:layout_marginRight=
"@dimen/default_gap"
/>
<Space
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
/>
<edu.liu.floodgame.ColorButton
android:layout_width=
"@dimen/color_button"
android:layout_height=
"@dimen/color_button"
android:layout_marginStart=
"@dimen/default_gap"
android:layout_marginLeft=
"@dimen/default_gap"
android:layout_marginEnd=
"@dimen/default_gap"
android:layout_marginRight=
"@dimen/default_gap"
/>
<Button
<Space
android:id=
"@+id/button"
android:layout_width=
"wrap_content"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_height=
"wrap_content"
android:background=
"#eea1c7"
android:layout_weight=
"1"
/>
tools:layout_conversion_absoluteHeight=
"48dp"
tools:layout_conversion_absoluteWidth=
"162dp"
<edu.liu.floodgame.ColorButton
tools:layout_editor_absoluteX=
"10dp"
android:layout_width=
"@dimen/color_button"
tools:layout_editor_absoluteY=
"546dp"
/>
android:layout_height=
"@dimen/color_button"
android:layout_marginStart=
"@dimen/default_gap"
<Button
android:layout_marginLeft=
"@dimen/default_gap"
android:id=
"@+id/button2"
android:layout_marginEnd=
"@dimen/default_gap"
android:layout_marginRight=
"@dimen/default_gap"
/>
<Space
android:layout_width=
"wrap_content"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"Button"
android:layout_weight=
"1"
/>
tools:layout_conversion_absoluteHeight=
"48dp"
tools:layout_conversion_absoluteWidth=
"230dp"
<edu.liu.floodgame.ColorButton
tools:layout_editor_absoluteX=
"172dp"
android:layout_width=
"@dimen/color_button"
tools:layout_editor_absoluteY=
"546dp"
/>
android:layout_height=
"@dimen/color_button"
android:layout_marginStart=
"@dimen/default_gap"
android:layout_marginLeft=
"@dimen/default_gap"
android:layout_marginEnd=
"@dimen/default_gap"
android:layout_marginRight=
"@dimen/default_gap"
/>
<Space
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
/>
<edu.liu.floodgame.ColorButton
android:layout_width=
"@dimen/color_button"
android:layout_height=
"@dimen/color_button"
android:layout_marginStart=
"@dimen/default_gap"
android:layout_marginLeft=
"@dimen/default_gap"
android:layout_marginEnd=
"@dimen/default_gap"
android:layout_marginRight=
"@dimen/default_gap"
/>
<Space
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
/>
<edu.liu.floodgame.ColorButton
android:layout_width=
"@dimen/color_button"
android:layout_height=
"@dimen/color_button"
android:layout_marginStart=
"@dimen/default_gap"
android:layout_marginLeft=
"@dimen/default_gap"
android:layout_marginEnd=
"@dimen/default_gap"
android:layout_marginRight=
"@dimen/default_gap"
/>
<Space
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
/>
<edu.liu.floodgame.ColorButton
android:layout_width=
"@dimen/color_button"
android:layout_height=
"@dimen/color_button"
android:layout_marginStart=
"@dimen/default_gap"
android:layout_marginLeft=
"@dimen/default_gap"
android:layout_marginEnd=
"@dimen/default_gap"
android:layout_marginRight=
"@dimen/default_gap"
/>
<Space
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
/>
<edu.liu.floodgame.ColorButton
android:layout_width=
"@dimen/color_button"
android:layout_height=
"@dimen/color_button"
android:layout_marginStart=
"@dimen/default_gap"
android:layout_marginLeft=
"@dimen/default_gap"
android:layout_marginEnd=
"@dimen/default_gap"
android:layout_marginRight=
"@dimen/default_gap"
/>
<Space
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
/>
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
app/src/main/res/values/dimens.xml
0 → 100644
View file @
95bc8475
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen
name=
"default_gap"
>
8dp
</dimen>
<dimen
name=
"color_button"
>
30dp
</dimen>
</resources>
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