GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
hand.c File Reference

Implementation of functions relative to manipulating and analyzing the contents of the Hand. More...

#include "hand.h"
#include "audio_utils.h"
#include "card.h"
#include "game.h"
#include "game/round.h"
#include "game_variables.h"
#include "graphic_utils.h"
#include "soundbank.h"
#include "util.h"
#include <tonc.h>

Go to the source code of this file.

Data Structures

struct  HandValues
 
struct  Hand
 

Typedefs

typedef struct Hand Hand
 

Functions

static ContainedHandTypes compute_contained_hand_types (void)
 
static enum HandType compute_hand_type (struct ContainedHandTypes contained_types)
 
const char * get_hand_type_name (enum HandType hand_type)
 
enum HandState get_hand_state (void)
 Get the hand state.
 
void set_hand_state (enum HandState new_hand_state)
 Set the hand state. Primarily used by the GAME_PLAYING game state.
 
CardObject ** get_hand_array (void)
 Get the hand array of Cards currently held in hand.
 
int get_hand_top (void)
 Get the position in hand array of the last card obtained.
 
void set_hand_top (int new_hand_top)
 Set the position in hand array of the last card obtained.
 
int hand_nb_held_cards (void)
 Get the current number of Cards in hand.
 
int hand_get_nb_selected_cards (void)
 Get the current number of selected Cards.
 
void hand_set_nb_selected_cards (int new_selections)
 Set the current number of selected Cards.
 
enum HandType get_hand_type (void)
 Get the current hand type.
 
ContainedHandTypesget_contained_hands (void)
 Get the contained hands within the selected hand.
 
static void print_hand_type (const char *hand_type_str)
 
void compute_hand_value_info (void)
 Determine the HandType and ContainedHandTypes of the currently selected Cards, then print the Hand's name, chips, and mult on screen.
 
void swap_cards_in_hand (int idx_a, int idx_b)
 Swaps the order of two cards in hand.
 
static void sort_hand_by_suit (void)
 
static void sort_hand_by_rank (void)
 
static bool shift_null_card_to_end (int null_card_idx)
 
void reorder_card_sprites_layers (void)
 Destroy the sprites of the Cards held in hand and recreate then in the same order as the Cards in the hand array. This allows Cards to render properly when help in hand during the round or a Tarot/Spectral booster pack.
 
void sort_cards (void)
 Sort the hand array according to the selected method to do that.
 
void hand_change_sort (bool to_sort_by_suit)
 Switch to the given sort method. Can sort playing cards in two ways: by rank and suit. The order of suits is as follows:
 
void hand_select_card (int index)
 Set the card at a given index in Hand as selected.
 
void hand_deselect_all_cards (void)
 Deselect all cards in hand.
 
static void get_hand_distribution (u8 ranks_out[NUM_RANKS], u8 suits_out[NUM_SUITS])
 Outputs the distribution of ranks and suits in the hand.
 
static u8 hand_contains_n_of_a_kind (u8 *ranks)
 
static bool hand_contains_two_pair (u8 *ranks)
 
static bool hand_contains_full_house (u8 *ranks)
 
static bool hand_contains_straight (u8 *ranks)
 
static bool hand_contains_flush (u8 *suits)
 
int find_flush_in_played_cards (CardObject **played, int top, int min_len, bool *out_selection)
 Finds the best flush (set of cards with the same suit) in the given array of played cards.
 
int find_straight_in_played_cards (CardObject **played, int top, int min_len, bool *out_selection)
 Finds the best straight in the given array of played cards.
 
void select_paired_cards_in_hand (CardObject **played, int played_top, bool *selection)
 This is used for the special case in "Four Fingers" where you can add a pair into a straight (e.g. AA234 should score all 5 cards)
 

Variables

static const HandValues HAND_BASE_VALUES []
 
static const Rect HAND_TYPE_RECT = {8, 64, 64, 72}
 
static Hand s_hand
 

Detailed Description

Implementation of functions relative to manipulating and analyzing the contents of the Hand.

Definition in file hand.c.

Function Documentation

◆ compute_contained_hand_types()

static ContainedHandTypes compute_contained_hand_types ( void  )
static

Definition at line 763 of file hand.c.

◆ compute_hand_type()

static enum HandType compute_hand_type ( struct ContainedHandTypes  contained_types)
static

Definition at line 861 of file hand.c.

◆ compute_hand_value_info()

void compute_hand_value_info ( void  )

Determine the HandType and ContainedHandTypes of the currently selected Cards, then print the Hand's name, chips, and mult on screen.

Definition at line 156 of file hand.c.

