Created
September 11, 2017 04:52
-
-
Save thata/611dabfa4d10019790d94453f6ba4302 to your computer and use it in GitHub Desktop.
micro:bitでライフゲーム。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let cells = [ | |
[false, false, false, false, false], | |
[false, false, false, false, false], | |
[false, false, false, false, false], | |
[false, false, false, false, false], | |
[false, false, false, false, false], | |
] | |
function next_generation() { | |
let newCells = [ | |
[false, false, false, false, false], | |
[false, false, false, false, false], | |
[false, false, false, false, false], | |
[false, false, false, false, false], | |
[false, false, false, false, false], | |
] | |
for (let x = 0; x < 5; x++) { | |
for (let y = 0; y < 5; y++) { | |
newCells[y][x] = next_generation_cell(x, y) | |
} | |
} | |
cells = newCells | |
} | |
function next_generation_cell(x: number, y: number) { | |
if (is_alive(x, y)) { | |
if (neighbors(x, y) < 2 || neighbors(x, y) >= 4) { | |
// 2未満、4以上は死 | |
return false | |
} else { | |
// それ以外はそのまま | |
return true | |
} | |
} else { | |
if (neighbors(x, y) == 3) { | |
// 3の場合は誕生 | |
return true | |
} else { | |
// それ以外はそのまま | |
return false | |
} | |
} | |
} | |
function neighbors(x: number, y: number) { | |
let n = 0 | |
let _x = 0 | |
let _y = 0 | |
// left | |
_x = (x == 0) ? 4 : (x - 1) | |
_y = y | |
if (is_alive(_x, _y)) { | |
n += 1 | |
} | |
// right | |
_x = (x == 4) ? 0 : (x + 1) | |
_y = y | |
if (is_alive(_x, _y)) { | |
n += 1 | |
} | |
// up | |
_x = x | |
_y = (y == 0) ? 4 : y - 1 | |
if (is_alive(_x, _y)) { | |
n += 1 | |
} | |
// down | |
_x = x | |
_y = (y == 4) ? 0 : y + 1 | |
if (is_alive(_x, _y)) { | |
n += 1 | |
} | |
// upleft | |
_x = (x == 0) ? 4 : (x - 1) | |
_y = (y == 0) ? 4 : y - 1 | |
if (is_alive(_x, _y)) { | |
n += 1 | |
} | |
// upright | |
_x = (x == 4) ? 0 : (x + 1) | |
_y = (y == 0) ? 4 : y - 1 | |
if (is_alive(_x, _y)) { | |
n += 1 | |
} | |
// downleft | |
_x = (x == 0) ? 4 : (x - 1) | |
_y = (y == 4) ? 0 : y + 1 | |
if (is_alive(_x, _y)) { | |
n += 1 | |
} | |
// downright | |
_x = (x == 4) ? 0 : (x + 1) | |
_y = (y == 4) ? 0 : y + 1 | |
if (is_alive(_x, _y)) { | |
n += 1 | |
} | |
return n | |
} | |
function is_alive(x: number, y: number) { | |
return cells[y][x] | |
} | |
function show_cells() { | |
for (let x = 0; x < 5; x++) { | |
for (let y = 0; y < 5; y++) { | |
if (cells[y][x]) { | |
led.plot(x, y) | |
} else { | |
led.unplot(x, y) | |
} | |
} | |
} | |
} | |
function random() { | |
// ランダムで初期化 | |
for (let x = 0; x < 5; x++) { | |
for (let y = 0; y < 5; y++) { | |
cells[y][x] = Math.randomBoolean() | |
} | |
} | |
} | |
function glider() { | |
// グライダーで初期化 | |
cells = [ | |
[false, false, false, false, false], | |
[false, false, true, false, false], | |
[false, false, true, false, true], | |
[false, false, true, true, false], | |
[false, false, false, false, false], | |
] | |
} | |
// 初期値はグライダー | |
glider() | |
basic.forever(() => { | |
show_cells() | |
next_generation() | |
basic.pause(500) | |
}) | |
input.onButtonPressed(Button.A, () => { | |
glider() | |
}) | |
input.onButtonPressed(Button.B, () => { | |
random() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment