棋牌游戏JS源码案例:押注、龙虎与倒计时功能实现
游戏概述
本文将介绍一个基于JavaScript的棋牌游戏核心功能实现,包含押注系统、龙虎对战玩法以及游戏倒计时功能。这些功能是棋牌类游戏开发中的常见需求,适用于多种游戏场景。
核心功能实现
1. 押注系统
class BettingSystem {
constructor() {
this.players = {};
this.pool = 0;
}
placeBet(playerId, amount, position) {
if (!this.players[playerId]) {
this.players[playerId] = { total: 0, bets: {} };
}
this.players[playerId].total += amount;
this.players[playerId].bets[position] =
(this.players[playerId].bets[position] || 0) + amount;
this.pool += amount;
return this.players[playerId];
}
}
2. 龙虎对战逻辑
class DragonTigerGame {
constructor() {
this.cards = [];
this.resetDeck();
}
resetDeck() {
this.cards = [];
for (let i = 1; i <= 13; i++) {
for (let j = 0; j < 4; j++) {
this.cards.push(i);
}
}
this.shuffle();
}
shuffle() {
for (let i = this.cards.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this.cards[i], this.cards[j]] = [this.cards[j], this.cards[i]];
}
}
deal() {
if (this.cards.length < 2) this.resetDeck();
return {
dragon: this.cards.pop(),
tiger: this.cards.pop()
};
}
determineWinner(dragon, tiger) {
if (dragon > tiger) return 'dragon';
if (tiger > dragon) return 'tiger';
return 'tie';
}
}
3. 倒计时功能
class GameTimer {
constructor(duration, onEnd) {
this.duration = duration;
this.remaining = duration;
this.onEnd = onEnd;
this.interval = null;
}
start() {
this.interval = setInterval(() => {
this.remaining--;
if (this.remaining <= 0) {
this.stop();
if (this.onEnd) this.onEnd();
}
}, 1000);
}
stop() {
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
}
}
reset() {
this.stop();
this.remaining = this.duration;
}
getFormattedTime() {
const mins = Math.floor(this.remaining / 60);
const secs = this.remaining % 60;
return `${mins}:${secs < 10 ? '0' : ''}${secs}`;
}
}
系统整合
class DragonTigerTable {
constructor() {
this.bettingSystem = new BettingSystem();
this.game = new DragonTigerGame();
this.timer = new GameTimer(30, this.endBettingPhase.bind(this));
this.gameState = 'waiting';
}
startGame() {
this.gameState = 'betting';
this.timer.reset();
this.timer.start();
}
endBettingPhase() {
this.gameState = 'dealing';
const result = this.game.deal();
const winner = this.game.determineWinner(result.dragon, result.tiger);
// 结算逻辑
this.settleBets(winner);
// 准备下一轮
setTimeout(() => {
this.startGame();
}, 5000);
}
settleBets(winner) {
// 实现结算逻辑
console.log(`本轮获胜方: ${winner}`);
}
}
应用示例
<div id="game-table">
<div id="timer">准备中...</div>
<div id="betting-area">
<button class="bet-btn" data-position="dragon">押龙</button>
<button class="bet-btn" data-position="tiger">押虎</button>
<button class="bet-btn" data-position="tie">押和</button>
</div>
<div id="result-display"></div>
</div>
<script>
const table = new DragonTigerTable();
document.querySelectorAll('.bet-btn').forEach(btn => {
btn.addEventListener('click', () => {
if (table.gameState === 'betting') {
const position = btn.dataset.position;
table.bettingSystem.placeBet('player1', 100, position);
console.log(`已押注 ${position} 100筹码`);
}
});
});
table.startGame();
</script>
总结
这个实现展示了棋牌游戏的核心功能模块:
- 押注系统管理玩家下注
- 龙虎对战处理游戏逻辑
- 倒计时控制游戏流程
开发者可以根据实际需求扩展功能,如添加更多游戏玩法、完善用户界面或增加多人联网功能。
这一切,似未曾拥有