From f17bd76b837443936ba093ddd8c55a30df872470 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sun, 2 Feb 2025 18:13:19 -0500 Subject: [PATCH] Get a game going --- card.h | 2 +- dekiyaku.c | 10 ++--- game.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++ game.h | 20 +++++++++ main.c | 119 +++-------------------------------------------------- teyaku.c | 4 +- 6 files changed, 147 insertions(+), 122 deletions(-) create mode 100644 game.c create mode 100644 game.h diff --git a/card.h b/card.h index e00e9b4..5cd7bd9 100644 --- a/card.h +++ b/card.h @@ -49,7 +49,7 @@ struct Card { }; struct Hand { - Card cards[48]; + Card *cards[48]; int count; }; diff --git a/dekiyaku.c b/dekiyaku.c index 6475888..fbe6cf3 100644 --- a/dekiyaku.c +++ b/dekiyaku.c @@ -6,20 +6,20 @@ void calculate_dekiyaku(const Hand h, Dekiyaku *d) { int brights = 0, ribbons = 0, ribbons_except_november = 0, blue_ribbons = 0, poetry_ribbons = 0, boar = 0, deer = 0, butterflies = 0; for (int i = 0; i < h.count; i++) { - const Card card = h.cards[i]; - switch (card.type) { + Card *card = h.cards[i]; + switch (card->type) { case BRIGHT: brights++; break; case RIBBON: ribbons++; - if (card.month != NOVEMBER) ribbons_except_november++; - switch (card.ribbon_type) { + if (card->month != NOVEMBER) ribbons_except_november++; + switch (card->ribbon_type) { case RIBBON_BLUE: blue_ribbons++; break; case RIBBON_POETRY: poetry_ribbons++; break; default: break; } break; case ANIMAL: - switch (card.month) { + switch (card->month) { case JUNE: butterflies++; break; case JULY: boar++; break; case OCTOBER: deer++; break; diff --git a/game.c b/game.c new file mode 100644 index 0000000..ce17783 --- /dev/null +++ b/game.c @@ -0,0 +1,114 @@ +#include + +#include "game.h" +#include "card.h" +#include "teyaku.h" +#include "dekiyaku.h" + +Vector2 mouse_pos; +char teyaku_calculation[400]; +char dekiyaku_calculation[400]; + +void initialize_game(Game *g) { + g->should_close = false; + for (int i = 0; i < 48; i++) { + CardType t = CHAFF; + RibbonType rt = RIBBON_NONE; + Month month = i / 4; + switch (i) { + case 0: + case 8: + case 28: + case 40: + case 44: + t = BRIGHT; break; + case 1: + case 5: + case 9: + t = RIBBON; rt = RIBBON_POETRY; break; + case 21: + case 33: + case 37: + t = RIBBON; rt = RIBBON_BLUE; break; + case 13: + case 17: + case 25: + case 42: + t = RIBBON; rt = RIBBON_PLAIN; break; + case 4: + case 12: + case 16: + case 20: + case 24: + case 29: + case 32: + case 36: + case 41: + t = ANIMAL; break; + } + g->cards[i] = (Card) { i, t, rt, month, (Vector2) { month * 75, (i % 4) * 123 }, false }; + } + + strcpy(teyaku_calculation, ""); + strcpy(dekiyaku_calculation, ""); + + Image cards_image = LoadImage("img/cards.png"); + g->cards_texture = LoadTextureFromImage(cards_image); + UnloadImage(cards_image); +} + +bool stale_calculation = true; +void run_frame(Game *g) { + if (IsMouseButtonPressed(0)) { + mouse_pos = GetMousePosition(); + for (int i = 0; i < 48; i++) { + if (point_within_card(&g->cards[i], mouse_pos)) { + g->cards[i].selected = !g->cards[i].selected; + stale_calculation = true; + break; + } + } + } + + int num_selected = 0; + if (stale_calculation) { + Hand h; + h.count = 0; + for (int i = 0; i < 48; i++) { + num_selected += g->cards[i].selected; + if (g->cards[i].selected) h.cards[h.count++] = &g->cards[i]; + } + + if (num_selected == 7) { + Teyaku t; + calculate_teyaku(h, &t); + teyaku_to_string(&t, teyaku_calculation); + } else { + strcpy(teyaku_calculation, ""); + } + + Dekiyaku d; + calculate_dekiyaku(h, &d); + dekiyaku_to_string(&d, dekiyaku_calculation); + stale_calculation = false; + } +} + +void draw_frame(Game *g) { + BeginDrawing(); + ClearBackground(RAYWHITE); + for (int i = 0; i < 48; i++) { + draw_card(&g->cards[i], &g->cards_texture); + } + + DrawText(teyaku_calculation, 10, 550, 25, BLACK); + DrawText(dekiyaku_calculation, 10, 500, 25, BLACK); + EndDrawing(); +} + +void run_until_closing(Game *g) { + while (!WindowShouldClose() && !g->should_close) { + run_frame(g); + draw_frame(g); + } +} diff --git a/game.h b/game.h new file mode 100644 index 0000000..d38e5f2 --- /dev/null +++ b/game.h @@ -0,0 +1,20 @@ +#ifndef _HF_GAME_ +#define _HF_GAME_ + +#include + +typedef struct Game Game; + +#include "card.h" + +struct Game { + bool should_close; + Card cards[48]; + Texture2D cards_texture; + Hand player_hand, left_hand, right_hand; +}; + +void initialize_game(Game *g); +void run_until_closing(Game *g); + +#endif diff --git a/main.c b/main.c index a5d6ab1..02f13a9 100644 --- a/main.c +++ b/main.c @@ -5,129 +5,20 @@ #include -#include "card.h" -#include "move.h" -#include "teyaku.h" -#include "dekiyaku.h" +#include "game.h" char *text = "こんにちわ、 世界!"; +Texture2D cards_texture; int main(int argc, char** argv) { InitWindow(900, 600, "Hanafuda Hachi-Hachi"); SetTargetFPS(60); - Image cards_image = LoadImage("img/cards.png"); - Texture2D cards_texture = LoadTextureFromImage(cards_image); - UnloadImage(cards_image); + Game g; + initialize_game(&g); - /* - Hand h; - h.cards[0] = (Card) { 1, BRIGHT, RIBBON_NONE, NOVEMBER, (Vector2) { 0, 0 } }; - h.cards[1] = (Card) { 1, ANIMAL, RIBBON_NONE, NOVEMBER, (Vector2) { 0, 0 } }; - h.cards[2] = (Card) { 1, RIBBON, RIBBON_PLAIN, NOVEMBER, (Vector2) { 0, 0 } }; - h.cards[3] = (Card) { 1, CHAFF, RIBBON_NONE, NOVEMBER, (Vector2) { 0, 0 } }; - h.cards[4] = (Card) { 1, CHAFF, RIBBON_NONE, DECEMBER, (Vector2) { 0, 0 } }; - h.cards[5] = (Card) { 1, CHAFF, RIBBON_NONE, DECEMBER, (Vector2) { 0, 0 } }; - h.cards[6] = (Card) { 1, CHAFF, RIBBON_NONE, DECEMBER, (Vector2) { 0, 0 } }; - h.count = 7; - - printf("Teyaku: %d\n", calculate_teyaku(h)); - */ - - Card cards[48]; - for (int i = 0; i < 48; i++) { - CardType t = CHAFF; - RibbonType rt = RIBBON_NONE; - Month month = i / 4; - switch (i) { - case 0: - case 8: - case 28: - case 40: - case 44: - t = BRIGHT; break; - case 1: - case 5: - case 9: - t = RIBBON; rt = RIBBON_POETRY; break; - case 21: - case 33: - case 37: - t = RIBBON; rt = RIBBON_BLUE; break; - case 13: - case 17: - case 25: - case 42: - t = RIBBON; rt = RIBBON_PLAIN; break; - case 4: - case 12: - case 16: - case 20: - case 24: - case 29: - case 32: - case 36: - case 41: - t = ANIMAL; break; - } - cards[i] = (Card) { i, t, rt, month, (Vector2) { month * 75, (i % 4) * 123 }, false }; - } - - // float delta; - Vector2 mouse_pos; - char teyaku_calculation[400]; - strcpy(teyaku_calculation, ""); - char dekiyaku_calculation[400]; - strcpy(dekiyaku_calculation, ""); - - while (!WindowShouldClose()) { - // delta = GetFrameTime(); - - if (IsMouseButtonPressed(0)) { - mouse_pos = GetMousePosition(); - for (int i = 0; i < 48; i++) { - if (point_within_card(&cards[i], mouse_pos)) { - cards[i].selected = !cards[i].selected; - strcpy(teyaku_calculation, ""); - break; - } - } - } - - BeginDrawing(); - ClearBackground(RAYWHITE); - int num_selected = 0; - for (int i = 0; i < 48; i++) { - num_selected += cards[i].selected; - draw_card(&cards[i], &cards_texture); - } - - if (strlen(teyaku_calculation) == 0) { - Hand h; - h.count = 0; - for (int i = 0; i < 48; i++) { - if (cards[i].selected) memcpy(&h.cards[h.count++], &cards[i], sizeof(Card)); - } - - if (num_selected == 7) { - Teyaku t; - calculate_teyaku(h, &t); - teyaku_to_string(&t, teyaku_calculation); - } else { - strcpy(teyaku_calculation, ""); - } - - Dekiyaku d; - calculate_dekiyaku(h, &d); - dekiyaku_to_string(&d, dekiyaku_calculation); - } - - DrawText(teyaku_calculation, 10, 550, 25, BLACK); - DrawText(dekiyaku_calculation, 10, 500, 25, BLACK); - EndDrawing(); - } + run_until_closing(&g); CloseWindow(); - return 0; } diff --git a/teyaku.c b/teyaku.c index 8066304..8984b66 100644 --- a/teyaku.c +++ b/teyaku.c @@ -7,7 +7,7 @@ SetTeyaku calculate_set_teyaku(const Hand h) { int month_counts[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int month_stands[12] = { 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1 }; for (int i = 0; i < h.count; i++) { - Card c = h.cards[i]; + Card c = *h.cards[i]; month_counts[c.month]++; } @@ -45,7 +45,7 @@ ChaffTeyaku calculate_chaff_teyaku(const Hand h) { int chaff = 0; for (int i = 0; i < h.count; i++) { - Card c = h.cards[i]; + Card c = *h.cards[i]; if (c.month == NOVEMBER) chaff++; // November cards are all counted as chaff here else if (c.type == BRIGHT) brights++; else if (c.type == RIBBON) ribbons++;