diff options
| author | Nicolas Paul <n@nc0.fr> | 2024-03-07 11:49:36 +0100 | 
|---|---|---|
| committer | Nicolas Paul <n@nc0.fr> | 2024-03-08 23:12:55 +0100 | 
| commit | 39748129f1f6c142857fa12a815c08aa31931953 (patch) | |
| tree | ec0636cf68c25cf4adf963f3da86ac34c4f26002 /life2/game/index.js | |
| parent | 7c4e400fe9991af8f395dec12a047a450558178e (diff) | |
game: Define game Rules
Rules a simple mathematical fuctions that determine the next state of
the cells they are applied to.  By their nature, rules either return a
new state, or do not apply.
Our implementation of rules allow modularity, where a rules either
return a state (Cell type) to notice a change, or nothing (the null
object) to notice no change.
The merit of such an implementation is that the last choice is
ultimately done by the board, which increases the flexibility of
downstream users.
For instance, a standard implementation of the game could determine the
final new state of a cell by reducing all the rules output on the given
cell and choosing the most occuring one:
    const cell = ...;
    const neighbors = board.getNeighbors(cell);
    const states = rules.map(r => r.execute(cell, neighbords))
                        .filter(s => s != null);
    const empty = states.filter(s => s === Cell.EMPTY)
                        .length;
    const teamA = states.filter(s => s === Cell.TEAM_A)
                        .length;
    const teamB = states.filter(s => s === Cell.TEAM_B)
                        .length;
    const newState = Math.max(empty, teamA, teamB);
    boad.changeCell(cell, newState);
Note that this is some fake code, the final API may not look like this.
Signed-off-by: Nicolas Paul <n@nc0.fr>
Diffstat (limited to 'life2/game/index.js')
| -rw-r--r-- | life2/game/index.js | 1 | 
1 files changed, 1 insertions, 0 deletions
diff --git a/life2/game/index.js b/life2/game/index.js index f60d7ec..ce6b355 100644 --- a/life2/game/index.js +++ b/life2/game/index.js @@ -22,3 +22,4 @@   */  export {Cell} from './cell'; +export {Rule, RuleFunction} from './rule';  | 
