commit b5c7603b611be3b241605ae59bf0c607c1f41509 Author: Kohei Watanabe Date: Wed Aug 23 23:48:09 2017 +0900 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3125540 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +build +yotta_targets +yotta_modules diff --git a/.yotta.json b/.yotta.json new file mode 100644 index 0000000..49df555 --- /dev/null +++ b/.yotta.json @@ -0,0 +1,5 @@ +{ + "build": { + "target": "bbc-microbit-classic-gcc,*" + } +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9775365 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2017 Kohei Watanabe. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9eb0ab8 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# microbit-life-of-game + +micro:bitでライフゲームが動きます + +## 動作手順 + +あらかじめ [Offline Development - micro:bit runtime](https://lancaster-university.github.io/microbit-docs/offline-toolchains/) などを参考に、yottaをインストールします + +``` +git clone https://github.com/kou029w/microbit-life-of-game +cd microbit-life-of-game +yt build +``` + +`./build/bbc-microbit-classic-gcc/source/microbit-life-of-game-combined.hex` をmicro:bitにコピーすると動作します diff --git a/module.json b/module.json new file mode 100644 index 0000000..c4388e8 --- /dev/null +++ b/module.json @@ -0,0 +1,10 @@ +{ + "name": "microbit-life-of-game", + "version": "0.0.1", + "license": "MIT", + "dependencies": { + "microbit": "lancaster-university/microbit" + }, + "targetDependencies": {}, + "bin": "./source" +} diff --git a/source/main.cpp b/source/main.cpp new file mode 100644 index 0000000..5a63183 --- /dev/null +++ b/source/main.cpp @@ -0,0 +1,58 @@ +#include "MicroBit.h" + +MicroBit uBit; +MicroBitImage field("0,1,0,0,0\n0,0,1,1,0\n1,1,0,0,0\n0,0,1,0,0\n0,0,0,0,0\n"); +enum Life {DIE = 0, LIVE = 255}; + +Life deadOrAlive(int x, int y) { + int survivor = 0; + + for (int row = (y > 0 ? -1 : 0); row <= (y < 4 ? 1 : 0); row++) { + for (int col = (x > 0 ? -1 : 0); col <= (x < 4 ? 1 : 0); col++) { + if (col == 0 && row == 0) + continue; + + if (field.getPixelValue(x + col, y + row)) + survivor++; + } + } + + if (field.getPixelValue(x, y)) { + if (survivor == 2 || survivor == 3) + return LIVE; + else + return DIE; + } else { + if (survivor == 3) + return LIVE; + else + return DIE; + } +} + +void fieldUpdate() { + MicroBitImage nextGeneration(5, 5); + + for (int y = 0; y < 5; y++) { + for (int x = 0; x < 5; x++) { + nextGeneration.setPixelValue(x, y, deadOrAlive(x, y)); + } + } + + field.paste(nextGeneration); +} + +void lifeOfGame() { + while(1) { + uBit.sleep(1000); + uBit.display.image.paste(field); + fieldUpdate(); + } +} + +int main() { + uBit.init(); + + while(1) + lifeOfGame(); +}