百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

哈哈哈,好神奇,AI生成的五子棋程序,不知道能不能运行

zhezhongyun 2025-08-06 23:59 54 浏览

<!DOCTYPE html>

<html lang="zh-CN">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>五子棋游戏</title>

<script src="https://cdn.tailwindcss.com"></script>

<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">

<script>

tailwind.config = {

theme: {

extend: {

colors: {

primary: '#8B5A2B',

secondary: '#D2B48C',

board: '#DEB887',

black: '#000000',

white: '#FFFFFF',

},

fontFamily: {

sans: ['Inter', 'system-ui', 'sans-serif'],

},

}

}

}

</script>

<style type="text/tailwindcss">

@layer utilities {

.content-auto {

content-visibility: auto;

}

.board-grid {

background-size: 100% 100%;

background-image: linear-gradient(to right, rgba(0,0,0,0.6) 1px, transparent 1px),

linear-gradient(to bottom, rgba(0,0,0,0.6) 1px, transparent 1px);

}

.piece-shadow {

filter: drop-shadow(0 4px 3px rgb(0 0 0 / 0.07)) drop-shadow(0 2px 2px rgb(0 0 0 / 0.06));

}

.piece-transition {

transition: all 0.2s ease-out;

}

.btn-hover {

transition: all 0.2s ease;

}

.btn-hover:hover {

transform: translateY(-2px);

box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);

}

}

</style>

</head>

<body class="bg-gray-100 min-h-screen flex flex-col items-center justify-center p-4 font-sans">

<div class="max-w-4xl w-full bg-white rounded-2xl shadow-xl overflow-hidden">

<div class="bg-primary text-white p-6 text-center">

<h1 class="text-[clamp(1.5rem,3vw,2.5rem)] font-bold">五子棋</h1>

<p class="text-secondary mt-2">经典对弈游戏</p>

</div>

<div class="p-6 md:p-8 flex flex-col md:flex-row gap-6">

<!-- 游戏区域 -->

<div class="flex-1 relative">

<div class="aspect-square bg-board rounded-lg shadow-lg overflow-hidden board-grid" style="background-size: calc(100% / 14) calc(100% / 14);">

<canvas id="gameCanvas" class="w-full h-full cursor-pointer"></canvas>

</div>

<div id="gameStatus" class="mt-4 p-3 bg-secondary/20 rounded-lg text-center">

<p id="statusText" class="font-medium">游戏开始! 黑棋先行</p>

</div>

</div>

<!-- 游戏控制和信息 -->

<div class="w-full md:w-80 flex flex-col gap-6">

<div class="bg-gray-50 rounded-lg p-5 shadow-sm">

<h2 class="text-lg font-semibold mb-3 flex items-center">

<i class="fa-solid fa-info-circle mr-2 text-primary"></i>游戏信息

</h2>

<div class="space-y-3">

<div class="flex items-center justify-between">

<span class="text-gray-600">当前回合</span>

<div class="flex items-center">

<div id="currentPlayer" class="w-6 h-6 rounded-full bg-black mr-2 piece-shadow"></div>

<span id="playerText">黑棋</span>

</div>

</div>

<div class="flex items-center justify-between">

<span class="text-gray-600">游戏时间</span>

<span id="gameTime" class="font-mono">00:00</span>

</div>

<div class="flex items-center justify-between">

<span class="text-gray-600">步数</span>

<span id="moveCount">0</span>

</div>

</div>

</div>

<div class="bg-gray-50 rounded-lg p-5 shadow-sm">

<h2 class="text-lg font-semibold mb-3 flex items-center">

<i class="fa-solid fa-crown mr-2 text-primary"></i>游戏规则

</h2>

<ul class="text-sm text-gray-600 space-y-2">

<li class="flex items-start">

<i class="fa-solid fa-circle text-xs mt-1.5 mr-2 text-primary"></i>

<span>黑棋和白棋轮流在棋盘上落子</span>

</li>

<li class="flex items-start">

<i class="fa-solid fa-circle text-xs mt-1.5 mr-2 text-primary"></i>

<span>先在横、竖或斜方向形成五子连线者获胜</span>

</li>

<li class="flex items-start">

