リバーシは、二人で対戦するボードゲームで、落ち着いた雰囲気の中で戦略的な思考を駆使して勝利を目指すことができます。以下が、実際に遊べるリバーシのコードです。
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>リバーシ</title>
<style>
td { width: 50px; height: 50px; border: 1px solid black;}
</style>
</head>
<body>
<h1>リバーシ</h1>
<div id="turn"></div>
<table>
<tbody>
<tr id="row1">
<td id="11" onclick="placePiece(this)"></td>
<td id="12" onclick="placePiece(this)"></td>
<td id="13" onclick="placePiece(this)"></td>
<td id="14" onclick="placePiece(this)"></td>
<td id="15" onclick="placePiece(this)"></td>
<td id="16" onclick="placePiece(this)"></td>
<td id="17" onclick="placePiece(this)"></td>
<td id="18" onclick="placePiece(this)"></td>
</tr>
<tr id="row2">
<td id="21" onclick="placePiece(this)"></td>
<td id="22" onclick="placePiece(this)"></td>
<td id="23" onclick="placePiece(this)"></td>
<td id="24" onclick="placePiece(this)"></td>
<td id="25" onclick="placePiece(this)"></td>
<td id="26" onclick="placePiece(this)"></td>
<td id="27" onclick="placePiece(this)"></td>
<td id="28" onclick="placePiece(this)"></td>
</tr>
<tr id="row3">
<td id="31" onclick="placePiece(this)"></td>
<td id="32" onclick="placePiece(this)"></td>
<td id="33" onclick="placePiece(this)"></td>
<td id="34" onclick="placePiece(this)"></td>
<td id="35" onclick="placePiece(this)"></td>
<td id="36" onclick="placePiece(this)"></td>
<td id="37" onclick="placePiece(this)"></td>
<td id="38" onclick="placePiece(this)"></td>
</tr>
<tr id="row4">
<td id="41" onclick="placePiece(this)"></td>
<td id="42" onclick="placePiece(this)"></td>
<td id="43" onclick="placePiece(this)"></td>
<td id="44" class="white" onclick="placePiece(this)"></td>
<td id="45" class="black" onclick="placePiece(this)"></td>
<td id="46" onclick="placePiece(this)"></td>
<td id="47" onclick="placePiece(this)"></td>
<td id="48" onclick="placePiece(this)"></td>
</tr>
<tr id="row5">
<td id="51" onclick="placePiece(this)"></td>
<td id="52" onclick="placePiece(this)"></td>
<td id="53" onclick="placePiece(this)"></td>
<td id="54" class="black" onclick="placePiece(this)"></td>
<td id="55" class="white" onclick="placePiece(this)"></td>
<td id="56" onclick="placePiece(this)"></td>
<td id="57" onclick="placePiece(this)"></td>
<td id="58" onclick="placePiece(this)"></td>
</tr>
<tr id="row6">
<td id="61" onclick="placePiece(this)"></td>
<td id="62" onclick="placePiece(this)"></td>
<td id="63" onclick="placePiece(this)"></td>
<td id="64" onclick="placePiece(this)"></td>
<td id="65" onclick="placePiece(this)"></td>
<td id="66" onclick="placePiece(this)"></td>
<td id="67" onclick="placePiece(this)"></td>
<td id="68" onclick="placePiece(this)"></td>
</tr>
<tr id="row7">
<td id="71" onclick="placePiece(this)"></td>
<td id="72" onclick="placePiece(this)"></td>
<td id="73" onclick="placePiece(this)"></td>
<td id="74" onclick="placePiece(this)"></td>
<td id="75" onclick="placePiece(this)"></td>
<td id="76" onclick="placePiece(this)"></td>
<td id="77" onclick="placePiece(this)"></td>
<td id="78" onclick="placePiece(this)"></td>
</tr>
<tr id="row8">
<td id="81" onclick="placePiece(this)"></td>
<td id="82" onclick="placePiece(this)"></td>
<td id="83" onclick="placePiece(this)"></td>
<td id="84" onclick="placePiece(this)"></td>
<td id="85" onclick="placePiece(this)"></td>
<td id="86" onclick="placePiece(this)"></td>
<td id="87" onclick="placePiece(this)"></td>
<td id="88" onclick="placePiece(this)"></td>
</tr>
</tbody>
</table>
<script>
let currentPlayer = "black";
let blackScore = 2;
let whiteScore = 2;
let piecesToFlip = [];
function placePiece(cell) {
if (cell.classList.length > 0) {
return;
}
piecesToFlip = [];
let row = cell.parentNode.rowIndex;
let col = cell.cellIndex;
let directionsToCheck = [[-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1]];
for (let i = 0; i < directionsToCheck.length; i++) {
let direction = directionsToCheck[i];
let tempPiecesToFlip = checkDirection(cell, direction[0], direction[1]);
piecesToFlip = piecesToFlip.concat(tempPiecesToFlip);
}
if (piecesToFlip.length == 0) {
return;
}
cell.classList.add(currentPlayer);
for (let i = 0; i < piecesToFlip.length; i++) {
piecesToFlip[i].classList.replace(getOpponent(currentPlayer), currentPlayer);
}
updateScore();
switchPlayers();
}
function checkDirection(cell, rowInc, colInc) {
let piecesToFlipInDirection = [];
let row = cell.parentNode.rowIndex;
let col = cell.cellIndex;
let opponentPlayer = getOpponent(currentPlayer);
let currentCell = document.getElementById((row + 1) + "" + (col + 1));
row += rowInc;
col += colInc;
while (row <= 7 && row >= 0 && col <= 7 && col >= 0) {
if (currentCell.classList.length == 0) {
return [];
} else if (currentCell.classList.contains(currentPlayer)) {
return piecesToFlipInDirection;
} else {
piecesToFlipInDirection.push(currentCell);
row += rowInc;
col += colInc;
currentCell = document.getElementById((row + 1) + "" + (col + 1));
}
}
return [];
}
function getOpponent(player) {
if (player == "black") {
return "white";
} else {
return "black";
}
}
function switchPlayers() {
if (currentPlayer == "black") {
currentPlayer = "white";
} else {
currentPlayer = "black";
}
document.getElementById("turn").innerText = "It's " + currentPlayer + "'s turn";
}
function updateScore() {
blackScore = 0;
whiteScore = 0;
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 8; j++) {
let cell = document.getElementById((i+1) + "" + (j+1));
if (cell.classList.contains("black")) {
blackScore++;
} else if (cell.classList.contains("white")) {
whiteScore++;
}
}
}
let totalScore = blackScore + whiteScore;
document.getElementById("blackScore").innerText = "Black: " + blackScore;
document.getElementById("whiteScore").innerText = "White: " + whiteScore;
if (totalScore == 64) {
if (blackScore > whiteScore) {
alert("Black wins!");
} else if (whiteScore > blackScore) {
alert("White wins!");
} else {
alert("Tie game!");
}
}
}
document.getElementById("turn").innerText = "It's " + currentPlayer + "'s turn";
document.getElementById("blackScore").innerText = "Black: " + blackScore;
document.getElementById("whiteScore").innerText = "White: " + whiteScore;
</script>
</body>
</html>
```
このコードでは、チェッカーのように、クリックした場所に黒か白のピースを置くだけです。ピースを置いたら、オプポーネントのピースの色を反転できます。同じ色の連続したピースをあちこちにおいてやって、最後に多い点数を取った方が勝者になります。
注意:このコードにはいくつかのセキュリティ上のリスクが存在する可能性があります。例えば、エバル関数はeval関数よりもかなり危険ですので、絶対に使用しないでください。また、このコードはテスト目的でのみ使用することをお勧めします。