Anonymous user
Playing cards: Difference between revisions
m
Fixed lang tags.
Underscore (talk | contribs) (→{{header|Perl 6}}: "multi prefix:<~>" → "method Str". This change makes it possible to privatize the instance variables, so I did.) |
m (Fixed lang tags.) |
||
Line 4:
=={{header|Ada}}==
Here is the package specification for a deck of playing cards.
<lang ada>package Playing_Cards is
pragma Elaborate_Body(Playing_Cards);
Line 31 ⟶ 30:
Deck_Offsets : Deck_Reference;
end record;
end Playing_Cards;</lang>
Here is the package body for that same playing card package. This implementation stores one array of cards in sorted order. Each deck contains an array of indices into that one array of cards. Shuffling the deck actually results in randomizing the order of those indices into the array of cards. This approach maximizes shuffling efficiency by only exchanging indices. It also maximizes memory efficiency since an array of cards requires more memory than an array of indices.
<lang ada>with Ada.Numerics.Discrete_Random;
With Ada.Text_IO;
Line 132 ⟶ 129:
end loop;
end loop;
end Playing_Cards;</lang>
=={{header|ALGOL 68}}==
Line 146 ⟶ 142:
* [http://www.csail.mit.edu/timeline/timeline.php?query=event&id=93 LOC was essential; Algol 68's REF was a mistake.]
<lang
# class members & attributes #
Line 693 ⟶ 689:
(sort list #'(lambda (x y)
(declare (ignore x y))
(zerop (random 2)))))</lang>
=={{header|D}}==
Line 906 ⟶ 901:
=={{header|Forth}}==
{{works with|GNU Forth}}
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<lang fortran>
IMPLICIT NONE
TYPE Card
CHARACTER(5) :: value
CHARACTER(8) :: suit
END TYPE Card
TYPE(Card) :: deck(52), hand(52)
TYPE(Card) :: temp
CHARACTER(5) :: pip(13) = (/"Two ", "Three", "Four ", "Five ", "Six ", "Seven", "Eight", "Nine ", "Ten ", &
"Jack ", "Queen", "King ", "Ace "/)
CHARACTER(8) :: suits(4) = (/"Clubs ", "Diamonds", "Hearts ", "Spades "/)
INTEGER :: i, j, n, rand, dealt = 0
REAL :: x
CONTAINS
SUBROUTINE Init_deck
! Create deck
DO i = 1, 4
DO j = 1, 13
deck((i-1)*13+j) = Card(pip(j), suits(i))
END DO
END DO
END SUBROUTINE Init_deck
SUBROUTINE Shuffle_deck
! Shuffle deck using Fisher-Yates algorithm
DO i = 52-dealt, 1, -1
CALL RANDOM_NUMBER(x)
rand = INT(x * i) + 1
temp = deck(rand)
deck(rand) = deck(i)
deck(i) = temp
END DO
END SUBROUTINE Shuffle_deck
SUBROUTINE Deal_hand(number)
! Deal from deck to hand
INTEGER :: number
DO i = 1, number
hand(i) = deck(dealt+1)
dealt = dealt + 1
END DO
END SUBROUTINE
SUBROUTINE Print_hand
! Print cards in hand
DO i = 1, dealt
WRITE (*, "(3A)") TRIM(deck(i)%value), " of ", TRIM(deck(i)%suit)
END DO
WRITE(*,*)
END SUBROUTINE Print_hand
SUBROUTINE Print_deck
! Print cards in deck
DO i = dealt+1, 52
WRITE (*, "(3A)") TRIM(deck(i)%value), " of ", TRIM(deck(i)%suit)
END DO
WRITE(*,*)
END SUBROUTINE Print_deck
END MODULE Cards</lang>
Example use:
<lang fortran>PROGRAM Playing_Cards
USE Cards
CALL Init_deck
CALL Shuffle_deck
CALL Deal_hand(5)
CALL Print_hand
CALL Print_deck
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.
Line 1,024 ⟶ 1,019:
Straightforward implementation with explicit names for pips and suits. A deck is just a list of cards. Dealing consists of splitting off cards from the beginning of the list by the usual pattern matching (not shown). Printing is automatic. Purely functional shuffling is a bit tricky, so here we just do the naive quadratic version. This also works for other than full decks.
<lang haskell>import System.Random
data Pip = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten |
Line 1,051 ⟶ 1,045:
shuffle' g [] _ ys = ys
shuffle' g (x:xs) n ys = shuffle' g' xs (n+1) (insertAt k x ys) where
(k,g') = randomR (0,n) g</lang>
=={{header|J}}==
)</lang>
Example use:
<lang j> load 'coutil'
1</lang>
=={{header|Java}}==
Line 1,170 ⟶ 1,163:
=={{header|JavaScript}}==
<lang javascript>function Card(pip, suit) {
this.pip = pip;
this.suit = suit;
Line 1,201 ⟶ 1,193:
return this.deck.shift();
};
}</lang>
=={{header|Logo}}==
{{works with|UCB Logo}}
<lang logo>make "suits {Diamonds Hearts Clubs Spades}
make "pips {Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King}
Line 1,239 ⟶ 1,229:
new.deck
shuffle.deck
repeat 5 [deal.card]</lang>
=={{header|M4}}==
<lang M4>define(`randSeed',141592653)dnl
define(`setRand',
`define(`randSeed',ifelse(eval($1<10000),1,`eval(20000-$1)',`$1'))')dnl
Line 1,283 ⟶ 1,271:
deal deal(`b')
deal deal(`b')
show(`b')</lang>
=={{header|OCaml}}==
Line 1,426 ⟶ 1,413:
def deal(self):
self.shuffle() # Can't tell what is next from self.deck
return self.deck.pop(0)</lang>
=={{header|R}}==
<lang R>pips <- c("2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace")
suit <- c("Clubs", "Diamonds", "Hearts", "Spades")
# Create a deck
Line 1,455 ⟶ 1,440:
deck <- deal(deck)
# While no-one is looking, sneakily deal a card from the bottom of the pack
deck <- deal(deck, FALSE)</lang>
=={{header|Ruby}}==
Line 1,702 ⟶ 1,686:
Each users hand is another edit buffer.
There is no need for any routines to display the deck or a hand, since each of them is displayed in an edit window.
<lang vedit>// Playing Cards, main program
Call("CREATE_DECK")
Line 1,785 ⟶ 1,768:
Buf_Switch(#11) // players hand
Reg_ins(9) // insert the cards here
Return</lang>
|