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
9eb5cbe0
Commit
9eb5cbe0
authored
Jul 05, 2019
by
Christopher League
Browse files
Lots missing, but game is playable
parent
54e3ff2f
Changes
10
Hide whitespace changes
Inline
Side-by-side
app/build.gradle
View file @
9eb5cbe0
...
...
@@ -26,4 +26,5 @@ dependencies {
testImplementation
'junit:junit:4.12'
androidTestImplementation
'com.android.support.test:runner:1.0.2'
androidTestImplementation
'com.android.support.test.espresso:espresso-core:3.0.2'
implementation
'com.android.support:gridlayout-v7:28.0.0'
}
app/src/main/AndroidManifest.xml
View file @
9eb5cbe0
...
...
@@ -10,6 +10,8 @@
android:supportsRtl=
"true"
android:theme=
"@style/AppTheme"
>
<activity
android:name=
".MainActivity"
>
</activity>
<activity
android:name=
".GridActivity"
>
<intent-filter>
<action
android:name=
"android.intent.action.MAIN"
/>
...
...
app/src/main/java/edu/liu/floodgame/ColorButton.java
0 → 100644
View file @
9eb5cbe0
package
edu.liu.floodgame
;
import
android.view.View
;
import
android.widget.LinearLayout
;
public
class
ColorButton
extends
View
implements
View
.
OnClickListener
{
int
color
;
ColorButton
(
GridActivity
activity
,
int
size
,
int
color
)
{
super
(
activity
);
LinearLayout
.
LayoutParams
lp
=
new
LinearLayout
.
LayoutParams
(
size
,
size
);
setLayoutParams
(
lp
);
this
.
color
=
color
;
setBackgroundColor
(
activity
.
colors
[
color
]);
setOnClickListener
(
this
);
}
@Override
public
void
onClick
(
View
view
)
{
System
.
out
.
println
(
"You clicked color "
+
color
);
((
GridActivity
)
getContext
()).
onClickColor
(
color
);
}
}
app/src/main/java/edu/liu/floodgame/FloodGrid.java
0 → 100644
View file @
9eb5cbe0
package
edu.liu.floodgame
;
public
interface
FloodGrid
{
int
numRows
();
int
numColumns
();
int
numCells
();
// Should always be NUM_ROWS*columns
int
getColorAt
(
int
row
,
int
column
);
int
getColorAt
(
int
index
);
void
setColorAt
(
int
row
,
int
column
,
int
color
);
void
setColorAt
(
int
index
,
int
color
);
}
app/src/main/java/edu/liu/floodgame/FloodGridArray2D.java
0 → 100644
View file @
9eb5cbe0
package
edu.liu.floodgame
;
public
class
FloodGridArray2D
implements
FloodGrid
{
private
int
[][]
grid
;
FloodGridArray2D
(
int
numRows
,
int
numColumns
)
{
if
(
numRows
*
numColumns
<
3
)
{
throw
new
IllegalArgumentException
(
"numRows*numColumns too small"
);
}
assert
numRows
>
0
;
grid
=
new
int
[
numRows
][
numColumns
];
}
@Override
public
int
numRows
()
{
return
grid
.
length
;
}
@Override
public
int
numColumns
()
{
return
grid
[
0
].
length
;
}
@Override
public
int
numCells
()
{
return
numRows
()
*
numColumns
();
}
@Override
public
int
getColorAt
(
int
row
,
int
column
)
{
return
grid
[
row
][
column
];
}
@Override
public
int
getColorAt
(
int
index
)
{
return
getColorAt
(
index
/
numColumns
(),
index
%
numColumns
());
}
@Override
public
void
setColorAt
(
int
row
,
int
column
,
int
color
)
{
grid
[
row
][
column
]
=
color
;
}
@Override
public
void
setColorAt
(
int
index
,
int
color
)
{
setColorAt
(
index
/
numColumns
(),
index
%
numColumns
(),
color
);
}
}
app/src/main/java/edu/liu/floodgame/FloodGridOps.java
0 → 100644
View file @
9eb5cbe0
package
edu.liu.floodgame
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.util.Random
;
public
class
FloodGridOps
{
static
void
randomize
(
FloodGrid
grid
,
Random
rng
,
int
numColors
)
{
if
(
numColors
<
3
)
{
throw
new
IllegalArgumentException
(
"numColors too small"
);
}
for
(
int
i
=
0
;
i
<
grid
.
numCells
();
i
++)
{
grid
.
setColorAt
(
i
,
rng
.
nextInt
(
numColors
));
}
}
static
String
toString
(
FloodGrid
grid
)
{
StringBuilder
buf
=
new
StringBuilder
();
for
(
int
row
=
0
;
row
<
grid
.
numRows
();
row
++)
{
for
(
int
col
=
0
;
col
<
grid
.
numColumns
();
col
++)
{
buf
.
append
(
grid
.
getColorAt
(
row
,
col
));
}
buf
.
append
(
'\n'
);
}
return
buf
.
toString
();
}
static
int
[]
toArray
(
FloodGrid
grid
)
{
int
[]
colors
=
new
int
[
grid
.
numCells
()];
for
(
int
i
=
0
;
i
<
colors
.
length
;
i
++)
{
colors
[
i
]
=
grid
.
getColorAt
(
i
);
}
return
colors
;
}
static
void
fromArray
(
FloodGrid
grid
,
int
[]
colors
)
{
for
(
int
i
=
0
;
i
<
grid
.
numCells
();
i
++)
{
grid
.
setColorAt
(
i
,
colors
[
i
]);
}
}
static
boolean
gameOver
(
FloodGrid
grid
)
{
int
goal
=
grid
.
getColorAt
(
0
);
for
(
int
i
=
1
;
i
<
grid
.
numCells
();
i
++)
{
if
(
grid
.
getColorAt
(
i
)
!=
goal
)
{
return
false
;
}
}
return
true
;
}
static
boolean
okayToVisit
(
FloodGrid
grid
,
boolean
[][]
alreadyVisited
,
int
row
,
int
column
,
int
color
)
{
return
(
row
>=
0
&&
row
<
grid
.
numRows
()
&&
column
>=
0
&&
column
<
grid
.
numColumns
()
&&
!
alreadyVisited
[
row
][
column
]
&&
grid
.
getColorAt
(
row
,
column
)
==
color
);
}
static
void
flood
(
FloodGrid
grid
,
boolean
[][]
alreadyVisited
,
int
row
,
int
column
,
int
previousColor
,
int
newColor
)
{
alreadyVisited
[
row
][
column
]
=
true
;
grid
.
setColorAt
(
row
,
column
,
newColor
);
// Down
if
(
okayToVisit
(
grid
,
alreadyVisited
,
row
+
1
,
column
,
previousColor
))
{
flood
(
grid
,
alreadyVisited
,
row
+
1
,
column
,
previousColor
,
newColor
);
}
// Right
if
(
okayToVisit
(
grid
,
alreadyVisited
,
row
,
column
+
1
,
previousColor
))
{
flood
(
grid
,
alreadyVisited
,
row
,
column
+
1
,
previousColor
,
newColor
);
}
// Up
if
(
okayToVisit
(
grid
,
alreadyVisited
,
row
-
1
,
column
,
previousColor
))
{
flood
(
grid
,
alreadyVisited
,
row
-
1
,
column
,
previousColor
,
newColor
);
}
// Left
if
(
okayToVisit
(
grid
,
alreadyVisited
,
row
,
column
-
1
,
previousColor
))
{
flood
(
grid
,
alreadyVisited
,
row
,
column
-
1
,
previousColor
,
newColor
);
}
}
static
void
flood
(
FloodGrid
grid
,
int
newColor
)
{
flood
(
grid
,
new
boolean
[
grid
.
numRows
()][
grid
.
numColumns
()],
0
,
0
,
grid
.
getColorAt
(
0
,
0
),
newColor
);
}
// Console version of the game!
public
static
void
main
(
String
[]
args
)
throws
IOException
{
final
int
numColors
=
4
;
FloodGrid
grid
=
new
FloodGridArray2D
(
5
,
7
);
randomize
(
grid
,
new
Random
(),
numColors
);
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
System
.
in
));
System
.
out
.
print
(
toString
(
grid
));
while
(!
gameOver
(
grid
))
{
System
.
out
.
print
(
"Your choice: "
);
String
line
=
reader
.
readLine
();
int
newColor
=
Integer
.
parseInt
(
line
);
if
(
newColor
>=
0
&&
newColor
<
numColors
)
{
flood
(
grid
,
newColor
);
System
.
out
.
print
(
toString
(
grid
));
}
else
{
System
.
out
.
println
(
"Error"
);
}
}
}
}
app/src/main/java/edu/liu/floodgame/GridActivity.java
0 → 100644
View file @
9eb5cbe0
package
edu.liu.floodgame
;
import
android.os.Bundle
;
import
android.support.annotation.Nullable
;
import
android.support.v7.app.AppCompatActivity
;
import
android.support.v7.widget.GridLayout
;
import
android.widget.LinearLayout
;
import
java.util.Random
;
public
class
GridActivity
extends
AppCompatActivity
implements
FloodGrid
{
final
int
[]
colors
=
{
0xFFfbb4ae
,
0xFFb3cde3
,
0xFFccebc5
,
0xFFdecbe4
,
0xFFfed9a6
,
0xFFffffcc
,
0xFFe5d8bd
,
0xFFfddaec
};
final
int
NUM_COLORS
=
colors
.
length
;
final
String
COLOR_ARRAY
=
"COLOR_ARRAY"
;
private
GridLayout
gridLayout
;
private
LinearLayout
buttonBar
;
@Override
protected
void
onCreate
(
@Nullable
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
setContentView
(
R
.
layout
.
activity_flood_grid
);
// Set up button bar
buttonBar
=
findViewById
(
R
.
id
.
buttonBar
);
buttonBar
.
removeAllViews
();
for
(
int
i
=
0
;
i
<
NUM_COLORS
;
i
++)
{
buttonBar
.
addView
(
new
ColorButton
(
this
,
100
,
i
));
}
// Set up grid
gridLayout
=
findViewById
(
R
.
id
.
grid
);
gridLayout
.
removeAllViews
();
final
int
NUM_ROWS
=
10
;
final
int
NUM_COLUMNS
=
8
;
final
int
NUM_CELLS
=
NUM_ROWS
*
NUM_COLUMNS
;
gridLayout
.
setColumnCount
(
NUM_COLUMNS
);
for
(
int
i
=
0
;
i
<
NUM_CELLS
;
i
++)
{
gridLayout
.
addView
(
new
GridCellView
(
this
,
100
));
}
if
(
savedInstanceState
==
null
)
{
System
.
out
.
println
(
"New random grid"
);
FloodGridOps
.
randomize
(
this
,
new
Random
(),
NUM_COLORS
);
}
else
{
System
.
out
.
println
(
"Restoring saved grid"
);
FloodGridOps
.
fromArray
(
this
,
savedInstanceState
.
getIntArray
(
COLOR_ARRAY
));
}
}
@Override
protected
void
onSaveInstanceState
(
Bundle
outState
)
{
super
.
onSaveInstanceState
(
outState
);
outState
.
putIntArray
(
COLOR_ARRAY
,
FloodGridOps
.
toArray
(
this
));
}
@Override
public
int
numRows
()
{
return
numCells
()
/
numColumns
();
}
@Override
public
int
numColumns
()
{
return
gridLayout
.
getColumnCount
();
}
@Override
public
int
numCells
()
{
return
gridLayout
.
getChildCount
();
}
@Override
public
int
getColorAt
(
int
row
,
int
column
)
{
return
getColorAt
(
row
*
numColumns
()
+
column
);
}
@Override
public
int
getColorAt
(
int
index
)
{
GridCellView
cell
=
(
GridCellView
)
gridLayout
.
getChildAt
(
index
);
return
cell
.
color
;
}
@Override
public
void
setColorAt
(
int
row
,
int
column
,
int
color
)
{
setColorAt
(
row
*
numColumns
()
+
column
,
color
);
}
@Override
public
void
setColorAt
(
int
index
,
int
color
)
{
GridCellView
cell
=
(
GridCellView
)
gridLayout
.
getChildAt
(
index
);
cell
.
setColor
(
color
);
}
public
void
onClickColor
(
int
color
)
{
FloodGridOps
.
flood
(
this
,
color
);
}
}
app/src/main/java/edu/liu/floodgame/GridCellView.java
0 → 100644
View file @
9eb5cbe0
package
edu.liu.floodgame
;
import
android.content.Context
;
import
android.support.v7.widget.GridLayout
;
import
android.view.View
;
public
class
GridCellView
extends
View
{
int
color
;
GridCellView
(
GridActivity
activity
,
int
size
)
{
super
(
activity
);
GridLayout
.
LayoutParams
lp
=
new
GridLayout
.
LayoutParams
();
lp
.
width
=
lp
.
height
=
size
;
setLayoutParams
(
lp
);
}
void
setColor
(
int
color
)
{
this
.
color
=
color
;
GridActivity
activity
=
(
GridActivity
)
getContext
();
setBackgroundColor
(
activity
.
colors
[
color
]);
}
}
app/src/main/java/edu/liu/floodgame/MainActivity.java
View file @
9eb5cbe0
...
...
@@ -38,6 +38,7 @@ public class MainActivity extends AppCompatActivity {
protected
void
onSaveInstanceState
(
Bundle
outState
)
{
super
.
onSaveInstanceState
(
outState
);
outState
.
putInt
(
COUNTER
,
counter
);
}
public
void
clickIncrement
(
View
v
)
{
...
...
app/src/main/res/layout/activity_flood_grid.xml
0 → 100644
View file @
9eb5cbe0
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:app=
"http://schemas.android.com/apk/res-auto"
xmlns:tools=
"http://schemas.android.com/tools"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
>
<android.support.v7.widget.GridLayout
android:id=
"@+id/grid"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
app:columnCount=
"2"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintTop_toTopOf=
"parent"
>
<View
android:id=
"@+id/sample1"
android:layout_width=
"50dp"
android:layout_height=
"50dp"
android:background=
"@color/colorPrimary"
/>
<View
android:id=
"@+id/sample2"
android:layout_width=
"50dp"
android:layout_height=
"50dp"
android:background=
"@color/colorAccent"
/>
<View
android:id=
"@+id/sample3"
android:layout_width=
"50dp"
android:layout_height=
"50dp"
android:background=
"@color/colorPrimaryDark"
/>
<View
android:id=
"@+id/sample4"
android:layout_width=
"50dp"
android:layout_height=
"50dp"
android:background=
"@color/colorPrimary"
/>
</android.support.v7.widget.GridLayout>
<LinearLayout
android:id=
"@+id/buttonBar"
android:layout_width=
"0dp"
android:layout_height=
"wrap_content"
android:layout_marginBottom=
"8dp"
android:background=
"#ccc"
android:orientation=
"horizontal"
android:padding=
"10dp"
android:paddingRight=
"0dp"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@+id/grid"
>
<Button
android:id=
"@+id/button"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:background=
"#eea1c7"
tools:layout_conversion_absoluteHeight=
"48dp"
tools:layout_conversion_absoluteWidth=
"162dp"
tools:layout_editor_absoluteX=
"10dp"
tools:layout_editor_absoluteY=
"546dp"
/>
<Button
android:id=
"@+id/button2"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"Button"
tools:layout_conversion_absoluteHeight=
"48dp"
tools:layout_conversion_absoluteWidth=
"230dp"
tools:layout_editor_absoluteX=
"172dp"
tools:layout_editor_absoluteY=
"546dp"
/>
</LinearLayout>
</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