Skip to content

Commit b07c100

Browse files
author
linyisonger
committed
啊 写完了吗?
1 parent f48b74d commit b07c100

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

092.中国象棋-暗棋模式.html

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
</div>
129129

130130
<script type="module">
131+
import { Randoms } from "https://gcore.jsdelivr.net/npm/@3r/tool/lib/randoms.js";
131132

132133
/**
133134
* 初始化平面数组
@@ -193,7 +194,9 @@
193194
CANNONS: "炮",
194195
PAWMS: '兵'
195196
}
196-
197+
const CHESS_PIECE_DARK = {
198+
DARK: '暗',
199+
}
197200
// 阵营类型
198201
const CAMP_TYPE = {
199202
BLACK: '黑',
@@ -345,8 +348,8 @@
345348
}
346349
else { // 暗棋
347350
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)
350353
}
351354
chineseChessBoard[y][x].dom.appendChild(dom)
352355
}
@@ -626,7 +629,7 @@
626629
]
627630

628631
// 田字格检测
629-
// targetPoint = targetPoint.filter(t => insideMatts(t.x, t.y))
632+
targetPoint = isDarkChessPiece(grid.dom) ? targetPoint.filter(t => insideMatts(t.x, t.y)) : targetPoint
630633

631634
// 在棋盘内部、非同阵营允许移动
632635
return targetPoint.filter(t => insideChessBoard(t.x, t.y) && !sameCamp(t.x, t.y)).map(t => new TargetPoint(t.x, t.y))
@@ -685,17 +688,30 @@
685688

686689
let nextOperateGrid = chineseChessBoard[y][x];
687690
let tempOperateChessPiece = chessManual[y][x];
691+
let currentChessPieceDom = currentOperateGrid.dom.querySelector('.chess-piece')
692+
let nextChessPieceDom = nextOperateGrid.dom.querySelector('.chess-piece')
688693

689-
690-
if (tempOperateChessPiece != null) { // 吃子
694+
if (tempOperateChessPiece != null) { // 吃子
695+
if (isDarkChessPiece(nextOperateGrid.dom)) {
696+
tempOperateChessPiece = randomChessPiece()
697+
}
691698
console.log('吃子', tempOperateChessPiece);
692-
nextOperateGrid.dom.querySelector('.chess-piece').remove()
699+
nextChessPieceDom.remove()
693700
if (tempOperateChessPiece.type == CHESS_PIECE_STANDARD.KING) { // 被吃掉的是将
694701
chessBoardOverBox.querySelector('.over-subtitle').textContent = `🎉${currentOperateCamp}方获胜!`
695702
chessBoardOverBox.classList.remove('hidden')
696703
}
697704
}
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)
699715
chessManual[y][x] = currentOperateChessPiece;
700716
chessManual[currentOperateGrid.y][currentOperateGrid.x] = null;
701717

@@ -714,7 +730,25 @@
714730
})
715731
}
716732

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+
}
717742

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+
}
718752

719753
loadChessManual();
720754
loadOverPanel();

0 commit comments

Comments
 (0)