<i class="fa-solid fa-circle text-xs mt-1.5 mr-2 text-primary"></i>

<span>点击棋盘上的交叉点放置棋子</span>

</li>

</ul>

</div>

<div class="flex gap-3">

<button id="restartBtn" class="flex-1 bg-primary hover:bg-primary/90 text-white py-3 px-4 rounded-lg font-medium btn-hover flex items-center justify-center">

<i class="fa-solid fa-refresh mr-2"></i>重新开始

</button>

<button id="undoBtn" class="flex-1 bg-gray-200 hover:bg-gray-300 text-gray-700 py-3 px-4 rounded-lg font-medium btn-hover flex items-center justify-center">

<i class="fa-solid fa-undo mr-2"></i>悔棋

</button>

</div>

</div>

</div>

<div class="bg-gray-50 p-4 text-center text-sm text-gray-500">

<p>(c) 2025 五子棋游戏 | 一个简单的 Web 游戏</p>

</div>

</div>

<!-- 胜利提示模态框 -->

<div id="winModal" class="fixed inset-0 bg-black/50 flex items-center justify-center z-50 hidden opacity-0 transition-opacity duration-300">

<div class="bg-white rounded-xl p-8 max-w-md w-full mx-4 transform transition-transform duration-300 scale-95">

<div class="text-center">

<div class="w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-4">

<i class="fa-solid fa-trophy text-3xl text-yellow-500"></i>

</div>

<h2 class="text-2xl font-bold mb-2" id="winnerText">黑棋获胜!</h2>

<p class="text-gray-600 mb-6">恭喜您赢得了这场精彩的比赛!</p>

<button id="newGameBtn" class="bg-primary hover:bg-primary/90 text-white py-3 px-8 rounded-lg font-medium btn-hover">

开始新游戏

</button>

</div>

</div>

</div>

<script>

