mixiユーザー(id:742325)

2026年01月15日08:44

35 view

一応退職日・・

 なんかシツコイとか突っ込まれそうだけど、ネタなしなんでw

 まぁ、離職票が入手できたら次の一手かな・・

 そういや、AI君、ライフゲームと脱出ゲームを合体したようなJavascripを
組み込んだHTMLを作らせて動かしてみたら、しばらく画面が勝手に進んで
少しだったら眺めてても面白かったかもしれないw

そのまま、コード貼ってみるw
HTMLの拡張子で保存すると開くなりw
仕様はコードをAIに尋ねると教えてくれると思う

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Life Trap - 永遠のカオス</title>
<style>
body { background: #1a1a1a; color: #eee; font-family: sans-serif; text-align: center; margin: 0; overflow: hidden; }
canvas { border: 2px solid #00ffff; background: #000; display: block; margin: 10px auto; box-shadow: 0 0 15px #00ffff; }
.controls { padding: 10px; background: #333; }
.status { color: #00ffff; font-family: monospace; font-size: 1.1em; margin: 5px; }
</style>
</head>
<body>

Wolves: 0 | Steps: 0 | Mode: Normal

<button id="startBtn" style="padding:8px 20px;">シミュレーション開始</button>

<canvas id="gameCanvas" width="600" height="600"></canvas>

<script>
window.onload = function() {
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const size = 12;
const cols = Math.floor(canvas.width / size);
const rows = Math.floor(canvas.height / size);
let grid = Array(cols).fill().map(() => Array(rows).fill(0));
let wolves = [];
let steps = 0;
let isRunning = false;
let timer = null;

function addWolf() {
const dirs = [{dx:1,dy:0},{dx:-1,dy:0},{dx:0,dy:1},{dx:0,dy:-1},{dx:1,dy:1},{dx:1,dy:-1},{dx:-1,dy:1},{dx:-1,dy:-1}];
const d = dirs[Math.floor(Math.random()*dirs.length)];
wolves.push({
x: Math.floor(Math.random() * cols),
y: Math.floor(Math.random() * rows),
lastDx: d.dx, lastDy: d.dy, straightCount: 0,
color: `hsl(${Math.random() * 360}, 100%, 60%)`,
alive: true, path: []
});
}

for(let i=0; i<7; i++) addWolf();

function countNeighbors(x, y) {
let sum = 0;
for (let i = -1; i < 2; i++) {
for (let j = -1; j < 2; j++) {
if (i === 0 && j === 0) continue;
let col = (x + i + cols) % cols;
let row = (y + j + rows) % rows;
sum += grid[col][row];
}
}
return sum;
}

function update() {
let nextGrid = grid.map(arr => [...arr]);

// 【ゆらぎ】1000歩ごとに過密ルールを切り替える
const isHardMode = (Math.floor(steps / 1000) % 2 === 0);
const densityLimit = isHardMode ? 4 : 3;

for (let x = 0; x < cols; x++) {
for (let y = 0; y < rows; y++) {
let neighbors = countNeighbors(x, y);
if (grid[x][y] === 1) {
if (neighbors < 2 || neighbors > densityLimit) nextGrid[x][y] = 0;
} else {
if (neighbors === 3) nextGrid[x][y] = 1;
}
}
}

wolves.forEach(w => {
if (!w.alive) return;
w.path.push({x: w.x, y: w.y});

if (steps % 4 === 0 && w.path.length >= 3) {
for (let i = 1; i <= 3; i++) {
let p = w.path[w.path.length - i];
nextGrid[p.x][p.y] = nextGrid[p.x][p.y] ? 0 : 1;
}
}

let bestMoves = [];
let minDanger = 9999;
const directions = [{dx:1,dy:0},{dx:-1,dy:0},{dx:0,dy:1},{dx:0,dy:-1},{dx:1,dy:1},{dx:1,dy:-1},{dx:-1,dy:1},{dx:-1,dy:-1}];

directions.forEach(d => {
let nx = (w.x + d.dx + cols) % cols;
let ny = (w.y + d.dy + rows) % rows;
let danger = countNeighbors(nx, ny) * 6;
if (nextGrid[nx][ny] === 1) danger += 200;
if (d.dx === w.lastDx && d.dy === w.lastDy) {
danger += (w.straightCount < 4) ? -30 : 100;
}

if (danger < minDanger) { minDanger = danger; bestMoves = [{x: nx, y: ny, dx: d.dx, dy: d.dy}]; }
else if (danger === minDanger) { bestMoves.push({x: nx, y: ny, dx: d.dx, dy: d.dy}); }
});

let move = bestMoves[Math.floor(Math.random() * bestMoves.length)];
if (nextGrid[move.x][move.y] === 1) {
// 【自爆】死ぬ瞬間に周囲を反転させてインパクトを残す
for(let i=-1; i<=1; i++) {
for(let j=-1; j<=1; j++) {
let ex = (w.x + i + cols) % cols;
let ey = (w.y + j + rows) % rows;
nextGrid[ex][ey] = nextGrid[ex][ey] ? 0 : 1;
}
}
w.alive = false;
// 【転生】少し経ってから新しい狼を投入
setTimeout(() => { if(isRunning) addWolf(); }, 2000);
} else {
if (move.dx === w.lastDx && move.dy === w.lastDy) w.straightCount++;
else w.straightCount = 0;
w.x = move.x; w.y = move.y;
w.lastDx = move.dx; w.lastDy = move.dy;
}
});

grid = nextGrid;
steps++;
const aliveCount = wolves.filter(w => w.alive).length;
document.getElementById('info').innerText = `Wolves: ${aliveCount}/${wolves.length} | Steps: ${steps} | Mode: ${isHardMode ? 'Hard (4)' : 'Soft (3)'}`;
draw();
}

function draw() {
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (let x = 0; x < cols; x++) {
for (let y = 0; y < rows; y++) {
if (grid[x][y]) {
ctx.fillStyle = (Math.floor(steps/1000)%2===0) ? '#ff6600' : '#00ff00';
ctx.fillRect(x * size, y * size, size - 1, size - 1);
}
}
}
wolves.forEach(w => {
if (w.alive) {
ctx.fillStyle = w.color;
ctx.beginPath();
ctx.arc(w.x * size + size/2, w.y * size + size/2, size/2, 0, Math.PI*2);
ctx.fill();
}
});
}

document.getElementById('startBtn').addEventListener('click', function() {
if (!isRunning) {
isRunning = true; this.innerText = "一時停止";
timer = setInterval(update, 60);
} else {
isRunning = false; this.innerText = "再開";
clearInterval(timer);
}
});
draw();
};
</script>
</body>
</html>
0 2

コメント

mixiユーザー

ログインしてコメントを確認・投稿する

<2026年01月>
日 月 火 水 木 金 土
    123
45678910
11121314151617
18192021222324
25262728293031