janaxs /
blackjack
| 1 | /** |
||
| 2 | * A module for a standard playing card. |
||
| 3 | * |
||
| 4 | * @module |
||
| 5 | */ |
||
| 6 | "use strict"; |
||
| 7 | |||
| 8 | class Card { |
||
| 9 | /** |
||
| 10 | * @constructor |
||
| 11 | * |
||
| 12 | * @param {object} options - Configure by sending options. |
||
| 13 | */ |
||
| 14 | constructor(options = {}) { |
||
| 15 | 28 | this.suits = options.suits || ["♣", "♦", "♠", "♥"]; |
|
| 16 | 28 | this.pipFace = options.pipFace || [ |
|
| 17 | "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" |
||
| 18 | ]; |
||
| 19 | 28 | this.rank = options.rank || [ |
|
| 20 | 14, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 |
||
| 21 | ]; |
||
| 22 | 28 | this.ranks = this.pipFace.length; |
|
| 23 | 28 | this.numOfCards = this.ranks * this.suits.length; |
|
| 24 | } |
||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | /** |
||
| 29 | * Get a card to display based on the id of the card. |
||
| 30 | * |
||
| 31 | * @param {integer} id - The id of the card. |
||
| 32 | * |
||
| 33 | * @returns {string} A string representing the card. |
||
| 34 | */ |
||
| 35 | getCard(id) { |
||
| 36 | var suit; |
||
| 37 | var pipFace; |
||
| 38 | |||
| 39 | 14 | if (id < 0 || id >= this.numOfCards) { |
|
| 40 | 2 | return undefined; |
|
| 41 | } |
||
| 42 | |||
| 43 | 12 | suit = Math.floor(id / this.ranks); |
|
| 44 | 12 | pipFace = Math.floor(id % this.ranks); |
|
| 45 | 12 | return this.suits[suit] + this.pipFace[pipFace]; |
|
| 46 | } |
||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * Get the card rank from its id. |
||
| 52 | * |
||
| 53 | * @param {integer} value - The id of the card. |
||
|
0 ignored issues
–
show
Documentation
introduced
by
Loading history...
|
|||
| 54 | * |
||
| 55 | * @returns {integer} A value representing its rank. |
||
| 56 | */ |
||
| 57 | getRank(id) { |
||
| 58 | 14 | if (id < 0 || id >= this.numOfCards) { |
|
| 59 | 2 | return undefined; |
|
| 60 | } |
||
| 61 | |||
| 62 | 12 | return this.rank[Math.floor(id % this.ranks)]; |
|
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | module.exports = Card; |
||
| 67 |