game-of-death

antagonistic game of life  https://xi.github.io/game-of-death/
git clone https://git.ce9e.org/game-of-death.git

commit
70e889069060c15d964d97b31423b837d7ef0caf
parent
496a0c97dfd8b77fa1a8c712a99b76023068c124
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2019-10-20 10:49
mv game state to state.game

Diffstat

M src/index.js 38 ++++++++++++++++++++------------------
M src/logic.js 2 +-
M src/template.js 10 +++++-----

3 files changed, 26 insertions, 24 deletions


diff --git a/src/index.js b/src/index.js

@@ -7,11 +7,13 @@ let state, tree;
    7     7 
    8     8 const init = function(wrapper) {
    9     9     state = {
   10    -1         board: logic.setupBoard(),
   11    -1         playing: false,
   12    -1         steps: 0,
   13    -1         currentPlayer: 1,
   14    -1         sandbox: true,
   -1    10         game: {
   -1    11             board: logic.setupBoard(),
   -1    12             currentPlayer: 1,
   -1    13             playing: false,
   -1    14             steps: 0,
   -1    15             sandbox: true,
   -1    16         },
   15    17     };
   16    18     tree = template(state);
   17    19     const element = vdom.mount(tree);
@@ -35,11 +37,11 @@ const on = function(eventType, selector, fn) {
   35    37 };
   36    38 
   37    39 const play = function() {
   38    -1     if (!state.playing && !state.steps) {
   -1    40     if (!state.game.playing && !state.game.steps) {
   39    41         return;
   40    42     }
   41    -1     if (!state.playing) {
   42    -1         state.steps -= 1;
   -1    43     if (!state.game.playing) {
   -1    44         state.game.steps -= 1;
   43    45     }
   44    46     logic.calculateNextGen(state);
   45    47     update();
@@ -49,7 +51,7 @@ const play = function() {
   49    51 };
   50    52 
   51    53 on('mousedown', '.board-cell', function(state, event) {
   52    -1     if (state.playing || state.steps) {
   -1    54     if (state.game.playing || state.game.steps) {
   53    55         return;
   54    56     }
   55    57     if (event.buttons != 1) {
@@ -59,34 +61,34 @@ on('mousedown', '.board-cell', function(state, event) {
   59    61     const board = row.parentElement;
   60    62     const x = Array.prototype.indexOf.call(row.children, this);
   61    63     const y = Array.prototype.indexOf.call(board.children, row);
   62    -1     const currentPlayer = state.currentPlayer === constants.EMPTY ? constants.GAIA : state.currentPlayer;
   63    -1     if (state.board[y][x] === currentPlayer) {
   64    -1         state.board[y][x] = constants.EMPTY;
   -1    64     const currentPlayer = state.game.currentPlayer === constants.EMPTY ? constants.GAIA : state.game.currentPlayer;
   -1    65     if (state.game.board[y][x] === currentPlayer) {
   -1    66         state.game.board[y][x] = constants.EMPTY;
   65    67     } else {
   66    -1         state.board[y][x] = currentPlayer;
   -1    68         state.game.board[y][x] = currentPlayer;
   67    69     }
   68    70 });
   69    71 
   70    72 on('click', '.js-next-gen', function(state) {
   71    -1     if (state.playing || state.steps) {
   -1    73     if (state.game.playing || state.game.steps) {
   72    74         return;
   73    75     }
   74    -1     state.steps = document.querySelector('[name="steps"]').value;
   -1    76     state.game.steps = document.querySelector('[name="steps"]').value;
   75    77     play();
   76    78 });
   77    79 
   78    80 on('click', '.js-play', function(state) {
   79    -1     state.playing = !state.playing;
   -1    81     state.game.playing = !state.game.playing;
   80    82     play();
   81    83 });
   82    84 
   83    85 on('click', '.js-current-player', function(state) {
   84    -1     state.currentPlayer = (state.currentPlayer + 1) % constants.playerCount;
   -1    86     state.game.currentPlayer = (state.game.currentPlayer + 1) % constants.playerCount;
   85    87 });
   86    88 
   87    89 on('click', '.js-export', function(state) {
   88    90     const download = document.createElement('a');
   89    -1     const s = JSON.stringify(state.board);
   -1    91     const s = JSON.stringify(state.game.board);
   90    92     download.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(s);
   91    93     download.download = 'board.json';
   92    94     download.hidden = true;

diff --git a/src/logic.js b/src/logic.js

@@ -12,7 +12,7 @@ const setupBoard = function() {
   12    12 };
   13    13 
   14    14 const calculateNextGen = function(state) {
   15    -1     const board = state.board;
   -1    15     const board = state.game.board;
   16    16     const calcBoard = [];
   17    17 
   18    18     // Calculate every player seperatly

diff --git a/src/template.js b/src/template.js

@@ -4,7 +4,7 @@ const renderBoard = function(state) {
    4     4     return h(
    5     5         'div',
    6     6         {'class': 'board'},
    7    -1         state.board.map(row => h(
   -1     7         state.game.board.map(row => h(
    8     8             'div',
    9     9             {'class': 'board-row'},
   10    10             row.map(player => h(
@@ -16,20 +16,20 @@ const renderBoard = function(state) {
   16    16 };
   17    17 
   18    18 const renderControls = function(state) {
   19    -1     if (state.sandbox) {
   -1    19     if (state.game.sandbox) {
   20    20         return h('div', {'class': 'board-controls'}, [
   21    21             h('input', {type: 'range', value: 50, name: 'speed'}),
   22    22             h('input', {type: 'number', value: 1, name: 'steps'}),
   23    23             h('button', {'class': 'js-next-gen'}, 'Next Gen'),
   24    -1             h('button', {'class': 'js-play'}, state.playing ? 'Pause' : 'Play'),
   25    -1             h('button', {'class': 'js-current-player fg-' + state.currentPlayer}, 'Current Player'),
   -1    24             h('button', {'class': 'js-play'}, state.game.playing ? 'Pause' : 'Play'),
   -1    25             h('button', {'class': 'js-current-player fg-' + state.game.currentPlayer}, 'Current Player'),
   26    26             h('button', {'class': 'js-export'}, 'Export'),
   27    27         ]);
   28    28     } else {
   29    29         return h('div', {'class': 'board-controls'}, [
   30    30             h('input', {type: 'hidden', value: 50, name: 'speed'}),
   31    31             h('input', {type: 'hidden', value: 1, name: 'steps'}),
   32    -1             h('button', {'class': 'js-play'}, state.playing ? 'Pause' : 'Play'),
   -1    32             h('button', {'class': 'js-play'}, state.game.playing ? 'Pause' : 'Play'),
   33    33         ]);
   34    34     }
   35    35 };