Skip to content

Commit 6f5d630

Browse files
author
linyisonger
committed
增加对比脚本
1 parent 8a5133b commit 6f5d630

File tree

1 file changed

+70
-16
lines changed

1 file changed

+70
-16
lines changed

093.七王五二三-人机对战.html

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,36 @@
390390
return TYPE_LEVEL[a] < TYPE_LEVEL[b]
391391
}
392392

393+
/**
394+
* 卡牌对比
395+
* A < B True
396+
* A > B False
397+
* @author linyisonger
398+
* @date 2025-03-15
399+
* @param {Card[]} a
400+
* @param {Card[]} b
401+
*/
402+
static comparisonByCards(a, b) {
403+
let groupType = Card.groupType(a)
404+
switch (groupType) {
405+
case GROUP_TYPE.LEAFLETS:
406+
case GROUP_TYPE.PAIRS:
407+
case GROUP_TYPE.FOUR:
408+
case GROUP_TYPE.TRIPLE:
409+
return Card.comparison(a[0], b[0])
410+
case GROUP_TYPE.TRIPLE_WITH_PAIRS:
411+
case GROUP_TYPE.FOUR_WITH_SINGLE:
412+
let aGroup = groupBy(a, c => c.type)
413+
let bGroup = groupBy(b, c => c.type)
414+
let aCard = null
415+
let bCard = null
416+
for (const key in aGroup) if (aGroup[key].length >= 3) aCard = aGroup[key][0]
417+
for (const key in bCard) if (bCard[key].length >= 3) bCard = bCard[key][0]
418+
return Card.comparison(aCard, bCard)
419+
}
420+
}
421+
422+
393423
/**
394424
* 数组中最小卡牌下表
395425
* @author linyisonger
@@ -452,14 +482,15 @@
452482
}
453483
else if (groupType === GROUP_TYPE.PAIRS) { // 一对
454484
// 是否有黑桃
455-
let hasSpade = cards.some(c => c.suits === CARD_SUITS.SPADE)
485+
let hasSpade = target.some(c => c.suits === CARD_SUITS.SPADE)
456486
// 单张卡牌类型
457487
let type = target[0].type
458488
for (const key in holdGroup) {
459489
if (holdGroup[key].length >= groupTypeNum) { // 张数大于等于2
460490
if (Card.comparisonByType(key, type)) delete holdGroup[key] // 当key < type 时
461491
if (hasSpade && key === type) delete holdGroup[key] // 相同
462492
}
493+
else delete holdGroup[key]
463494
}
464495
// 排序后取最小的组合的前两张
465496
let keys = Object.keys(holdGroup)
@@ -474,11 +505,18 @@
474505
if (holdGroup[key].length >= groupTypeNum) { // 张数大于等于 牌型张数
475506
if (Card.comparisonByType(key, type)) delete holdGroup[key] // 当key < type 时
476507
}
508+
else delete holdGroup[key]
477509
}
510+
// 排序后取最小的组合的前两张
511+
let keys = Object.keys(holdGroup)
478512
if (keys.length > 0) return holdGroup[keys[0]].slice(0, groupTypeNum)
479513
}
480514
return []
481515
}
516+
517+
518+
519+
482520
}
483521

484522
class Round {
@@ -571,6 +609,7 @@
571609
* @param {Round} round
572610
*/
573611
discard(round) {
612+
console.log('discard');
574613
if (this.status === PLAYER_STATUS.AUTO) {
575614
console.log('自动', round.lastCards);
576615
let cards = this.betterCards(round.lastCards) // 最小能大上的牌
@@ -658,11 +697,11 @@
658697
console.log('游戏结束');
659698
return;
660699
}
661-
662-
663-
this.currentRound.loadUILastCards();
700+
console.log("nextPlayerDiscard");
664701
this.nextPlayer(); // 下一个玩家
665702
if (this.currentRound.end) { // 检测回合是否结束
703+
console.log('检测回合是否结束');
704+
uiEvent.publish('updateCardTable', [])
666705
this.allRounds.push(this.currentRound);
667706
this.nextRound();
668707
}
@@ -682,9 +721,13 @@
682721

683722
for (let i = 0; i < this.joinPlayers.length; i++) {
684723
this.currentPlayer.pickup();
724+
if (this.currentPlayer.status === PLAYER_STATUS.AUTO) uiEvent.publish('updateRobotBaseInfo', this.currentPlayer.holdCards)
725+
if (this.currentPlayer.status === PLAYER_STATUS.MANUAL) uiEvent.publish('updatePlayerHoldCards', this.currentPlayer.holdCards)
685726
this.nextPlayer();
686727
}
687728

729+
730+
688731
if (this.allRounds.length === 0) {
689732
console.log('最小牌面对比');
690733
// 牌面最小
@@ -699,15 +742,16 @@
699742
uiEvent.publish('updatePlayerBaseInfo', this.currentPlayer)
700743
uiEvent.publish('updateRobotBaseInfo', this.joinPlayers[(minCardIndex + 1) % this.joinPlayers.length])
701744
uiEvent.subscribe('discard', (cards) => {
702-
console.log('出牌', cards);
745+
// 隐藏玩家操作按钮
746+
uiEvent.publish('hidePlayerOperationButtons')
703747
// 出牌操作
704748
this.currentPlayer.holdCards = this.currentPlayer.holdCards.filter(h => !cards.find(c => c.type === h.type && c.suits === h.suits))
749+
uiEvent.publish('updatePlayerHoldCards', this.currentPlayer.holdCards)
705750
// 一条记录
706751
this.currentRound.allRecord.push(new Record(this.currentPlayer, cards))
707752
// 下一个出牌
708753
this.nextPlayerDiscard()
709-
// 隐藏玩家操作按钮
710-
uiEvent.publish('hidePlayerOperationButtons')
754+
711755
})
712756

713757
console.log('玩家牌面最小', this.currentPlayer);
@@ -812,7 +856,7 @@
812856
*/
813857
static cardConvertToImg(card) {
814858
if (!card) return ''
815-
return `<img src="${this.url}"/>`
859+
return `<img src="${card.url}"/>`
816860
}
817861

818862

@@ -845,7 +889,8 @@
845889
for (let i = 0; i < cardItemDomList.length; i++) {
846890
const cardItemDom = cardItemDomList.item(i)
847891
const cardItem = holdCards[i]
848-
cardItemDom.hidden = !cardItem
892+
if (cardItem) cardItemDom.classList.remove('hidden')
893+
else cardItemDom.classList.add('hidden')
849894
}
850895
}
851896

@@ -934,7 +979,9 @@
934979

935980
// 不符合规则
936981
let irregular = playerSelectedGroupType == null && playerSelectedCards.length // 选择不为空 但组为空
937-
|| cardTableGroupType != null && playerSelectedGroupType != cardTableGroupType // 牌桌组类型不为空 并且 组类型 不相同
982+
|| cardTableGroupType != null && playerSelectedGroupType != cardTableGroupType && playerSelectedCards.length // 牌桌组类型不为空 并且 组类型 不相同
983+
|| Card.comparisonByCards(playerSelectedCards, cardTableCards) // 对比大小
984+
938985

939986
if (irregular) {
940987
disBtn.classList.add('hidden')
@@ -949,10 +996,6 @@
949996
})
950997

951998

952-
disBtn.addEventListener('click', function () {
953-
let playerSelectedCards = UIController.playerSelectedCards // 玩家选择的手牌
954-
uiEvent('discard', playerSelectedCards)
955-
})
956999
}
9571000

9581001

@@ -968,13 +1011,19 @@
9681011
const powerless = playerOperationButtons.querySelector('.powerless'); // 没有能大过的牌
9691012
const nonCompliance = playerOperationButtons.querySelector('.non-compliance'); // 不符合规则
9701013

1014+
powerless.classList.add('hidden');
9711015
nonCompliance.classList.add('hidden');
9721016

9731017
passBtn.addEventListener('click', () => {
974-
// 回合出牌
9751018
uiEvent.publish('discard', [])
9761019
})
9771020

1021+
disBtn.addEventListener('click', function () {
1022+
let playerSelectedCards = UIController.playerSelectedCards // 玩家选择的手牌
1023+
uiEvent.publish('discard', playerSelectedCards)
1024+
UIController.playerContainer.querySelectorAll(`.card-item.selected`).forEach(selected => selected.classList.remove('selected'))
1025+
})
1026+
9781027
}
9791028

9801029
/**
@@ -983,6 +1032,7 @@
9831032
* @date 2025-03-17
9841033
*/
9851034
static showPlayerOperationButtons() {
1035+
console.log('showPlayerOperationButtons');
9861036
const playerOperationButtons = UIController.playerContainer.querySelector('.player-operation-buttons')
9871037
playerOperationButtons.classList.remove('hidden')
9881038

@@ -994,7 +1044,8 @@
9941044
const passBtn = playerOperationButtons.querySelector('.pass-btn'); // 不要
9951045
const powerless = playerOperationButtons.querySelector('.powerless'); // 没有能大过的牌
9961046

997-
if (hintCards.length) {
1047+
console.log(hintCards, playerOperationButtons);
1048+
if (hintCards.length || cardTableCards.length == 0) {
9981049
disBtn.classList.remove('hidden')
9991050
powerless.classList.add('hidden')
10001051
if (cardTableCards.length) passBtn.classList.remove('hidden')
@@ -1013,6 +1064,8 @@
10131064
* @date 2025-03-17
10141065
*/
10151066
static hidePlayerOperationButtons() {
1067+
console.log('hidePlayerOperationButtons');
1068+
10161069
const playerOperationButtons = UIController.playerContainer.querySelector('.player-operation-buttons')
10171070
playerOperationButtons.classList.add('hidden')
10181071
}
@@ -1024,6 +1077,7 @@
10241077
const uiEvent = new EventBus()
10251078

10261079
UIController.initPlayerHoldCards()
1080+
UIController.initPlayerOperationButtons();
10271081

10281082
uiEvent.subscribe('updateRobotHoldCards', UIController.updateRobotHoldCards)
10291083
uiEvent.subscribe('updatePlayerHoldCards', UIController.updatePlayerHoldCards)

0 commit comments

Comments
 (0)