From baa79fc646c9baacc72b547d73c700ebbfa43ebb Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 15 Jun 2021 07:14:26 -0700 Subject: [PATCH] Implemented card array sorting --- src/game/game.c | 10 ++++++++++ src/poker/card.c | 27 ++++++++++++++++++++++++++- src/poker/card.h | 4 +++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/game/game.c b/src/game/game.c index 2fd2575c..b22029d0 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -13,6 +13,16 @@ bool gameInit(game_t *game) { // Init the engine and the rendering pipeline engineInit(&game->engine, game); + + // Shufflemuh + card_t hand[CARD_DECK_SIZE]; + for(uint8_t i = 0; i < CARD_DECK_SIZE; i++) { + hand[i] = i; + } + arrayShuffle(sizeof(card_t), hand, CARD_DECK_SIZE); + + cardHandSort(hand, CARD_DECK_SIZE); + printf("Bruh moment\n"); // Hand off to the poker logic. pokerInit(&game->poker, &game->engine); diff --git a/src/poker/card.c b/src/poker/card.c index c1033ee8..ad5044be 100644 --- a/src/poker/card.c +++ b/src/poker/card.c @@ -24,5 +24,30 @@ void cardDeal(card_t *source, uint8_t *sourceSize, card_t *dest, } void cardHandSort(card_t *cards, uint8_t length) { - arraySortUint8(cards, length); + // arraySortUint8(cards, length); + arraySort(sizeof(card_t), cards, (int32_t)length, &_cardSorter); +} + +int32_t _cardSorter(void *left, void *right) { + card_t cardL = *((card_t *)left); + card_t cardR = *((card_t *)right); + + uint8_t suitL = cardGetSuit(cardL); + uint8_t suitR = cardGetSuit(cardR); + + if(suitL != suitR) return suitL - suitR; + + + uint8_t numberL = cardGetNumber(cardL); + uint8_t numberR = cardGetNumber(cardR); + + // Make Aces have higher weight (reversed) + if(numberL == CARD_ACE) { + return numberR == CARD_ACE ? 0 : -1; + } else if(numberR == CARD_ACE) { + return 1; + } + + // Get the suit and the value of the card (reversed) + return numberR - numberL; } \ No newline at end of file diff --git a/src/poker/card.h b/src/poker/card.h index 791088f0..448fe8db 100644 --- a/src/poker/card.h +++ b/src/poker/card.h @@ -41,4 +41,6 @@ void cardDeal(card_t *source, uint8_t *sourceSize, card_t *dest, * @param cards Hand of cards to sort. * @param length Length of the array of cards. */ -void cardHandSort(card_t *cards, uint8_t length); \ No newline at end of file +void cardHandSort(card_t *cards, uint8_t length); + +int32_t _cardSorter(void *left, void *right); \ No newline at end of file