This repository was archived by the owner on May 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeckhandler.c
More file actions
167 lines (128 loc) · 4.65 KB
/
deckhandler.c
File metadata and controls
167 lines (128 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* deckhandler.c
* Library to handle a deck of cards
* <https://github.com/theimpossibleastronaut/deckhandler>
*
MIT License
Copyright (c) 2019-2025 Andy Alt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pcg_basic.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "deckhandler.h"
static pcg32_random_t rng;
static const char *suits[] = {"Hearts ", "Diamonds", "Spades ", "Clubs "};
static const char *faces[] = {"Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King", "Ace"};
const DH_Card DH_card_back = {
.face_val = DH_CARD_BACK,
.suit = DH_CARD_BACK,
};
const DH_Card DH_card_null = {
.face_val = DH_CARD_NULL,
.suit = DH_CARD_NULL,
};
bool DH_is_card_back(DH_Card a) { return a.face_val == DH_CARD_BACK && a.suit == DH_CARD_BACK; }
bool DH_is_card_null(DH_Card a) { return a.face_val == DH_CARD_NULL && a.suit == DH_CARD_NULL; }
void DH_pcg_srand(uint64_t initstate, uint64_t initseq) {
pcg32_srandom_r(&rng, initstate, initseq);
return;
}
void DH_pcg_srand_auto(void) {
uint64_t initstate = time(NULL) ^ (intptr_t)&printf;
uint64_t initseq = (intptr_t)&faces;
pcg32_srandom_r(&rng, initstate, initseq);
}
static void DH_init_deck(DH_Deck *deck) {
deck->top_card = 0;
int card = 0;
int8_t suit = 0;
int8_t face = DH_CARD_ACE;
while (suit < DH_SUIT_MAX) {
deck->card[card].face_val = face++;
deck->card[card].suit = suit;
if (face > DH_CARD_KING) {
face = DH_CARD_ACE;
suit++;
}
card++;
}
return;
}
DH_Deck DH_get_new_deck(void) {
DH_Deck deck;
DH_init_deck(&deck);
return deck;
}
DH_Card DH_deal_top_card(DH_Deck *deck) {
if (deck->top_card == DH_CARDS_IN_DECK) {
deck->top_card = 0;
puts("deckhandler: deck wrapped");
}
DH_Card card = deck->card[deck->top_card];
deck->top_card++;
return card;
}
static void swap(DH_Deck *deck_dh, int i, int j) {
DH_Card tmp = deck_dh->card[i];
deck_dh->card[i] = deck_dh->card[j];
deck_dh->card[j] = tmp;
}
void DH_shuffle_deck(DH_Deck *deck) {
for (int i = DH_CARDS_IN_DECK - 1; i > 0; --i) {
int j = pcg32_boundedrand_r(&rng, i + 1);
swap(deck, i, j);
}
deck->top_card = 0;
}
void DH_cut_deck(DH_Deck *deck, const int cut_point) {
if (cut_point <= 0 || cut_point >= DH_CARDS_IN_DECK)
return; // No cut if point is out of bounds
DH_Card temp[DH_CARDS_IN_DECK];
int i, j = 0;
// Copy bottom half first
for (i = cut_point; i < DH_CARDS_IN_DECK; ++i)
temp[j++] = deck->card[i];
// Then copy top half
for (i = 0; i < cut_point; ++i)
temp[j++] = deck->card[i];
// Copy back to original deck
memcpy(deck->card, temp, sizeof(temp));
}
const char *DH_get_card_face(DH_Card card) { return faces[card.face_val - 1]; }
const char *DH_get_card_suit(DH_Card card) { return suits[card.suit]; }
static const char *const DH_suit_unicode[] = {[DH_SUIT_DIAMONDS] = "\u2666",
[DH_SUIT_HEARTS] = "\u2665",
[DH_SUIT_SPADES] = "\u2660",
[DH_SUIT_CLUBS] = "\u2663"};
const char *DH_get_unicode_suit(DH_suit suit) {
if (suit >= 0 && suit <= DH_SUIT_CLUBS)
return DH_suit_unicode[suit];
return "?";
}
const char *DH_get_card_unicode_suit(DH_Card card) { return DH_get_unicode_suit(card.suit); }
const char *DH_get_card_face_str(int val) {
static const char *faces_single[] = {NULL, "A", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "J", "Q", "K", "A"};
if (val < 1 || val > 14)
return "?";
return faces_single[val];
}