使用类 -- 改造扑克牌并增加洗牌和发牌功能(四)

164 阅读1分钟

扑克牌小练习

  1. 目标:创建一副扑克牌(不包括大小王),打印该扑克牌

  2. 使用枚举改造程序

  3. 用接口改造程序,加入大小王

  4. 用类改造程序,增加洗牌和发牌功能

emum 枚举定义类型

export enum Color {
    heart = "♥",
    spade = "♠",
    club = "♣",
    diamond = "♦"
}

export enum Mark {
    A = "A",
    two = "2",
    three = "3",
    four = "4",
    five = "5",
    six = "6",
    seven = "7",
    eight = "8",
    nine = "9",
    ten = "10",
    eleven = "J",
    twelve = "Q",
    king = "K"
}

interface 接口定义

import { Color, Mark } from "./enums";

export type Deck = Card[]

export interface Card {
    getString(): string
}

export interface NormalCard extends Card {
    color: Color
    mark: Mark
}

// 加入大小王类型并且继承卡片类型
export interface Joker extends Card {
    type: "big" | "small"
}

使用类进行改造

import { Card, Joker } from "./types";
import { Mark, Color } from "./enums";

interface PublishResult {
    player1: Deck,
    player2: Deck,
    player3: Deck,
    left: Deck
}

export class Deck {
    private cards: Card[] = [];

    constructor(cards?: Card[]) {
        if (cards) {
            this.cards = cards;
        }
        else {
            this.init();
        }
    }

    private init() {
        const marks = Object.values(Mark)
        const colors = Object.values(Color)
        for (const m of marks) {
            for (const c of colors) {
                this.cards.push({
                    color: c,
                    mark: m,
                    getString() {
                        return this.color + this.mark;
                    }
                } as Card)
            }
        }
        let joker: Joker = {
            type: "small",
            getString() {
                return "jo";
            }
        }
        this.cards.push(joker)
        joker = {
            type: "big",
            getString() {
                return "JO"
            }
        }
        this.cards.push(joker);
    }

    print() {
        let result = "\n";
        this.cards.forEach((card, i) => {
            result += card.getString() + "\t";
            if ((i + 1) % 6 === 0) {
                result += "\n";
            }
        })
        console.log(result);
    }

    /**
     * 洗牌
     */
    shuffle() {
        // [x1,x2,x3,x4,x5,x6,x7]
        for (let i = 0; i < this.cards.length; i++) {
            const targetIndex = this.getRandom(0, this.cards.length);
            const temp = this.cards[i];
            this.cards[i] = this.cards[targetIndex];
            this.cards[targetIndex] = temp;
        }
    }

    //发完牌后,得到的结果有4个card[]
    publish(): PublishResult {
        let player1: Deck, player2: Deck, player3: Deck, left: Deck;
        player1 = this.takeCards(17);
        player2 = this.takeCards(17);
        player3 = this.takeCards(17);
        left = new Deck(this.cards);

        return {
            player1,
            player2,
            player3,
            left
        };
    }

    private takeCards(n: number): Deck {
        const cards: Card[] = [];
        for (let i = 0; i < n; i++) {
            cards.push(this.cards.shift() as Card);
        }
        return new Deck(cards);
    }

    /**
     * 无法取到最大值
     * @param min 
     * @param max 
     */
    private getRandom(min: number, max: number) {
        const dec = max - min;
        return Math.floor(Math.random() * dec + min);
    }
}

使用

import { Deck } from "./deck";

const deck = new Deck();
deck.shuffle();
console.log("=========洗牌之后=======")
deck.print();
const result = deck.publish();
console.log("=========发牌之后=======")

console.log("===========玩家1========")
result.player1.print();

console.log("===========玩家2========")
result.player2.print();

console.log("===========玩家3========")
result.player3.print();

console.log("===========桌面========")
result.left.print();