个人技术分享

Card

export class Card{
    rank: number;
    suit: string;
    constructor(suit:string, rank:number){
        this.rank = rank;
        this.suit = suit;
    }
    toString():string{
        let new_rank = ''
        switch(this.rank){
            case 1:
                {
                    new_rank = 'A'
                    break
                }
            case 11:
                {
                    new_rank = 'J'
                    break
                }
            case 12:
                {
                    new_rank = 'Q'
                    break
                }
            case 13:
                {
                    new_rank = 'K'
                    break
                }
            default:
                {
                    new_rank = this.rank+''
                    break
                }
        }
        return this.suit + new_rank;
    }
}

let card:Card = new Card('红桃',12)
console.log(card.toString());

Poker

import { Card } from "./card";

export class Poker{
    suits: string[] = ['红桃','黑桃','方片','梅花'];
    cards: Array<Card> = new Array<Card>(52);
    constructor(){
        let count = 0;
        for(let i = 0; i < this.suits.length; i++){
            for(let j = 0; j < 13; j++){
                let card_tmp = new Card(this.suits[i], j+1);
                this.cards[count++] = card_tmp;
            }
        }
    }

    outputCard(){
        let count = 0;
        for(let i = 0; i < this.suits.length; i++){
            for(let j = 0; j < 13; j++){
                if(count % 13 == 0 && count != 0){
                    console.log()
                }
                process.stdout.write(this.cards[count++].toString() + ' ')
            }
        }
    }

    shuffleCard(){
        for(let i = 0; i < this.cards.length; i++){
            let random_idx = Math.floor(Math.random() * this.cards.length);
            [this.cards[i],this.cards[random_idx]] =  [this.cards[random_idx],this.cards[i]]
        }
    }
}

let poker: Poker = new Poker()
poker.outputCard()
poker.shuffleCard()
console.log()
console.log()
poker.outputCard()