Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generation/5 #6

Merged
merged 5 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lifegame-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
"plugin:vue/essential",
"@vue/prettier"
],
"rules": {},
"rules": {
"no-console": "off"
},
"parserOptions": {
"parser": "babel-eslint"
},
Expand Down
16 changes: 12 additions & 4 deletions lifegame-vue/src/components/Board.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<div class="board">
<button @click="nextGeneration" class="generation">next generation</button>
<div v-for="(cellsState, n) in cellsStateArray" :key="n" class="row">
<div v-for="(cellState, m) in cellsState" :key="String(m)+n" class="col">
<Cell :state="cellState" />
Expand All @@ -11,19 +12,26 @@
<script>
import Cell from "@/components/Cell.vue";

import { calcNextGeneration } from "@/scripts/lifegame.js";

export default {
name: "Board",
data: function () {
data: function() {
return {
cellsStateArray: [...Array(100)].map(() => [...Array(100)].map(() => this.generateRandomState()))
}
cellsStateArray: [...Array(100)].map(() =>
[...Array(100)].map(() => this.generateRandomState())
)
};
},
components: {
Cell
},
methods: {
generateRandomState: function () {
generateRandomState: function() {
return Math.random() >= 0.5; //https://stackoverflow.com/a/36756480
},
nextGeneration: function() {
this.cellsStateArray = calcNextGeneration(this.cellsStateArray);
}
}
};
Expand Down
56 changes: 56 additions & 0 deletions lifegame-vue/src/scripts/lifegame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export function calcNextGeneration(statesArray) {
const size = statesArray.length;
const nextStatesArray = [...Array(size)].map(() => Array(size));
// 速度重視
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
const neighborhood = calcNeighborhood(statesArray, i, j);
const currentState = statesArray[i][j];
nextStatesArray[i][j] = calcnNextStateByNeighborhood(
currentState,
neighborhood
);
}
}
return nextStatesArray;
}

function calcNeighborhood(a, i, j) {
return [
accessState(a, i - 1, j - 1),
accessState(a, i - 1, j),
accessState(a, i - 1, j + 1),
accessState(a, i, j - 1),
accessState(a, i, j + 1),
accessState(a, i + 1, j - 1),
accessState(a, i + 1, j),
accessState(a, i + 1, j + 1)
];
}

function calcnNextStateByNeighborhood(currentState, neighborhood) {
const aliveNumber = neighborhood.filter(s => s).length;
if (currentState) {
if (2 <= aliveNumber && aliveNumber <= 3) {
return true;
} else if (aliveNumber <= 1 || 4 <= aliveNumber) {
return false;
}
} else {
if (aliveNumber === 3) {
return true;
}
return false;
}
}

function accessState(a, i, j) {
// 範囲外はdead扱いにしてるけど,後でトーラス的な位置関係にする
if (a[i] !== undefined) {
if (a[i][j] !== undefined) {
return a[i][j];
}
return false;
}
return false;
}
53 changes: 53 additions & 0 deletions lifegame-vue/tests/unit/example.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,57 @@ describe("Board.vue", () => {
const cellArray = wrapper.findAll(Cell);
expect(cellArray).toHaveLength(100 * 100);
});
describe("generation buttonについて", () => {
it("generation buttonを持っている", () => {
expect(wrapper.contains("button.generation")).toBe(true);
});
const stateTest = (testStates, expectState) => {
wrapper = mount(Board, {
propsData: { size: 3 }
});
wrapper.setData({ cellsStateArray: testStates });
wrapper.find("button.generation").trigger("click");
expect(wrapper.vm.cellsStateArray[1][1]).toBe(expectState);
};
describe("generation buttonを押したら世代が進む", () => {
it("過密は死", () => {
const dieStates = [
[true, true, true],
[true, true, false],
[false, false, false]
];
stateTest(dieStates, false);
});
it("誕生は生", () => {
const liveStates = [
[true, true, false],
[true, false, false],
[false, false, false]
];
stateTest(liveStates, true);
const liveStates2 = [
[true, true, false],
[false, false, false],
[false, false, false]
];
stateTest(liveStates2, false);
});
it("生存は生", () => {
const liveStates = [
[true, false, false],
[true, true, false],
[false, false, false]
];
stateTest(liveStates, true);
});
it("過疎は死", () => {
const liveStates = [
[false, false, false],
[true, true, false],
[false, false, false]
];
stateTest(liveStates, false);
});
});
});
});