Playing cards: Difference between revisions

m
fix
(add FreeBASIC)
m (fix)
Line 2,391:
}
</lang>
 
=={{header|FreeBASIC}}==
<lang freebasic>#define NSWAPS 100
 
type card
suit : 2 as ubyte
face : 4 as ubyte
'the remaining 2 bits are unused
end type
 
dim shared as string*8 Suits(0 to 3) = {"Spades", "Clubs", "Hearts", "Diamonds"}
dim shared as string*8 Faces(0 to 12) = {"Ace", "Two", "Three", "Four", "Five",_
"Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}
 
sub newdeck( deck() as card )
'produces an unshuffled deck of 52 cards
redim preserve deck(0 to 51) as card
for s as ubyte = 0 to 3
for f as ubyte = 0 to 12
deck(13*s + f).suit = s
deck(13*s + f).face = f
next f
next s
end sub
 
function deal( deck() as card ) as card
'deals one card from the top of the deck, returning that
'card and removing it from the deck
dim as card dealt = deck(ubound(deck))
redim preserve deck(0 to ubound(deck) - 1)
return dealt
end function
 
function card_name( c as card ) as string
'returns the name of a single given card
return Faces(c.face) + " of " + Suits(c.suit)
end function
 
sub print_deck( deck() as card )
'displays the contents of the deck,
'with the top card (next to be dealt) first
for i as byte = ubound(deck) to 0 step -1
print card_name( deck(i) )
next i
end sub
 
sub shuffle_deck( deck() as card )
dim as integer n = ubound(deck)+1
for i as integer = 1 to NSWAPS
swap deck( int(rnd*n) ), deck( int(rnd*n) )
next i
end sub
redim as card deck(0 to 0) 'allocate a new deck
newdeck(deck()) 'set up the new deck
print "Dealing a card: ", card_name( deal( deck() ) )
for j as integer = 1 to 41 'deal another 41 cards and discard them
deal(deck())
next j
shuffle_deck(deck()) 'shuffle the remaining cards
print_deck(deck()) 'display the last ten cards</lang>
{{out}}<pre>Dealing a card: King of Diamonds
Ace of Spades
Eight of Spades
Nine of Spades
Two of Spades
Ten of Spades
Six of Spades
Five of Spades
Three of Spades
Four of Spades
Seven of Spades</pre>
 
=={{header|Forth}}==
Line 2,580 ⟶ 2,508:
END PROGRAM</lang>
This creates a new deck, shuffles it, deals five cards to hand, prints the cards in hand and then prints the cards remaining in the deck.
 
=={{header|FreeBASIC}}==
<lang freebasic>#define NSWAPS 100
 
type card
suit : 2 as ubyte
face : 4 as ubyte
'the remaining 2 bits are unused
end type
 
dim shared as string*8 Suits(0 to 3) = {"Spades", "Clubs", "Hearts", "Diamonds"}
dim shared as string*8 Faces(0 to 12) = {"Ace", "Two", "Three", "Four", "Five",_
"Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}
 
sub newdeck( deck() as card )
'produces an unshuffled deck of 52 cards
redim preserve deck(0 to 51) as card
for s as ubyte = 0 to 3
for f as ubyte = 0 to 12
deck(13*s + f).suit = s
deck(13*s + f).face = f
next f
next s
end sub
 
function deal( deck() as card ) as card
'deals one card from the top of the deck, returning that
'card and removing it from the deck
dim as card dealt = deck(ubound(deck))
redim preserve deck(0 to ubound(deck) - 1)
return dealt
end function
 
function card_name( c as card ) as string
'returns the name of a single given card
return Faces(c.face) + " of " + Suits(c.suit)
end function
 
sub print_deck( deck() as card )
'displays the contents of the deck,
'with the top card (next to be dealt) first
for i as byte = ubound(deck) to 0 step -1
print card_name( deck(i) )
next i
end sub
 
sub shuffle_deck( deck() as card )
dim as integer n = ubound(deck)+1
for i as integer = 1 to NSWAPS
swap deck( int(rnd*n) ), deck( int(rnd*n) )
next i
end sub
redim as card deck(0 to 0) 'allocate a new deck
newdeck(deck()) 'set up the new deck
print "Dealing a card: ", card_name( deal( deck() ) )
for j as integer = 1 to 41 'deal another 41 cards and discard them
deal(deck())
next j
shuffle_deck(deck()) 'shuffle the remaining cards
print_deck(deck()) 'display the last ten cards</lang>
{{out}}<pre>Dealing a card: King of Diamonds
Ace of Spades
Eight of Spades
Nine of Spades
Two of Spades
Ten of Spades
Six of Spades
Five of Spades
Three of Spades
Four of Spades
Seven of Spades</pre>
 
=={{header|Go}}==
781

edits