3 分钟!AI 从零开发五子棋全过程曝光,网友:这效率我服了
zhezhongyun 2025-08-03 09:28 34 浏览
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>优雅五子棋</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 20px;
}
.container {
max-width: 800px;
width: 100%;
margin: 0 auto;
text-align: center;
}
h1 {
color: #2c3e50;
margin-bottom: 30px;
font-size: 2.5rem;
text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
}
.game-info {
display: flex;
justify-content: space-between;
width: 480px;
margin: 0 auto 20px;
font-size: 1.2rem;
color: #34495e;
}
.player-info {
padding: 10px 20px;
border-radius: 30px;
transition: all 0.3s ease;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.black {
background-color: #2c3e50;
color: white;
}
.white {
background-color: #ecf0f1;
color: #2c3e50;
border: 1px solid #bdc3c7;
}
.active {
transform: translateY(-5px);
box-shadow: 0 8px 15px rgba(0,0,0,0.2);
}
.board {
display: grid;
grid-template-columns: repeat(15, 32px);
grid-template-rows: repeat(15, 32px);
gap: 0;
margin: 0 auto;
background-color: #e8bb6e;
border: 2px solid #8d6e3a;
box-shadow: 0 10px 20px rgba(0,0,0,0.2);
border-radius: 4px;
position: relative;
}
.cell {
width: 32px;
height: 32px;
position: relative;
cursor: pointer;
}
.cell::before {
content: '';
position: absolute;
top: 50%;
left: 0;
width: 100%;
height: 1px;
background-color: #8d6e3a;
z-index: 1;
}
.cell::after {
content: '';
position: absolute;
left: 50%;
top: 0;
width: 1px;
height: 100%;
background-color: #8d6e3a;
z-index: 1;
}
.piece {
position: absolute;
width: 28px;
height: 28px;
border-radius: 50%;
top: 2px;
left: 2px;
z-index: 2;
box-shadow: 0 3px 5px rgba(0,0,0,0.3);
transition: transform 0.2s;
}
.piece.black {
background-color: #2c3e50;
background: radial-gradient(circle at 30% 30%, #34495e, #2c3e50);
}
.piece.white {
background-color: #ecf0f1;
background: radial-gradient(circle at 30% 30%, #ffffff, #bdc3c7);
}
.piece.last-move {
box-shadow: 0 0 0 2px #e74c3c, 0 3px 5px rgba(0,0,0,0.3);
}
.controls {
margin-top: 30px;
}
button {
background-color: #3498db;
color: white;
border: none;
padding: 12px 24px;
font-size: 1rem;
border-radius: 30px;
cursor: pointer;
margin: 0 10px;
transition: all 0.3s ease;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
button:hover {
background-color: #2980b9;
transform: translateY(-2px);
box-shadow: 0 6px 8px rgba(0,0,0,0.15);
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.5);
z-index: 100;
justify-content: center;
align-items: center;
}
.modal-content {
background-color: white;
padding: 40px;
border-radius: 10px;
text-align: center;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
max-width: 400px;
width: 90%;
}
.modal h2 {
color: #2c3e50;
margin-bottom: 20px;
}
.modal button {
margin-top: 20px;
}
@media (max-width: 600px) {
.board {
grid-template-columns: repeat(15, 20px);
grid-template-rows: repeat(15, 20px);
}
.cell {
width: 20px;
height: 20px;
}
.piece {
width: 18px;
height: 18px;
top: 1px;
left: 1px;
}
.game-info {
width: 300px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>优雅五子棋</h1>
<div class="game-info">
<div class="player-info black active" id="black-info">黑棋回合</div>
<div class="player-info white" id="white-info">白棋</div>
</div>
<div class="board" id="board"></div>
<div class="controls">
<button id="restart-btn">重新开始</button>
<button id="undo-btn">悔棋</button>
</div>
</div>
<div class="modal" id="win-modal">
<div class="modal-content">
<h2 id="winner-message">黑棋获胜!</h2>
<button id="play-again-btn">再玩一局</button>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const board = document.getElementById('board');
const blackInfo = document.getElementById('black-info');
const whiteInfo = document.getElementById('white-info');
const restartBtn = document.getElementById('restart-btn');
const undoBtn = document.getElementById('undo-btn');
const winModal = document.getElementById('win-modal');
const winnerMessage = document.getElementById('winner-message');
const playAgainBtn = document.getElementById('play-again-btn');
const BOARD_SIZE = 15;
let currentPlayer = 'black';
let gameBoard = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(null));
let gameOver = false;
let moveHistory = [];
// 初始化棋盘
function initBoard() {
board.innerHTML = '';
for (let i = 0; i < BOARD_SIZE; i++) {
for (let j = 0; j < BOARD_SIZE; j++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.dataset.row = i;
cell.dataset.col = j;
cell.addEventListener('click', handleCellClick);
board.appendChild(cell);
}
}
}
// 处理点击事件
function handleCellClick(e) {
if (gameOver) return;
const row = parseInt(e.target.dataset.row);
const col = parseInt(e.target.dataset.col);
if (gameBoard[row][col] !== null) return;
placePiece(row, col);
if (checkWin(row, col)) {
gameOver = true;
const winner = currentPlayer === 'black' ? '黑棋' : '白棋';
winnerMessage.textContent = `${winner}获胜!`;
winModal.style.display = 'flex';
} else {
currentPlayer = currentPlayer === 'black' ? 'white' : 'black';
updatePlayerInfo();
}
}
// 放置棋子
function placePiece(row, col) {
gameBoard[row][col] = currentPlayer;
moveHistory.push({ row, col, player: currentPlayer });
const cell = document.querySelector(`.cell[data-row="${row}"][data-col="${col}"]`);
const piece = document.createElement('div');
piece.className = `piece ${currentPlayer}`;
// 移除上一个棋子的last-move类
const lastMove = document.querySelector('.last-move');
if (lastMove) {
lastMove.classList.remove('last-move');
}
piece.classList.add('last-move');
cell.appendChild(piece);
// 添加动画效果
piece.style.transform = 'scale(0)';
setTimeout(() => {
piece.style.transform = 'scale(1)';
}, 10);
}
// 检查获胜
function checkWin(row, col) {
const directions = [
[0, 1], // 水平
[1, 0], // 垂直
[1, 1], // 对角线
[1, -1] // 反对角线
];
const player = gameBoard[row][col];
for (const [dx, dy] of directions) {
let count = 1;
// 正向检查
for (let i = 1; i < 5; i++) {
const newRow = row + i * dx;
const newCol = col + i * dy;
if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE || gameBoard[newRow][newCol] !== player) {
break;
}
count++;
}
// 反向检查
for (let i = 1; i < 5; i++) {
const newRow = row - i * dx;
const newCol = col - i * dy;
if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE || gameBoard[newRow][newCol] !== player) {
break;
}
count++;
}
if (count >= 5) {
return true;
}
}
return false;
}
// 更新玩家信息显示
function updatePlayerInfo() {
if (currentPlayer === 'black') {
blackInfo.classList.add('active');
whiteInfo.classList.remove('active');
} else {
blackInfo.classList.remove('active');
whiteInfo.classList.add('active');
}
}
// 重新开始游戏
function restartGame() {
gameBoard = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(null));
currentPlayer = 'black';
gameOver = false;
moveHistory = [];
updatePlayerInfo();
initBoard();
winModal.style.display = 'none';
}
// 悔棋
function undoMove() {
if (moveHistory.length === 0 || gameOver) return;
const lastMove = moveHistory.pop();
gameBoard[lastMove.row][lastMove.col] = null;
const cell = document.querySelector(`.cell[data-row="${lastMove.row}"][data-col="${lastMove.col}"]`);
const piece = cell.querySelector('.piece');
if (piece) {
piece.remove();
}
// 更新最后一个棋子的样式
const newLastMove = moveHistory[moveHistory.length - 1];
if (newLastMove) {
const newCell = document.querySelector(`.cell[data-row="${newLastMove.row}"][data-col="${newLastMove.col}"]`);
const newPiece = newCell.querySelector('.piece');
if (newPiece) {
newPiece.classList.add('last-move');
}
}
currentPlayer = lastMove.player;
updatePlayerInfo();
}
// 事件监听
restartBtn.addEventListener('click', restartGame);
undoBtn.addEventListener('click', undoMove);
playAgainBtn.addEventListener('click', restartGame);
// 初始化游戏
initBoard();
});
</script>
</body>
</html>
相关推荐
- Opinion丨Struggle Against U.S. Mind colonization in the Global South
-
Editor'snote:Thismonth,XinhuaNewsAgency'sThinkTankreleasedareporttitled"Colonizationof...
- 爱可可AI论文推介(2020.11.4)_爱可可女装旗舰店
-
LG-机器学习CV-计算机视觉CL-计算与语言AS-音频与语音RO-机器人(*表示值得重点关注)1、[LG]*CombiningLabelPropagationan...
- 何新:罗马伪史考英文版序言_罗马史学
-
2019-10-2514:48:27何新:罗马伪史考序言(英文译本)HeXin:PreambleofResearchonPseudo-historyofRome1Afewyear...
- XPeng Stock Rises Over 4% after Q2 Revenue and EV Margin Set Records
-
TMTPOST--TheAmericandepositaryreceipts(ADRs)ofXPengInc.rosearound4.2%onTuesdayaftert...
- 英汉世界语部首(八)_英文部首字典
-
本节讲八个部首,分别是:弓gōng【ECWLrad】bow廾gǒng【ECWLrad】twen广guǎng【ECWLrad】vast己jǐ【ECWLrad】self已yǐ...
- 一课译词:划水_划水是什么地方的方言
-
[Photo/SIPA]懒惰是人类的天性,因此才总有人会在工作时“划水”。“划水【huáshuǐ】”,本意是指“用胳膊划的动作(makestrokeswithone’sarms)”,延伸为“...
- 首测!GPT-4o做Code Review可行吗?
-
编辑|言征出品|51CTO技术栈(微信号:blog51cto)近日,OpenAI一记重拳,推出了GPT-4o(“o”表示“omni”),将语音识别和对话方面的优势展示的淋漓尽致。几乎可以肯定,...
- C++|漫谈STL细节及内部原理_c++ stl详解
-
1988年,AlexanderStepanov开始进入惠普的PaloAlto实验室工作,在随后的4年中,他从事的是有关磁盘驱动器方面的工作。直到1992年,由于参加并主持了实验室主任BillWo...
- C++ inline关键字深度解析:不止于优化的头文件定义许可
-
在C++开发中,几乎每个程序员都用过inline关键字,但多数人只停留在“内联优化”的表层理解。事实上,inline的真正威力在于它打破了C++的单一定义规则(ODR)限制,成为头文件中安全定义函数的...
- 实用 | 10分钟教你搭建一个嵌入式web服务器
-
之前分享的文章中提到了几种可以在嵌入式中使用的web服务器。嵌入式web服务器就是把web服务器移植到嵌入式系统的服务器。它仍然是基于http文本协议进行通信的,具有标准的接口形式,对客户端...
- 中间语言格式_中间格式文本是什么
-
在通常情况下,编译器会将目标语言转换成某种中间语言格式,而不是直接将源代码转换成二进制机器指令,不少c语言编译器,都会将代码编译成汇编语言,然后再通过汇编语言编译器将汇编代码转换成目标机器可执行的二进...
- 一线开发大牛带你深度解析探讨模板解释器,解释器的生成
-
解释器生成解释器的机器代码片段都是在TemplateInterpreterGenerator::generate_all()中生成的,下面将分小节详细展示该函数的具体细节,以及解释器某个组件的机器代码...
- 干货,Web开发和前端开发逆天工具大全
-
微信ID:WEB_wysj(点击关注)◎◎◎◎◎◎◎◎◎一┳═┻︻▄(点击页底“阅读原文”前往下载)●●●逆天工具CDN资源库国内Bootstrap中文网开源项目免费CDN服务36...
- 移动端rem+vw适配_移动端web页面适配方案
-
rem:rem是相对单位,设置根元素html的font-size,比如给html设置字体大小为100px,1rem=100px;rem缺点:1.和根元素font-size值强耦合,系统字...
- 从零搭建 React 开发 H5 模板_react html5
-
项目创建创建项目文件夹mkdir react-democd react-demonpm init -y依赖安装yarn add rea...
- 一周热门
- 最近发表
-
- Opinion丨Struggle Against U.S. Mind colonization in the Global South
- 爱可可AI论文推介(2020.11.4)_爱可可女装旗舰店
- 何新:罗马伪史考英文版序言_罗马史学
- XPeng Stock Rises Over 4% after Q2 Revenue and EV Margin Set Records
- 英汉世界语部首(八)_英文部首字典
- 一课译词:划水_划水是什么地方的方言
- 首测!GPT-4o做Code Review可行吗?
- C++|漫谈STL细节及内部原理_c++ stl详解
- C++ inline关键字深度解析:不止于优化的头文件定义许可
- 实用 | 10分钟教你搭建一个嵌入式web服务器
- 标签列表
-
- HTML 教程 (33)
- HTML 简介 (35)
- HTML 实例/测验 (32)
- HTML 测验 (32)
- JavaScript 和 HTML DOM 参考手册 (32)
- HTML 拓展阅读 (30)
- HTML文本框样式 (31)
- HTML滚动条样式 (34)
- HTML5 浏览器支持 (33)
- HTML5 新元素 (33)
- HTML5 WebSocket (30)
- HTML5 代码规范 (32)
- HTML5 标签 (717)
- HTML5 标签 (已废弃) (75)
- HTML5电子书 (32)
- HTML5开发工具 (34)
- HTML5小游戏源码 (34)
- HTML5模板下载 (30)
- HTTP 状态消息 (33)
- HTTP 方法:GET 对比 POST (33)
- 键盘快捷键 (35)
- 标签 (226)
- HTML button formtarget 属性 (30)
- opacity 属性 (32)
- transition 属性 (33)