node.js创建扑克牌模拟发牌洗牌

1.创建牌
brand.js

export class pai{
    constructor(){
        this.arr = [];
        var hua = [{name:'j'},{name:'Q'},{name:'K'},{name:'A'},{name:2}];
        for(let i = 1 ;i <=4;i++){
            let mm = 0;
            for(let j = 3;j<=15;j++){
                 if(j<=10){
                     this.arr.push({
                         decor:i,
                         face:j
                     })
                 }else{
                    this.arr.push({
                        decor:i,
                        face:hua[mm].name
                    })
                     mm++
                 }
            }
        }
        this.arr.push({
            decor:1,
            face:"大王"
        },{
            decor:2,
            face:"小王"
        })
       this.pp()
    }
    pp(){
        return this.arr
    }
    tostring(){
        let arr1 = [];
        for(let i = 0 ;i<this.arr.length;i++){
            arr1.push(this.arr[i].face)
        }
        return  arr1.join();
    }
}

2.洗牌
shuffle.js

import {pai} from './brand.js';

// console.log(arr);

export function shuff(){
    let a = new pai();
    let arr = a.arr 
    let arr1  =[]
    // for(let i = 0 ; i<arr.length;i++){
        let i = 0;
        let l = 0;
    while( l < arr.length){
        let kk = arr.length+1
        let mm = Math.floor(Math.random()*(kk-1));
        arr1.push(arr[mm]);
        arr.splice(mm,1);
        i++;
     }
    return arr1
}

3.发牌

licensing.js

import {shuff} from './shuffle.js';


export function fa(){
    let arr = shuff();
    let A = [];
    let B = [];
    let C = [];
    let d = Math.random();
    for(let i = 0 ;i<arr.length-3;i+=3){
        A.push(arr[i]);
        B.push(arr[i+1]);
        C.push(arr[i+2]);
    }
    // console.log(A,B,C);
    if(d>0.7){
        A.push(arr[51],arr[52],arr[53])
    }else if(d > 0.4 && d <= 0.7){
        B.push(arr[51],arr[52],arr[53])
    }else{
        C.push(arr[51],arr[52],arr[53])
    }
    console.log( {"张三":A,"李四":B,"王五":C})
}

在这里插入图片描述
启动
xifa.js

import {pai} from './brand.js';
import  {shuff} from './shuffle.js';
import  {fa} from './licensing.js';
fa()

html链入

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="./module/xifa.js" type="module"></script>
</body>
</html>

面试题

1.fromEntries

Object.prototype.sx_fromEntries = function (arr) {
    const obj = {}
    for (let i = 0; i < arr.length; i++) {
        const [key, value] = arr[i]
        obj[key] = value
    }
    return obj
}

console.log(Object.sx_fromEntries([['name', '林三心'], ['age', 22], ['gender', '男']]))
// { name: '林三心', age: 22, gender: '男' }

2.keys

Object.prototype.sx_keys = function (obj) {
    const keys = []
    for (let key in obj) {
        obj.hasOwnProperty(key) && res.push(key)
    }
    return keys
}

console.log(Object.keys(obj))
// [ 'name', 'age', 'gender' ]


3.instanceOf

function instanceOf(father, child) {
    const fp = father.prototype
    var cp = child.__proto__

    while (cp) {
        if (cp === fp) {
            return true
        }
        cp = cp.__proto__
    }

    return false
}

function Person(name) {
    this.name = name
}
const sx = new Person('林三心')

console.log(instanceOf(Person, sx)) // true
console.log(instanceOf(Person, sx2)) // false


猜你喜欢

转载自blog.csdn.net/qq_45256777/article/details/121322470