<!DOCTYPE html><html lang="ja">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<title>均等分割 × 表裏リング+ジャンプ(モバイル)</title>
<style>
:root { --bg:#0f172a; --card:#111827; --sub:#94a3b8; --text:#e5e7eb; --accent:#ffffff; }
*{box-sizing:border-box} body{margin:0;background:var(--bg);color:var(--text);font-family:system-ui, -apple-system, Segoe UI, Roboto, "Helvetica Neue", Arial}
.wrap{max-width:960px;margin:0 auto;padding:16px;}
h1{font-size:20px;margin:8px 0 12px}
.grid{display:grid;grid-template-columns:1fr;gap:12px}
@media(min-width:720px){.grid{grid-template-columns:1fr 1fr}}
.card{background:var(--card);border-radius:16px;padding:12px;box-shadow:0 6px 18px rgba(0,0,0,.25)}
.row{display:flex;align-items:center;gap:10px;margin:8px 0}
.row > label{font-size:14px;color:var(--sub);min-width:72px}
input[type="number"]{width:84px;padding:8px 10px;border-radius:10px;border:1px solid #334155;background:#0b1220;color:var(--text)}
input[type="range"]{width:100%}
select{padding:8px 10px;border-radius:10px;border:1px solid #334155;background:#0b1220;color:var(--text)}
button{padding:10px 14px;border-radius:999px;border:1px solid #334155;background:#0b1220;color:var(--text)}
button.primary{background:#2563eb;border-color:#2563eb}
button:active{transform:translateY(1px)}
.pies{display:grid;grid-template-columns:1fr 1fr;gap:12px}
.pieWrap{background:#0b1220;border-radius:16px;padding:8px;text-align:center}
small{display:block;color:var(--sub);margin-top:6px}
.history{max-height:40vh;overflow:auto;border:1px solid #334155;border-radius:12px;padding:10px;background:#0b1220;line-height:1.7}
.sticky-controls{position:sticky;top:0;background:linear-gradient(#0f172a, #0f172aEA 70%, transparent);padding-top:4px;padding-bottom:8px;z-index:1}
.hint{font-size:12px;color:#9ca3af}
</style>
</head>
<body>
<div class="wrap">
<h1>均等分割 × 表裏リング+ジャンプ(モバイル)</h1>
<div class="grid">
<div class="card">
<div class="sticky-controls">
<div class="row"><label>表の個数</label><input id="frontCount" type="number" min="1" max="24" value="6"></div>
<div class="row"><label>裏の個数</label><input id="backCount" type="number" min="1" max="24" value="4"></div>
<div class="row"><label>ジャンプ上限</label><input id="jumpLimit" type="number" min="0" max="30" value="2"></div>
<div class="row"><label>ジャンプ確率</label><input id="jumpProb" type="range" min="0" max="1" step="0.05" value="0.30"><span id="jumpProbVal">0.30</span></div>
<div class="row"><label>速度(ms)</label><input id="intervalMs" type="range" min="300" max="2500" step="50" value="900"><span id="intervalVal">900</span></div>
<div class="row">
<label>初動</label>
<select id="startSide">
<option value="front">表</option>
<option value="back">裏</option>
</select>
<span>セグメント</span>
<input id="startIndex" type="number" min="0" value="0">
<button id="apply" class="primary">適用</button>
</div>
<div class="row">
<button id="toggle">自動開始</button>
<button id="stepBtn">1ステップ</button>
<button id="reset">初期化</button>
</div>
</div>
<div class="pies">
<div class="pieWrap"><svg id="frontPie" width="240" height="240"></svg><small>表</small></div>
<div class="pieWrap"><svg id="backPie" width="240" height="240"></svg><small>裏</small></div>
</div>
</div>
<div class="card">
<div class="row" style="justify-content:space-between"><div>移動履歴</div><div class="hint">(シンプル表示)</div></div>
<div id="history" class="history"></div>
</div>
</div>
</div> <script>
// ===== Utilities =====
const TAU = Math.PI * 2;
const svgNS = "http://www.w3.org/2000/svg";
const $ = (id) => document.getElementById(id);
const hsl = (i) => `hsl(${(i*57)%360} 90% 55%)`;
function arcPath(cx, cy, r, a0, a1) {
const large = a1 - a0 > Math.PI ? 1 : 0;
const x0 = cx + r * Math.cos(a0), y0 = cy + r * Math.sin(a0);
const x1 = cx + r * Math.cos(a1), y1 = cy + r * Math.sin(a1);
return `M ${cx} ${cy} L ${x0} ${y0} A ${r} ${r} 0 ${large} 1 ${x1} ${y1} Z`;
}
function drawPie(svg, count, pointerIdx) {
while (svg.firstChild) svg.removeChild(svg.firstChild);
const size = Math.min(svg.viewBox.baseVal.width || svg.width.baseVal.value, svg.viewBox.baseVal.height || svg.height.baseVal.value);
const r = size/2; const cx = r, cy = r;
let a = -Math.PI/2;
for (let i=0;i<count;i++){
const start=a; const end=a+TAU/count; a=end;
const path = document.createElementNS(svgNS, 'path');
path.setAttribute('d', arcPath(cx,cy,r,start,end));
path.setAttribute('fill', hsl(i));
path.setAttribute('opacity', '0.9');
svg.appendChild(path);
}
if (pointerIdx != null && count>0){
const ang = -Math.PI/2 + (pointerIdx+0.5)/count*TAU;
const x = cx + r*0.75*Math.cos(ang);
const y = cy + r*0.75*Math.sin(ang);
const dot = document.createElementNS(svgNS, 'circle');
dot.setAttribute('cx', x); dot.setAttribute('cy', y);
dot.setAttribute('r', 6); dot.setAttribute('fill', '#fff');
dot.setAttribute('stroke', '#000'); dot.setAttribute('stroke-width', '1.5');
svg.appendChild(dot);
}
}
// ===== State =====
let frontCount = parseInt($("frontCount").value);
let backCount = parseInt($("backCount").value);
let total = () => Math.max(1, frontCount + backCount);
let combinedIndex = 0; // 0..frontCount-1 => 表, frontCount.. => 裏
let step = 0;
let running = false, timer = null;
let jumpLimit = parseInt($("jumpLimit").value);
let jumpsUsed = 0;
let jumpProb = parseFloat($("jumpProb").value);
let intervalMs = parseInt($("intervalMs").value);
const history = [];
function sideAndSeg() {
const idx = ((combinedIndex % total()) + total()) % total();
if (idx < frontCount) return { side:'front', seg: idx };
return { side:'back', seg: idx - frontCount };
}
function pushHistory(note=""){
const cur = sideAndSeg();
step++;
history.push(`${step}. [${cur.side==='front'?'表':'裏'}] セグメント ${cur.seg}${note? ' ← '+note: ''}`);
$("history").innerHTML = history.join("<br>");
}
function redraw(){
const cur = sideAndSeg();
drawPie($("frontPie"), frontCount, cur.side==='front'?cur.seg:null);
drawPie($("backPie"), backCount, cur.side==='back'?cur.seg:null);
$("jumpProbVal").textContent = jumpProb.toFixed(2);
$("intervalVal").textContent = String(intervalMs);
$("startIndex").max = (($("startSide").value==='front'? frontCount: backCount) - 1);
}
function tick(){
// ジャンプ(回数制限内のみ)
if (jumpsUsed < jumpLimit && Math.random() < jumpProb && total()>0){
const cur = sideAndSeg();
if (cur.side==='front' && backCount>0){
const mapped = Math.floor(cur.seg * backCount / Math.max(1, frontCount));
combinedIndex = frontCount + mapped;
jumpsUsed++;
pushHistory(`ジャンプ (${jumpsUsed}回目)`);
redraw();
return;
} else if (cur.side==='back' && frontCount>0){
const mapped = Math.floor(cur.seg * frontCount / Math.max(1, backCount));
combinedIndex = mapped;
jumpsUsed++;
pushHistory(`ジャンプ (${jumpsUsed}回目)`);
redraw();
return;
}
}
// 通常の横移動
combinedIndex = (combinedIndex + 1) % total();
pushHistory("");
redraw();
}
function start(){ if (running) return; running=true; $("toggle").textContent='一時停止'; timer=setInterval(tick, intervalMs); }
function stop(){ if (!running) return; running=false; $("toggle").textContent='自動開始'; clearInterval(timer); timer=null; }
// ===== Events =====
$("toggle").addEventListener('click', ()=> running? stop(): start());
$("stepBtn").addEventListener('click', tick);
$("reset").addEventListener('click', ()=>{ stop(); history.length=0; $("history").innerHTML=''; jumpsUsed=0; step=0; redraw(); });
$("frontCount").addEventListener('change', (e)=>{ frontCount=parseInt(e.target.value||'1'); redraw(); });
$("backCount").addEventListener('change', (e)=>{ backCount=parseInt(e.target.value||'1'); redraw(); });
$("jumpLimit").addEventListener('change', (e)=>{ jumpLimit=parseInt(e.target.value||'0'); });
$("jumpProb").addEventListener('input', (e)=>{ jumpProb=parseFloat(e.target.value||'0'); redraw(); });
$("intervalMs").addEventListener('input', (e)=>{ intervalMs=parseInt(e.target.value||'1000'); if (running){ stop(); start(); } redraw(); });
$("apply").addEventListener('click', ()=>{
const side = $("startSide").value;
const idx = Math.max(0, parseInt($("startIndex").value||'0'));
combinedIndex = (side==='front'? 0: frontCount) + Math.min(idx, (side==='front'? frontCount: backCount)-1);
history.length=0; $("history").innerHTML=''; jumpsUsed=0; step=0; redraw();
});
$("startSide").addEventListener('change', ()=>{ $("startIndex").value = 0; redraw(); });
// ===== init =====
pushHistory('初期位置');
redraw();
</script></body>
</html>