Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma Ada_2022;
- pragma Extensions_Allowed (On);
- with Ada.Text_IO; use Ada.Text_IO;
- with Ada.Numerics.Discrete_Random;
- procedure Test_Card is
- package Card_Package is
- CC_error: exception;
- type Card_Suite is (Clubs,Spades,Hearts,Diamonds);
- type Card_Value is (Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Ace,Jack,Queen,King);
- type Cards is record
- Suite: Card_Suite;
- Value: Card_Value;
- end record;
- Max_Card_Number : constant :=
- (Card_Suite'Pos(Card_Suite'Last)+1)
- *(Card_Value'Pos(Card_value'Last)+1);
- type Card_list is array (Natural range <>) of Cards;
- subtype Card_number_type is Natural range 0..Max_Card_Number;
- function Default_list (Card_number: Card_number_type) return Card_List;
- type Card_Deck (Number: Card_number_type := Max_card_number) is record
- List: Card_list (1..Number):= Default_list(Number);
- end record;
- procedure Deal (Card: out Cards; Deck: in out Card_Deck);
- end Card_Package;
- package body Card_Package is
- procedure Deal (Card: out Cards; Deck: in out Card_Deck) is
- begin
- if Deck.Number < 1 then raise Constraint_Error; end if;
- Card := Deck.List(Deck.List'First);
- Deck := (Deck.Number - 1, Deck.list(2..Deck.List'Last));
- end Deal;
- function Default_list (Card_number: Card_number_type) return Card_List is
- package RSP is new Ada.Numerics.Discrete_Random (Card_suite);
- package RVP is new Ada.Numerics.Discrete_Random (Card_value);
- use RSP, RVP;
- Value_Gen: RVP.Generator;
- Suite_Gen: RSP.Generator;
- type Array_boolean is array (Card_suite,Card_Value) of Boolean;
- Cards_done: Array_boolean := (others => (others => False));
- Random_Card: Cards;
- List: Card_List (1..Card_Number);
- begin
- Reset(Value_Gen); Reset(Suite_Gen);
- for I in 1..Card_Number loop
- loop
- Random_Card := (Random(Suite_Gen),Random(Value_Gen));
- exit when not Cards_Done (Random_Card.Suite,Random_Card.Value);
- end loop;
- List(I) := Random_Card;
- Cards_Done (Random_Card.Suite, Random_Card.Value) := true;
- end loop;
- return List;
- end Default_list;
- end Card_Package;
- use Card_Package;
- D : Card_Deck(10);
- C : Cards;
- begin
- Put_line (D.Number'Image, D.List'Length'Image);
- Deal (C, D);
- end Test_Card;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement