RosettaCodeData/Task/Playing-cards/M2000-Interpreter/playing-cards.m2000
2023-07-01 13:44:08 -04:00

108 lines
3.2 KiB
Text

Module PlayCards {
Font "Arial" ' Ensure characters exist for Suits
Cls 15,0
Pen 0
Inventory Suits = "♠":=0, "♥":=4, "♦":=4, "♣":=0 'suit -> color
Inventory Cards = "two":=2, "three":=3, "four":=4, "five":=5
Append Cards, "six":=6, "seven":=7, "eight":=8, "nine":=9
Append Cards, "ten":=10, "jack":=10, "queen":=10, "king":=10, "ace":=1
DealerMoney=0
PrintCardOnly = Lambda Suits, Cards (k, nl=True)-> {
For k {
Pen Suits(.suit!) {
If nl then {
Print Part @(10), Eval$(Suits, .suit)+Eval$(Cards, .card)
Print
} Else Print Eval$(Suits, .suit)+Eval$(Cards, .card),
}
}
}
' Using a stack object
StackPack = Stack
Module AppendArray (N, A) {
Stack N {Data !A}
}
Class OneCard {
suit=-1, Card
Class:
Module OneCard {
\\ ? for optional reading
read ? .suit, .card
}
}
Decks=1
Dim Pack(Len(Cards)*Len(Suits)*Decks)
k=0
\\ fill cards to Pack()
For times=1 To Decks {
N=each(Suits)
While N {
M=each(Cards)
While M {
Pack(k)=OneCard(N^, M^)
k++
}
}
}
DisplayAll() ' in order
Suffle()
DisplayAll() ' at random positions
Print
Card=OneCard()
Print "Cards in Deck:";Len(StackPack)
For i=1 to 60 {
NextCard()
Print "Get Card:";
Call PrintCardOnly(Card)
Print
Print "Cards in Deck:";Len(StackPack)
DisplayDeck()
Print
}
Sub Suffle()
Print
Local N=Len(Pack())-1, N2, i, j, total=N*4+4, cur=1
For j=1 To 4 {
For i=0 To N {
If cur Mod 4=3 Then Print Over format$("Suffle {0:0}%",cur/total*100)
N2=random(0, N)
While N2=i {N2=random(0, N)}
Swap Pack(i), Pack(N2)
cur++
}
}
AppendArray StackPack, Pack()
Print
End Sub
Sub DisplayDeck()
local m=each(StackPack)
While m {
Call PrintCardOnly(StackItem(m), False)
}
End Sub
Sub DisplayAll()
For k=0 To Len(Pack())-1 {
PrintCard(k)
}
End Sub
Sub PrintCard(k)
For Pack(k) {
Pen Suits(.suit!) {
Print Eval$(Suits, .suit)+Eval$(Cards, .card),
}
}
End Sub
Sub NextCard()
If Len(StackPack)=0 Then {
Suffle()
Stack StackPack {
Drop Random(0, 51)
}
}
Stack StackPack {
Read Card
}
End Sub
}
PlayCards