当前位置:首页 > JavaScript

js实现填字

2026-03-15 05:52:57JavaScript

实现填字游戏的 JavaScript 方法

基础实现思路
使用 HTML 表格构建填字网格,通过 JavaScript 动态生成字母输入框,绑定事件监听实现交互。核心步骤包括初始化网格、验证输入、提示功能等。

HTML 结构示例

js实现填字

<div id="crossword-container"></div>

核心代码实现

初始化网格

function createGrid(rows, cols) {
  const container = document.getElementById('crossword-container');
  const table = document.createElement('table');

  for (let i = 0; i < rows; i++) {
    const tr = document.createElement('tr');
    for (let j = 0; j < cols; j++) {
      const td = document.createElement('td');
      const input = document.createElement('input');
      input.type = 'text';
      input.maxLength = 1;
      input.dataset.row = i;
      input.dataset.col = j;
      td.appendChild(input);
      tr.appendChild(td);
    }
    table.appendChild(tr);
  }
  container.appendChild(table);
}

验证答案逻辑

js实现填字

function checkSolution(solutionMatrix) {
  const inputs = document.querySelectorAll('input');
  let isCorrect = true;

  inputs.forEach(input => {
    const row = parseInt(input.dataset.row);
    const col = parseInt(input.dataset.col);
    if (input.value.toUpperCase() !== solutionMatrix[row][col]) {
      isCorrect = false;
      input.classList.add('incorrect');
    } else {
      input.classList.remove('incorrect');
    }
  });

  return isCorrect;
}

进阶功能实现

键盘导航

function setupKeyboardNavigation() {
  document.querySelectorAll('input').forEach(input => {
    input.addEventListener('keyup', (e) => {
      const row = parseInt(e.target.dataset.row);
      const col = parseInt(e.target.dataset.col);

      if (e.key.match(/^[a-zA-Z]$/)) {
        // 自动聚焦下一个输入框
        const nextInput = document.querySelector(
          `input[data-row="${row}"][data-col="${col + 1}"]`
        );
        if (nextInput) nextInput.focus();
      }
    });
  });
}

提示系统

function showHint(row, col, letter) {
  const input = document.querySelector(
    `input[data-row="${row}"][data-col="${col}"]`
  );
  input.value = letter;
  input.classList.add('hint');
}

样式优化建议

table {
  border-collapse: collapse;
  margin: 20px auto;
}
td {
  border: 1px solid #ddd;
  width: 30px;
  height: 30px;
  text-align: center;
}
input {
  width: 100%;
  height: 100%;
  border: none;
  text-align: center;
  text-transform: uppercase;
  font-size: 16px;
}
.incorrect {
  background-color: #ffcccc;
}
.hint {
  color: #6666ff;
}

数据结构示例

// 答案矩阵示例
const solution = [
  ['H', 'E', 'L', 'L', 'O'],
  ['W', '', 'O', '', 'L'],
  ['O', '', 'R', '', 'D'],
  ['R', 'L', 'D', 'O', 'W']
];

// 初始化 4x5 的填字游戏
createGrid(4, 5);
setupKeyboardNavigation();

通过以上方法可实现基础填字游戏功能,可根据需求扩展词库加载、难度分级等特性。

标签: js
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https://…

js实现计算器

js实现计算器

实现基础计算器功能 使用JavaScript创建一个基础计算器需要处理数字输入、运算符逻辑和结果显示。以下是一个简单的实现示例: let currentInput = '0'; let previo…

js实现二叉树

js实现二叉树

二叉树的基本概念 二叉树是一种树形数据结构,每个节点最多有两个子节点,分别称为左子节点和右子节点。二叉树的常见操作包括插入、删除、遍历等。 二叉树的节点定义 在JavaScript中,二叉树的节点可…

js实现上传文件

js实现上传文件

文件上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现文件上传功能。 <input type="file"…

实现继承js

实现继承js

实现继承的方式 在JavaScript中,实现继承可以通过多种方式完成,每种方式都有其优缺点。以下是几种常见的实现继承的方法: 原型链继承 原型链继承是最基本的继承方式,通过将子类的原型指向父类的实…

js 实现页面刷新

js 实现页面刷新

使用 location.reload() location.reload() 是原生 JavaScript 提供的页面刷新方法。调用该方法会重新加载当前页面,相当于点击浏览器的刷新按钮。 // 基本…