comp.lang.ada
 help / color / mirror / Atom feed
From: rieachus@comcast.net
Subject: Playing cards.
Date: Sat, 26 Mar 2016 11:50:55 -0700 (PDT)
Date: 2016-03-26T11:50:55-07:00	[thread overview]
Message-ID: <72436e28-13a7-4580-9503-0bd7111f4bab@googlegroups.com> (raw)

I am working on a package which provides playing cards (and a surface) for programs that need.  I'm working on a program for testing bridge bidding systems. In other words to find hands that have no defined bid, or more than one bid in a must situation.

I would like to avoid using GTK if possible, to make installation easier, but if anyone already has such a package no need to duplicate effort.  Package spec is below.  If you want to discuss the random number issues, start a new discussion. I am sure it will get long quickly. ;-)

package Cards is
-- an object, model, view of a deck of cards and a playing surface.
-- decks can be single or double, regular or pinocle, with or without jokers.
-- Cards can be invisible, located on the table, or in a "hand", cards may be
-- assigned to a stack or stacks on the table, and are face up or down.

   type Card is private;
   type Deck is array (Integer range <>) of Card;

   type Decks is (Single, Double, Single_Pinochle, Double_Pinochle,
     One_Joker, Two_Jokers);

   function Create_Deck(Deck_Type: Decks) return Deck;

   type Rank is (Ace, Deuce, Trey, Four, Five, Six, Seven, Eight, Nine,
                 Ten, Jack, Queen, King);

   type Suit is (Spade, Heart, Diamond, Club, Joker);
   -- Joker rank is undefined, but can be assigned, for example when
   -- showing a poker hand.

   function Rank_Is (C: in Card) return Rank;
   function Suit_Is (C: in Card) return Suit;

   procedure Shuffle(D: in out Deck);
   -- all cards are set to in_hand, face_down. And
   -- well shuffled.  Long note:  A 52 card deck has 52! (~8x10^67)
   -- possible orders.  Game mechanics lower the number of different
   -- deals. For example there are only 5.36x10^28 differen bridge
   -- deals.  Obviosly, a 32-bit (around 4 billion if unsigned)
   -- pseudo-random number generator won't do justice. A 64-bit PRNG
   -- is not much better.  The method used here is to use four
   -- different unsigned PRNGs to assign values to cards in turn,
   -- then sort on these values.  It is a task left to the reader to
   -- compute what percentage of the possible bridge deals can
   -- actually occur. ;-)

   procedure Face (C: in Card);
   -- make a card visible;

   procedure Face_Down (C: in Card);
   -- show the back of the card if/when it is displayed.

   function Face_Up return Boolean;

   type Position is record
      X: Positive;
      Y: Positive;
   end record;

   procedure Create_Table(Far_Corner: Position := (320, 320));
   -- Initial value is really there it insure that tables always have sizes.

   procedure Play(C: Card; P: Position);
   -- move the card from its current location to the table and turn it face
   -- up if necessary.

   procedure Discard(C: Card; P: Position; Display: Boolean := True);
   -- same intent as Play but with the card face down, or completely gone
   -- from the display. Note that discarded cards are still part of the
   -- deck.

   procedure In_Hand(C: Card);
   -- place a card in the hand.

   function In_Hand (C: Card) return Boolean;

   function Hand return Deck;
   -- the deck in this case is an array of cards usually smaller than the
   -- original deck. returns a full array from 1 to n of the cards
   --  currently in the hand.

   procedure Sort_Hand (Suits: Boolean := True; Up: Boolean := True);
   -- sort the hand.  Suits first if suits is true; higher ranking cards
   -- get higher numbers if Up is true.

   procedure Update;
   -- Update the physical display to match the current card locations.
   -- Fancy transition graphics optional.

private

  type Real_Card;

  type Card is access all Real_Card;

  -- Model magic is going on here.  Creating a deck does not need to
  -- use the heap, and will usually create an array of Real_Cards on
  -- the stack.  When you leave the scope of a Deck, everything goes
  -- away in finalization.

end Cards;

             reply	other threads:[~2016-03-26 18:50 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-26 18:50 rieachus [this message]
2016-03-26 20:46 ` Playing cards Nasser M. Abbasi
2016-03-26 21:51 ` rieachus
2016-03-28 21:13   ` Randy Brukardt
2016-03-29  0:08     ` Dennis Lee Bieber
2016-03-29  3:29 ` rieachus
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox