扑克牌
js
function Poker(point, color) {
this.point = point;
this.color = color;
}
function Deck(type = 0) {
const count = type === 1 ? 52 : 54;
const customPoints = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];
const curtomColors = ["♦️", "♣️", "♥️", "♠️"];
const jokerPoint = "Joker";
const jokerColors = ["🤡", "🃏"];
this.cards = [];
this.init = () => {
for (let i = 0, len = count; i < len; ) {
// 普通牌
curtomColors.forEach((color) => {
customPoints.forEach((point) => {
this.cards.push(new Poker(point, color));
i++;
});
});
// 大小王
type === 0 &&
jokerColors.forEach((color) => {
this.cards.push(new Poker(jokerPoint, color));
i++;
});
}
};
this.wash = () => {
this.cards = this.cards.sort((a, b) => (Math.random() > 0.5 ? -1 : 1));
};
}
function Play() {
const playerCount = 4;
this.playerCards = [
{ name: "zhang", cards: [] },
{ name: "li", cards: [] },
{ name: "qian", cards: [] },
{ name: "wu", cards: [] },
];
const deck = new Deck(1);
deck.init();
deck.wash();
const cards = deck.cards;
let current = 0;
cards.forEach((card, index) => {
this.playerCards[current].cards.push(card);
if (current < playerCount - 1) current++;
else current = 0;
});
// 打牌
this.playing = () => {
// 随机生成一个数字,决定那个先出牌
let currentPlayerIndex = Math.floor(Math.random() * playerCount);
// 定义牌面大小和花色的顺序
const pointOrder = [
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
"A",
"2",
];
const colorOrder = ["♦️", "♣️", "♥️", "♠️"];
// 判断牌面大小
function compareCards(card1, card2) {
let point1Index = pointOrder.indexOf(card1.point);
let point2Index = pointOrder.indexOf(card2.point);
if (point1Index !== point2Index) return point1Index > point2Index;
return colorOrder.indexOf(card1.color) > colorOrder.indexOf(card2.color);
}
// 游戏结束标志
let gameEnded = false;
// 游戏循环
const gameLoop = () => {
if (gameEnded) return; // 如果游戏已经结束,直接返回
console.log(`当前轮到 ${this.playerCards[currentPlayerIndex].name} 出牌`);
// 当前玩家出最小的牌
let currentCard = this.playerCards[currentPlayerIndex].cards.shift();
if (!currentCard) {
console.log("游戏结束,没有玩家可以继续出牌");
return;
}
setTimeout(() => {
console.log(
`${this.playerCards[currentPlayerIndex].name} 出了 ${currentCard.point}${currentCard.color}`
);
// 检查是否有玩家已经打完所有牌
for (let i = 0; i < playerCount; i++) {
if (this.playerCards[i].cards.length === 0) {
console.log(`${this.playerCards[i].name} 赢得了比赛!`);
gameEnded = true; // 设置游戏结束标志
return;
}
}
// 检查其他玩家是否有更大的牌可以出
let found = false; // 初始化 found 为 false
for (let i = 0; i < playerCount; i++) {
if (i === currentPlayerIndex) continue;
for (let j = 0; j < this.playerCards[i].cards.length; j++) {
if (compareCards(this.playerCards[i].cards[j], currentCard)) {
setTimeout(() => {
console.log(
`${this.playerCards[i].name} 可以跟上 ${this.playerCards[i].cards[j].point}${this.playerCards[i].cards[j].color}`
);
this.playerCards[i].cards.splice(j, 1); // 移除已出的牌
currentPlayerIndex = i; // 更新当前玩家
found = true;
gameLoop(); // 继续游戏循环
}, 1000 + Math.floor(Math.random() * 2000)); // 1-3秒的延迟
return;
}
}
if (found) break;
}
// 如果没有人能接上,则下一个玩家出牌
if (!found) {
setTimeout(() => {
currentPlayerIndex = (currentPlayerIndex + 1) % playerCount;
gameLoop(); // 继续游戏循环
}, 1000 + Math.floor(Math.random() * 2000)); // 1-3秒的延迟
}
}, 1000 + Math.floor(Math.random() * 2000)); // 1-3秒的延迟
};
// 开始游戏循环
gameLoop();
};
}
// 创建并开始游戏
const game = new Play();
game.playing();