% Nick Neufeld
% February 10, 2007
% Start of an object oriented card game
type Card :
record
suit : string
value : string
end record
% class for full deck of cards
class FullDeck
import Card
export initialize, shuffle, deal
% function that generates the original deck of cards
fcn initialize : array 1 .. 52 of Card
var cards : array 1 .. 52 of Card
var suits : array 1 .. 4 of string := init ("Hearts", "Diamonds", "Spades", "Clubs")
var values : array 1 .. 13 of string := init ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King")
var count : int := 1
for i : 1 .. 13
for q : 1 .. 4
cards (count).value := values (i)
cards (count).suit := suits (q)
count += 1
end for
end for
result cards
end initialize
% function that shuffles the deck of cards
fcn shuffle (cards : array 1 .. 52 of Card) : array 1 .. 52 of Card
var shuffled : array 1 .. 52 of Card
var doneShuffle : array 1 .. 52 of boolean
var randPos : int := 0
var count : int := 1
for i : 1 .. 52
doneShuffle (i) := false
end for
loop
randPos := Rand.Int (1, 52)
if not doneShuffle (randPos) then
shuffled (randPos) := cards (count)
doneShuffle (randPos) := true
count += 1
end if
exit when count = 53
end loop
result shuffled
end shuffle
% function that deals the cards to four players
fcn deal (cards : array 1 .. 52 of Card) : array 1 .. 4, 1 .. 13 of Card
var dealtCards : array 1 .. 4, 1 .. 13 of Card
var count : int := 1
for i : 1 .. 13
for q : 1 .. 4
dealtCards (q, i) := cards (count)
count += 1
end for
end for
result dealtCards
end deal
end FullDeck
var deck : pointer to FullDeck
new FullDeck, deck
var allCards := deck -> shuffle (deck -> initialize)
/* for i : 1 .. 52
put allCards (i).value, " of ", allCards (i).suit
end for */
var pcards := deck -> deal (allCards)
put ""
for i : 1 .. 4
put "Player ", i, " cards: "
for q : 1 .. 13
put pcards (i, q).value, " of ", pcards (i, q).suit
end for
put ""
end for
|