Playing cards: Difference between revisions

Dealing works now
(Dealing works now)
Line 1,453:
=={{header|VBScript}}==
Haven't got the dealing part worked out yet.
=={{header|VBScript}}==
===Implementation===
=====Implementation=====
<lang vb>class playingcard
<lang vb>
class playingcard
dim suit
dim pips
end class
 
class carddeck
dim suitnames
private suitnames
suitnames = split("hearts,diamonds,clubs,spades",",")
private pipnames
private cardno
private deck(52)
private nTop
sub class_initialize
dim suit
dim pips
suitnames = split("H,D,C,S",",")
pipnames = split("A,2,3,4,5,6,7,8,9,10,J,Q,K",",")
cardno = 0
 
for suit = 1 to 4
dim pipnames
for pips = 1 to 13
pipnames = split("ace,two,three,four,five,six,seven,eight,nine,ten,jack,queen,king",",")
set deck(cardno) = new playingcard
deck(cardno).suit = suitnames(suit-1)
deck(cardno).pips = pipnames(pips-1)
cardno = cardno + 1
next
next
nTop = 0
end sub
public sub showdeck
dim a
redim a(51-nTop)
for i = nTop to 51
a(i) = deck(i).pips & deck(i).suit
next
wscript.echo join( a, ", ")
end sub
public sub shuffle
dim r
randomize timer
for i = nTop to 51
r = int( rnd * ( 52 - nTop ) )
if r <> i then
objswap deck(i),deck(r)
end if
next
end sub
 
public function deal()
dim cardno
set deal = deck( nTop )
cardno = 0
nTop = nTop + 1
end function
 
public property get cardsRemaining
dim deck(52)
cardsRemaining = 52 - nTop
end property
private sub objswap( a, b )
dim tmp
set tmp = a
set a = b
set b = tmp
end sub
end class
</lang>
 
=====Invocation=====
dim suit
<lang vb>
dim pips
dim pack
set pack = new carddeck
wscript.echo "--before shuffle"
pack.showdeck
pack.shuffle
wscript.echo "--before shuffle"
pack.showdeck
 
dim card
for suit = 1 to 4
for pipsi = 1 to 1352
set card = pack.deal
set deck(cardno) = new playingcard
deck(cardno).suit = suitnames(suit-1)
deck(cardno).pips = pipnames(pips-1)
cardno = cardno + 1
next
next
wscript.echo "--dealt a card, it's the", card.pips, "of", card.suit
wscript.echo "--", pack.cardsRemaining, "cards remaining"
if pack.cardsRemaining <> 0 then
pack.showdeck
end if
 
</lang>
wscript.echo "-- before shuffling"
for i = 0 to 51 step 4
wscript.echo deck(i).pips,"of",deck(i).suit,",",deck(i+1).pips,"of",deck(i+1).suit,",",deck(i+2).pips,"of",deck(i+2).suit,",",deck(i+3).pips,"of",deck(i+3).suit
next
 
=====Output=====
dim r
<pre>
randomize timer
--before shuffle
for i = 0 to 51
AH, 2H, 3H, 4H, 5H, 6H, 7H, 8H, 9H, 10H, JH, QH, KH, AD, 2D, 3D, 4D, 5D, 6D, 7D, 8D, 9D, 10D, JD, QD, KD, AC, 2C, 3C, 4C, 5C, 6C, 7C, 8C, 9C, 10C, JC, QC, KC, AS, 2S, 3S, 4S, 5S, 6S, 7S, 8S, 9S, 10S, JS, QS, KS
r = int( rnd * 52 )
--before shuffle
if r <> i then
QD, QH, 4S, KD, JC, 10H, JD, 6D, 2S, 4C, 4D, 8H, QC, 5S, JH, KS, 6H, 8S, 7D, 10D, AD, 9S, KH, 2D, 3S, AC, JS, 3D, 9D, 3H, 5C, 10S, KC, 6C, AH, AS, 6S, 5H, 3C, 4H, 9H, 8C, 7S, 9C, 10C, 2C, 7H, 5D, QS, 2H, 7C, 8D
objswap deck(i),deck(r)
--dealt a card, it's the 8 of D
end if
-- 0 cards remaining
next
</pre>
 
wscript.echo "-- after shuffling"
for i = 0 to 51 step 4
wscript.echo deck(i).pips,"of",deck(i).suit,",", _
deck(i+1).pips,"of",deck(i+1).suit,",", _
deck(i+2).pips,"of",deck(i+2).suit,",",_
deck(i+3).pips,"of",deck(i+3).suit
next
 
sub objswap( a, b )
dim tmp
set tmp = a
set a = b
set b = tmp
end sub
</lang>
===Output===
<lang vb>>cscript "C:\foo\cards.vbs"
Anonymous user