Skip to content
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
13 changes: 2 additions & 11 deletions src/iframe/life-game.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let patternWidth = 0;
let previewCells = [];

//盤面の大きさ
let boardSize = 20;
const boardSize = 20;
const cellSize = 450 / boardSize;

//セルの色
Expand Down Expand Up @@ -89,7 +89,6 @@ function renderBoard() {
}
}
rerender();
stop();
} else {
window.parent.postMessage(
{
Expand All @@ -105,7 +104,7 @@ function renderBoard() {
e.preventDefault();
if (!isPlacingTemplate) {
isDragging = true;
board[i][j] = board[i][j] === 1 ? 0 : 1;
board[i][j] = board[i][j] ? 0 : 1;
dragMode = board[i][j];
button.style.backgroundColor = board[i][j] ? aliveCellColor : deadCellColor;
}
Expand Down Expand Up @@ -169,14 +168,6 @@ function rerender() {
if (currentCellColor !== expectedCellColor) {
button.style.backgroundColor = expectedCellColor;
}

// セルサイズの更新
const currentCellsize = button.style.width;
const expectedCellsize = `${cellSize}px`;
if (currentCellsize !== expectedCellsize) {
button.style.width = expectedCellsize;
button.style.height = expectedCellsize;
}
}
}
}
Expand Down
53 changes: 15 additions & 38 deletions src/lib/assets/life-game-rules/lifespan.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

let timer = "stop";
let generationFigure = 0;
let isDragging = false;
let dragMode = 0; // 1: 黒にする, 0: 白にする
Expand All @@ -11,8 +10,8 @@ let patternWidth = 0;
let previewCells = [];

//変数設定
let boardSize = 20; //盤面の大きさ(20x20)
const cellSize = 600 / boardSize; //セルの大きさ(px)
const boardSize = 20; //盤面の大きさ(20x20)
const cellSize = 450 / boardSize; //セルの大きさ(px)
const maxLifespan = 2; // セルの最大寿命

// around: 周囲の生きたセル数, currentLifespan: 現在の寿命
Expand Down Expand Up @@ -54,6 +53,14 @@ const table = document.getElementById("game-board");

//盤面をBoardに従って変更する関数達(Boardを変更したら実行する)
function renderBoard() {
// bodyを中央配置に設定
document.body.style.display = "flex";
document.body.style.justifyContent = "center";
document.body.style.alignItems = "center";
document.body.style.minHeight = "100vh";
document.body.style.margin = "0";
document.body.style.padding = "0";

// 初回の盤面生成
table.innerHTML = "";
for (let i = 0; i < boardSize; i++) {
Expand Down Expand Up @@ -97,7 +104,6 @@ function renderBoard() {
}
rerender();
generationChange(0);
stop();
} else {
window.parent.postMessage(
{
Expand All @@ -111,16 +117,16 @@ function renderBoard() {
};
button.onmousedown = (e) => {
e.preventDefault();
if (timer === "stop" && !isPlacingTemplate) {
if (!isPlacingTemplate) {
isDragging = true;
board[i][j] = !board[i][j];
board[i][j] = board[i][j] ? 0 : 1;
dragMode = board[i][j];
button.lifespan = board[i][j] ? maxLifespan : 0;
button.style.backgroundColor = getStyle(board[i][j], button.lifespan);
}
};
button.onmouseenter = () => {
if (isDragging && timer === "stop" && board[i][j] !== dragMode && !isPlacingTemplate) {
if (isDragging && board[i][j] !== dragMode && !isPlacingTemplate) {
board[i][j] = dragMode;
button.lifespan = board[i][j] ? maxLifespan : 0;
button.style.backgroundColor = getStyle(board[i][j], button.lifespan);
Expand Down Expand Up @@ -180,14 +186,6 @@ function rerender() {
if (currentCellColor !== expectedCellColor) {
button.style.backgroundColor = expectedCellColor;
}

// セルサイズの更新
const currentCellsize = button.style.width;
const expectedCellsize = `${cellSize}px`;
if (currentCellsize !== expectedCellsize) {
button.style.width = expectedCellsize;
button.style.height = expectedCellsize;
}
}
}
}
Expand All @@ -197,7 +195,6 @@ document.addEventListener("mouseup", () => {
});

renderBoard();
progressBoard();

function generationChange(num) {
//現在の世代を表すgenerationFigureを変更し、文章も変更
Expand Down Expand Up @@ -271,14 +268,6 @@ on.progress = () => {
progressBoard();
};

on.play = () => {
timer = "start";
};

on.pause = () => {
timer = "stop";
};

on.board_reset = () => {
//すべて白にBoardを変更
board = Array.from({ length: boardSize }, () => Array.from({ length: boardSize }, () => 0));
Expand All @@ -295,18 +284,8 @@ on.board_randomize = () => {
generationChange(0);
};

on.request_sync = () => {
window.parent.postMessage(
{
type: "sync",
data: {
generationFigure: generationFigure,
boardSize: boardSize,
},
},
"*",
);
console.log("generationFigure:", generationFigure, "boardSize:", boardSize);
on.get_boardsize = () => {
window.parent.postMessage({ type: "get_boardsize", data: boardSize }, "*");
};

on.place_template = (template) => {
Expand All @@ -315,7 +294,6 @@ on.place_template = (template) => {
patternWidth = patternShape[0].length;
isPlacingTemplate = true;
table.style.cursor = "crosshair";
stop();
};

on.save_board = async () => {
Expand All @@ -326,5 +304,4 @@ on.apply_board = (newBoard) => {
board = newBoard;
renderBoard();
generationChange(0);
stop();
};
57 changes: 17 additions & 40 deletions src/lib/assets/life-game-rules/probabilistics.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

let timer = "stop";
let generationFigure = 0;
let isDragging = false;
let dragMode = 0; // 1: 黒にする, 0: 白にする
Expand All @@ -11,18 +10,18 @@ let patternWidth = 0;
let previewCells = [];

//変数設定
let boardSize = 20; //盤面の大きさ(20x20)
const cellSize = 600 / boardSize; //セルの大きさ(px)
const boardSize = 20; //盤面の大きさ(20x20)
const cellSize = 450 / boardSize; //セルの大きさ(px)

// around: 周囲の生きたセル数 self: 自身が生きているかどうか
function isNextAlive(around, self) {
// 自身が生きている & 周囲が 2 か 3 で生存 50%の確率でこのルールに従う
if (self && 2 <= around && around <= 3) {
return Math.random() > 0.5 ? self : !self;
return Math.random() > 0.5 ? 1 : 0;
}
// 自身が生きている & 周囲が2,3以外で死亡 50%の確率でこのルールに従う
if (self && (around < 2 || around > 3)) {
return Math.random() > 0.5 ? !self : self;
return Math.random() > 0.5 ? 0 : 1;
}
// 自身が死んでいる & 周囲が 3 で誕生 50%の確率でこのルールに従う
if (!self && around === 3) {
Expand All @@ -45,6 +44,14 @@ const table = document.getElementById("game-board");

//盤面をBoardに従って変更する関数達(Boardを変更したら実行する)
function renderBoard() {
// bodyを中央配置に設定
document.body.style.display = "flex";
document.body.style.justifyContent = "center";
document.body.style.alignItems = "center";
document.body.style.minHeight = "100vh";
document.body.style.margin = "0";
document.body.style.padding = "0";

// 初回の盤面生成
table.innerHTML = "";
for (let i = 0; i < boardSize; i++) {
Expand Down Expand Up @@ -80,7 +87,6 @@ function renderBoard() {
}
rerender();
generationChange(0);
stop();
} else {
window.parent.postMessage(
{
Expand All @@ -94,15 +100,15 @@ function renderBoard() {
};
button.onmousedown = (e) => {
e.preventDefault();
if (timer === "stop" && !isPlacingTemplate) {
if (!isPlacingTemplate) {
isDragging = true;
board[i][j] = !board[i][j];
board[i][j] = board[i][j] ? 0 : 1;
dragMode = board[i][j];
button.style.backgroundColor = board[i][j] ? "black" : "white";
}
};
button.onmouseenter = () => {
if (isDragging && timer === "stop" && board[i][j] !== dragMode && !isPlacingTemplate) {
if (isDragging && board[i][j] !== dragMode && !isPlacingTemplate) {
board[i][j] = dragMode;
button.style.backgroundColor = board[i][j] ? "black" : "white";
}
Expand Down Expand Up @@ -160,14 +166,6 @@ function rerender() {
if (currentCellColor !== expectedCellColor) {
button.style.backgroundColor = expectedCellColor;
}

// セルサイズの更新
const currentCellsize = button.style.width;
const expectedCellsize = `${cellSize}px`;
if (currentCellsize !== expectedCellsize) {
button.style.width = expectedCellsize;
button.style.height = expectedCellsize;
}
}
}
}
Expand All @@ -177,7 +175,6 @@ document.addEventListener("mouseup", () => {
});

renderBoard();
progressBoard();

function generationChange(num) {
//現在の世代を表すgenerationFigureを変更し、文章も変更
Expand Down Expand Up @@ -234,14 +231,6 @@ on.progress = () => {
progressBoard();
};

on.play = () => {
timer = "start";
};

on.pause = () => {
timer = "stop";
};

on.board_reset = () => {
//すべて白にBoardを変更
board = Array.from({ length: boardSize }, () => Array.from({ length: boardSize }, () => 0));
Expand All @@ -258,18 +247,8 @@ on.board_randomize = () => {
generationChange(0);
};

on.request_sync = () => {
window.parent.postMessage(
{
type: "sync",
data: {
generationFigure: generationFigure,
boardSize: boardSize,
},
},
"*",
);
console.log("generationFigure:", generationFigure, "boardSize:", boardSize);
on.get_boardsize = () => {
window.parent.postMessage({ type: "get_boardsize", data: boardSize }, "*");
};

on.place_template = (template) => {
Expand All @@ -278,7 +257,6 @@ on.place_template = (template) => {
patternWidth = patternShape[0].length;
isPlacingTemplate = true;
table.style.cursor = "crosshair";
stop();
};

on.save_board = async () => {
Expand All @@ -289,5 +267,4 @@ on.apply_board = (newBoard) => {
board = newBoard;
renderBoard();
generationChange(0);
stop();
};