DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>五子棋游戏title>
<style>
.board {
display: grid;
grid-template-columns: repeat(15, 40px);
grid-template-rows: repeat(15, 40px);
gap: 1px;
background-color: #f0d9b5;
}
.cell {
width: 40px;
height: 40px;
background-color: #fff;
border: 1px solid #000;
cursor: pointer;
}
.cell:hover {
background-color: #ccc;
}
#reset-button {
display: block;
margin-top: 10px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
.player-one {
background-color: #000;
border-radius: 50%;
}
.player-two {
background-color: #fff;
border-radius: 50%;
}
style>
head>
<body>
<div class="board" id="board">
div>
<button id="reset-button">重新开始button>
<script>
const board = document.getElementById("board");
const resetButton = document.getElementById("reset-button");
const gridSize = 15;
const cellSize = 40;
let currentPlayer = "player-one";
let gameOver = false;
for (let row = 0; row < gridSize; row++) {
for (let col = 0; col < gridSize; col++) {
const cell = document.createElement("div");
cell.className = "cell";
cell.dataset.row = row;
cell.dataset.col = col;
cell.addEventListener("click", handleCellClick);
board.appendChild(cell);
}
}
function handleCellClick(event) {
if (gameOver) return;
const cell = event.target;
if (!cell.classList.contains("player-one") && !cell.classList.contains("player-two")) {
cell.classList.add(currentPlayer);
if (checkForWin(parseInt(cell.dataset.row), parseInt(cell.dataset.col))) {
showWinMessage();
} else {
currentPlayer = currentPlayer === "player-one" ? "player-two" : "player-one";
}
}
}
function showWinMessage() {
gameOver = true;
alert(currentPlayer === "player-one" ? "玩家一获胜!" : "玩家二获胜!");
}
function resetGame() {
const cells = document.querySelectorAll(".cell");
cells.forEach((cell) => {
cell.classList.remove("player-one", "player-two");
});
currentPlayer = "player-one";
gameOver = false;
}
resetButton.addEventListener("click", resetGame);
function checkForWin(row, col) {
const currentPlayerClass = currentPlayer === "player-one" ? "player-one" : "player-two";
if (
checkDirection(row, col, 0, 1, currentPlayerClass) ||
checkDirection(row, col, 0, -1, currentPlayerClass)
) {
showWinMessage();
return;
}
if (
checkDirection(row, col, 1, 0, currentPlayerClass) ||
checkDirection(row, col, -1, 0, currentPlayerClass)
) {
showWinMessage();
return;
}
if (
checkDirection(row, col, 1, 1, currentPlayerClass) ||
checkDirection(row, col, -1, -1, currentPlayerClass) ||
checkDirection(row, col, 1, -1, currentPlayerClass) ||
checkDirection(row, col, -1, 1, currentPlayerClass)
) {
showWinMessage();
return;
}
}
function checkDirection(row, col, rowDelta, colDelta, currentPlayerClass) {
let count = 0;
for (let i = 0; i < 5; i++) {
const newRow = row + i * rowDelta;
const newCol = col + i * colDelta;
if (
newRow >= 0 &&
newRow < gridSize &&
newCol >= 0 &&
newCol < gridSize &&
document.querySelector(`[data-row="${newRow}"][data-col="${newCol}"]`).classList.contains(currentPlayerClass)
) {
count++;
} else {
break;
}
}
return count >= 5;
}
script>
body>
html>

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172