156 lines
No EOL
3.4 KiB
HTML
156 lines
No EOL
3.4 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Strategy Grid</title>
|
|
<style>
|
|
body {
|
|
font-family: sans-serif;
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
margin-top: 30px;
|
|
}
|
|
|
|
#controls {
|
|
margin-bottom: 15px;
|
|
}
|
|
|
|
#stepBtn {
|
|
font-size: 14px;
|
|
padding: 6px 16px;
|
|
cursor: pointer;
|
|
}
|
|
|
|
#container {
|
|
display: flex;
|
|
flex-direction: row;
|
|
align-items: flex-start;
|
|
gap: 30px;
|
|
}
|
|
|
|
#grid {
|
|
display: grid;
|
|
gap: 1px;
|
|
background-color: #333;
|
|
border: 1px solid #333;
|
|
}
|
|
|
|
.cell {
|
|
width: 12px;
|
|
height: 12px;
|
|
}
|
|
|
|
#legend {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 10px;
|
|
padding-top: 4px;
|
|
}
|
|
|
|
.legend-item {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
font-size: 14px;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.legend-color {
|
|
width: 14px;
|
|
height: 14px;
|
|
display: inline-block;
|
|
border: 1px solid #333;
|
|
flex-shrink: 0;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<h1>Strategy Grid</h1>
|
|
<div id="controls">
|
|
<button id="stepBtn">Step</button>
|
|
</div>
|
|
<div id="container">
|
|
<div id="grid"></div>
|
|
<div id="legend"></div>
|
|
</div>
|
|
|
|
<script src="main.js"></script>
|
|
<script>
|
|
// This file only handles rendering.
|
|
// ALLC, ALLD, RAND, TFT, GRIM, MAJ and the Party class
|
|
// are already defined in script.js and are not redefined here.
|
|
|
|
// ---- Color / function mapping ----
|
|
|
|
const COLORS = {
|
|
allwaysCooperate: "#2ecc71", // green
|
|
allwaysDefect: "#e74c3c", // red
|
|
randomChoice: "#95a5a6", // gray
|
|
titForThat: "#3498db", // blue
|
|
grimTrigger: "#8e44ad", // purple
|
|
majority: "#f39c12" // orange
|
|
};
|
|
|
|
const LABELS = {
|
|
allwaysCooperate: "Always Cooperate",
|
|
allwaysDefect: "Always Defect",
|
|
randomChoice: "Random",
|
|
titForThat: "Tit-for-Tat",
|
|
grimTrigger: "Grim Trigger",
|
|
majority: "Majority"
|
|
};
|
|
|
|
// ---- Rendering ----
|
|
|
|
function renderGrid(party) {
|
|
const gridEl = document.getElementById("grid");
|
|
gridEl.innerHTML = "";
|
|
gridEl.style.gridTemplateColumns = `repeat(${party.length}, 12px)`;
|
|
|
|
for (let i = 0; i < party.length; i++) {
|
|
for (let j = 0; j < party.length; j++) {
|
|
const fn = party.grid[i][j];
|
|
const cell = document.createElement("div");
|
|
cell.className = "cell";
|
|
cell.style.backgroundColor = COLORS[fn.name] || "#000";
|
|
cell.title = LABELS[fn.name] || fn.name;
|
|
gridEl.appendChild(cell);
|
|
}
|
|
}
|
|
}
|
|
|
|
function renderLegend() {
|
|
const legendEl = document.getElementById("legend");
|
|
for (const key in COLORS) {
|
|
const item = document.createElement("div");
|
|
item.className = "legend-item";
|
|
|
|
const colorBox = document.createElement("span");
|
|
colorBox.className = "legend-color";
|
|
colorBox.style.backgroundColor = COLORS[key];
|
|
|
|
const label = document.createElement("span");
|
|
label.textContent = LABELS[key];
|
|
|
|
item.appendChild(colorBox);
|
|
item.appendChild(label);
|
|
legendEl.appendChild(item);
|
|
}
|
|
}
|
|
|
|
// ---- Execution ----
|
|
|
|
const party = new Party(60);
|
|
renderGrid(party);
|
|
renderLegend();
|
|
|
|
document.getElementById("stepBtn").addEventListener("click", () => {
|
|
party.step();
|
|
renderGrid(party);
|
|
});
|
|
</script>
|
|
|
|
</body>
|
|
</html> |