-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from sondregj/development
💡 Rule functions
- Loading branch information
Showing
13 changed files
with
206 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "@sondregj/conway", | ||
"version": "0.1.0", | ||
"description": "Conway's Game of Life in TypeScript", | ||
"version": "1.0.0", | ||
"description": "Cellular Automata in TypeScript", | ||
"author": "Sondre Gjellestad <[email protected]> (https://sondregjellestad.space)", | ||
"homepage": "https://github.com/sondregj/conway#readme", | ||
"repository": { | ||
|
@@ -38,6 +38,7 @@ | |
"dist/**/*" | ||
], | ||
"keywords": [ | ||
"game-of-life", "conway" | ||
"game-of-life", | ||
"conway" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { BoardTick, RuleFunction } from './types' | ||
|
||
import { conwayRules } from './rules' | ||
|
||
export const advance: BoardTick = (board, rules: RuleFunction = conwayRules) => ({ | ||
cells: board.cells.map((row, yIndex) => | ||
row | ||
.map((cell, xIndex) => rules(board, cell, xIndex, yIndex)) | ||
.map(alive => ({ alive })), | ||
), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,7 @@ | ||
import { Board, BoardTick, Cell } from './types' | ||
export { advance } from './advance' | ||
|
||
interface BoardInitializerConfig { | ||
random?: boolean | ||
} | ||
export { initializeBoard } from './initializer' | ||
export { conwayRules } from './rules' | ||
export { neighborCount } from './utils' | ||
|
||
export const initializeBoard = ( | ||
width: number, | ||
height: number, | ||
config: BoardInitializerConfig = {}, | ||
): Board => { | ||
const { random } = config | ||
|
||
const cells: Cell[][] = [] | ||
|
||
for (let y = 0; y < height; y++) { | ||
cells[y] = [] | ||
|
||
for (let x = 0; x < width; x++) { | ||
cells[y][x] = { | ||
alive: random ? Math.random() > 0.5 : false, | ||
} | ||
} | ||
} | ||
|
||
return { width, height, cells } | ||
} | ||
|
||
export const neighborCount = (board: Board, x: number, y: number): number => { | ||
const targets = [ | ||
[x - 1, y - 1], | ||
[x - 1, y], | ||
[x - 1, y + 1], | ||
[x, y - 1], | ||
[x, y + 1], | ||
[x + 1, y - 1], | ||
[x + 1, y], | ||
[x + 1, y + 1], | ||
] | ||
.filter( | ||
([xIndex, yIndex]) => | ||
Math.min(xIndex, yIndex) >= 0 && | ||
xIndex < board.width && | ||
yIndex < board.height, | ||
) | ||
.map(([xIndex, yIndex]) => ({ x: xIndex, y: yIndex })) | ||
|
||
return targets | ||
.map(target => board.cells[target.y][target.x]) | ||
.reduce((sum, cell) => (cell.alive ? sum + 1 : sum), 0) | ||
} | ||
|
||
export const advance: BoardTick = board => { | ||
const { width, height } = board | ||
|
||
const cells = board.cells.map((row, yIndex) => | ||
row.map((cell, xIndex) => { | ||
const neighbors: number = neighborCount(board, xIndex, yIndex) | ||
|
||
if (!cell.alive && neighbors === 3) { | ||
return { alive: true } | ||
} | ||
|
||
if (neighbors < 2) { | ||
return { alive: false } | ||
} | ||
|
||
if (neighbors > 3) { | ||
return { alive: false } | ||
} | ||
|
||
return { ...cell } | ||
}), | ||
) | ||
|
||
return { width, height, cells } | ||
} | ||
|
||
export { Board, BoardTick, Cell } from './types' | ||
export { Board, BoardTick, Cell, RuleFunction } from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Board, Cell } from './types' | ||
|
||
interface BoardInitializerConfig { | ||
random?: boolean | ||
} | ||
|
||
export const initializeBoard = ( | ||
width: number, | ||
height: number, | ||
config: BoardInitializerConfig = {}, | ||
): Board => { | ||
const { random } = config | ||
|
||
const cells: Cell[][] = [] | ||
|
||
for (let y = 0; y < height; y++) { | ||
cells[y] = [] | ||
|
||
for (let x = 0; x < width; x++) { | ||
cells[y][x] = { | ||
alive: random ? Math.random() > 0.5 : false, | ||
} | ||
} | ||
} | ||
|
||
return { cells } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { RuleFunction } from './types' | ||
import { neighborCount } from './utils' | ||
|
||
export const conwayRules: RuleFunction = (board, cell, x, y) => { | ||
const neighbors = neighborCount(board, x, y) | ||
|
||
if (!cell.alive && neighbors === 3) { | ||
return true | ||
} | ||
|
||
if (neighbors < 2) { | ||
return false | ||
} | ||
|
||
if (neighbors > 3) { | ||
return false | ||
} | ||
|
||
return cell.alive | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
// Data types | ||
export interface Cell { | ||
alive: boolean | ||
} | ||
|
||
export interface Board { | ||
width: number | ||
height: number | ||
|
||
cells: Cell[][] | ||
} | ||
|
||
// Transform functions | ||
export type BoardTick = (board: Board) => Board | ||
export type BoardTick = (board: Board, ...args: any[]) => Board | ||
|
||
export type RuleFunction = (board: Board, cell: Cell, x: number, y: number) => boolean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Board } from './types' | ||
|
||
export const neighborCount = (board: Board, x: number, y: number): number => { | ||
return [ | ||
[x - 1, y - 1], | ||
[x - 1, y], | ||
[x - 1, y + 1], | ||
[x, y - 1], | ||
[x, y + 1], | ||
[x + 1, y - 1], | ||
[x + 1, y], | ||
[x + 1, y + 1], | ||
] | ||
.filter( | ||
([xIndex, yIndex]) => | ||
Math.min(xIndex, yIndex) >= 0 && | ||
xIndex < board.cells[0].length && | ||
yIndex < board.cells.length, | ||
) | ||
.map(([xIndex, yIndex]) => ({ x: xIndex, y: yIndex })) | ||
.map(target => board.cells[target.y][target.x]) | ||
.reduce((sum, cell) => (cell.alive ? sum + 1 : sum), 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { advance, Board } from '../src' | ||
|
||
test('Advance function works as expected', () => { | ||
const world: Board = { | ||
cells: [ | ||
[{ alive: false }, { alive: false }, { alive: true }], | ||
[{ alive: true }, { alive: false }, { alive: true }], | ||
[{ alive: false }, { alive: true }, { alive: true }], | ||
], | ||
} | ||
|
||
const day1 = advance(world) | ||
|
||
const expected: Board = { | ||
cells: [ | ||
[{ alive: false }, { alive: true }, { alive: false }], | ||
[{ alive: false }, { alive: false }, { alive: true }], | ||
[{ alive: false }, { alive: true }, { alive: true }], | ||
], | ||
} | ||
|
||
expect(day1).toStrictEqual(expected) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Board, neighborCount } from '../src' | ||
|
||
test('Neighbor count function works as expected', () => { | ||
const world: Board = { | ||
cells: [ | ||
[{ alive: false }, { alive: false }, { alive: true }], | ||
[{ alive: true }, { alive: false }, { alive: true }], | ||
[{ alive: false }, { alive: true }, { alive: true }], | ||
], | ||
} | ||
|
||
const count = neighborCount(world, 1, 1) | ||
|
||
expect(count).toStrictEqual(5) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters