Advertisement
evanescente-ondine

Untitled

Jul 13th, 2024 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. pragma Ada_2022;
  2. pragma Extensions_Allowed (On);
  3. with Ada.Text_IO; use Ada.Text_IO;
  4. with Ada.Numerics.Discrete_Random;
  5.  
  6. procedure Test_Card is
  7. package Card_Package is
  8. CC_error: exception;
  9. type Card_Suite is (Clubs,Spades,Hearts,Diamonds);
  10. type Card_Value is (Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Ace,Jack,Queen,King);
  11. type Cards is record
  12. Suite: Card_Suite;
  13. Value: Card_Value;
  14. end record;
  15. Max_Card_Number : constant :=
  16. (Card_Suite'Pos(Card_Suite'Last)+1)
  17. *(Card_Value'Pos(Card_value'Last)+1);
  18. type Card_list is array (Natural range <>) of Cards;
  19.  
  20. subtype Card_number_type is Natural range 0..Max_Card_Number;
  21.  
  22. function Default_list (Card_number: Card_number_type) return Card_List;
  23. type Card_Deck (Number: Card_number_type := Max_card_number) is record
  24. List: Card_list (1..Number):= Default_list(Number);
  25. end record;
  26. procedure Deal (Card: out Cards; Deck: in out Card_Deck);
  27. end Card_Package;
  28.  
  29. package body Card_Package is
  30. procedure Deal (Card: out Cards; Deck: in out Card_Deck) is
  31. begin
  32. if Deck.Number < 1 then raise Constraint_Error; end if;
  33. Card := Deck.List(Deck.List'First);
  34. Deck := (Deck.Number - 1, Deck.list(2..Deck.List'Last));
  35. end Deal;
  36. function Default_list (Card_number: Card_number_type) return Card_List is
  37. package RSP is new Ada.Numerics.Discrete_Random (Card_suite);
  38. package RVP is new Ada.Numerics.Discrete_Random (Card_value);
  39. use RSP, RVP;
  40. Value_Gen: RVP.Generator;
  41. Suite_Gen: RSP.Generator;
  42. type Array_boolean is array (Card_suite,Card_Value) of Boolean;
  43. Cards_done: Array_boolean := (others => (others => False));
  44. Random_Card: Cards;
  45. List: Card_List (1..Card_Number);
  46. begin
  47. Reset(Value_Gen); Reset(Suite_Gen);
  48. for I in 1..Card_Number loop
  49. loop
  50. Random_Card := (Random(Suite_Gen),Random(Value_Gen));
  51. exit when not Cards_Done (Random_Card.Suite,Random_Card.Value);
  52. end loop;
  53. List(I) := Random_Card;
  54. Cards_Done (Random_Card.Suite, Random_Card.Value) := true;
  55. end loop;
  56. return List;
  57. end Default_list;
  58. end Card_Package;
  59. use Card_Package;
  60. D : Card_Deck(10);
  61. C : Cards;
  62. begin
  63. Put_line (D.Number'Image, D.List'Length'Image);
  64. Deal (C, D);
  65. end Test_Card;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement