├── GoF.dart ├── LICENSE ├── readme.md └── screenshot.jpg /GoF.dart: -------------------------------------------------------------------------------- 1 | //Made with dartpad.dartlang.org <3 2 | import 'dart:async'; 3 | import 'package:flutter/material.dart'; 4 | 5 | final Color cellColor = Colors.lightBlue; 6 | final Color bgColor = new Color.fromARGB(255, 245, 245, 255); 7 | final TargetPlatform platform = TargetPlatform.android; 8 | 9 | List> cells; 10 | final width = 700.0, height = 700.0; 11 | final xRes = 50, yRes = 50; 12 | double cWidth, cHeight; 13 | bool paused = false, fillMode = true; 14 | 15 | void main() { 16 | runApp(GameOfLife()); 17 | } 18 | 19 | class GOLPainter extends CustomPainter { 20 | List> hiddenMat1 = new List>(xRes); 21 | List> hiddenMat2 = new List>(xRes); 22 | 23 | GOLPainter(double width, double height) { 24 | cWidth = width / xRes; 25 | cHeight = height / yRes; 26 | cells = hiddenMat1; 27 | 28 | for (var i = 0; i < xRes; i++) { 29 | hiddenMat1[i] = new List(yRes); 30 | hiddenMat2[i] = new List(yRes); 31 | for (var j = 0; j < yRes; j++) hiddenMat1[i][j] = false; 32 | } 33 | } 34 | 35 | void update() { 36 | if (paused) return; 37 | List> next = (cells == hiddenMat1 ? hiddenMat2 : hiddenMat1); 38 | for (var i = 0; i < xRes; i++) 39 | for (var j = 0; j < yRes; j++) { 40 | int nCount = countNeighbours(i, j); 41 | next[i][j] = !cells[i][j] && nCount == 3 || 42 | cells[i][j] && nCount >= 2 && nCount <= 3; 43 | } 44 | cells = next; 45 | } 46 | 47 | int countNeighbours(int x, int y) { 48 | int count = 0; 49 | for (var i = x - 1; i <= x + 1; i++) 50 | for (var j = y - 1; j <= y + 1; j++) { 51 | if (cells[(i + xRes) % xRes][(j + yRes) % yRes]) count++; 52 | } 53 | count -= (cells[x][y] ? 1 : 0); 54 | return count; 55 | } 56 | 57 | @override 58 | void paint(Canvas canvas, Size size) { 59 | canvas.drawRect(Offset(0, 0) & Size(cWidth * xRes, cHeight * yRes), 60 | Paint()..color = bgColor); 61 | for (var i = 0; i < xRes; i++) 62 | for (var j = 0; j < yRes; j++) { 63 | if (cells[i][j]) drawCell(canvas, i * cWidth, j * cHeight, cells[i][j]); 64 | } 65 | } 66 | 67 | @override 68 | bool shouldRepaint(GOLPainter oldDelegate) { 69 | return true; 70 | } 71 | 72 | void drawCell(Canvas canvas, num x, num y, bool cellState) { 73 | canvas.drawRect( 74 | Offset(x, y) & Size(cWidth, cHeight), Paint()..color = cellColor); 75 | } 76 | } 77 | 78 | class GameOfLife extends StatefulWidget { 79 | @override 80 | State createState() { 81 | return _GameOfLifeState(); 82 | } 83 | } 84 | 85 | class _GameOfLifeState extends State { 86 | IconData icon = Icons.pause; 87 | Timer timer; 88 | GOLPainter painter = GOLPainter(width, height); 89 | 90 | @override 91 | void initState() { 92 | timer = new Timer.periodic(Duration(milliseconds: 100), (Timer t) { 93 | setState(() { 94 | painter.update(); 95 | }); 96 | }); 97 | super.initState(); 98 | } 99 | 100 | void _pointerClick(PointerEvent e) { 101 | fillMode = 102 | !cells[e.localPosition.dx ~/ cWidth][e.localPosition.dy ~/ cHeight]; 103 | _pointerDraw(e); 104 | } 105 | 106 | void _pointerDraw(PointerEvent e) { 107 | cells[e.localPosition.dx ~/ cWidth][e.localPosition.dy ~/ cHeight] = 108 | fillMode; 109 | } 110 | 111 | @override 112 | Widget build(BuildContext context) { 113 | return MaterialApp( 114 | debugShowCheckedModeBanner: false, 115 | home: Scaffold( 116 | appBar: AppBar(title: Text("Conways Game of Flutter")), 117 | body: Container( 118 | constraints: BoxConstraints.expand(), 119 | child: Column( 120 | crossAxisAlignment: CrossAxisAlignment.center, 121 | mainAxisAlignment: MainAxisAlignment.center, 122 | children: [ 123 | Container( 124 | child: Listener( 125 | onPointerMove: _pointerDraw, 126 | onPointerDown: _pointerClick, 127 | child: SizedBox( 128 | width: width, 129 | height: height, 130 | child: CustomPaint( 131 | key: ValueKey(timer.tick), 132 | painter: painter, 133 | ), 134 | ), 135 | ), 136 | ), 137 | ], 138 | ), 139 | ), 140 | floatingActionButton: FloatingActionButton( 141 | child: new Icon(icon, size: 25.0), 142 | onPressed: () { 143 | paused = !paused; 144 | icon = icon == Icons.pause ? Icons.play_arrow : Icons.pause; 145 | }, 146 | ), 147 | ), 148 | ); 149 | } 150 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 SlaxXxX 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Conway's Game of Flutter 2 | 3 | Simple implementation of conway's game of life ( https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life ) 4 | 5 | 6 | 7 | Play with it on Dartpad: https://dartpad.dartlang.org/d651454148dd0e305999961c05cde575 8 | 9 | Add/Remove cells by clicking or dragging with your mouse. 10 | 11 | Pause/Resume the simulation to draw your piece of art in peace. 12 | -------------------------------------------------------------------------------- /screenshot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Slx-hub/ConwaysGameOfFlutter/76761949cabb9025ac533c97a79fcf96a9042ff5/screenshot.jpg --------------------------------------------------------------------------------