|
128 | 128 | </div> |
129 | 129 |
|
130 | 130 | <script type="module"> |
| 131 | + import { Randoms } from "https://gcore.jsdelivr.net/npm/@3r/tool/lib/randoms.js"; |
131 | 132 |
|
132 | 133 | /** |
133 | 134 | * 初始化平面数组 |
|
193 | 194 | CANNONS: "炮", |
194 | 195 | PAWMS: '兵' |
195 | 196 | } |
196 | | - |
| 197 | + const CHESS_PIECE_DARK = { |
| 198 | + DARK: '暗', |
| 199 | + } |
197 | 200 | // 阵营类型 |
198 | 201 | const CAMP_TYPE = { |
199 | 202 | BLACK: '黑', |
|
345 | 348 | } |
346 | 349 | else { // 暗棋 |
347 | 350 | darkChessPiece.push(chessPiece) |
348 | | - dom.setAttribute('src', `./assets/chess-piece/暗-${chessPiece.camp}.png`) |
349 | | - dom.setAttribute('data-type', '暗') |
| 351 | + dom.setAttribute('src', `./assets/chess-piece/${CHESS_PIECE_DARK.DARK}-${chessPiece.camp}.png`) |
| 352 | + dom.setAttribute('data-type', CHESS_PIECE_DARK.DARK) |
350 | 353 | } |
351 | 354 | chineseChessBoard[y][x].dom.appendChild(dom) |
352 | 355 | } |
|
626 | 629 | ] |
627 | 630 |
|
628 | 631 | // 田字格检测 |
629 | | - // targetPoint = targetPoint.filter(t => insideMatts(t.x, t.y)) |
| 632 | + targetPoint = isDarkChessPiece(grid.dom) ? targetPoint.filter(t => insideMatts(t.x, t.y)) : targetPoint |
630 | 633 |
|
631 | 634 | // 在棋盘内部、非同阵营允许移动 |
632 | 635 | return targetPoint.filter(t => insideChessBoard(t.x, t.y) && !sameCamp(t.x, t.y)).map(t => new TargetPoint(t.x, t.y)) |
|
685 | 688 |
|
686 | 689 | let nextOperateGrid = chineseChessBoard[y][x]; |
687 | 690 | let tempOperateChessPiece = chessManual[y][x]; |
| 691 | + let currentChessPieceDom = currentOperateGrid.dom.querySelector('.chess-piece') |
| 692 | + let nextChessPieceDom = nextOperateGrid.dom.querySelector('.chess-piece') |
688 | 693 |
|
689 | | - |
690 | | - if (tempOperateChessPiece != null) { // 吃子 |
| 694 | + if (tempOperateChessPiece != null) { // 吃子 |
| 695 | + if (isDarkChessPiece(nextOperateGrid.dom)) { |
| 696 | + tempOperateChessPiece = randomChessPiece() |
| 697 | + } |
691 | 698 | console.log('吃子', tempOperateChessPiece); |
692 | | - nextOperateGrid.dom.querySelector('.chess-piece').remove() |
| 699 | + nextChessPieceDom.remove() |
693 | 700 | if (tempOperateChessPiece.type == CHESS_PIECE_STANDARD.KING) { // 被吃掉的是将 |
694 | 701 | chessBoardOverBox.querySelector('.over-subtitle').textContent = `🎉${currentOperateCamp}方获胜!` |
695 | 702 | chessBoardOverBox.classList.remove('hidden') |
696 | 703 | } |
697 | 704 | } |
698 | | - nextOperateGrid.dom.appendChild(currentOperateGrid.dom.querySelector('.chess-piece')) |
| 705 | + |
| 706 | + |
| 707 | + if (isDarkChessPiece(currentOperateGrid.dom)) { |
| 708 | + currentOperateChessPiece = randomChessPiece() |
| 709 | + console.log('明子', currentOperateChessPiece); |
| 710 | + currentChessPieceDom.removeAttribute('data-type') |
| 711 | + currentChessPieceDom.setAttribute('src', `./assets/chess-piece/${currentOperateChessPiece.type}-${currentOperateChessPiece.camp}.png`) |
| 712 | + } |
| 713 | + |
| 714 | + nextOperateGrid.dom.appendChild(currentChessPieceDom) |
699 | 715 | chessManual[y][x] = currentOperateChessPiece; |
700 | 716 | chessManual[currentOperateGrid.y][currentOperateGrid.x] = null; |
701 | 717 |
|
|
714 | 730 | }) |
715 | 731 | } |
716 | 732 |
|
| 733 | + /** |
| 734 | + * 随机棋子 |
| 735 | + * @author linyisonger |
| 736 | + * @date 2025-03-03 |
| 737 | + */ |
| 738 | + function randomChessPiece() { |
| 739 | + let idx = Randoms.int(0, darkChessPiece.length) |
| 740 | + return darkChessPiece.splice(idx, 1).shift() |
| 741 | + } |
717 | 742 |
|
| 743 | + /** |
| 744 | + * 是否是暗棋 |
| 745 | + * @author linyisonger |
| 746 | + * @date 2025-03-03 |
| 747 | + * @param {HTMLImageElement} dom |
| 748 | + */ |
| 749 | + function isDarkChessPiece(dom) { |
| 750 | + return dom.querySelector('.chess-piece').getAttribute('data-type') === CHESS_PIECE_DARK.DARK |
| 751 | + } |
718 | 752 |
|
719 | 753 | loadChessManual(); |
720 | 754 | loadOverPanel(); |
|
0 commit comments