Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Y
yatzy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tfabre
yatzy
Commits
eb31a43d
Commit
eb31a43d
authored
1 year ago
by
Thomas FABRE
Browse files
Options
Downloads
Patches
Plain Diff
Refactor fours with Roll and generify summing method
parent
39a2e7fe
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/io/takima/Yatzy.java
+10
-13
10 additions, 13 deletions
src/main/java/io/takima/Yatzy.java
src/test/java/io/takima/YatzyTest.java
+3
-3
3 additions, 3 deletions
src/test/java/io/takima/YatzyTest.java
with
13 additions
and
16 deletions
src/main/java/io/takima/Yatzy.java
+
10
−
13
View file @
eb31a43d
...
...
@@ -19,16 +19,23 @@ public final class Yatzy {
return
0
;
}
private
static
int
sumDiceThatMatchWithValue
(
Roll
roll
,
int
value
)
{
return
roll
.
toStream
().
filter
(
dice
->
dice
==
value
).
sum
();
}
public
static
int
ones
(
Roll
roll
)
{
return
roll
.
toStream
().
filter
(
dice
->
dice
==
1
).
sum
(
);
return
sumDiceThatMatchWithValue
(
roll
,
1
);
}
public
static
int
twos
(
Roll
roll
)
{
return
roll
.
toStream
().
filter
(
dice
->
dice
==
2
).
sum
(
);
return
sumDiceThatMatchWithValue
(
roll
,
2
);
}
public
static
int
threes
(
Roll
roll
)
{
return
roll
.
toStream
().
filter
(
dice
->
dice
==
3
).
sum
();
return
sumDiceThatMatchWithValue
(
roll
,
3
);
}
public
static
int
fours
(
Roll
roll
)
{
return
sumDiceThatMatchWithValue
(
roll
,
4
);
}
public
static
int
pair
(
int
d1
,
int
d2
,
int
d3
,
int
d4
,
int
d5
)
{
...
...
@@ -168,16 +175,6 @@ public final class Yatzy {
return
0
;
}
public
int
fours
()
{
int
sum
=
0
;
for
(
int
at
=
0
;
at
!=
5
;
at
++)
{
if
(
dice
[
at
]
==
4
)
{
sum
+=
4
;
}
}
return
sum
;
}
public
int
fives
()
{
int
s
=
0
;
for
(
int
i
=
0
;
i
<
dice
.
length
;
i
++)
{
...
...
This diff is collapsed.
Click to expand it.
src/test/java/io/takima/YatzyTest.java
+
3
−
3
View file @
eb31a43d
...
...
@@ -57,9 +57,9 @@ class YatzyTest {
@Test
void
foursShouldSumAllFourDices
()
{
assertAll
(
()
->
assertEquals
(
12
,
new
Yatzy
(
4
,
4
,
4
,
5
,
5
)
.
fours
(
)),
()
->
assertEquals
(
8
,
new
Yatzy
(
4
,
4
,
5
,
5
,
5
)
.
fours
(
)),
()
->
assertEquals
(
4
,
new
Yatzy
(
4
,
5
,
5
,
5
,
5
)
.
fours
(
))
()
->
assertEquals
(
12
,
Yatzy
.
fours
(
roll
(
4
,
4
,
4
,
5
,
5
))),
()
->
assertEquals
(
8
,
Yatzy
.
fours
(
roll
(
4
,
4
,
5
,
5
,
5
))),
()
->
assertEquals
(
4
,
Yatzy
.
fours
(
roll
(
4
,
5
,
5
,
5
,
5
)))
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment