Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
live-coding-fr
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Redmine
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
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
twitch
live-coding-fr
Merge requests
!80
Resolve "Game of life part 2"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Resolve "Game of life part 2"
106-game-of-life-part-2
into
master
Overview
0
Commits
1
Pipelines
0
Changes
7
Merged
Colin DAMON
requested to merge
106-game-of-life-part-2
into
master
3 years ago
Overview
0
Commits
1
Pipelines
0
Changes
7
Expand
Closes
#106 (closed)
Edited
3 years ago
by
Colin DAMON
0
0
Merge request reports
Compare
master
version 1
b4706c8f
3 years ago
master (base)
and
latest version
latest version
0fbb81e4
1 commit,
3 years ago
version 1
b4706c8f
1 commit,
3 years ago
7 files
+
218
−
70
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
7
Search (e.g. *.vue) (Ctrl+P)
game-of-life/src/main/java/fr/ippon/life/Cell.java
+
8
−
41
Options
@@ -2,24 +2,16 @@ package fr.ippon.life;
import
java.util.Set
;
public
class
Cell
{
public
record
Cell
(
int
row
,
int
column
)
{
private
static
final
int
LOW_POPULATION_THRESHOLD
=
2
;
private
static
final
int
HIGH_POPULATION_THRESHOLD
=
3
;
private
static
final
int
BORN_THRESHOLD
=
HIGH_POPULATION_THRESHOLD
;
private
final
int
row
;
private
final
int
column
;
public
Cell
(
int
row
,
int
column
)
{
this
.
row
=
row
;
this
.
column
=
column
;
}
boolean
stayAlive
(
Set
<
Cell
>
aliveCells
)
{
long
neighbourgCount
=
countNeigbours
(
aliveCells
);
return
neighbourgCount
==
LOW_POPULATION_THRESHOLD
||
neighbourgCount
==
HIGH_POPULATION_THRESHOLD
;
return
neighbourgCount
==
LOW_POPULATION_THRESHOLD
||
neighbourgCount
==
HIGH_POPULATION_THRESHOLD
;
}
boolean
born
(
Set
<
Cell
>
aliveCells
)
{
@@ -27,7 +19,9 @@ public class Cell {
}
private
long
countNeigbours
(
Set
<
Cell
>
aliveCells
)
{
return
aliveCells
.
stream
().
filter
(
this
::
isNeighbour
).
count
();
return
aliveCells
.
stream
()
.
filter
(
this
::
isNeighbour
)
.
count
();
}
private
boolean
isNeighbour
(
Cell
other
)
{
@@ -35,38 +29,11 @@ public class Cell {
return
false
;
}
return
delta
(
row
,
other
.
row
)
<=
1
&&
delta
(
column
,
other
.
column
)
<=
1
;
return
delta
(
row
,
other
.
row
)
<=
1
&&
delta
(
column
,
other
.
column
)
<=
1
;
}
private
static
int
delta
(
int
first
,
int
second
)
{
return
Math
.
abs
(
first
-
second
);
}
public
int
getRow
()
{
return
row
;
}
public
int
getColumn
()
{
return
column
;
}
@Override
public
int
hashCode
()
{
final
int
prime
=
31
;
int
result
=
1
;
result
=
prime
*
result
+
column
;
result
=
prime
*
result
+
row
;
return
result
;
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
return
true
;
if
(
obj
==
null
)
return
false
;
if
(
getClass
()
!=
obj
.
getClass
())
return
false
;
Cell
other
=
(
Cell
)
obj
;
if
(
column
!=
other
.
column
)
return
false
;
if
(
row
!=
other
.
row
)
return
false
;
return
true
;
}
}
Loading