document.addEventListener('DOMContentLoaded', () => {

// 游戏常量

const BOARD_SIZE = 15; // 15x15的棋盘

const CELL_SIZE = Math.min(window.innerWidth * 0.8 / BOARD_SIZE, window.innerHeight * 0.6 / BOARD_SIZE);

const PIECE_SIZE = CELL_SIZE * 0.8;

// 游戏状态

let gameBoard = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(0));

let currentPlayer = 1; // 1: 黑棋, 2: 白棋

let gameActive = true;

let moveHistory = [];

let gameTime = 0;

let timerInterval;

// DOM元素

const canvas = document.getElementById('gameCanvas');

const ctx = canvas.getContext('2d');

const statusText = document.getElementById('statusText');

const currentPlayerEl = document.getElementById('currentPlayer');

const playerText = document.getElementById('playerText');

const moveCountEl = document.getElementById('moveCount');

const gameTimeEl = document.getElementById('gameTime');

const restartBtn = document.getElementById('restartBtn');

const undoBtn = document.getElementById('undoBtn');

const winModal = document.getElementById('winModal');

const winnerText = document.getElementById('winnerText');

const newGameBtn = document.getElementById('newGameBtn');

// 设置Canvas尺寸

canvas.width = CELL_SIZE * (BOARD_SIZE - 1);

canvas.height = CELL_SIZE * (BOARD_SIZE - 1);

// 绘制棋盘

function drawBoard() {

ctx.clearRect(0, 0, canvas.width, canvas.height);

// 绘制网格线

ctx.strokeStyle = '#8B4513';

ctx.lineWidth = 1.5;

for (let i = 0; i < BOARD_SIZE; i++) {

// 水平线

ctx.beginPath();

ctx.moveTo(0, i * CELL_SIZE);

ctx.lineTo(canvas.width, i * CELL_SIZE);

ctx.stroke();

// 垂直线

ctx.beginPath();

ctx.moveTo(i * CELL_SIZE, 0);

ctx.lineTo(i * CELL_SIZE, canvas.height);

ctx.stroke();

}

// 绘制天元和星位

const starPoints = [

{x: 3, y: 3}, {x: 3, y: 11}, {x: 7, y: 7},

{x: 11, y: 3}, {x: 11, y: 11}

];

starPoints.forEach(point => {

ctx.beginPath();

ctx.arc(point.x * CELL_SIZE, point.y * CELL_SIZE, 4, 0, Math.PI * 2);

ctx.fillStyle = '#8B4513';

ctx.fill();

});

// 绘制棋子

for (let i = 0; i < BOARD_SIZE; i++) {

for (let j = 0; j < BOARD_SIZE; j++) {

if (gameBoard[i][j] !== 0) {

drawPiece(i, j, gameBoard[i][j]);

}

}

}

}

// 绘制棋子

function drawPiece(row, col, player) {

const x = col * CELL_SIZE;

const y = row * CELL_SIZE;

// 棋子阴影

ctx.beginPath();

ctx.arc(x, y, PIECE_SIZE / 2 + 2, 0, Math.PI * 2);

ctx.fillStyle = 'rgba(0, 0, 0, 0.2)';

ctx.fill();

// 棋子本体

ctx.beginPath();

ctx.arc(x, y, PIECE_SIZE / 2, 0, Math.PI * 2);

if (player === 1) {

// 黑棋 - 渐变效果

const gradient = ctx.createRadialGradient(

x - PIECE_SIZE / 6, y - PIECE_SIZE / 6, PIECE_SIZE / 10,

x, y, PIECE_SIZE / 2

);

gradient.addColorStop(0, '#555');

gradient.addColorStop(1, '#000');

ctx.fillStyle = gradient;

} else {

// 白棋 - 渐变效果

const gradient = ctx.createRadialGradient(

x - PIECE_SIZE / 6, y - PIECE_SIZE / 6, PIECE_SIZE / 10,

x, y, PIECE_SIZE / 2

);

gradient.addColorStop(0, '#fff');

gradient.addColorStop(1, '#ddd');

ctx.fillStyle = gradient;

}

ctx.fill();

// 棋子边缘

ctx.strokeStyle = player === 1 ? '#333' : '#ccc';

ctx.lineWidth = 1;

ctx.stroke();

}

// 检查胜利条件

function checkWin(row, col, player) {

const directions = [

[1, 0], // 水平

[0, 1], // 垂直

[1, 1], // 对角线

[1, -1] // 反对角线

];

for (const [dx, dy] of directions) {

let count = 1; // 当前位置已经有一个棋子

// 正向检查

for (let i = 1; i < 5; i++) {

const newRow = row + i * dy;

const newCol = col + i * dx;

if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE) {

break;

}

if (gameBoard[newRow][newCol] === player) {

count++;

} else {

break;

}

}

// 反向检查

for (let i = 1; i < 5; i++) {

const newRow = row - i * dy;

const newCol = col - i * dx;

if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE) {

break;

}

if (gameBoard[newRow][newCol] === player) {

count++;

} else {

break;

}

}

if (count >= 5) {

return true;

}

}

return false;

}

// 检查平局

function checkDraw() {

for (let i = 0; i < BOARD_SIZE; i++) {

for (let j = 0; j < BOARD_SIZE; j++) {

if (gameBoard[i][j] === 0) {

return false; // 还有空位,不是平局

}

}

}

return true; // 棋盘已满,平局

}

// 更新游戏状态显示

function updateGameStatus() {

if (gameActive) {

statusText.textContent = `游戏进行中 - ${currentPlayer === 1 ? '黑棋' : '白棋'}回合`;

currentPlayerEl.className = `w-6 h-6 rounded-full ${currentPlayer === 1 ? 'bg-black' : 'bg-white border border-gray-300'} mr-2 piece-shadow`;

playerText.textContent = currentPlayer === 1 ? '黑棋' : '白棋';

}

moveCountEl.textContent = moveHistory.length;

}

// 更新游戏时间

function updateGameTime() {

gameTime++;

const minutes = Math.floor(gameTime / 60).toString().padStart(2, '0');

const seconds = (gameTime % 60).toString().padStart(2, '0');

gameTimeEl.textContent = `${minutes}:${seconds}`;

}

// 开始计时

function startTimer() {

clearInterval(timerInterval);

timerInterval = setInterval(updateGameTime, 1000);

}

// 停止计时

function stopTimer() {

clearInterval(timerInterval);

}

// 显示胜利模态框

