|
1 | 1 | //Imports« |
2 | 2 |
|
3 | 3 | const {Com} = LOTW.globals.ShellMod.comClasses; |
4 | | -const{isArr,isStr,isEOF,log,jlog,cwarn,cerr}=LOTW.api.util; |
| 4 | +const{log,jlog,cwarn,cerr}=LOTW.api.util; |
5 | 5 |
|
6 | 6 | //» |
7 | 7 |
|
| 8 | +class Card {/* « */ |
| 9 | +constructor(rank, suit, rankSymbol, suitEmoji) { |
| 10 | + this.rank = rank; |
| 11 | + this.suit = suit; |
| 12 | + this.rankSymbol = rankSymbol; |
| 13 | + this.suitEmoji = suitEmoji; |
| 14 | +} |
| 15 | + |
| 16 | +toLiteral() { |
| 17 | + return `${this.rank}-${this.suit}`; |
| 18 | +} |
| 19 | + |
| 20 | +toPictograph() { |
| 21 | + return `${this.rankSymbol}-${this.suitEmoji}`; |
| 22 | +} |
| 23 | +}/* » */ |
| 24 | +class Deck {/* « */ |
| 25 | + |
| 26 | +constructor(type) {/* « */ |
| 27 | + this.type = type; |
| 28 | + if (type !== 'standard') { |
| 29 | + throw new Error('Only "standard" deck type is supported'); |
| 30 | + } |
| 31 | + this.cards = []; |
| 32 | + this.dealt = []; |
| 33 | + this.suits = []; |
| 34 | + this.rankSymbols = []; |
| 35 | + this.suitEmojis = {}; |
| 36 | + this.numRanks = 0; |
| 37 | + this.initializeStandardDeck(); |
| 38 | +}/* » */ |
| 39 | + |
| 40 | +initializeStandardDeck() {/* « */ |
| 41 | +this.suits = ['spades', 'hearts', 'diamonds', 'clubs']; |
| 42 | +this.rankSymbols = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']; |
| 43 | +this.suitEmojis = { |
| 44 | + spades: '\u{2660}', |
| 45 | + hearts: '\u{2665}', |
| 46 | + diamonds: '\u{2666}', |
| 47 | + clubs: '\u{2663}' |
| 48 | +}; |
| 49 | +this.numRanks = this.rankSymbols.length; |
| 50 | + |
| 51 | +let index = 0; |
| 52 | +for (let suit of this.suits) { |
| 53 | + for (let rank = 1; rank <= this.numRanks; rank++) { |
| 54 | + this.cards[index] = new Card(rank, suit, this.rankSymbols[rank - 1], this.suitEmojis[suit]); |
| 55 | + this.dealt[index] = 0; |
| 56 | + index++; |
| 57 | + } |
| 58 | +} |
| 59 | +}/* » */ |
| 60 | + |
| 61 | +deal(index) {/* « */ |
| 62 | +if (index < 0 || index >= this.cards.length || this.dealt[index] === 1) { |
| 63 | + return null; |
| 64 | +} |
| 65 | +this.dealt[index] = 1; |
| 66 | +return this.cards[index]; |
| 67 | +}/* » */ |
| 68 | + |
| 69 | +getIndexFromLiteral(literal) {/* « */ |
| 70 | +const [rankStr, suit] = literal.split('-'); |
| 71 | +const rank = parseInt(rankStr); |
| 72 | +const suitIndex = this.suits.indexOf(suit); |
| 73 | +if (rank < 1 || rank > this.numRanks || suitIndex === -1) { |
| 74 | + return -1; |
| 75 | +} |
| 76 | +return (suitIndex * this.numRanks) + (rank - 1); |
| 77 | +}/* » */ |
| 78 | +getIndexFromPictograph(pictograph) {/* « */ |
| 79 | +const [rankSymbol, emoji] = pictograph.split('-'); |
| 80 | +const rank = this.rankSymbols.indexOf(rankSymbol) + 1; |
| 81 | +const suit = Object.keys(this.suitEmojis).find(key => this.suitEmojis[key] === emoji); |
| 82 | +if (!suit || rank < 1) { |
| 83 | + return -1; |
| 84 | +} |
| 85 | +const suitIndex = this.suits.indexOf(suit); |
| 86 | +return (suitIndex * this.numRanks) + (rank - 1); |
| 87 | +}/* » */ |
| 88 | +getRankFromIndex(index) {/* « */ |
| 89 | +if (index < 0 || index >= this.cards.length) { |
| 90 | + return -1; |
| 91 | +} |
| 92 | +return this.cards[index].rank; |
| 93 | +}/* » */ |
| 94 | +getRankFromLiteral(literal) {/* « */ |
| 95 | +const index = this.getIndexFromLiteral(literal); |
| 96 | +return this.getRankFromIndex(index); |
| 97 | +}/* » */ |
| 98 | +getRankFromPictograph(pictograph) {/* « */ |
| 99 | +const index = this.getIndexFromPictograph(pictograph); |
| 100 | +return this.getRankFromIndex(index); |
| 101 | +}/* » */ |
| 102 | + |
| 103 | +toString(){/* « */ |
| 104 | +return `Deck(${this.type})`; |
| 105 | +}/* » */ |
| 106 | + |
| 107 | +}/* » */ |
| 108 | + |
| 109 | +class Player {/* « */ |
| 110 | +constructor(index) { |
| 111 | + this.name = `${index + 1}`; |
| 112 | + this.cards = []; |
| 113 | +} |
| 114 | +}/* » */ |
| 115 | + |
| 116 | +class Dealer { |
| 117 | + |
| 118 | +constructor(deckType, players, cardsPerPlayer) {/* « */ |
| 119 | + this.deck = new Deck(deckType); |
| 120 | + this.players = players; |
| 121 | + this.cardsPerPlayer = cardsPerPlayer; |
| 122 | + this.validateDeal(); |
| 123 | +}/* » */ |
| 124 | +validateDeal() {/* « */ |
| 125 | + const totalCards = this.players.length * this.cardsPerPlayer; |
| 126 | + if (totalCards <= 0 || totalCards > this.deck.cards.length) { |
| 127 | + throw new Error('Invalid number of cards to deal'); |
| 128 | + } |
| 129 | +}/* » */ |
| 130 | +deal() {/* « */ |
| 131 | + let availableIndices = Array.from({ length: this.deck.cards.length }, (_, i) => i); |
| 132 | + for (let player of this.players) { |
| 133 | + player.cards = []; |
| 134 | + for (let i = 0; i < this.cardsPerPlayer; i++) { |
| 135 | + if (availableIndices.length === 0) break; |
| 136 | + const randomIndex = Math.floor(Math.random() * availableIndices.length); |
| 137 | + const cardIndex = availableIndices.splice(randomIndex, 1)[0]; |
| 138 | + const card = this.deck.deal(cardIndex); |
| 139 | + if (card) player.cards.push(card); |
| 140 | + } |
| 141 | + } |
| 142 | +}/* » */ |
| 143 | + |
| 144 | +} |
| 145 | + |
8 | 146 | //Commands« |
9 | 147 | const com_poker = class extends Com { |
10 | 148 | run(){ |
11 | | - this.ok("Hello Poker2: That was honslerativity!!!!!"); |
| 149 | + |
| 150 | +let numPlayers = 4; |
| 151 | +let numCards = 5; |
| 152 | +let players = []; |
| 153 | +for (let i=0; i <numPlayers; i++){ |
| 154 | +let player = new Player(i); |
| 155 | +players.push(player); |
| 156 | +} |
| 157 | +let dealer = new Dealer("standard", players, numCards); |
| 158 | +dealer.deal(); |
| 159 | +log(players); |
| 160 | +//log(dealer); |
| 161 | + |
| 162 | +// this.ok("Hello Poker2: That was honslerativity!!!!!"); |
| 163 | +//let deck = new Deck("standard"); |
| 164 | +//log(deck); |
| 165 | +//this.out(deck); |
| 166 | +this.ok(); |
12 | 167 | } |
13 | 168 | } |
14 | 169 | //» |
|
0 commit comments