ChatGTP - 使用js实现升级游戏

发布时间 2023-08-15 16:03:19作者: kitty20180903suzhou

实现一个完整且复杂的升级游戏涉及到很多方面,包括玩家操作、游戏状态管理、游戏规则和逻辑等等。下面是一个较为简化的升级游戏的代码示例:

// 游戏状态枚举
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方法判断出牌是否符合规则和是否大于上家牌,根据规则进行判断胜负,并最终决出最终胜利者。

这个示例中的游戏逻辑相对比较简单,你可以根据自己的需求来修改和扩展代码,实现更复杂的升级游戏。