◆ find_flush_in_played_cards()

int find_flush_in_played_cards ( CardObject **  played,
int  top,
int  min_len,
bool *  out_selection 
)

Finds the best flush (set of cards with the same suit) in the given array of played cards.

Normally a Flush is made of 5 cards of the same suit, but this function takes into account the Four Fingers joker which allows for Flushes made of 4 cards only.

The cards belonging to that flush will be marked as selected in the out_selection array.

Parameters
playedArray of pointers to CardObject representing played cards.
topIndex of the top of the played stack.
min_lenMinimum number of cards required for a flush.
out_selectionOutput array of bools; set to true for cards in the best flush, false otherwise.
Returns
The number of cards in the best flush found, or 0 if no flush meets min_len.

Definition at line 544 of file hand.c.

◆ find_straight_in_played_cards()

int find_straight_in_played_cards ( CardObject **  played,
int  top,
int  min_len,
bool *  out_selection 
)

Finds the best straight in the given array of played cards.

Normally, a Straight is a set of 5 cards with sequential ranks, but this function takes into account the Four Fingers and Shortcut jokers, which respectively allow for Straights made of 4 cards and with gaps of 1 rank between two cards.

The cards belonging to that straight will be marked as selected in the out_selection array.

Parameters
playedArray of pointers to CardObject representing played cards.
topIndex of the top of the played stack.
min_lenMinimum number of cards required for a straight.
out_selectionOutput array of bools; set to true for cards in the best straight, false otherwise.
Returns
The number of cards in the best straight found, or 0 if no straight meets min_len.

Definition at line 587 of file hand.c.

◆ get_contained_hands()

ContainedHandTypes * get_contained_hands ( void  )

Get the contained hands within the selected hand.

Returns
ContainedHandTypes*
See also
compute_hand_value_info

Definition at line 135 of file hand.c.

◆ get_hand_array()

CardObject ** get_hand_array ( void  )

Get the hand array of Cards currently held in hand.

Returns
CardObject**

Definition at line 100 of file hand.c.

◆ get_hand_distribution()

static void get_hand_distribution ( u8  ranks_out[NUM_RANKS],
u8  suits_out[NUM_SUITS] 
)
static

Outputs the distribution of ranks and suits in the hand.

Parameters
ranks_outoutput - updated such as ranks_out[rank] is the number of cards of rank in the hand. Must be of size NUM_RANKS.
suits_outoutput - updated such as suits_out[suit] is the number of cards if suit in the hand Must be of size NUM_SUITS

Definition at line 352 of file hand.c.

◆ get_hand_state()

enum HandState get_hand_state ( void  )

Get the hand state.

Returns
enum HandState
See also
set_hand_state

Definition at line 90 of file hand.c.

◆ get_hand_top()

int get_hand_top ( void  )

Get the position in hand array of the last card obtained.

Returns
int
See also
get_hand_array

Definition at line 105 of file hand.c.

◆ get_hand_type()

enum HandType get_hand_type ( void  )

Get the current hand type.

Returns
enum HandType
See also
compute_hand_value_info

Definition at line 130 of file hand.c.

◆ get_hand_type_name()

const char * get_hand_type_name ( enum HandType  hand_type)

Definition at line 80 of file hand.c.

◆ hand_change_sort()

void hand_change_sort ( bool  to_sort_by_suit)

Switch to the given sort method. Can sort playing cards in two ways: by rank and suit. The order of suits is as follows:

SPADES > HEARTS > CLUBS > DIAMONDS
Parameters
to_sort_by_suit

Definition at line 294 of file hand.c.

◆ hand_contains_flush()

static bool hand_contains_flush ( u8 *  suits)
static

Definition at line 530 of file hand.c.

◆ hand_contains_full_house()

static bool hand_contains_full_house ( u8 *  ranks)
static

Definition at line 397 of file hand.c.

◆ hand_contains_n_of_a_kind()

static u8 hand_contains_n_of_a_kind ( u8 *  ranks)
static

Definition at line 371 of file hand.c.

◆ hand_contains_straight()

static bool hand_contains_straight ( u8 *  ranks)
static

Definition at line 421 of file hand.c.

◆ hand_contains_two_pair()

static bool hand_contains_two_pair ( u8 *  ranks)
static

Definition at line 382 of file hand.c.

◆ hand_deselect_all_cards()

void hand_deselect_all_cards ( void  )

Deselect all cards in hand.

Definition at line 324 of file hand.c.

◆ hand_get_nb_selected_cards()

int hand_get_nb_selected_cards ( void  )

Get the current number of selected Cards.

Returns
card_selections

