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

3 分钟!AI 从零开发五子棋全过程曝光,网友:这效率我服了

zhezhongyun 2025-08-03 09:28 2 浏览

<!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>

相关推荐

3 分钟!AI 从零开发五子棋全过程曝光,网友:这效率我服了

<!DOCTYPEhtml><htmllang="zh-CN"><head><metacharset="UTF-8...

一行代码实现display&quot;过渡动画&quot;原理

作者:Peter谭老师转发链接:https://mp.weixin.qq.com/s/XhwPOv62gypzq5MhhP-5vg写本文的起因上篇文章,提到如何让display出现过渡动画,却没有仔...

脑洞:琼恩·雪诺、蝙蝠侠和魔形女的灵魂宠物了解一下

AlekseiVinogradovisaRussianfreelancedigitalartistwhoshareshisskillsandtalentwith120k...

浏览器的渲染机制、重绘、重排

1、什么是重排和重绘网页生成过程:HTML被HTML解析器解析成DOM树css则被css解析器解析成CSSOM树结合DOM树和CSSOM树,生成一棵渲染树(RenderTree)生成布局(flo...

托福写作高频考题写作思路&amp;词汇丨考虫独家

科技话题与媒体话题是托福写作的常考话题很多考生对这两类话题里的专有词汇表达也许很不了解所以今天就跟随考虫托福写作老师刘云龙老师一起来学习在这些话题的写作里你可以使用哪些有用的表达。希望大家有收获!记得...

在优麒麟上使用 Electron 开发桌面应用

使用Web标准来创建桌面GUI,上手快、成本低、跨平台、自适应分辨率,这些都是Electron的优势。作者/来源:优麒麟Electron是由Github开发,用HTML、CSS和...

php手把手教你做网站(三十八)jquery 转轮盘抽奖,开盲盒

抽奖和开盲盒性质一样的都是通过ajax读取后台的随机数据。1、转轮盘本来是想直接绘图实现轮盘,但是没有找到怎么填充文字,只好把轮盘弄成了背景图,通常用于游戏抽道具,商城积分抽奖,公司年末员工抽奖点击抽...

用 CSS 整活!3D 轮播图手把手教学,快乐代码敲出来

兄弟们,今天咱来搞点好玩的——用CSS整一个3D轮播图!咱野生程序员就是要在代码里找乐子,技术和快乐咱都得要!代码是写不完的,但咱能自己敲出快乐来,走起!一、先整个容器,搭个舞台咋先写一个...

实现一个超酷的 3D 立体卡片效 #前端开发

今天我们来实现一个超酷的3D立体卡片效果。正常情况下就是一个普通的图片展示卡片,鼠标悬停的时候图片会跳出卡片,并将影子投射到背景卡片上,在视觉上有一个3D立体感。html主要分成3个部分:容器→背景层...

Vue 3 Teleport与Suspense:解决UI难题的两个&quot;隐藏大招&quot;

模态框的"层级噩梦"与Teleport的救赎"这个模态框怎么又被父容器截断了?"团队协作开发后台系统时,小张第N次遇到这个问题。多层嵌套的组件结构里,弹窗被overfl...

让交互更加生动!有意思的鼠标跟随 3D 旋转动效

今天,群友问了这样一个问题,如下所示的鼠标跟随交互效果,如何实现:简单分析一下,这个交互效果主要有两个核心:借助了CSS3D的能力元素的旋转需要和鼠标的移动相结合本文,就将讲述如何使用纯CSS...

填坑:transform元素导致zindex失效终极方法

今天遇到了使用css3动画的元素层级被放大置顶的问题,ios浏览器上没问题,安卓原生浏览器和安卓微信上有问题。使用了css3动画的元素z-index失效,兄弟元素设置多高的z-index都盖不住解决办...

诡异的层级错乱:一个被transform隐藏的CSS陷阱

周五下午三点十七分,设计部突然发来紧急截图——原本应该悬浮在顶部的导航菜单,此刻正诡异地被下方的轮播图遮挡。我盯着屏幕上错乱的层级关系,手指下意识地敲下z-index:9999,心里清楚这不过是程序...

动画篇--碎片动画

本文授权转载,作者:Sindri的小巢(简书)前言从最开始动笔动画篇的博客,至今已经过去了四个多月。这段时间回头看了看自己之前的动画文章,发现用来讲解动画的例子确实不那么的赏心悦目。于是这段时间总是想...

Nature:大洋转换断层处的拉张构造与两阶段地壳增生

Nature:大洋转换断层处的拉张构造与两阶段地壳增生转换断层是三种基本的板块边界之一,全球总长度超过48000km(Bird,2003),它们的发现为板块构造理论的建立奠定了重要的基础(Wil...