From f852af7cd71e6601d92285ac2e50593325da1e2d Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sun, 2 Feb 2025 18:30:45 -0500 Subject: [PATCH] Shuffle cards --- card.c | 14 ++++++++++++++ card.h | 1 + game.c | 5 +++++ game.h | 2 +- 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/card.c b/card.c index 37be84b..f5e08b2 100644 --- a/card.c +++ b/card.c @@ -1,3 +1,6 @@ +#include +#include +#include #include "card.h" static Vector2 card_size = (Vector2) { CARD_WIDTH, CARD_HEIGHT }; @@ -26,3 +29,14 @@ bool point_within_card(Card *c, Vector2 point) { return point.x > c->position.x && point.x < c->position.x + card_size.x && point.y > c->position.y && point.y < c->position.y + card_size.y; } + +void shuffle_hand(Hand *h) { + srand(time(NULL)); + Card *swap; + for (int i = h->count - 1; i >= 0; i--) { + int index = rand() % (i + 1); + swap = h->cards[i]; + h->cards[i] = h->cards[index]; + h->cards[index] = swap; + } +} diff --git a/card.h b/card.h index 5cd7bd9..c36702a 100644 --- a/card.h +++ b/card.h @@ -55,5 +55,6 @@ struct Hand { void draw_card(Card *c, Texture2D *cards_texture); bool point_within_card(Card *c, Vector2 v); +void shuffle_hand(Hand *h); #endif diff --git a/game.c b/game.c index ce17783..2bcfe04 100644 --- a/game.c +++ b/game.c @@ -1,4 +1,5 @@ #include +#include #include "game.h" #include "card.h" @@ -47,8 +48,12 @@ void initialize_game(Game *g) { t = ANIMAL; break; } g->cards[i] = (Card) { i, t, rt, month, (Vector2) { month * 75, (i % 4) * 123 }, false }; + g->deck.cards[i] = &g->cards[i]; + g->deck.count++; } + shuffle_hand(&g->deck); + strcpy(teyaku_calculation, ""); strcpy(dekiyaku_calculation, ""); diff --git a/game.h b/game.h index d38e5f2..2ca6dc6 100644 --- a/game.h +++ b/game.h @@ -11,7 +11,7 @@ struct Game { bool should_close; Card cards[48]; Texture2D cards_texture; - Hand player_hand, left_hand, right_hand; + Hand player_hand, left_hand, right_hand, deck; }; void initialize_game(Game *g);