把你这个大的方法中重复的部分都拆分成小的方法,示例如下: function airPoint() { const directions = [ [0, 1], [1, 0], [1, 1], [1, -1] // 四个方向:水平、垂直、两个对角线 ]; // 封装一个函数用于放置棋子 function placePiece(x, y) { boxs.value[x][y].place = 2; fourDetial = determineEquare3(4, 2, { x, y, place: 2 }); airPlace.push(x * row.value + y); } // 封装一个函数来检查是否有可放置棋子的位置 function checkAndPlace(x, y) { if (boxs.value[x]?.[y]?.place === 0) { placePiece(x, y); curUser.value = 1; return true; } return false; } // 检查是否有四个连在一起的情况 if (!isEmptyObject(fourDetial)) { const { type, geyi, x, y, times } = fourDetial; if (geyi) { for (let i = x; i > x - times + 1; i--) { if (checkAndPlace(i, y)) { return; } } } else { for (const [dx, dy] of directions) { const newX = x + dx * times; const newY = y + dy * times; if (checkAndPlace(newX, newY)) { return; } } } } // 检查是否有阻挡用户的情况 const temp = determineEquare3(); if (temp) { const { type, geyi, x, y, times } = temp; if (geyi) { for (let i = x; i > x - times + 1; i--) { if (checkAndPlace(i, y)) { return; } } } else { for (const [dx, dy] of directions) { const newX = x + dx * times; const newY = y + dy * times; if (checkAndPlace(newX, newY)) { return; } } } } else { // 企图完成五个连线 airFiveLine(); } curUser.value = 1; }