Definition at line 120 of file hand.c.

◆ hand_nb_held_cards()

int hand_nb_held_cards ( void  )

Get the current number of Cards in hand.

Returns
hand_top + 1
See also
get_hand_top

Definition at line 115 of file hand.c.

◆ hand_select_card()

void hand_select_card ( int  index)

Set the card at a given index in Hand as selected.

Parameters
indexIndex of card to select in hand

Definition at line 303 of file hand.c.

◆ hand_set_nb_selected_cards()

void hand_set_nb_selected_cards ( int  new_selections)

Set the current number of selected Cards.

Parameters
new_selections
See also
hand_get_nb_selected_cards

Definition at line 125 of file hand.c.

◆ print_hand_type()

static void print_hand_type ( const char *  hand_type_str)
static

Definition at line 140 of file hand.c.

◆ reorder_card_sprites_layers()

void reorder_card_sprites_layers ( void  )

Destroy the sprites of the Cards held in hand and recreate then in the same order as the Cards in the hand array. This allows Cards to render properly when help in hand during the round or a Tarot/Spectral booster pack.

Definition at line 244 of file hand.c.

◆ select_paired_cards_in_hand()

void select_paired_cards_in_hand ( CardObject **  played,
int  top,
bool *  selection 
)

This is used for the special case in "Four Fingers" where you can add a pair into a straight (e.g. AA234 should score all 5 cards)

Parameters
playedArray of pointers to CardObject representing played cards.
topIndex of the top of the played stack.
selectionIn/Out array of bools; cards from a straight are already set to true, so we add to those the leftover ones that form a pair with an already selected card.

Definition at line 731 of file hand.c.

◆ set_hand_state()

void set_hand_state ( enum  HandState)

Set the hand state. Primarily used by the GAME_PLAYING game state.

See also
HandState

Definition at line 95 of file hand.c.

◆ set_hand_top()

void set_hand_top ( int  new_hand_top)

Set the position in hand array of the last card obtained.

Parameters
new_hand_top
See also
get_hand_top

Definition at line 110 of file hand.c.

◆ shift_null_card_to_end()

static bool shift_null_card_to_end ( int  null_card_idx)
inlinestatic

Definition at line 215 of file hand.c.

◆ sort_cards()

void sort_cards ( void  )

Sort the hand array according to the selected method to do that.

See also
hand_change_sort

Definition at line 280 of file hand.c.

◆ sort_hand_by_rank()

static void sort_hand_by_rank ( void  )
inlinestatic

Definition at line 199 of file hand.c.

◆ sort_hand_by_suit()

static void sort_hand_by_suit ( void  )
inlinestatic

Definition at line 181 of file hand.c.

◆ swap_cards_in_hand()

void swap_cards_in_hand ( int  idx_a,
int  idx_b 
)

Swaps the order of two cards in hand.

Parameters
idx_aindex of the first card
idx_bindex of the second card

Definition at line 174 of file hand.c.

Variable Documentation

◆ HAND_BASE_VALUES

const HandValues HAND_BASE_VALUES[]
static
Initial value:
= {
{.chips = 0, .mult = 0, .display_name = NULL },
{.chips = 5, .mult = 1, .display_name = "Hi-Card"},
{.chips = 10, .mult = 2, .display_name = "Pair" },
{.chips = 20, .mult = 2, .display_name = "2 Pair" },
{.chips = 30, .mult = 3, .display_name = "3 OAK" },
{.chips = 30, .mult = 4, .display_name = "Strt" },
{.chips = 35, .mult = 4, .display_name = "Flush" },
{.chips = 40, .mult = 4, .display_name = "Full H" },
{.chips = 60, .mult = 7, .display_name = "4 OAK" },
{.chips = 100, .mult = 8, .display_name = "Strt F" },
{.chips = 100, .mult = 8, .display_name = "Royal F"},
{.chips = 120, .mult = 12, .display_name = "5 OAK" },
{.chips = 140, .mult = 14, .display_name = "Flush H"},
{.chips = 160, .mult = 16, .display_name = "Flush 5"}
}

Definition at line 27 of file hand.c.

◆ HAND_TYPE_RECT

const Rect HAND_TYPE_RECT = {8, 64, 64, 72}
static

Definition at line 46 of file hand.c.

◆ s_hand

Hand s_hand
static
Initial value:
= {
.cards = {NULL},
.hand_top = -1,
.hand_selections = 0,
.hand_type = NONE,
.contained_hands = {{{0}}},
.state = HAND_DRAW,
.sort_by_suit = false
}

Definition at line 64 of file hand.c.