Giter Site home page Giter Site logo

gameoflife's Introduction

gameOfLife:

https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life

All tests run clean and tests(processGrid.test.ts) show it successfully processes 3x3or 4x4.

3x3
// input:
// 0 1 0
// 1 1 1
// 0 0 0

// output:
// 1 1 1
// 1 1 1
// 0 1 0

4x4
// input:
// 0 1 0 0
// 1 0 1 1
// 0 0 0 1
// 1 1 0 1

// output:
// 0 1 1 0
// 0 1 1 1
// 1 0 0 1
// 0 0 1 0

Still needs a way to randomize an initial state and also calculating out of bounds dead cell recreation, but the app should take in an input grid and result in a successful alive snapshot of the cells.

usage

Install dependencies

npm install

Run tests

npm test

Build project

npm run script:build

Run project

npm run script:run

Sample output based on a grid size of 5 with gridGenerator(): ()

Index.ts:

import { gridGenerator } from './gridGenerator';
import { processGrid } from './processGrid';
import { Cell } from './Cell';

const awesomeThingsHappen = (grid: Array<Cell>, n: number): void => {
  const gr2 = processGrid(grid, n);
  console.log(gr2);
  setTimeout(() => {
    awesomeThingsHappen(gr2, n);
  }, 10000);
};

const [gr, n] = gridGenerator(5); // gridGenerator assigns a cell as being alive if the sum of x and y cooridnates equals an even number and dead if they sum upto an odd number
console.log('Initial grid: ', gr);
awesomeThingsHappen(gr, n);

Output:

