Skip to content

Commit bfcd3a6

Browse files
committed
Rename functions and enums
1 parent 5adc05f commit bfcd3a6

File tree

5 files changed

+81
-81
lines changed

5 files changed

+81
-81
lines changed

SWIG_INTERFACE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ deckhandler.seed()
3030
deck = deckhandler.get_new_deck()
3131

3232
# Shuffle the deck.
33-
deckhandler.dh_shuffle_deck(deck)
33+
deckhandler.DH_shuffle_deck(deck)
3434

3535
# Print out the whole deck.
36-
for i in range(deckhandler.CARDS_IN_DECK):
36+
for i in range(deckhandler.DH_CARDS_IN_DECK):
3737
card = deckhandler.deck_dh_get(deck, i)
3838
print("face: {}, suit: {}".format(deckhandler.DH_get_card_face(card), deckhandler.DH_get_card_suit(card)))
3939
```

deckhandler.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,45 +38,45 @@
3838

3939
static pcg32_random_t rng;
4040

41-
static const char *dh_suits[] = {"Hearts ", "Diamonds", "Spades ", "Clubs "};
41+
static const char *suits[] = {"Hearts ", "Diamonds", "Spades ", "Clubs "};
4242

43-
static const char *dh_faces[] = {"Ace", "2", "3", "4", "5", "6", "7",
43+
static const char *faces[] = {"Ace", "2", "3", "4", "5", "6", "7",
4444
"8", "9", "10", "Jack", "Queen", "King"};
4545

46-
const DH_Card dh_card_back = {
46+
const DH_Card DH_card_back = {
4747
.face_val = -1,
4848
.suit = -1,
4949
};
5050

51-
const DH_Card dh_card_null = {
51+
const DH_Card DH_card_null = {
5252
.face_val = -2,
5353
.suit = -2,
5454
};
5555

56-
void dh_pcg_srand(uint64_t initstate, uint64_t initseq) {
56+
void DH_pcg_srand(uint64_t initstate, uint64_t initseq) {
5757
pcg32_srandom_r(&rng, initstate, initseq);
5858
return;
5959
}
6060

61-
void dh_pcg_srand_auto(void) {
61+
void DH_pcg_srand_auto(void) {
6262
uint64_t initstate = time(NULL) ^ (intptr_t)&printf;
63-
uint64_t initseq = (intptr_t)&dh_faces;
63+
uint64_t initseq = (intptr_t)&faces;
6464
pcg32_srandom_r(&rng, initstate, initseq);
6565
}
6666

67-
static void dh_init_deck(DH_Deck *deck) {
67+
static void DH_init_deck(DH_Deck *deck) {
6868
deck->top_card = 0;
6969

7070
int card = 0;
7171
int suit = 0;
72-
int face = ACE;
72+
int face = DH_CARD_ACE;
7373

74-
while (suit < MAX_SUITS) {
74+
while (suit < DH_SUIT_MAX) {
7575
deck->card[card].face_val = face++;
7676
deck->card[card].suit = suit;
7777

78-
if (face > KING) {
79-
face = ACE;
78+
if (face > DH_CARD_KING) {
79+
face = DH_CARD_ACE;
8080
suit++;
8181
}
8282

@@ -85,14 +85,14 @@ static void dh_init_deck(DH_Deck *deck) {
8585
return;
8686
}
8787

88-
DH_Deck dh_get_new_deck(void) {
88+
DH_Deck DH_get_new_deck(void) {
8989
DH_Deck deck;
90-
dh_init_deck(&deck);
90+
DH_init_deck(&deck);
9191
return deck;
9292
}
9393

94-
DH_Card dh_deal_top_card(DH_Deck *deck) {
95-
if (deck->top_card == CARDS_IN_DECK) {
94+
DH_Card DH_deal_top_card(DH_Deck *deck) {
95+
if (deck->top_card == DH_CARDS_IN_DECK) {
9696
deck->top_card = 0;
9797
puts("deckhandler: deck wrapped");
9898
}
@@ -107,27 +107,27 @@ static void swap(DH_Deck *deck_dh, int i, int j) {
107107
deck_dh->card[j] = tmp;
108108
}
109109

110-
void dh_shuffle_deck(DH_Deck *deck) {
111-
for (int i = CARDS_IN_DECK - 1; i > 0; --i) {
110+
void DH_shuffle_deck(DH_Deck *deck) {
111+
for (int i = DH_CARDS_IN_DECK - 1; i > 0; --i) {
112112
int j = pcg32_boundedrand_r(&rng, i + 1);
113113
swap(deck, i, j);
114114
}
115115
deck->top_card = 0;
116116
}
117117

118-
const char *DH_get_card_face(DH_Card card) { return dh_faces[card.face_val - 1]; }
118+
const char *DH_get_card_face(DH_Card card) { return faces[card.face_val - 1]; }
119119

120-
const char *DH_get_card_suit(DH_Card card) { return dh_suits[card.suit]; }
120+
const char *DH_get_card_suit(DH_Card card) { return suits[card.suit]; }
121121

122122
const char *DH_get_card_unicode_suit(DH_Card card) {
123123
switch (card.suit) {
124-
case DIAMONDS:
124+
case DH_SUIT_DIAMONDS:
125125
return "\u2666";
126-
case HEARTS:
126+
case DH_SUIT_HEARTS:
127127
return "\u2665";
128-
case SPADES:
128+
case DH_SUIT_SPADES:
129129
return "\u2660";
130-
case CLUBS:
130+
case DH_SUIT_CLUBS:
131131
return "\u2663";
132132
default:
133133
return "?";

deckhandler.h

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,33 @@ extern "C" {
2424

2525
/// Enumeration of the four standard card suits.
2626
enum {
27-
HEARTS, ///< Hearts suit
28-
DIAMONDS, ///< Diamonds suit
29-
SPADES, ///< Spades suit
30-
CLUBS, ///< Clubs suit
31-
MAX_SUITS ///< Number of suits
27+
DH_SUIT_HEARTS, ///< Hearts suit
28+
DH_SUIT_DIAMONDS, ///< Diamonds suit
29+
DH_SUIT_SPADES, ///< Spades suit
30+
DH_SUIT_CLUBS, ///< Clubs suit
31+
DH_SUIT_MAX ///< Number of suits
3232
};
3333

3434
/// Enumeration of card face values (Ace can be high or low).
3535
enum card_face {
36-
ACE = 1, ///< Ace (low)
37-
TWO,
38-
THREE,
39-
FOUR,
40-
FIVE,
41-
SIX,
42-
SEVEN,
43-
EIGHT,
44-
NINE,
45-
TEN,
46-
JACK,
47-
QUEEN,
48-
KING,
49-
ACE_HIGH ///< Ace (high) for straight evaluation, not dealt
36+
DH_CARD_ACE = 1, ///< Ace (low)
37+
DH_CARD_TWO,
38+
DH_CARD_THREE,
39+
DH_CARD_FOUR,
40+
DH_CARD_FIVE,
41+
DH_CARD_SIX,
42+
DH_CARD_SEVEN,
43+
DH_CARD_EIGHT,
44+
DH_CARD_NINE,
45+
DH_CARD_TEN,
46+
DH_CARD_JACK,
47+
DH_CARD_QUEEN,
48+
DH_CARD_KING,
49+
DH_CARD_ACE_HIGH ///< Ace (high) for straight evaluation, not dealt
5050
};
5151

5252
/// Total number of cards in a standard deck.
53-
#define CARDS_IN_DECK 52
53+
#define DH_CARDS_IN_DECK 52
5454

5555
/**
5656
* @DH_Card
@@ -61,15 +61,15 @@ typedef struct {
6161
int suit; ///< Suit of the card (see enum)
6262
} DH_Card;
6363

64-
extern const DH_Card dh_card_back;
65-
extern const DH_Card dh_card_null;
64+
extern const DH_Card DH_card_back;
65+
extern const DH_Card DH_card_null;
6666

6767
/**
6868
* @DH_Deck
6969
* @brief Represents a full deck of 52 playing cards.
7070
*/
7171
typedef struct {
72-
DH_Card card[CARDS_IN_DECK]; ///< Array of all cards in the deck
72+
DH_Card card[DH_CARDS_IN_DECK]; ///< Array of all cards in the deck
7373
int top_card;
7474
} DH_Deck;
7575

@@ -79,43 +79,43 @@ typedef struct {
7979
* @param initstate Initialization state value.
8080
* @param initseq Initialization sequence value.
8181
*/
82-
void dh_pcg_srand(uint64_t initstate, uint64_t initseq);
82+
void DH_pcg_srand(uint64_t initstate, uint64_t initseq);
8383

8484
/**
8585
* @brief Automatically seed the PCG random number generator with internal defaults.
8686
*
8787
* Uses `time(NULL)` and pointer values for entropy.
8888
*/
89-
void dh_pcg_srand_auto(void);
89+
void DH_pcg_srand_auto(void);
9090

9191
/**
9292
* @brief Create and initialize a new deck of cards.
9393
*
9494
* This function returns a new `dh_deck` struct that is initialized to a full, shuffled deck.
95-
* Internally, it calls `dh_init_deck()` to perform the initialization.
95+
* Internally, it calls `DH_init_deck()` to perform the initialization.
9696
*
9797
* @return A fully initialized deck of cards.
9898
*/
99-
DH_Deck dh_get_new_deck(void);
99+
DH_Deck DH_get_new_deck(void);
100100

101101
/**
102102
* @brief Deal the top card from the deck.
103103
*
104104
* Deals the card currently at the top of the deck and advances the deck position.
105-
* If all cards have been dealt (`top_card == CARDS_IN_DECK`), the deck wraps and begins from the
105+
* If all cards have been dealt (`top_card == DH_CARDS_IN_DECK`), the deck wraps and begins from the
106106
* top again, resetting `top_card` to 0 and printing a warning message.
107107
*
108108
* @param deck Pointer to the deck from which to deal a card.
109109
* @return The card at the current top position of the deck.
110110
*/
111-
DH_Card dh_deal_top_card(DH_Deck *deck);
111+
DH_Card DH_deal_top_card(DH_Deck *deck);
112112

113113
/**
114114
* @brief Shuffle a deck of cards using the PCG random number generator.
115115
*
116116
* @param deck_dh Pointer to the deck to shuffle.
117117
*/
118-
void dh_shuffle_deck(DH_Deck *deck_dh);
118+
void DH_shuffle_deck(DH_Deck *deck_dh);
119119

120120
/**
121121
* @brief Get the string name of a card's face value (e.g., "Ace", "10", "King").

swig/deckhandler.i

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ deck_dh_get(DH_Deck deck, int i) {
2020

2121
void
2222
seed(void) {
23-
dh_pcg_srand_auto();
23+
DH_pcg_srand_auto();
2424
}
2525
%}

test/test_01.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,57 +37,57 @@ int main(int argc, char *argv[]) {
3737
(void)argc;
3838
(void)argv;
3939
/* declare a deck using type "DH_Deck" (defined in deckhandler.h) */
40-
DH_Deck deck_a = dh_get_new_deck();
40+
DH_Deck deck_a = DH_get_new_deck();
4141

4242
int deals_num = 0;
4343
int deals_max = 3;
4444

45-
dh_pcg_srand(1, 1);
45+
DH_pcg_srand(1, 1);
4646

4747
do {
4848
/* shuffle the deck */
4949
puts("\n\n\t]=[ Shuffling ]=[\n");
50-
dh_shuffle_deck(&deck_a);
50+
DH_shuffle_deck(&deck_a);
5151

5252
/* show each card in the deck */
5353
int deal;
54-
for (deal = 0; deal < CARDS_IN_DECK; deal++) {
54+
for (deal = 0; deal < DH_CARDS_IN_DECK; deal++) {
5555
/* The "faces" and "suits" arrays are initialized in deckhandler.c */
5656
// fprintf (stderr, "%s of %s\n", DH_get_card_face(deck_a.card[deal]),
5757
// DH_get_card_suit(deck_a.card[deal]));
58-
DH_Card card = dh_deal_top_card(&deck_a);
58+
DH_Card card = DH_deal_top_card(&deck_a);
5959
fprintf(stderr, "%i\n", card.face_val);
6060
if (deal < 4 && deals_num == 0) {
6161
switch (deal) {
6262
case 0:
63-
assert(card.face_val == TWO);
63+
assert(card.face_val == DH_CARD_TWO);
6464
break;
6565
case 1:
66-
assert(card.face_val == FIVE);
66+
assert(card.face_val == DH_CARD_FIVE);
6767
break;
6868
case 2:
69-
assert(card.face_val == FIVE);
69+
assert(card.face_val == DH_CARD_FIVE);
7070
break;
7171
case 3:
72-
assert(card.face_val == FIVE);
72+
assert(card.face_val == DH_CARD_FIVE);
7373
break;
7474
case 4:
75-
assert(card.face_val == QUEEN);
75+
assert(card.face_val == DH_CARD_QUEEN);
7676
break;
7777
case 5:
78-
assert(card.face_val == JACK);
78+
assert(card.face_val == DH_CARD_JACK);
7979
break;
8080
case 6:
81-
assert(card.face_val == KING);
81+
assert(card.face_val == DH_CARD_KING);
8282
break;
8383
case 7:
84-
assert(card.face_val == SIX);
84+
assert(card.face_val == DH_CARD_SIX);
8585
break;
8686
case 8:
87-
assert(card.face_val == NINE);
87+
assert(card.face_val == DH_CARD_NINE);
8888
break;
8989
case 9:
90-
assert(card.face_val == THREE);
90+
assert(card.face_val == DH_CARD_THREE);
9191
break;
9292
}
9393
}
@@ -98,31 +98,31 @@ int main(int argc, char *argv[]) {
9898
int total_decks = 4;
9999
DH_Deck deck_num[total_decks];
100100
for (int i = 0; i < total_decks; i++)
101-
deck_num[i] = dh_get_new_deck();
101+
deck_num[i] = DH_get_new_deck();
102102

103103
int which_deck;
104104
for (which_deck = 0; which_deck < total_decks; which_deck++)
105-
dh_shuffle_deck(&deck_num[which_deck]);
105+
DH_shuffle_deck(&deck_num[which_deck]);
106106

107107
/* There's no function in the library (yet) that shuffles multiple decks
108108
* together, but using the method below will provide a close simulation */
109-
for (int i = 0; i < CARDS_IN_DECK; i++) {
109+
for (int i = 0; i < DH_CARDS_IN_DECK; i++) {
110110
for (which_deck = 0; which_deck < total_decks; which_deck++) {
111-
DH_Card card = dh_deal_top_card(&deck_num[which_deck]);
111+
DH_Card card = DH_deal_top_card(&deck_num[which_deck]);
112112
printf("%s of %s\n", DH_get_card_face(card), DH_get_card_suit(card));
113-
if (i == CARDS_IN_DECK - 1) {
113+
if (i == DH_CARDS_IN_DECK - 1) {
114114
switch (which_deck) {
115115
case 0:
116-
assert(card.face_val == TEN);
116+
assert(card.face_val == DH_CARD_TEN);
117117
break;
118118
case 1:
119-
assert(card.face_val == TWO);
119+
assert(card.face_val == DH_CARD_TWO);
120120
break;
121121
case 2:
122-
assert(card.face_val == THREE);
122+
assert(card.face_val == DH_CARD_THREE);
123123
break;
124124
case 3:
125-
assert(card.face_val == ACE);
125+
assert(card.face_val == DH_CARD_ACE);
126126
break;
127127
}
128128
}

0 commit comments

Comments
 (0)