写一个人机对战的五子棋网页版
- User
写一个人机对战的五子棋网页版
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
写一个人机对战的五子棋网页版
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
下面是一个简单的人机对战五子棋网页版的实现,使用HTML、CSS和JavaScript编写。
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>五子棋</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="game-container">
<canvas id="game-canvas" width="600" height="600"></canvas>
<div class="game-info">
<button id="restart-button">重新开始</button>
<div id="game-status"></div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
style.css
.game-container {
width: 600px;
margin: 40px auto;
text-align: center;
}
#game-canvas {
border: 1px solid #ccc;
}
.game-info {
margin-top: 20px;
}
#restart-button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
#game-status {
font-size: 18px;
margin-top: 10px;
}
script.js
// 获取canvas元素
const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext('2d');
// 设置棋盘大小
const boardSize = 15;
const gridSize = 40;
// 初始化棋盘
let board = [];
for (let i = 0; i < boardSize; i++) {
board[i] = [];
for (let j = 0; j < boardSize; j++) {
board[i][j] = 0; // 0表示空,1表示黑子,2表示白子
}
}
// 绘制棋盘
function drawBoard() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < boardSize; i++) {
for (let j = 0; j < boardSize; j++) {
ctx.beginPath();
ctx.arc(i * gridSize + gridSize / 2, j * gridSize + gridSize / 2, 5, 0, 2 * Math.PI);
ctx.fillStyle = '#ccc';
ctx.fill();
}
}
for (let i = 0; i < boardSize; i++) {
ctx.beginPath();
ctx.moveTo(i * gridSize + gridSize / 2, gridSize / 2);
ctx.lineTo(i * gridSize + gridSize / 2, (boardSize - 1) * gridSize + gridSize / 2);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(gridSize / 2, i * gridSize + gridSize / 2);
ctx.lineTo((boardSize - 1) * gridSize + gridSize / 2, i * gridSize + gridSize / 2);
ctx.stroke();
}
}
// 绘制棋子
function drawPiece(x, y, type) {
ctx.beginPath();
ctx.arc(x * gridSize + gridSize / 2, y * gridSize + gridSize / 2, 15, 0, 2 * Math.PI);
ctx.fillStyle = type === 1 ? '#000' : '#fff';
ctx.fill();
}
// 判断是否获胜
function checkWin(x, y, type) {
let count = 0;
// 横向
for (let i = x - 4; i <= x + 4; i++) {
if (i >= 0 && i < boardSize && board[i][y] === type) {
count++;
if (count >= 5) return true;
} else {
count = 0;
}
}
count = 0;
// 纵向
for (let i = y - 4; i <= y + 4; i++) {
if (i >= 0 && i < boardSize && board[x][i] === type) {
count++;
if (count >= 5) return true;
} else {
count = 0;
}
}
count = 0;
// 斜向(左上到右下)
for (let i = -4; i <= 4; i++) {
if (x + i >= 0 && x + i < boardSize && y + i >= 0 && y + i < boardSize && board[x + i][y + i] === type) {
count++;
if (count >= 5) return true;
} else {
count = 0;
}
}
count = 0;
// 斜向(右上到左下)
for (let i = -4; i <= 4; i++) {
if (x + i >= 0 && x + i < boardSize && y - i >= 0 && y - i < boardSize && board[x + i][y - i] === type) {
count++;
if (count >= 5) return true;
} else {
count = 0;
}
}
return false;
}
// 处理用户落子
canvas.addEventListener('click', (e) => {
const x = Math.floor((e.clientX - canvas.offsetLeft) / gridSize);
const y = Math.floor((e.clientY - canvas.offsetTop) / gridSize);
if (board[x][y] === 0) {
board[x][y] = 1; // 用户执黑子
drawPiece(x, y, 1);
if (checkWin(x, y, 1)) {
document.getElementById('game-status').innerText = '你赢了!';
return;
}
// 电脑落子
let maxScore = -1;
let bestX, bestY;
for (let i = 0; i < boardSize; i++) {
for (let j = 0; j < boardSize; j++) {
if (board[i][j] === 0) {
let score = getScore(i, j, 2);
if (score > maxScore) {
maxScore = score;
bestX = i;
bestY = j;
}
}
}
}
board[bestX][bestY] = 2; // 电脑执白子
drawPiece(bestX, bestY, 2);
if (checkWin(bestX, bestY, 2)) {
document.getElementById('game-status').innerText = '电脑赢了!';
}
}
});
// 评估函数(简单实现)
function getScore(x, y, type) {
let score = 0;
// 检查是否有相邻的相同棋子
if (x > 0 && board[x - 1][y] === type) score++;
if (x < boardSize - 1 && board[x + 1][y] === type) score++;
if (y > 0 && board[x][y - 1] === type) score++;
if (y < boardSize - 1 && board[x][y + 1] === type) score++;
return score;
}
// 重新开始按钮事件处理
document.getElementById('restart-button').addEventListener('click', () => {
board = [];
for (let i = 0; i < boardSize; i++) {
board[i] = [];
for (let j = 0; j < boardSize; j++) {
board[i][j] = 0;
}
}
drawBoard();
document.getElementById('game-status').innerText = '';
});
drawBoard();
这个实现中,用户执黑子,电脑执白子。用户点击棋盘落子后,电脑会根据简单的评估函数选择最佳落子位置。游戏结束后,会显示获胜信息。用户可以点击“重新开始”按钮重新开始游戏。
注意,这个实现比较简单,电脑的AI比较弱,可以通过改进评估函数或使用更复杂的算法(如Minimax算法)来提高电脑的AI水平。