function showWinModal(winner) {

gameActive = false;

stopTimer();

winnerText.textContent = `${winner === 1 ? '黑棋' : '白棋'}获胜!`;

winModal.classList.remove('hidden');

// 添加动画效果

setTimeout(() => {

winModal.classList.add('opacity-100');

winModal.querySelector('div').classList.remove('scale-95');

winModal.querySelector('div').classList.add('scale-100');

}, 10);

}

// 隐藏胜利模态框

function hideWinModal() {

winModal.classList.remove('opacity-100');

winModal.querySelector('div').classList.remove('scale-100');

winModal.querySelector('div').classList.add('scale-95');

setTimeout(() => {

winModal.classList.add('hidden');

}, 300);

}

// 重置游戏

function resetGame() {

gameBoard = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(0));

currentPlayer = 1;

gameActive = true;

moveHistory = [];

gameTime = 0;

drawBoard();

updateGameStatus();

gameTimeEl.textContent = '00:00';

stopTimer();

startTimer();

hideWinModal();

}

// 悔棋

function undoMove() {

if (moveHistory.length === 0 || !gameActive) {

return;

}

const lastMove = moveHistory.pop();

gameBoard[lastMove.row][lastMove.col] = 0;

currentPlayer = lastMove.player; // 回到上一个玩家

drawBoard();

updateGameStatus();

}

// 点击棋盘事件

canvas.addEventListener('click', (e) => {

if (!gameActive) return;

const rect = canvas.getBoundingClientRect();

const scaleX = canvas.width / rect.width;

const scaleY = canvas.height / rect.height;

// 计算点击的格子坐标

const x = (e.clientX - rect.left) * scaleX;

const y = (e.clientY - rect.top) * scaleY;

const col = Math.round(x / CELL_SIZE);

const row = Math.round(y / CELL_SIZE);

// 检查坐标是否在棋盘内且为空

if (row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && gameBoard[row][col] === 0) {

// 落子

gameBoard[row][col] = currentPlayer;

moveHistory.push({row, col, player: currentPlayer});

// 添加落子动画效果

drawBoard();

// 检查是否胜利

if (checkWin(row, col, currentPlayer)) {

showWinModal(currentPlayer);

return;

}

// 检查是否平局

if (checkDraw()) {

gameActive = false;

stopTimer();

statusText.textContent = '游戏结束 - 平局!';

return;

}

// 切换玩家

currentPlayer = currentPlayer === 1 ? 2 : 1;

updateGameStatus();

}

});

// 鼠标悬停预览效果

canvas.addEventListener('mousemove', (e) => {

if (!gameActive) return;

const rect = canvas.getBoundingClientRect();

const scaleX = canvas.width / rect.width;

const scaleY = canvas.height / rect.height;

// 计算鼠标所在的格子坐标

const x = (e.clientX - rect.left) * scaleX;

const y = (e.clientY - rect.top) * scaleY;

const col = Math.round(x / CELL_SIZE);

const row = Math.round(y / CELL_SIZE);

// 清除之前的预览

drawBoard();

// 如果坐标在棋盘内且为空,绘制预览棋子

if (row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && gameBoard[row][col] === 0) {

ctx.beginPath();

ctx.arc(col * CELL_SIZE, row * CELL_SIZE, PIECE_SIZE / 2, 0, Math.PI * 2);

if (currentPlayer === 1) {

ctx.fillStyle = 'rgba(0, 0, 0, 0.3)';

} else {

ctx.fillStyle = 'rgba(255, 255, 255, 0.6)';

}

ctx.fill();

}

});

// 鼠标离开棋盘时重绘

canvas.addEventListener('mouseleave', () => {

drawBoard();

});

// 事件监听

restartBtn.addEventListener('click', resetGame);

undoBtn.addEventListener('click', undoMove);

newGameBtn.addEventListener('click', resetGame);


// 初始化游戏

drawBoard();

updateGameStatus();

startTimer();

});

</script>

</body>

</html>

相关推荐

VSCode中值得推荐的常用的23个高效前端插件(工具篇)(一)

