スコア
X: 0 - O: 0以下が、JavaScriptを使ったまるばつゲームの例です。セキュリティ脆弱性がある部分は除去し、alertを使用しないようにしました。
```html
<!DOCTYPE html>
<html>
<head>
<title>まるばつゲーム</title>
<style type="text/css">
body {
font-family: Arial;
text-align: center;
}
.board {
margin: 0 auto;
display: flex;
flex-wrap: wrap;
width: 360px;
height: 360px;
border: 3px solid black;
border-radius: 10px;
overflow: hidden;
user-select: none;
}
.board div {
box-sizing: border-box;
width: 120px;
height: 120px;
font-size: 5rem;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.3s ease;
}
.board div:hover {
background-color: #ddd;
}
.board .x:before {
content: "×";
color: #f44336;
}
.board .o:before {
content: "○";
color: #2196f3;
}
.restart {
margin-top: 20px;
background-color: #2196f3;
color: white;
border: none;
border-radius: 5px;
padding: 10px 20px;
font-size: 1.2rem;
cursor: pointer;
transition: all 0.3s ease;
}
.restart:hover {
background-color: #0d8ef1;
}
.win-message {
font-size: 3rem;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="board"></div>
<button class="restart">もう一度遊ぶ</button>
<script type="text/javascript">
// ボードのセルの配列
const cells = Array.from(document.querySelectorAll(".board div"));
// OプレーヤーかXプレーヤーか
let currentPlayer = "x";
// 勝利条件
const winConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
// スコア
let xScore = 0;
let oScore = 0;
// 勝利判定
function checkWin(player) {
return winConditions.some(condition =>
condition.every(index => cells[index].classList.contains(player))
);
}
// 引き分け判定
function checkDraw() {
return cells.every(cell => cell.classList.contains("x") || cell.classList.contains("o"));
}
// マスクリック時の処理
function cellClicked(e) {
if (e.target.classList.contains("x") || e.target.classList.contains("o")) {
return; // 既にクリックされたセルは無視する
}
e.target.classList.add(currentPlayer); // クリックしたセルに「x」または「o」を追加する
if (checkWin(currentPlayer)) {
document.querySelector(".win-message").textContent = `${currentPlayer.toUpperCase()}の勝利!`; // 勝利メッセージを表示する
if (currentPlayer === "x") {
xScore++;
} else {
oScore++;
}
updateScore();
cellLockAll();
return; // 勝利したプレーヤーに処理を渡す
}
if (checkDraw()) {
document.querySelector(".win-message").textContent = "引き分け!"; // 引き分けメッセージを表示する
cellLockAll();
return; // 引き分けの場合は処理終了
}
// プレーヤー交替
currentPlayer = currentPlayer === "x" ? "o" : "x";
document.querySelector(".win-message").textContent = `${currentPlayer.toUpperCase()}の番です。`; // 現在のプレーヤーを表示する
}
// 「もう一度遊ぶ」ボタンクリック時の処理
function restartClicked() {
cells.forEach(cell => {
cell.classList.remove("x", "o"); // クリックしたセルに「x」または「o」を追加する
});
cellUnlockAll();
currentPlayer = "x";
document.querySelector(".win-message").textContent = `${currentPlayer.toUpperCase()}の番です。`; // 現在のプレーヤーを表示する
}
// 全てのセルロック(クリック不可能)にする
function cellLockAll() {
cells.forEach(cell => {
cell.style.cursor = "default";
});
}
// 全てのセルアンロック(クリック可能)にする
function cellUnlockAll() {
cells.forEach(cell => {
cell.style.cursor = "pointer";
});
}
// スコア表示を更新する
function updateScore() {
document.querySelector(".x-score").textContent = xScore;
document.querySelector(".o-score").textContent = oScore;
}
// イベントリスナーを設定する
cells.forEach(cell => {
cell.addEventListener("click", cellClicked);
});
document.querySelector(".restart").addEventListener("click", restartClicked);
document.querySelector(".win-message").textContent = `${currentPlayer.toUpperCase()}の番です。`;
</script>
<h2 class="win-message"></h2>
<p>スコア</p>
X: <span class="x-score">0</span> - O: <span class="o-score">0</span>
</body>
</html>
```
※このコードは、HTML、CSS、JavaScriptをすべて同一ファイルに記述しています。実際には、それぞれ別々のファイルに記述することが推奨されています。