[script:*run] Initial grid:  [
[script:*run]   Cell { alive: true, x: 0, y: 0 },
[script:*run]   Cell { alive: false, x: 0, y: 1 },
[script:*run]   Cell { alive: true, x: 0, y: 2 },
[script:*run]   Cell { alive: false, x: 0, y: 3 },
[script:*run]   Cell { alive: true, x: 0, y: 4 },
[script:*run]   Cell { alive: false, x: 1, y: 0 },
[script:*run]   Cell { alive: true, x: 1, y: 1 },
[script:*run]   Cell { alive: false, x: 1, y: 2 },
[script:*run]   Cell { alive: true, x: 1, y: 3 },
[script:*run]   Cell { alive: false, x: 1, y: 4 },
[script:*run]   Cell { alive: true, x: 2, y: 0 },
[script:*run]   Cell { alive: false, x: 2, y: 1 },
[script:*run]   Cell { alive: true, x: 2, y: 2 },
[script:*run]   Cell { alive: false, x: 2, y: 3 },
[script:*run]   Cell { alive: true, x: 2, y: 4 },
[script:*run]   Cell { alive: false, x: 3, y: 0 },
[script:*run]   Cell { alive: true, x: 3, y: 1 },
[script:*run]   Cell { alive: false, x: 3, y: 2 },
[script:*run]   Cell { alive: true, x: 3, y: 3 },
[script:*run]   Cell { alive: false, x: 3, y: 4 },
[script:*run]   Cell { alive: true, x: 4, y: 0 },
[script:*run]   Cell { alive: false, x: 4, y: 1 },
[script:*run]   Cell { alive: true, x: 4, y: 2 },
[script:*run]   Cell { alive: false, x: 4, y: 3 },
[script:*run]   Cell { alive: true, x: 4, y: 4 }
[script:*run] ]
[script:*run] [
[script:*run]   Cell { alive: true, x: 0, y: 1 },
[script:*run]   Cell { alive: true, x: 0, y: 2 },
[script:*run]   Cell { alive: true, x: 0, y: 3 },
[script:*run]   Cell { alive: true, x: 1, y: 0 },
[script:*run]   Cell { alive: true, x: 1, y: 4 },
[script:*run]   Cell { alive: true, x: 2, y: 0 },
[script:*run]   Cell { alive: true, x: 2, y: 4 },
[script:*run]   Cell { alive: true, x: 3, y: 0 },
[script:*run]   Cell { alive: true, x: 3, y: 4 },
[script:*run]   Cell { alive: true, x: 4, y: 1 },
[script:*run]   Cell { alive: true, x: 4, y: 2 },
[script:*run]   Cell { alive: true, x: 4, y: 3 }
[script:*run] ]
[script:*run] [
[script:*run]   Cell { alive: true, x: 0, y: 1 },
[script:*run]   Cell { alive: true, x: 0, y: 2 },
[script:*run]   Cell { alive: true, x: 0, y: 3 },
[script:*run]   Cell { alive: true, x: 1, y: 0 },
[script:*run]   Cell { alive: true, x: 1, y: 2 },
[script:*run]   Cell { alive: true, x: 1, y: 4 },
[script:*run]   Cell { alive: true, x: 2, y: 0 },
[script:*run]   Cell { alive: true, x: 2, y: 1 },
[script:*run]   Cell { alive: true, x: 2, y: 3 },
[script:*run]   Cell { alive: true, x: 2, y: 4 },
[script:*run]   Cell { alive: true, x: 3, y: 0 },
[script:*run]   Cell { alive: true, x: 3, y: 2 },
[script:*run]   Cell { alive: true, x: 3, y: 4 },
[script:*run]   Cell { alive: true, x: 4, y: 1 },
[script:*run]   Cell { alive: true, x: 4, y: 2 },
[script:*run]   Cell { alive: true, x: 4, y: 3 }
[script:*run] ]
[script:*run] [
[script:*run]   Cell { alive: true, x: 0, y: 1 },
[script:*run]   Cell { alive: true, x: 0, y: 2 },
[script:*run]   Cell { alive: true, x: 0, y: 3 },
[script:*run]   Cell { alive: true, x: 1, y: 0 },
[script:*run]   Cell { alive: true, x: 1, y: 4 },
[script:*run]   Cell { alive: true, x: 2, y: 0 },
[script:*run]   Cell { alive: true, x: 2, y: 4 },
[script:*run]   Cell { alive: true, x: 3, y: 0 },
[script:*run]   Cell { alive: true, x: 3, y: 4 },
[script:*run]   Cell { alive: true, x: 4, y: 1 },
[script:*run]   Cell { alive: true, x: 4, y: 2 },
[script:*run]   Cell { alive: true, x: 4, y: 3 }
[script:*run] ]
[script:*run] [
[script:*run]   Cell { alive: true, x: 0, y: 1 },
[script:*run]   Cell { alive: true, x: 0, y: 2 },
[script:*run]   Cell { alive: true, x: 0, y: 3 },
[script:*run]   Cell { alive: true, x: 1, y: 0 },
[script:*run]   Cell { alive: true, x: 1, y: 2 },
[script:*run]   Cell { alive: true, x: 1, y: 4 },
[script:*run]   Cell { alive: true, x: 2, y: 0 },
[script:*run]   Cell { alive: true, x: 2, y: 1 },
[script:*run]   Cell { alive: true, x: 2, y: 3 },
[script:*run]   Cell { alive: true, x: 2, y: 4 },
[script:*run]   Cell { alive: true, x: 3, y: 0 },
[script:*run]   Cell { alive: true, x: 3, y: 2 },
[script:*run]   Cell { alive: true, x: 3, y: 4 },
[script:*run]   Cell { alive: true, x: 4, y: 1 },
[script:*run]   Cell { alive: true, x: 4, y: 2 },
[script:*run]   Cell { alive: true, x: 4, y: 3 }
[script:*run] ]

...
...
(initial grid)
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1

(result a)
0 1 1 1 0
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
0 1 1 1 0

(result b)
0 1 1 1 0
1 0 1 0 1
1 1 0 1 1
1 0 1 0 1
0 1 1 1 0

(result a)
0 1 1 1 0
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
0 1 1 1 0

(result b)
0 1 1 1 0
1 0 1 0 1
1 1 0 1 1
1 0 1 0 1
0 1 1 1 0

(result a)
0 1 1 1 0
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
0 1 1 1 0

(result b)
0 1 1 1 0
1 0 1 0 1
1 1 0 1 1
1 0 1 0 1
0 1 1 1 0

....
....

gameoflife's People

Contributors

aperuru avatar chaitanyakolluru avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.