VSCode是我们前端开发的一个强大的IDE,所以选择趁手好用的插件是提高开发效率,然后剩下的时间用来摸鱼是很有必要滴。工具篇(23)Chinese(Simplified)vscode我们都知道是...

高级前端进阶,用gulp提升你的开发效率

前言:这两天动手配置了一下gulp,发现gulp配置简单,构建速度快,在某些使用场景下还是个不错的选择,本文从零开始构建,到最后打包发布到生成环境。通过本文可以快速上手gulp,文末附送github源...

Chrome 110 3大新特性!CSS支持画中画!

大家好,很高兴又见面了,我是"前端进阶",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发!今天带着大家一起看看最新发布的Chrome1...

用html中If语句——判断ie浏览器的版本

if语句的代码的语法非常简单,,就是一个if判断语句来判断浏览器的类型和版本,应用类似<!--[iflteIE6]>和<![endif]-->语法结构包孕起来...

谷歌浏览器怎么开启无痕浏览_谷歌浏览器怎么开启无痕浏览模式

很多用户在使用谷歌浏览器时,不希望留下任何上痕迹,开启无痕浏览器是最好的选择。这个模式下可以更好的保护个人隐私记录,给你带来更加安全的冲浪体验,接下来就给大家详细介绍下谷歌浏览器的无痕浏览模式,希望对...

Linux命令那么多,其实只需要记住这些就足够了!

你好,这里是网络技术联盟站,我是瑞哥。Linux命令行是一个强大且灵活的工具,可以极大地提高用户的工作效率和系统管理能力。我们都知道,Linux命令非常多,但是在实际的工作中,日常使用到的命令并不多,...

Linux如何查看文件_linux如何查看文件大小

Linux如何查看目录下的所有文件?用ls(list)查看当前目录下的所有文件和子目录。Ls查看目录下的文件,怎么区分是目录还是文件呢?第一种方式,我们可以通过颜色来区分目录和文件。默认情况下,目录显...

Linux系统man命令使用详解_linux man命令详解

man命令是在Linux和Unix系统上用于查看系统手册页(manualpages)的工具。手册页提供了关于系统命令、函数和文件的详细文档。命令语法:man[选项][命令或主题]参数:[选项]...

linux ps命令详解_linux中ps

linux中ps只显示进程的静态快照,及瞬间的进程状态,它拥有众多的风格,可分为3组:UNIX风格,BSD风格,GNU风格,本文介绍UNIX风格的ps指令。参数ps[-aefFly][-ppid...

如何在 Linux 上查找系统硬件信息?hwinfo命令很强大!

hwinfo是一个功能强大的硬件信息查询工具,专为Linux系统设计。它能够提供系统中几乎所有硬件组件的详细信息,包括但不限于CPU、内存、硬盘、网络设备、USB设备、显卡、声卡等。与其他常...

Linux Shell 入门教程(二):常用命令大全与使用技巧

在上一节《理解Linux与Shell》中,我们了解了Linux是什么、Shell是什么以及常见的Shell类型。这一篇,我们将正式动手操作,掌握使用频率最高、最实用的Linux命令...

SpringBoot应用部署神器:可视化服务管理脚本让运维更轻松

在SpringBoot应用的生产环境部署中,传统的手动启停服务方式不仅效率低下,还容易出错。今天分享一个功能强大的可视化服务管理脚本,让SpringBoot应用的部署和运维变得简单高效。痛点分析:传统...

一次虚拟机性能问题导致的应用故障

最近我负责维护的一套语音平台出了问题。故障现象据客户反馈是转入IVR以后没有正常响应,客户无奈挂机了。老实说,刚开始接到用户反馈的时候,我是不太相信的。我们的系统平时运行运行很稳定,客户的并发数不大,...

linux中的常用命令_linux常用命令及含义

linux中的常用命令linux中的命令统称shell命令shell是一个命令行解释器,将用户命令解析为操作系统所能理解的指令,实现用户与操作系统的交互shell终端:我们平时输入命令,执行程序的那个...

linux学习笔记——常用命令-文件处理命令

ls目录处理命令:ls全名:list命令路径:/bin/ls执行权限:所有用户ls–ala--alll–long-i查看i节点ls–i查看i节点命令名称:mkdir命令英文原意:m...