Code Monkey home page Code Monkey logo

the-game-of-life's Introduction

Game of Life

This is an implementation of the Game of Life, a cellular automaton devised by British mathematician John Horton Conway in 1970. The game consists of a grid of cells, each of which can be in one of two states: alive or dead. The state of a cell at time t is determined by the state of its eight neighbors at time t-1, according to the following rules:

A dead cell with exactly three live neighbors becomes a live cell (birth). A live cell with two or three live neighbors survives (survival). In all other cases, a cell dies or remains dead (death). The implementation is written in JavaScript using the p5.js library, and is based on a 2D array representing the grid of cells.

Usage

To run the code, you can copy and paste it into an HTML file with the p5.js library included, or you can run it using the p5.js web editor at editor.p5js.org.

Code Explanation

make2DArray(cols, rows): a function that creates a 2D array with cols columns and rows rows, filled with undefined values. setup(): a p5.js function that runs once at the beginning of the sketch, and sets up the canvas and the initial state of the grid. draw(): a p5.js function that runs repeatedly in a loop, and updates the state of the grid and draws it on the canvas. countNeighbours(grid, x, y): a function that counts the number of live neighbors of the cell at position (x, y) in the grid. next: a new 2D array that represents the next state of the grid, computed based on the current state of the grid. The rules of the Game of Life are implemented in the draw() function, where each cell is checked against its neighbors to determine its next state.

Modification

You can modify the resolution of the grid by changing the value of the res variable, and you can modify the size of the canvas by changing the arguments of the createCanvas() function in the setup() function. You can also modify the rules of the game by changing the conditions in the if...else if...else block in the draw() function.

alt_text alt_text

the-game-of-life's People

Contributors

peterspeeder avatar

Stargazers

 avatar Liarley Dantas 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.