ChatGTP - 使用js實現升級游戲
實現一個完整且復雜的升級游戲涉及到很多方面,包括玩家操作、游戲狀態管理、游戲規則和邏輯等等。下面是一個較為簡化的升級游戲的代碼示例:
// 游戲狀態枚舉
const GameState = {
NOT_STARTED: 'NOT_STARTED',
BETTING: 'BETTING',
PLAYING: 'PLAYING',
END: 'END'
};
class Player {
constructor(name, money) {
this.name = name;
this.money = money;
this.hand = [];
}
}
class Game {
constructor(numPlayers) {
this.numPlayers = numPlayers;
this.players = [];
this.state = GameState.NOT_STARTED;
this.currentPlayerIndex = -1;
this.round = 0;
this.pot = 0;
}
// 開始游戲
start() {
if (this.state !== GameState.NOT_STARTED) {
console.log('游戲已經開始!');
return;
}
this.state = GameState.BETTING;
console.log('游戲開始!');
console.log('請玩家下注!');
}
// 玩家下注
placeBet(playerIndex, amount) {
if (this.state !== GameState.BETTING) {
console.log('請等待其他玩家下注!');
return;
}
const player = this.players[playerIndex];
if (amount > player.money) {
console.log('你沒有足夠的金錢下注!');
return;
}
player.money -= amount;
this.pot += amount;
console.log(`${player.name} 下注了 ${amount} 元!`);
// 檢查是否所有玩家完成下注
if (this.players.every(player => player.money === 0)) {
this.dealCards();
} else {
this.currentPlayerIndex = (this.currentPlayerIndex + 1) % this.numPlayers;
console.log(`請 ${this.players[this.currentPlayerIndex].name} 下注!`);
}
}
// 發牌
dealCards() {
this.state = GameState.PLAYING;
console.log('正在發牌...');
// 初始化牌組
const deck = [];
for (let i = 2; i <= 10; i++) {
deck.push(i, i, i, i, 'J', 'J', 'Q', 'Q', 'K', 'K', 'A', 'A');
}
deck.push('小王', '小王', '大王', '大王');
// 洗牌函數
const shuffle = function (deck) {
for (let i = deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[deck[i], deck[j]] = [deck[j], deck[i]];
}
return deck;
}
const shuffledDeck = shuffle(deck);
for (let i = 0; i < this.numPlayers; i++) {
const player = this.players[i];
player.hand = shuffledDeck.splice(0, 17);
}
this.currentPlayerIndex = 0;
console.log(`第 ${this.round + 1} 輪開始!`);
console.log(`${this.players[this.currentPlayerIndex].name} 先行動!`);
}
// 玩家出牌
play(playerIndex, cards) {
if (this.state !== GameState.PLAYING) {
console.log('請等待你的回合!');
return;
}
const player = this.players[playerIndex];
const currentPlayer = this.players[this.currentPlayerIndex];
if (playerIndex !== this.currentPlayerIndex) {
console.log('還沒有輪到你出牌!');
return;
}
if (!this.isValidMove(cards)) {
console.log('出牌不合法!');
return;
}
player.hand = player.hand.filter(card => !cards.includes(card));
currentPlayer.hand = currentPlayer.hand.concat(cards);
currentPlayer.hand.sort((a, b) => this.getCardValue(b) - this.getCardValue(a));
console.log(`${player.name} 出牌:${cards.join(' ')}`);
if (player.hand.length === 0) {
this.round++;
if (this.round === 3) {
this.state = GameState.END;
console.log(`游戲結束!奪得最終勝利的玩家是 ${this.findWinner().name}!`);
} else {
this.currentPlayerIndex = (this.currentPlayerIndex + 1) % this.numPlayers;
console.log(`第 ${this.round + 1} 輪開始!`);
console.log(`${this.players[this.currentPlayerIndex].name} 先出牌!`);
}
} else {
this.currentPlayerIndex = (this.currentPlayerIndex + 1) % this.numPlayers;
console.log(`${this.players[this.currentPlayerIndex].name} 下一個出牌!`);
}
}
// 尋找最終勝利的玩家
findWinner() {
let highestMoney = -1;
let winner = null;
for (let player of this.players) {
if (player.money > highestMoney) {
highestMoney = player.money;
winner = player;
}
}
return winner;
}
// 點數轉換函數
getCardValue(card) {
if (!isNaN(parseInt(card))) {
return parseInt(card);
}
switch (card) {
case 'J':
return 11;
case 'Q':
return 12;
case 'K':
return 13;
case 'A':
return 14;
case '小王':
return 15;
case '大王':
return 16;
default:
return 0;
}
}
// 判斷出牌是否合法
isValidMove(cards) {
// 檢查出牌是否符合規則
if (!this.isStraight(cards) && !this.isSameRank(cards)) {
return false;
}
// 檢查出牌是否大于上家
if (this.currentPlayerIndex > 0) {
const prevPlayer = this.players[(this.currentPlayerIndex - 1) % this.numPlayers];
const prevCards = prevPlayer.hand.slice(0, cards.length);
if (this.getCardValue(cards[0]) <= this.getCardValue(prevCards[0])) {
return false;
}
}
return true;
}
// 判斷是否是順子
isStraight(cards) {
if (cards.length < 5) {
return false;
}
const values = cards.map(card => this.getCardValue(card));
values.sort((a, b) => b - a);
for (let i = 1; i < values.length; i++) {
if (values[i - 1] - values[i] !== 1) {
return false;
}
}
return true;
}
// 判斷是否是相同點數
isSameRank(cards) {
if (cards.length < 5) {
return false;
}
const ranks = new Set(cards.map(card => card.slice(0, -1)));
return ranks.size === 1;
}
}
// 運行游戲
const game = new Game(3);
game.players.push(new Player('張三', 100));
game.players.push(new Player('李四', 100));
game.players.push(new Player('王五', 100));
game.start();
game.placeBet(0, 30);
game.placeBet(1, 30);
game.placeBet(2, 30);
game.play(0, ['2?', '2?', '2?']);
game.play(1, ['3?', '3?', '3?']);
game.play(2, ['4?', '4?', '4?']);
在這個代碼示例中,我們定義了一個Game類來管理升級游戲的狀態和邏輯,使用Player類表示每個玩家。游戲開始后,玩家依次下注,下注完畢后進行發牌,然后輪流出牌,每輪出牌按照輪到玩家的上家的牌決定大小。游戲通過isValidMove方法判斷出牌是否符合規則和是否大于上家牌,根據規則進行判斷勝負,并最終決出最終勝利者。
這個示例中的游戲邏輯相對比較簡單,你可以根據自己的需求來修改和擴展代碼,實現更復雜的升級游戲。
posted on 2023-08-15 15:57 kitty20180903suzhou 閱讀(58) 評論(0) 收藏 舉報
浙公網安備 33010602011771號