Skip to content

Commit e528e6f

Browse files
committed
Wheee! What a lineup!!!
1 parent 83eceeb commit e528e6f

2 files changed

Lines changed: 163 additions & 3 deletions

File tree

apps/Terminal.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2700,13 +2700,18 @@ lines array (otherwise, the message gets printed onto the actor's screen.
27002700

27012701
if (!isStr(out)) {
27022702
//cwarn("Here is the non-string object");
2703-
log(out);
27042703
//This is not a bug since it is perfectly "okay" (I think) to pass aribtrary objects
27052704
//*through* a pipeline... but not out the end of it.
2705+
if (out.toString instanceof Function){
2706+
out = out.toString();
2707+
}
2708+
else {
2709+
log(out);
27062710
let str = `non-String object found in output stream (see console)`;
27072711
if (opts.name) str = `${opts.name}: ${str}`;
27082712
out = `sh: ${str}`;
27092713
opts = {isWrn: true};
2714+
}
27102715
}
27112716

27122717
let {didFmt, colors, pretty, isErr, isSuc, isWrn, isInf, noBr} = opts;

coms/games/poker.js

Lines changed: 157 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,169 @@
11
//Imports«
22

33
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;
55

66
//»
77

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+
8146
//Commands«
9147
const com_poker = class extends Com {
10148
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();
12167
}
13168
}
14169
//»

0 commit comments

Comments
 (0)