Skip to content

制作一个斗地主洗牌发牌的程序

划分模块:

  1. 工具模块,导出一个函数,用于将一个数组中的所有内容乱序排列

  2. 扑克牌构造函数(类)

    1. 属性
      1. 花色(1~4,♣、♥、♦、♠)
      2. 牌面(1~15,14小王,15大王)
    2. 方法
      1. toString:得到该扑克牌的字符串
  3. 入口模块(入口文件)

    1. 创建54张扑克牌
    2. 洗牌
    3. 发牌

    答案

    main.js

    js
    /* 
    制作一个斗地主洗牌发牌的程序
    
    划分模块:
    
    1. 工具模块,导出一个函数,用于将一个数组中的所有内容乱序排列
    2. 扑克牌构造函数(类)
       1. 属性
          1. 花色(1~4,♣、♥、♦、♠)
          2. 牌面(1~15,14小王,15大王)
       2. 方法
          1. toString:得到该扑克牌的字符串
    3. 入口模块(入口文件)
       1. 创建54张扑克牌
       2. 洗牌
       3. 发牌
    
    */
    
    const Poker = require("./poker");
    const utils = require("./utils");
    
    //创建扑克
    let allPokers = [];
    for (let i = 1; i <= 13; i++) {
      //循环牌面
      for (let j = 1; j <= 4; j++) {
        //循环花色
        allPokers.push(new Poker(j, i).toString());
      }
    }
    allPokers.push(new Poker(0, 14).toString(), new Poker(0, 15).toString());
    
    //洗牌
    utils.sortPoker(allPokers);
    
    //发牌
    const p1 = allPokers.slice(0, 17);
    const p2 = allPokers.slice(17, 34);
    const p3 = allPokers.slice(34, 51);
    const desk = allPokers.slice(51);
    
    console.log("玩家1:", utils.sortPokerPlayer(p1));
    console.log("玩家2:", utils.sortPokerPlayer(p2));
    console.log("玩家3:", utils.sortPokerPlayer(p3));
    console.log("桌面:", desk);
    
    // console.log(allPokers);

    poker.js

    js
    /**
     * 扑克类
     */
    class Poker {
      /**
       * @param {numebr} type   1♣、2♥、3♦、4♠
       * @param {number} num   1~15,14小王,15大王
       */
      constructor(type, num) {
        this.type = type;
        this.num = num;
      }
      /**
       *将扑克牌的花色和字面拼接
       * @returns
       */
      toString() {
        let str = "";
        //判断花色
        if (this.type === 0) {
          str = "";
        } else if (this.type === 1) {
          str = "♣";
        } else if (this.type === 2) {
          str = "♥";
        } else if (this.type === 3) {
          str = "♦";
        } else if (this.type === 4) {
          str = "♠";
        }
        //判断牌面
        if (this.num >= 2 && this.num <= 10) {
          str += this.num;
        } else if (this.num === 1) {
          str += "A";
        } else if (this.num === 11) {
          str += "J";
        } else if (this.num === 12) {
          str += "Q";
        } else if (this.num === 13) {
          str += "K";
        } else if (this.num === 14) {
          str += "joker";
        } else if (this.num === 15) {
          str += "Joker";
        }
    
        return str;
      }
    }
    
    module.exports = Poker;

    utils.js

    js
    /**
     *对扑克牌进行排序
     * @param {Array} pokerArr
     * @returns {Array} 打乱后得扑克数组
     */
    function sortPoker(pokerArr) {
      return pokerArr.sort(() => {
        return Math.random() - 0.5;
      });
    }
    /**
     * 对玩家的手中的牌进行排序
     * @param {string[]} pokerArr
     */
    function sortPokerPlayer(pokerArr) {
      //处理大小王
      let hasJoker = false;
      let hasjoker = false;
      pokerArr.forEach((item, index) => {
        if (item === "joker") {
          hasjoker = true;
          pokerArr.splice(index, 1);
        }
        if (item === "Joker") {
          hasJoker = true;
          pokerArr.splice(index, 1);
        }
      });
      //排序
      pokerArr.sort((a, b) => {
        //数值映射
        const rankMap = {
          3: 3,
          4: 4,
          5: 5,
          6: 6,
          7: 7,
          8: 8,
          9: 9,
          10: 10,
          J: 11,
          Q: 12,
          K: 13,
          A: 14,
          2: 15,
        };
        // 截取出数字
        let rankA = rankMap[a.slice(1)];
        let rankB = rankMap[b.slice(1)];
        // 比较
        return rankA - rankB;
      });
    
      // 重置大小王
      if (hasjoker) {
        pokerArr.push("joker");
      }
      if (hasJoker) {
        pokerArr.push("Joker");
      }
      hasJoker = hasjoker = false;
    
      return pokerArr;
    }
    
    module.exports = {
      sortPoker,
      sortPokerPlayer,
    };

MIT License