Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
4
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
C
Conway
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Adrian Kosmaczewski
Conway
Commits
4bf8467a
Commit
4bf8467a
authored
Sep 26, 2020
by
Adrian Kosmaczewski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adapted code to use Map instead of classic JS object
parent
ede34aac
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
66 additions
and
66 deletions
+66
-66
TypeScript/src/Coord.ts
TypeScript/src/Coord.ts
+1
-1
TypeScript/src/World.ts
TypeScript/src/World.ts
+45
-44
TypeScript/src/WorldView.ts
TypeScript/src/WorldView.ts
+2
-3
TypeScript/src/main_browser.ts
TypeScript/src/main_browser.ts
+7
-8
TypeScript/src/main_cli.ts
TypeScript/src/main_cli.ts
+7
-6
TypeScript/tests/tests.ts
TypeScript/tests/tests.ts
+4
-4
No files found.
TypeScript/src/Coord.ts
View file @
4bf8467a
...
...
@@ -5,7 +5,7 @@ export class Coord {
return
`
${
this
.
x
}
:
${
this
.
y
}
`
}
static
createF
romString
(
string
:
string
):
Coord
{
static
f
romString
(
string
:
string
):
Coord
{
let
parts
:
string
[]
=
string
.
split
(
"
:
"
)
let
c
:
number
[]
=
parts
.
map
(
parseFloat
)
return
new
Coord
(
c
[
0
],
c
[
1
])
...
...
TypeScript/src/World.ts
View file @
4bf8467a
...
...
@@ -18,37 +18,38 @@ let makeRange = (size: number): number[] => {
export
class
World
{
cells
=
new
Map
<
string
,
Cell
>
();
constructor
(
public
size
:
number
,
aliveCells
:
string
[]
)
{
constructor
(
public
size
:
number
,
aliveCells
:
Array
<
Coord
>
)
{
this
.
size
=
size
let
aliveCellStrings
:
string
[]
=
aliveCells
.
map
(
item
=>
item
.
toString
()
)
let
range
=
makeRange
(
size
)
for
(
let
a
of
range
)
{
for
(
let
b
of
range
)
{
let
coord
=
`
${
a
}
:
${
b
}
`
if
(
aliveCell
s
!==
null
&&
aliveCells
.
indexOf
(
coord
)
>=
0
)
{
this
.
cells
.
set
(
coord
,
Cell
.
alive
)
let
coord
=
new
Coord
(
a
,
b
)
if
(
aliveCell
Strings
.
includes
(
coord
.
toString
())
)
{
this
.
cells
.
set
(
coord
.
toString
()
,
Cell
.
alive
)
}
else
{
this
.
cells
.
set
(
coord
,
Cell
.
dead
)
this
.
cells
.
set
(
coord
.
toString
()
,
Cell
.
dead
)
}
}
}
}
evolve
():
World
{
let
alive
=
Array
<
string
>
()
let
alive
=
Array
<
Coord
>
()
let
range
=
[
-
1
,
0
,
1
]
this
.
cells
.
forEach
((
cell
,
coordText
)
=>
{
let
c
=
Coord
.
createF
romString
(
coordText
)
let
c
oord
=
Coord
.
f
romString
(
coordText
)
let
count
=
0
range
.
map
(
(
a
)
=>
{
count
+=
range
.
map
(
(
b
)
=>
{
return
`
${
c
.
x
+
a
}
:
${
c
.
y
+
b
}
`
}).
filter
(
(
c
)
=>
{
range
.
map
(
a
=>
{
count
+=
range
.
map
(
b
=>
{
return
(
new
Coord
(
coord
.
x
+
a
,
coord
.
y
+
b
)).
toString
()
}).
filter
(
c
=>
{
return
c
!=
coordText
}).
map
(
(
c
)
=>
{
}).
map
(
c
=>
{
return
this
.
cells
.
get
(
c
)
}).
filter
(
(
o
)
=>
{
}).
filter
(
o
=>
{
return
o
==
Cell
.
alive
}).
length
})
...
...
@@ -56,13 +57,13 @@ export class World {
switch
(
cell
)
{
case
Cell
.
alive
:
{
if
(
count
==
2
||
count
==
3
)
{
alive
.
push
(
coord
Text
)
alive
.
push
(
coord
)
}
break
}
case
Cell
.
dead
:
{
if
(
count
==
3
)
{
alive
.
push
(
coord
Text
)
alive
.
push
(
coord
)
}
break
}
...
...
@@ -96,52 +97,52 @@ export class World {
return
str
}
static
blinker
(
origin
:
Coord
):
string
[]
{
static
blinker
(
origin
:
Coord
):
Array
<
Coord
>
{
return
[
`
${
origin
.
x
}
:
${
origin
.
y
}
`
,
`
${
origin
.
x
+
1
}
:
${
origin
.
y
}
`
,
`
${
origin
.
x
+
2
}
:
${
origin
.
y
}
`
new
Coord
(
origin
.
x
,
origin
.
y
)
,
new
Coord
(
origin
.
x
+
1
,
origin
.
y
)
,
new
Coord
(
origin
.
x
+
2
,
origin
.
y
)
]
}
static
beacon
(
origin
:
Coord
):
string
[]
{
static
beacon
(
origin
:
Coord
):
Array
<
Coord
>
{
return
[
`
${
origin
.
x
}
:
${
origin
.
y
}
`
,
`
${
origin
.
x
+
1
}
:
${
origin
.
y
}
`
,
`
${
origin
.
x
}
:
${
origin
.
y
+
1
}
`
,
`
${
origin
.
x
+
1
}
:
${
origin
.
y
+
1
}
`
,
`
${
origin
.
x
+
2
}
:
${
origin
.
y
+
2
}
`
,
`
${
origin
.
x
+
3
}
:
${
origin
.
y
+
2
}
`
,
`
${
origin
.
x
+
2
}
:
${
origin
.
y
+
3
}
`
,
`
${
origin
.
x
+
3
}
:
${
origin
.
y
+
3
}
`
new
Coord
(
origin
.
x
,
origin
.
y
)
,
new
Coord
(
origin
.
x
+
1
,
origin
.
y
)
,
new
Coord
(
origin
.
x
,
origin
.
y
+
1
)
,
new
Coord
(
origin
.
x
+
1
,
origin
.
y
+
1
)
,
new
Coord
(
origin
.
x
+
2
,
origin
.
y
+
2
)
,
new
Coord
(
origin
.
x
+
3
,
origin
.
y
+
2
)
,
new
Coord
(
origin
.
x
+
2
,
origin
.
y
+
3
)
,
new
Coord
(
origin
.
x
+
3
,
origin
.
y
+
3
)
]
}
static
glider
(
origin
:
Coord
):
string
[]
{
static
glider
(
origin
:
Coord
):
Array
<
Coord
>
{
return
[
`
${
origin
.
x
+
2
}
:
${
origin
.
y
+
2
}
`
,
`
${
origin
.
x
+
1
}
:
${
origin
.
y
+
2
}
`
,
`
${
origin
.
x
}
:
${
origin
.
y
+
2
}
`
,
`
${
origin
.
x
+
2
}
:
${
origin
.
y
+
1
}
`
,
`
${
origin
.
x
+
1
}
:
${
origin
.
y
}
`
new
Coord
(
origin
.
x
+
2
,
origin
.
y
+
2
)
,
new
Coord
(
origin
.
x
+
1
,
origin
.
y
+
2
)
,
new
Coord
(
origin
.
x
,
origin
.
y
+
2
)
,
new
Coord
(
origin
.
x
+
2
,
origin
.
y
+
1
)
,
new
Coord
(
origin
.
x
+
1
,
origin
.
y
)
]
}
static
block
(
origin
:
Coord
):
string
[]
{
static
block
(
origin
:
Coord
):
Array
<
Coord
>
{
return
[
`
${
origin
.
x
}
:
${
origin
.
y
}
`
,
`
${
origin
.
x
+
1
}
:
${
origin
.
y
}
`
,
`
${
origin
.
x
}
:
${
origin
.
y
+
1
}
`
,
`
${
origin
.
x
+
1
}
:
${
origin
.
y
+
1
}
`
new
Coord
(
origin
.
x
,
origin
.
y
)
,
new
Coord
(
origin
.
x
+
1
,
origin
.
y
)
,
new
Coord
(
origin
.
x
,
origin
.
y
+
1
)
,
new
Coord
(
origin
.
x
+
1
,
origin
.
y
+
1
)
]
}
static
tub
(
origin
:
Coord
):
string
[]
{
static
tub
(
origin
:
Coord
):
Array
<
Coord
>
{
return
[
`
${
origin
.
x
+
1
}
:
${
origin
.
y
}
`
,
`
${
origin
.
x
}
:
${
origin
.
y
+
1
}
`
,
`
${
origin
.
x
+
2
}
:
${
origin
.
y
+
1
}
`
,
`
${
origin
.
x
+
1
}
:
${
origin
.
y
+
2
}
`
new
Coord
(
origin
.
x
+
1
,
origin
.
y
)
,
new
Coord
(
origin
.
x
,
origin
.
y
+
1
)
,
new
Coord
(
origin
.
x
+
2
,
origin
.
y
+
1
)
,
new
Coord
(
origin
.
x
+
1
,
origin
.
y
+
2
)
]
}
}
TypeScript/src/WorldView.ts
View file @
4bf8467a
...
...
@@ -35,10 +35,9 @@ export class WorldView {
this
.
context
.
lineWidth
=
3
this
.
context
.
strokeStyle
=
"
white
"
this
.
context
.
fillStyle
=
"
black
"
Object
.
keys
(
this
.
currentWorld
.
cells
).
forEach
(
coordText
=>
{
let
cell
=
this
.
currentWorld
.
cells
[
coordText
]
this
.
currentWorld
.
cells
.
forEach
((
cell
,
coordText
)
=>
{
if
(
cell
==
Cell
.
alive
)
{
let
coord
=
Coord
.
createF
romString
(
coordText
)
let
coord
=
Coord
.
f
romString
(
coordText
)
const
x
=
coord
.
x
*
horizontalStep
const
y
=
coord
.
y
*
verticalStep
this
.
context
?.
fillRect
(
x
,
y
,
horizontalStep
,
verticalStep
)
...
...
TypeScript/src/main_browser.ts
View file @
4bf8467a
...
...
@@ -2,12 +2,13 @@ import { World } from './World'
import
{
Coord
}
from
'
./Coord
'
import
{
WorldView
}
from
'
./WorldView
'
let
alive
=
World
.
blinker
(
new
Coord
(
0
,
1
))
alive
=
alive
.
concat
(
World
.
beacon
(
new
Coord
(
10
,
10
)))
alive
=
alive
.
concat
(
World
.
glider
(
new
Coord
(
4
,
5
)))
alive
=
alive
.
concat
(
World
.
block
(
new
Coord
(
1
,
10
)))
alive
=
alive
.
concat
(
World
.
block
(
new
Coord
(
18
,
3
)))
alive
=
alive
.
concat
(
World
.
tub
(
new
Coord
(
6
,
1
)))
let
blinker
=
World
.
blinker
(
new
Coord
(
0
,
1
))
let
beacon
=
World
.
beacon
(
new
Coord
(
10
,
10
))
let
glider
=
World
.
glider
(
new
Coord
(
4
,
5
))
let
block1
=
World
.
block
(
new
Coord
(
1
,
10
))
let
block2
=
World
.
block
(
new
Coord
(
18
,
3
))
let
tub
=
World
.
tub
(
new
Coord
(
6
,
1
))
let
alive
=
blinker
.
concat
(
beacon
).
concat
(
glider
).
concat
(
block1
).
concat
(
block2
).
concat
(
tub
)
let
canvas
=
document
.
getElementById
(
'
worldView
'
)
as
HTMLCanvasElement
let
generationLabel
=
document
.
getElementById
(
'
generationLabel
'
)
as
HTMLParagraphElement
...
...
@@ -15,8 +16,6 @@ let worldView = new WorldView(canvas)
let
world
=
new
World
(
30
,
alive
)
let
generation
=
0
console
.
warn
(
'
Starting
'
)
console
.
log
(
`
${
world
}
`
)
worldView
.
world
=
world
...
...
TypeScript/src/main_cli.ts
View file @
4bf8467a
...
...
@@ -14,12 +14,13 @@ const sleep = (ms : number) => {
})
}
let
alive
=
World
.
blinker
(
new
Coord
(
0
,
1
))
alive
=
alive
.
concat
(
World
.
beacon
(
new
Coord
(
10
,
10
)))
alive
=
alive
.
concat
(
World
.
glider
(
new
Coord
(
4
,
5
)))
alive
=
alive
.
concat
(
World
.
block
(
new
Coord
(
1
,
10
)))
alive
=
alive
.
concat
(
World
.
block
(
new
Coord
(
18
,
3
)))
alive
=
alive
.
concat
(
World
.
tub
(
new
Coord
(
6
,
1
)))
let
blinker
=
World
.
blinker
(
new
Coord
(
0
,
1
))
let
beacon
=
World
.
beacon
(
new
Coord
(
10
,
10
))
let
glider
=
World
.
glider
(
new
Coord
(
4
,
5
))
let
block1
=
World
.
block
(
new
Coord
(
1
,
10
))
let
block2
=
World
.
block
(
new
Coord
(
18
,
3
))
let
tub
=
World
.
tub
(
new
Coord
(
6
,
1
))
let
alive
=
blinker
.
concat
(
beacon
).
concat
(
glider
).
concat
(
block1
).
concat
(
block2
).
concat
(
tub
)
let
world
=
new
World
(
30
,
alive
)
let
generation
=
0
...
...
TypeScript/tests/tests.ts
View file @
4bf8467a
...
...
@@ -23,10 +23,10 @@ tape("Blinker", (t : any) => {
let
alive
=
World
.
blinker
(
new
Coord
(
0
,
1
));
let
original
=
new
World
(
3
,
alive
);
let
gen1
=
original
.
evolve
();
let
expectedAlive
:
string
[]
=
[
"
1:0
"
,
"
1:1
"
,
"
1:2
"
let
expectedAlive
:
Coord
[]
=
[
new
Coord
(
1
,
0
)
,
new
Coord
(
1
,
1
)
,
new
Coord
(
1
,
2
)
];
let
expected
=
new
World
(
3
,
expectedAlive
);
t
.
equal
(
JSON
.
stringify
(
expected
.
cells
),
JSON
.
stringify
(
gen1
.
cells
));
...
...
Write
Preview
Markdown
is supported
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