Let me deal you a hand
This commit is contained in:
parent
f852af7cd7
commit
de975d524d
36
card.c
36
card.c
@ -1,6 +1,5 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
|
||||||
#include "card.h"
|
#include "card.h"
|
||||||
|
|
||||||
static Vector2 card_size = (Vector2) { CARD_WIDTH, CARD_HEIGHT };
|
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_vert = i_vert * CARD_HEIGHT;
|
||||||
int pos_horiz = i_horiz * CARD_WIDTH;
|
int pos_horiz = i_horiz * CARD_WIDTH;
|
||||||
|
|
||||||
DrawTexturePro(
|
if (c->visible) {
|
||||||
*cards_texture,
|
DrawTexturePro(
|
||||||
(Rectangle) { pos_horiz, pos_vert, CARD_WIDTH, CARD_HEIGHT },
|
*cards_texture,
|
||||||
(Rectangle) { c->position.x, c->position.y, card_size.x, card_size.y },
|
(Rectangle) { pos_horiz, pos_vert, CARD_WIDTH, CARD_HEIGHT },
|
||||||
(Vector2) { 0, 0 },
|
(Rectangle) { c->position.x, c->position.y, card_size.x, card_size.y },
|
||||||
0.,
|
(Vector2) { 0, 0 },
|
||||||
RAYWHITE
|
0.,
|
||||||
);
|
RAYWHITE
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
DrawRectangleRec((Rectangle) { c->position.x, c->position.y, card_size.x, card_size.y }, BLACK);
|
||||||
|
}
|
||||||
|
|
||||||
if (c->selected) {
|
if (c->selected) {
|
||||||
DrawCircle(c->position.x + 10, c->position.y + 10, 10, BLUE);
|
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) {
|
void shuffle_hand(Hand *h) {
|
||||||
srand(time(NULL));
|
|
||||||
Card *swap;
|
Card *swap;
|
||||||
for (int i = h->count - 1; i >= 0; i--) {
|
for (int i = h->count - 1; i >= 0; i--) {
|
||||||
int index = rand() % (i + 1);
|
int index = rand() % (i + 1);
|
||||||
@ -40,3 +43,16 @@ void shuffle_hand(Hand *h) {
|
|||||||
h->cards[index] = swap;
|
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--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
3
card.h
3
card.h
@ -46,15 +46,18 @@ struct Card {
|
|||||||
Month month;
|
Month month;
|
||||||
Vector2 position;
|
Vector2 position;
|
||||||
bool selected;
|
bool selected;
|
||||||
|
bool visible;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Hand {
|
struct Hand {
|
||||||
Card *cards[48];
|
Card *cards[48];
|
||||||
int count;
|
int count;
|
||||||
|
Vector2 position;
|
||||||
};
|
};
|
||||||
|
|
||||||
void draw_card(Card *c, Texture2D *cards_texture);
|
void draw_card(Card *c, Texture2D *cards_texture);
|
||||||
bool point_within_card(Card *c, Vector2 v);
|
bool point_within_card(Card *c, Vector2 v);
|
||||||
void shuffle_hand(Hand *h);
|
void shuffle_hand(Hand *h);
|
||||||
|
void deal(Hand *from, Hand *to, int count);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
27
game.c
27
game.c
@ -11,6 +11,7 @@ char teyaku_calculation[400];
|
|||||||
char dekiyaku_calculation[400];
|
char dekiyaku_calculation[400];
|
||||||
|
|
||||||
void initialize_game(Game *g) {
|
void initialize_game(Game *g) {
|
||||||
|
g->deck.count = 0;
|
||||||
g->should_close = false;
|
g->should_close = false;
|
||||||
for (int i = 0; i < 48; i++) {
|
for (int i = 0; i < 48; i++) {
|
||||||
CardType t = CHAFF;
|
CardType t = CHAFF;
|
||||||
@ -47,13 +48,31 @@ void initialize_game(Game *g) {
|
|||||||
case 41:
|
case 41:
|
||||||
t = ANIMAL; break;
|
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.cards[i] = &g->cards[i];
|
||||||
g->deck.count++;
|
g->deck.count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
shuffle_hand(&g->deck);
|
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(teyaku_calculation, "");
|
||||||
strcpy(dekiyaku_calculation, "");
|
strcpy(dekiyaku_calculation, "");
|
||||||
|
|
||||||
@ -63,7 +82,8 @@ void initialize_game(Game *g) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool stale_calculation = true;
|
bool stale_calculation = true;
|
||||||
void run_frame(Game *g) {
|
|
||||||
|
void handle_input(Game *g) {
|
||||||
if (IsMouseButtonPressed(0)) {
|
if (IsMouseButtonPressed(0)) {
|
||||||
mouse_pos = GetMousePosition();
|
mouse_pos = GetMousePosition();
|
||||||
for (int i = 0; i < 48; i++) {
|
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;
|
int num_selected = 0;
|
||||||
if (stale_calculation) {
|
if (stale_calculation) {
|
||||||
Hand h;
|
Hand h;
|
||||||
|
16
main.c
16
main.c
@ -2,6 +2,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
|
|
||||||
@ -11,14 +12,15 @@ char *text = "こんにちわ、 世界!";
|
|||||||
Texture2D cards_texture;
|
Texture2D cards_texture;
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
InitWindow(900, 600, "Hanafuda Hachi-Hachi");
|
srand(time(NULL));
|
||||||
SetTargetFPS(60);
|
InitWindow(900, 600, "Hanafuda Hachi-Hachi");
|
||||||
|
SetTargetFPS(60);
|
||||||
|
|
||||||
Game g;
|
Game g;
|
||||||
initialize_game(&g);
|
initialize_game(&g);
|
||||||
|
|
||||||
run_until_closing(&g);
|
run_until_closing(&g);
|
||||||
|
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user