html实现黑白棋

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>黑白棋</title>
    <style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
        }
        .status-bar {
            margin-bottom: 10px;
            font-size: 20px;
            font-weight: bold;
        }
        .board {
            display: grid;
            grid-template-columns: repeat(8, 50px);
            grid-gap: 1px;
            width: 400px; /* 8个单元格 * 50px + 7个间隙 * 1px */
        }
        .cell {
            width: 50px;
            height: 50px;
            background-color: #0A0;
            position: relative;
            border: 1px solid #000;
            cursor: pointer;
        }
        .disk {
            width: 100%;
            height: 100%;
            border-radius: 50%;
            position: absolute;
            top: 0;
            left: 0;
        }
        .black {
            background-color: #000;
        }
        .white {
            background-color: #FFF;
        }
    </style>
</head>
<body>
<div class="status-bar" id="statusBar">当前轮到:黑方</div>
<div id="board" class="board"></div>
<script>
    const boardSize = 8;
    let board = Array.from({length: boardSize}, () => Array(boardSize).fill(null));
    let currentPlayer = 'black';
    function initializeBoard() {
        const boardElement = document.getElementById('board');
        for (let i = 0; i < boardSize; i++) {
            for (let j = 0; j < boardSize; j++) {
                const cell = document.createElement('div');
                cell.className = 'cell';
                cell.dataset.row = i;
                cell.dataset.col = j;
                cell.addEventListener('click', cellClickHandler);
                boardElement.appendChild(cell);
            }
        }
        // 初始化棋盘中间的四个棋子
        placeDisk(3, 3, 'white');
        placeDisk(4, 4, 'white');
        placeDisk(3, 4, 'black');
        placeDisk(4, 3, 'black');
        drawBoard();
    }
    function drawBoard() {
        const boardElement = document.getElementById('board');
        boardElement.innerHTML = '';
        for (let i = 0; i < boardSize; i++) {
            for (let j = 0; j < boardSize; j++) {
                const cell = document.createElement('div');
                cell.className = 'cell';
                cell.dataset.row = i;
                cell.dataset.col = j;
                cell.addEventListener('click', cellClickHandler);
                if (board[i][j]) {
                    const disk = document.createElement('div');
                    disk.className = `disk ${board[i][j]}`;
                    cell.appendChild(disk);
                }
                boardElement.appendChild(cell);
            }
        }
    }
    function cellClickHandler(event) {
        const row = parseInt(event.target.dataset.row, 10);
        const col = parseInt(event.target.dataset.col, 10);
        makeMove(row, col);
    }
    function makeMove(x, y) {
        if (board[x][y] !== null) return; // 该位置已被占用
        const directions = [
            [-1, -1], [-1, 0], [-1, 1],
            [0, -1], [0, 1],
            [1, -1], [1, 0], [1, 1]
        ];
        let tilesToFlip = [];
        for (let [dx, dy] of directions) {
            let nx = x + dx;
            let ny = y + dy;
            let line = [];
            while (nx >= 0 && nx < boardSize && ny >= 0 && ny < boardSize) {
                if (board[nx][ny] === oppositeColor(currentPlayer)) {
                    line.push([nx, ny]);
                    nx += dx;
                    ny += dy;
                } else if (board[nx][ny] === currentPlayer) {
                    tilesToFlip = tilesToFlip.concat(line);
                    break;
                } else {
                    line = [];
                    break;
                }
            }
        }
        if (tilesToFlip.length > 0) {
            board[x][y] = currentPlayer;
            for (let [tx, ty] of tilesToFlip) {
                board[tx][ty] = currentPlayer;
            }
            currentPlayer = oppositeColor(currentPlayer);
            drawBoard();
            updateStatusBar();
        }
    }
    function placeDisk(x, y, color) {
        board[x][y] = color;
    }
    function oppositeColor(color) {
        return color === 'black' ? 'white' : 'black';
    }
    function updateStatusBar() {
        const statusBarElement = document.getElementById('statusBar');
        statusBarElement.textContent = `当前轮到:${currentPlayer === 'black' ? '黑方' : '白方'}`;
    }
    initializeBoard();
</script>
</body>
</html>
相关推荐
じòぴé南冸じょうげん2 小时前
若依框架favicon.ico缓存更新问题解决方案:本地生效但线上未更新
前端·javascript·前端框架·html
狮子座的男孩2 小时前
js基础高级:01、数据类型(typeof、instanceof、===的使用)、数据与变量与内存(定义、赋值与内存关系、引用变量赋值、js调函数传参)
前端·javascript·经验分享·数据类型·数据与变量与内存·赋值与内存关系·引用变量赋值
Cyclo-5 小时前
PDFJS 在React中的引入 使用组件打开文件流PDF
前端·react.js·pdf
椒盐螺丝钉7 小时前
Vue Router应用:组件跳转
前端·javascript·vue.js
顾安r7 小时前
11.20 开源APP
服务器·前端·javascript·python·css3
敲敲了个代码8 小时前
CSS 像素≠物理像素:0.5px 效果的核心密码是什么?
前端·javascript·css·学习·面试
少云清8 小时前
【软件测试】5_基础知识 _CSS
前端·css·tensorflow
倔强青铜三8 小时前
AI编程革命:React + shadcn/ui 将终结前端框架之战
前端·人工智能·ai编程
二川bro8 小时前
第57节:Three.js企业级应用架构
开发语言·javascript·架构
天外飞雨道沧桑9 小时前
前端开发 Cursor MCP 提效工具配置
前端·vscode·ai编程·开发工具·cursor