File tree Expand file tree Collapse file tree 3 files changed +27
-12
lines changed Expand file tree Collapse file tree 3 files changed +27
-12
lines changed Original file line number Diff line number Diff line change @@ -59,7 +59,7 @@ void dh_pcg_srand_auto(void) {
59
59
pcg32_srandom_r (& rng , initstate , initseq );
60
60
}
61
61
62
- void dh_init_deck (struct dh_deck * deck ) {
62
+ static void dh_init_deck (struct dh_deck * deck ) {
63
63
deck -> top_card = 0 ;
64
64
65
65
int card = 0 ;
@@ -77,10 +77,15 @@ void dh_init_deck(struct dh_deck *deck) {
77
77
78
78
card ++ ;
79
79
}
80
-
81
80
return ;
82
81
}
83
82
83
+ struct dh_deck dh_get_new_deck (void ) {
84
+ struct dh_deck deck ;
85
+ dh_init_deck (& deck );
86
+ return deck ;
87
+ }
88
+
84
89
struct dh_card dh_deal_top_card (struct dh_deck * deck ) {
85
90
if (deck -> top_card == CARDS_IN_DECK ) {
86
91
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);
88
88
void dh_pcg_srand_auto (void );
89
89
90
90
/**
91
- * @brief Initialize a deck in a sorted order (no shuffling) .
91
+ * @brief Create and initialize a new deck of cards .
92
92
*
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.
94
97
*/
95
- void dh_init_deck ( struct dh_deck * deck );
98
+ struct dh_deck dh_get_new_deck ( void );
96
99
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
+ */
97
110
struct dh_card dh_deal_top_card (struct dh_deck * deck );
98
111
99
112
/**
Original file line number Diff line number Diff line change @@ -37,10 +37,7 @@ int main(int argc, char *argv[]) {
37
37
(void )argc ;
38
38
(void )argv ;
39
39
/* 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 ();
44
41
45
42
int deals_num = 0 ;
46
43
int deals_max = 3 ;
@@ -100,12 +97,12 @@ int main(int argc, char *argv[]) {
100
97
puts ("\n\n\t]=[ Create 4 decks, shuffle each one, and deal them all out ]=[\n" );
101
98
int total_decks = 4 ;
102
99
struct dh_deck deck_num [total_decks ];
100
+ for (int i = 0 ; i < total_decks ; i ++ )
101
+ deck_num [i ] = dh_get_new_deck ();
103
102
104
103
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 ++ )
107
105
dh_shuffle_deck (& deck_num [which_deck ]);
108
- }
109
106
110
107
/* There's no function in the library (yet) that shuffles multiple decks
111
108
* together, but using the method below will provide a close simulation */
You can’t perform that action at this time.
0 commit comments