当前位置:首页 > JavaScript

js实现棋盘

2026-04-05 08:23:29JavaScript

使用HTML和JavaScript创建棋盘

创建一个棋盘可以通过HTML的表格元素结合JavaScript动态生成。以下是实现方法:

js实现棋盘

<!DOCTYPE html>
<html>
<head>
    <title>棋盘</title>
    <style>
        table {
            border-collapse: collapse;
        }
        td {
            width: 50px;
            height: 50px;
            border: 1px solid black;
        }
        .black {
            background-color: black;
        }
        .white {
            background-color: white;
        }
    </style>
</head>
<body>
    <div id="chessboard"></div>

    <script>
        function createChessboard(size) {
            const board = document.getElementById('chessboard');
            const table = document.createElement('table');

            for (let i = 0; i < size; i++) {
                const row = document.createElement('tr');
                for (let j = 0; j < size; j++) {
                    const cell = document.createElement('td');
                    cell.className = (i + j) % 2 === 0 ? 'white' : 'black';
                    row.appendChild(cell);
                }
                table.appendChild(row);
            }
            board.appendChild(table);
        }

        createChessboard(8); // 创建8x8棋盘
    </script>
</body>
</html>

使用Canvas绘制棋盘

另一种方法是使用HTML5 Canvas元素绘制棋盘:

js实现棋盘

<!DOCTYPE html>
<html>
<head>
    <title>Canvas棋盘</title>
    <style>
        canvas {
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <canvas id="chessCanvas" width="400" height="400"></canvas>

    <script>
        const canvas = document.getElementById('chessCanvas');
        const ctx = canvas.getContext('2d');
        const size = 8; // 8x8棋盘
        const cellSize = canvas.width / size;

        function drawChessboard() {
            for (let i = 0; i < size; i++) {
                for (let j = 0; j < size; j++) {
                    ctx.fillStyle = (i + j) % 2 === 0 ? 'white' : 'black';
                    ctx.fillRect(j * cellSize, i * cellSize, cellSize, cellSize);
                    ctx.strokeRect(j * cellSize, i * cellSize, cellSize, cellSize);
                }
            }
        }

        drawChessboard();
    </script>
</body>
</html>

添加交互功能

可以为棋盘添加点击事件,实现棋子放置功能:

// 在Canvas示例基础上添加
canvas.addEventListener('click', function(event) {
    const rect = canvas.getBoundingClientRect();
    const x = event.clientX - rect.left;
    const y = event.clientY - rect.top;

    const col = Math.floor(x / cellSize);
    const row = Math.floor(y / cellSize);

    // 绘制红色棋子
    ctx.beginPath();
    ctx.arc(
        col * cellSize + cellSize / 2,
        row * cellSize + cellSize / 2,
        cellSize / 2 - 5,
        0,
        Math.PI * 2
    );
    ctx.fillStyle = 'red';
    ctx.fill();
    ctx.stroke();
});

使用CSS Grid创建棋盘

纯CSS方案结合少量JavaScript:

<!DOCTYPE html>
<html>
<head>
    <title>CSS Grid棋盘</title>
    <style>
        #chessGrid {
            display: grid;
            grid-template-columns: repeat(8, 50px);
            grid-template-rows: repeat(8, 50px);
            width: 400px;
            height: 400px;
        }
        .cell {
            border: 1px solid black;
        }
        .black {
            background-color: black;
        }
        .white {
            background-color: white;
        }
    </style>
</head>
<body>
    <div id="chessGrid"></div>

    <script>
        const grid = document.getElementById('chessGrid');
        const size = 8;

        for (let i = 0; i < size * size; i++) {
            const cell = document.createElement('div');
            const row = Math.floor(i / size);
            const col = i % size;
            cell.className = `cell ${(row + col) % 2 === 0 ? 'white' : 'black'}`;
            grid.appendChild(cell);
        }
    </script>
</body>
</html>

这些方法提供了不同技术路线实现棋盘效果,可根据项目需求选择合适方案。表格方法简单直接,Canvas适合需要复杂交互的场景,CSS Grid则展示了现代CSS布局能力。

标签: 棋盘js
分享给朋友:

相关文章

js实现分页

js实现分页

分页的基本实现思路 在JavaScript中实现分页功能通常需要结合前端和后端逻辑。前端负责渲染分页控件和处理用户交互,后端负责提供分页数据。 前端分页实现 纯前端分页适用于数据量较小的情况,可以直…

js实现图片预览

js实现图片预览

使用FileReader API实现图片预览 通过FileReader对象读取用户选择的图片文件并显示预览: const input = document.getElementById('image…

原生js实现轮播图

原生js实现轮播图

基本结构搭建 HTML部分需要包含轮播图容器、图片列表及导航按钮。结构示例如下: <div class="slider-container"> <div class="slid…

js分组实现

js分组实现

分组实现方法 在JavaScript中,可以通过多种方式实现数组或对象的分组操作。以下是几种常见的方法: 使用Array.prototype.reduce() 通过reduce方法可以轻松实现数组分…

js实现dh

js实现dh

JavaScript 实现 DH(Diffie-Hellman)密钥交换 Diffie-Hellman(DH)密钥交换是一种安全协议,允许双方在不安全的通信信道上建立一个共享密钥。以下是如何在 Jav…

js实现原理

js实现原理

JavaScript 实现原理 JavaScript 是一种解释型语言,其实现原理涉及多个核心组件和运行机制,包括引擎、运行时环境、事件循环等。 引擎与解释执行 现代 JavaScript 引擎(如…