adds jokers 6-16. Extract hand type detection logic from hand_get_type to be used by jokers as well

This commit is contained in:
theturtlemafia
2025-07-27 12:51:30 -04:00
parent 095fb455cc
commit a72458c2c3
6 changed files with 332 additions and 98 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ typedef struct
u8 rank;
} Card;
typedef struct
typedef struct CardObject
{
Card *card;
SpriteObject *sprite_object;
+7
View File
@@ -72,4 +72,11 @@ enum HandType
void game_init();
void game_update();
// Utility functions for other files
typedef struct CardObject CardObject; // forward declaration, actually declared in card.h
CardObject **get_hand_array(void);
CardObject **get_played_array(void);
int get_hand_top(void);
int get_played_top(void);
#endif // GAME_H
+16
View File
@@ -0,0 +1,16 @@
#ifndef HAND_ANALYSIS_H
#define HAND_ANALYSIS_H
#include <tonc.h>
#include "card.h"
void get_hand_distribution(u8 *ranks_out, u8 *suits_out);
void get_played_distribution(u8 *ranks_out, u8 *suits_out);
u8 hand_contains_n_of_a_kind(u8 *ranks);
bool hand_contains_two_pair(u8 *ranks);
bool hand_contains_full_house(u8 *ranks);
bool hand_contains_straight(u8 *ranks);
bool hand_contains_flush(u8 *suits);
#endif
+61 -97
View File
@@ -8,6 +8,7 @@
#include "util.h"
#include "sprite.h"
#include "card.h"
#include "hand_analysis.h"
#include "blind.h"
#include "joker.h"
#include "graphic_utils.h"
@@ -282,6 +283,22 @@ void sort_cards()
}
}
CardObject **get_hand_array(void) {
return hand;
}
int get_hand_top(void) {
return hand_top;
}
CardObject **get_played_array(void) {
return played;
}
int get_played_top(void) {
return played_top;
}
enum HandType hand_get_type()
{
enum HandType res_hand_type = NONE;
@@ -295,117 +312,64 @@ enum HandType hand_get_type()
res_hand_type = HIGH_CARD;
u8 suits[NUM_SUITS] = {0};
u8 ranks[NUM_RANKS] = {0};
for (int i = 0; i <= hand_top; i++)
{
if (hand[i] != NULL && card_object_is_selected(hand[i]))
{
suits[hand[i]->card->suit]++;
ranks[hand[i]->card->rank]++;
}
}
u8 suits[NUM_SUITS];
u8 ranks[NUM_RANKS];
get_hand_distribution(ranks, suits);
// Check for flush
for (int i = 0; i < NUM_SUITS; i++)
{
if (suits[i] >= MAX_SELECTION_SIZE) // if i add jokers just MAX_SELECTION_SIZE - 1 for four fingers
{
res_hand_type = FLUSH;
break;
}
}
if (hand_contains_flush(suits))
res_hand_type = FLUSH;
// Check for straight
for (int i = 0; i < NUM_RANKS - 4; i++)
{
if (ranks[i] && ranks[i + 1] && ranks[i + 2] && ranks[i + 3] && ranks[i + 4])
{
if (res_hand_type == FLUSH)
{
res_hand_type = STRAIGHT_FLUSH;
}
else
{
res_hand_type = STRAIGHT;
}
break;
}
if (hand_contains_straight(ranks)) {
if (res_hand_type == FLUSH)
res_hand_type = STRAIGHT_FLUSH;
else
res_hand_type = STRAIGHT;
}
// Check for ace low straight
if (ranks[ACE] && ranks[TWO] && ranks[THREE] && ranks[FOUR] && ranks[FIVE])
{
res_hand_type = STRAIGHT;
// Check for royal flush vs regular straight flush
if (res_hand_type == STRAIGHT_FLUSH) {
if (ranks[TEN] && ranks[JACK] && ranks[QUEEN] && ranks[KING] && ranks[ACE])
return ROYAL_FLUSH;
return STRAIGHT_FLUSH;
}
// Check for royal flush
if (res_hand_type == STRAIGHT_FLUSH && ranks[TEN] && ranks[JACK] && ranks[QUEEN] && ranks[KING] && ranks[ACE])
{
res_hand_type = ROYAL_FLUSH;
return res_hand_type;
// The following can be optimized better but not sure how much it matters
u8 n_of_a_kind = hand_contains_n_of_a_kind(ranks);
if (n_of_a_kind >= 5) {
if (res_hand_type == FLUSH) {
return FLUSH_FIVE;
}
return FIVE_OF_A_KIND;
}
// Check for straight flush
if (res_hand_type == STRAIGHT_FLUSH)
{
res_hand_type = STRAIGHT_FLUSH;
return res_hand_type;
if (n_of_a_kind == 4) {
return FOUR_OF_A_KIND;
}
for (int i = 0; i < NUM_RANKS; i++)
{
if (ranks[i] >= 5)
{
if (res_hand_type == FLUSH)
{
res_hand_type = FLUSH_FIVE;
return res_hand_type;
}
else
{
res_hand_type = FIVE_OF_A_KIND;
return res_hand_type;
}
}
else if (ranks[i] == 4)
{
res_hand_type = FOUR_OF_A_KIND;
return res_hand_type;
}
else if (ranks[i] == 3)
{
if (res_hand_type == PAIR)
{
res_hand_type = FULL_HOUSE;
return res_hand_type;
}
else
{
res_hand_type = THREE_OF_A_KIND;
}
}
else if (ranks[i] == 2)
{
if (res_hand_type == THREE_OF_A_KIND)
{
res_hand_type = FULL_HOUSE;
return res_hand_type;
}
else if (res_hand_type == PAIR)
{
res_hand_type = TWO_PAIR;
return res_hand_type;
}
else
{
res_hand_type = PAIR;
}
}
if (n_of_a_kind == 3 && hand_contains_full_house(ranks)) {
return FULL_HOUSE;
}
return res_hand_type;
// Flush is more valuable than the remaining hand types, so return now
if (res_hand_type == FLUSH) {
return FLUSH;
}
if (n_of_a_kind == 3) {
return THREE_OF_A_KIND;
}
if (n_of_a_kind == 2) {
if (hand_contains_two_pair(ranks)) {
return TWO_PAIR;
}
return PAIR;
}
return res_hand_type; // should be HIGH_CARD
}
/* Copies the appropriate item into the top left panel (blind/shop icon)
+83
View File
@@ -0,0 +1,83 @@
#include "hand_analysis.h"
#include "card.h"
#include "game.h"
static void get_distribution(CardObject **cards, int top, u8 *ranks_out, u8 *suits_out) {
for (int i = 0; i < NUM_RANKS; i++) ranks_out[i] = 0;
for (int i = 0; i < NUM_SUITS; i++) suits_out[i] = 0;
for (int i = 0; i <= top; i++) {
if (cards[i] && card_object_is_selected(cards[i])) {
ranks_out[cards[i]->card->rank]++;
suits_out[cards[i]->card->suit]++;
}
}
}
void get_hand_distribution(u8 *ranks_out, u8 *suits_out) {
get_distribution(get_hand_array(), get_hand_top(), ranks_out, suits_out);
}
void get_played_distribution(u8 *ranks_out, u8 *suits_out) {
get_distribution(get_played_array(), get_played_top(), ranks_out, suits_out);
}
// Returns the highest N of a kind. So a full-house would return 3.
u8 hand_contains_n_of_a_kind(u8 *ranks) {
u8 highest_n = 0;
for (int i = 0; i < NUM_RANKS; i++) {
if (ranks[i] > highest_n)
highest_n = ranks[i];
}
return highest_n;
}
bool hand_contains_two_pair(u8 *ranks) {
bool contains_other_pair = false;
for (int i = 0; i < NUM_RANKS; i++) {
if (ranks[i] >= 2) {
if (contains_other_pair)
return true;
contains_other_pair = true;
}
}
return false;
}
bool hand_contains_full_house(u8 *ranks) {
bool contains_pair = false;
bool contains_three = false;
for (int i = 0; i < NUM_RANKS; i++) {
if (ranks[i] == 2) {
contains_pair = true;
}
if (ranks[i] == 3) {
contains_three = true;
}
}
return contains_pair && contains_three;
}
bool hand_contains_straight(u8 *ranks) {
for (int i = 0; i < NUM_RANKS - 4; i++)
{
if (ranks[i] && ranks[i + 1] && ranks[i + 2] && ranks[i + 3] && ranks[i + 4])
return true;
}
// Check for ace low straight
if (ranks[ACE] && ranks[TWO] && ranks[THREE] && ranks[FOUR] && ranks[FIVE])
return true;
return false;
}
bool hand_contains_flush(u8 *suits) {
for (int i = 0; i < NUM_SUITS; i++)
{
if (suits[i] >= MAX_SELECTION_SIZE) // this allows MAX_SELECTION_SIZE - 1 for four fingers joker
{
return true;
}
}
return false;
}
+164
View File
@@ -1,5 +1,6 @@
#include "joker.h"
#include "util.h"
#include "hand_analysis.h"
static JokerEffect default_joker_effect(Joker *joker, Card *scored_card) {
@@ -31,12 +32,175 @@ static JokerEffect gluttonous_joker_effect(Joker *joker, Card *scored_card) {
return sinful_joker_effect(scored_card, CLUBS);
}
static JokerEffect jolly_joker_effect(Joker *joker, Card *scored_card) {
JokerEffect effect = {0};
if (scored_card != NULL)
return effect; // if card != null, we are not at the end-phase of scoring yet
// This is really inefficient but the only way at the moment to check for whole-hand conditions
u8 suits[NUM_SUITS];
u8 ranks[NUM_RANKS];
get_played_distribution(ranks, suits);
if (hand_contains_n_of_a_kind(ranks) >= 2)
effect.mult = 8;
return effect;
}
static JokerEffect zany_joker_effect(Joker *joker, Card *scored_card) {
JokerEffect effect = {0};
if (scored_card != NULL)
return effect; // if card != null, we are not at the end-phase of scoring yet
u8 suits[NUM_SUITS];
u8 ranks[NUM_RANKS];
get_played_distribution(ranks, suits);
if (hand_contains_n_of_a_kind(ranks) >= 3)
effect.mult = 12;
return effect;
}
static JokerEffect mad_joker_effect(Joker *joker, Card *scored_card) {
JokerEffect effect = {0};
if (scored_card != NULL)
return effect; // if card != null, we are not at the end-phase of scoring yet
u8 suits[NUM_SUITS];
u8 ranks[NUM_RANKS];
get_played_distribution(ranks, suits);
if (hand_contains_two_pair(ranks))
effect.mult = 10;
return effect;
}
static JokerEffect crazy_joker_effect(Joker *joker, Card *scored_card) {
JokerEffect effect = {0};
if (scored_card != NULL)
return effect; // if card != null, we are not at the end-phase of scoring yet
u8 suits[NUM_SUITS];
u8 ranks[NUM_RANKS];
get_played_distribution(ranks, suits);
if (hand_contains_straight(ranks))
effect.mult = 12;
return effect;
}
static JokerEffect droll_joker_effect(Joker *joker, Card *scored_card) {
JokerEffect effect = {0};
if (scored_card != NULL)
return effect; // if card != null, we are not at the end-phase of scoring yet
u8 suits[NUM_SUITS];
u8 ranks[NUM_RANKS];
get_played_distribution(ranks, suits);
if (hand_contains_flush(suits))
effect.mult = 10;
return effect;
}
static JokerEffect sly_joker_effect(Joker *joker, Card *scored_card) {
JokerEffect effect = {0};
if (scored_card != NULL)
return effect; // if card != null, we are not at the end-phase of scoring yet
u8 suits[NUM_SUITS];
u8 ranks[NUM_RANKS];
get_played_distribution(ranks, suits);
if (hand_contains_n_of_a_kind(ranks) >= 2)
effect.chips = 50;
return effect;
}
static JokerEffect wily_joker_effect(Joker *joker, Card *scored_card) {
JokerEffect effect = {0};
if (scored_card != NULL)
return effect; // if card != null, we are not at the end-phase of scoring yet
u8 suits[NUM_SUITS];
u8 ranks[NUM_RANKS];
get_played_distribution(ranks, suits);
if (hand_contains_n_of_a_kind(ranks) >= 3)
effect.chips = 100;
return effect;
}
static JokerEffect clever_joker_effect(Joker *joker, Card *scored_card) {
JokerEffect effect = {0};
if (scored_card != NULL)
return effect; // if card != null, we are not at the end-phase of scoring yet
u8 suits[NUM_SUITS];
u8 ranks[NUM_RANKS];
get_played_distribution(ranks, suits);
if (hand_contains_two_pair(ranks))
effect.chips = 80;
return effect;
}
static JokerEffect devious_joker_effect(Joker *joker, Card *scored_card) {
JokerEffect effect = {0};
if (scored_card != NULL)
return effect; // if card != null, we are not at the end-phase of scoring yet
u8 suits[NUM_SUITS];
u8 ranks[NUM_RANKS];
get_played_distribution(ranks, suits);
if (hand_contains_straight(ranks))
effect.chips = 100;
return effect;
}
static JokerEffect crafty_joker_effect(Joker *joker, Card *scored_card) {
JokerEffect effect = {0};
if (scored_card != NULL)
return effect; // if card != null, we are not at the end-phase of scoring yet
u8 suits[NUM_SUITS];
u8 ranks[NUM_RANKS];
get_played_distribution(ranks, suits);
if (hand_contains_flush(suits))
effect.chips = 80;
return effect;
}
static JokerEffect half_joker_effect(Joker *joker, Card *scored_card) {
JokerEffect effect = {0};
if (scored_card != NULL)
return effect; // if card != null, we are not at the end-phase of scoring yet
if (get_hand_top() + 1 <= 3) // game.c has hand_get_size() but it's not accesible here
effect.mult = 20;
return effect;
}
const JokerInfo joker_registry[] = {
{ COMMON_JOKER, 2, default_joker_effect }, // DEFAULT_JOKER_ID = 0
{ COMMON_JOKER, 5, greedy_joker_effect }, // GREEDY_JOKER_ID = 1
{ COMMON_JOKER, 5, lusty_joker_effect }, // etc...
{ COMMON_JOKER, 5, wrathful_joker_effect },
{ COMMON_JOKER, 5, gluttonous_joker_effect },
{ COMMON_JOKER, 3, jolly_joker_effect },
{ COMMON_JOKER, 4, zany_joker_effect },
{ COMMON_JOKER, 4, mad_joker_effect },
{ COMMON_JOKER, 4, crazy_joker_effect },
{ COMMON_JOKER, 4, droll_joker_effect },
{ COMMON_JOKER, 3, sly_joker_effect },
{ COMMON_JOKER, 4, wily_joker_effect },
{ COMMON_JOKER, 4, clever_joker_effect },
{ COMMON_JOKER, 4, devious_joker_effect },
{ COMMON_JOKER, 4, crafty_joker_effect },
{ COMMON_JOKER, 5, half_joker_effect },
};
static const size_t joker_registry_size = NUM_ELEM_IN_ARR(joker_registry);