From de975d524d53432fab1f268d2359fcede5be0058 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sun, 2 Feb 2025 19:07:49 -0500 Subject: [PATCH] Let me deal you a hand --- card.c | 36 ++++++++++++++++++++++++++---------- card.h | 3 +++ game.c | 27 +++++++++++++++++++++++++-- main.c | 16 +++++++++------- 4 files changed, 63 insertions(+), 19 deletions(-) diff --git a/card.c b/card.c index f5e08b2..1748867 100644 --- a/card.c +++ b/card.c @@ -1,6 +1,5 @@ #include #include -#include #include "card.h" static Vector2 card_size = (Vector2) { CARD_WIDTH, CARD_HEIGHT }; @@ -12,14 +11,19 @@ void draw_card(Card *c, Texture2D *cards_texture) { int pos_vert = i_vert * CARD_HEIGHT; int pos_horiz = i_horiz * CARD_WIDTH; - DrawTexturePro( - *cards_texture, - (Rectangle) { pos_horiz, pos_vert, CARD_WIDTH, CARD_HEIGHT }, - (Rectangle) { c->position.x, c->position.y, card_size.x, card_size.y }, - (Vector2) { 0, 0 }, - 0., - RAYWHITE - ); + if (c->visible) { + DrawTexturePro( + *cards_texture, + (Rectangle) { pos_horiz, pos_vert, CARD_WIDTH, CARD_HEIGHT }, + (Rectangle) { c->position.x, c->position.y, card_size.x, card_size.y }, + (Vector2) { 0, 0 }, + 0., + RAYWHITE + ); + } else { + DrawRectangleRec((Rectangle) { c->position.x, c->position.y, card_size.x, card_size.y }, BLACK); + } + if (c->selected) { DrawCircle(c->position.x + 10, c->position.y + 10, 10, BLUE); } @@ -31,7 +35,6 @@ bool point_within_card(Card *c, Vector2 point) { } void shuffle_hand(Hand *h) { - srand(time(NULL)); Card *swap; for (int i = h->count - 1; i >= 0; i--) { int index = rand() % (i + 1); @@ -40,3 +43,16 @@ void shuffle_hand(Hand *h) { h->cards[index] = swap; } } + +void add_to_hand(Hand *h, Card *c) { + h->cards[h->count++] = c; + c->position = (Vector2) { h->position.x + (h->count * (CARD_WIDTH + 10)), h->position.y }; +} + +void deal(Hand *from, Hand *to, int count) { + for (int i = 0; i < count; i++) { + Card *t = from->cards[from->count - 1]; + add_to_hand(to, t); + from->count--; + } +} diff --git a/card.h b/card.h index c36702a..92d9110 100644 --- a/card.h +++ b/card.h @@ -46,15 +46,18 @@ struct Card { Month month; Vector2 position; bool selected; + bool visible; }; struct Hand { Card *cards[48]; int count; + Vector2 position; }; void draw_card(Card *c, Texture2D *cards_texture); bool point_within_card(Card *c, Vector2 v); void shuffle_hand(Hand *h); +void deal(Hand *from, Hand *to, int count); #endif diff --git a/game.c b/game.c index 2bcfe04..a2ea379 100644 --- a/game.c +++ b/game.c @@ -11,6 +11,7 @@ char teyaku_calculation[400]; char dekiyaku_calculation[400]; void initialize_game(Game *g) { + g->deck.count = 0; g->should_close = false; for (int i = 0; i < 48; i++) { CardType t = CHAFF; @@ -47,13 +48,31 @@ void initialize_game(Game *g) { case 41: t = ANIMAL; break; } - g->cards[i] = (Card) { i, t, rt, month, (Vector2) { month * 75, (i % 4) * 123 }, false }; + g->cards[i] = (Card) { i, t, rt, month, (Vector2) { 800, 100 }, false, false }; g->deck.cards[i] = &g->cards[i]; g->deck.count++; } shuffle_hand(&g->deck); + g->player_hand.count = 0; + g->player_hand.position = (Vector2) { 20, 450 }; + g->right_hand.count = 0; + g->right_hand.position = (Vector2) { 20, 100 }; + g->left_hand.count = 0; + g->left_hand.position = (Vector2) { 20, 300 }; + + deal(&g->deck, &g->player_hand, 4); + deal(&g->deck, &g->right_hand, 4); + deal(&g->deck, &g->left_hand, 4); + deal(&g->deck, &g->player_hand, 3); + deal(&g->deck, &g->right_hand, 3); + deal(&g->deck, &g->left_hand, 3); + + for (int i = 0; i < g->player_hand.count; i++) { + g->player_hand.cards[i]->visible = true; + } + strcpy(teyaku_calculation, ""); strcpy(dekiyaku_calculation, ""); @@ -63,7 +82,8 @@ void initialize_game(Game *g) { } bool stale_calculation = true; -void run_frame(Game *g) { + +void handle_input(Game *g) { if (IsMouseButtonPressed(0)) { mouse_pos = GetMousePosition(); for (int i = 0; i < 48; i++) { @@ -74,7 +94,10 @@ void run_frame(Game *g) { } } } +} +void run_frame(Game *g) { + handle_input(g); int num_selected = 0; if (stale_calculation) { Hand h; diff --git a/main.c b/main.c index 02f13a9..af7acf6 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ #include #include #include +#include #include @@ -11,14 +12,15 @@ char *text = "こんにちわ、 世界!"; Texture2D cards_texture; int main(int argc, char** argv) { - InitWindow(900, 600, "Hanafuda Hachi-Hachi"); - SetTargetFPS(60); + srand(time(NULL)); + InitWindow(900, 600, "Hanafuda Hachi-Hachi"); + SetTargetFPS(60); - Game g; - initialize_game(&g); + Game g; + initialize_game(&g); - run_until_closing(&g); + run_until_closing(&g); - CloseWindow(); - return 0; + CloseWindow(); + return 0; }