File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -59,7 +59,7 @@ void dh_pcg_srand_auto(void) {
5959 pcg32_srandom_r (& rng , initstate , initseq );
6060}
6161
62- void dh_init_deck (struct dh_deck * deck ) {
62+ static void dh_init_deck (struct dh_deck * deck ) {
6363 deck -> top_card = 0 ;
6464
6565 int card = 0 ;
@@ -77,10 +77,15 @@ void dh_init_deck(struct dh_deck *deck) {
7777
7878 card ++ ;
7979 }
80-
8180 return ;
8281}
8382
83+ struct dh_deck dh_get_new_deck (void ) {
84+ struct dh_deck deck ;
85+ dh_init_deck (& deck );
86+ return deck ;
87+ }
88+
8489struct dh_card dh_deal_top_card (struct dh_deck * deck ) {
8590 if (deck -> top_card == CARDS_IN_DECK ) {
8691 deck -> top_card = 0 ;
Original file line number Diff line number Diff line change @@ -88,12 +88,25 @@ void dh_pcg_srand(uint64_t initstate, uint64_t initseq);
8888void dh_pcg_srand_auto (void );
8989
9090/**
91- * @brief Initialize a deck in a sorted order (no shuffling) .
91+ * @brief Create and initialize a new deck of cards .
9292 *
93- * @param deck_dh Pointer to the deck to initialize.
93+ * This function returns a new `dh_deck` struct that is initialized to a full, shuffled deck.
94+ * Internally, it calls `dh_init_deck()` to perform the initialization.
95+ *
96+ * @return A fully initialized deck of cards.
9497 */
95- void dh_init_deck ( struct dh_deck * deck );
98+ struct dh_deck dh_get_new_deck ( void );
9699
100+ /**
101+ * @brief Deal the top card from the deck.
102+ *
103+ * Deals the card currently at the top of the deck and advances the deck position.
104+ * If all cards have been dealt (`top_card == CARDS_IN_DECK`), the deck wraps and begins from the
105+ * top again, resetting `top_card` to 0 and printing a warning message.
106+ *
107+ * @param deck Pointer to the deck from which to deal a card.
108+ * @return The card at the current top position of the deck.
109+ */
97110struct dh_card dh_deal_top_card (struct dh_deck * deck );
98111
99112/**
Original file line number Diff line number Diff line change @@ -37,10 +37,7 @@ int main(int argc, char *argv[]) {
3737 (void )argc ;
3838 (void )argv ;
3939 /* declare a deck using type "struct dh_deck" (defined in deckhandler.h) */
40- struct dh_deck deck_a ;
41-
42- /* initialize the deck */
43- dh_init_deck (& deck_a );
40+ struct dh_deck deck_a = dh_get_new_deck ();
4441
4542 int deals_num = 0 ;
4643 int deals_max = 3 ;
@@ -100,12 +97,12 @@ int main(int argc, char *argv[]) {
10097 puts ("\n\n\t]=[ Create 4 decks, shuffle each one, and deal them all out ]=[\n" );
10198 int total_decks = 4 ;
10299 struct dh_deck deck_num [total_decks ];
100+ for (int i = 0 ; i < total_decks ; i ++ )
101+ deck_num [i ] = dh_get_new_deck ();
103102
104103 int which_deck ;
105- for (which_deck = 0 ; which_deck < total_decks ; which_deck ++ ) {
106- dh_init_deck (& deck_num [which_deck ]);
104+ for (which_deck = 0 ; which_deck < total_decks ; which_deck ++ )
107105 dh_shuffle_deck (& deck_num [which_deck ]);
108- }
109106
110107 /* There's no function in the library (yet) that shuffles multiple decks
111108 * together, but using the method below will provide a close simulation */
You can’t perform that action at this time.
0 commit comments