first commit

This commit is contained in:
Nebel 2017-08-23 23:48:09 +09:00
commit b5c7603b61
6 changed files with 112 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
build
yotta_targets
yotta_modules

5
.yotta.json Normal file
View file

@ -0,0 +1,5 @@
{
"build": {
"target": "bbc-microbit-classic-gcc,*"
}
}

21
LICENSE Normal file
View file

@ -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.

15
README.md Normal file
View file

@ -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にコピーすると動作します

10
module.json Normal file
View file

@ -0,0 +1,10 @@
{
"name": "microbit-life-of-game",
"version": "0.0.1",
"license": "MIT",
"dependencies": {
"microbit": "lancaster-university/microbit"
},
"targetDependencies": {},
"bin": "./source"
}

58
source/main.cpp Normal file
View file

@ -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();
}