From 337d93438cf5f76f0da0c08e3647fc8807a45448 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sun, 13 Jun 2021 18:16:35 -0700 Subject: [PATCH] Added some array utils. --- include/dawn/dawn.h | 1 + include/dawn/poker/player.h | 3 +- include/dawn/util/array.h | 18 +++++++++ src/game/game.c | 2 +- src/game/game.h | 3 ++ src/poker/card.c | 4 ++ src/poker/card.h | 25 +++++++++++- src/poker/player.c | 17 ++++++++ src/poker/player.h | 19 ++++++++- src/poker/render/player.c | 4 +- src/poker/round/deal.c | 23 +---------- src/poker/round/deal.h | 1 + src/poker/round/winner.c | 20 ++-------- src/poker/round/winner.h | 1 + src/util/array.c | 77 +++++++++++++++++++++++++++++++++++++ src/util/array.h | 69 +++++++++++++++++++++++++++++++++ 16 files changed, 242 insertions(+), 45 deletions(-) create mode 100644 include/dawn/util/array.h create mode 100644 src/util/array.c create mode 100644 src/util/array.h diff --git a/include/dawn/dawn.h b/include/dawn/dawn.h index 402f1503..22c7967f 100644 --- a/include/dawn/dawn.h +++ b/include/dawn/dawn.h @@ -45,6 +45,7 @@ #include "poker/strings.h" // Utility Objects +#include "util/array.h" #include "util/list.h" #include "util/math.h" #include "util/rand.h" \ No newline at end of file diff --git a/include/dawn/poker/player.h b/include/dawn/poker/player.h index 36e3464e..d162a0ab 100644 --- a/include/dawn/poker/player.h +++ b/include/dawn/poker/player.h @@ -45,6 +45,7 @@ typedef struct { // Rendering assets texture_t bodyTexture; primitive_t bodyPrimitive; + texture_t faceTexture; - primitive_t facePrimitive; + primitive_t facePrimitive; } pokerplayer_t; \ No newline at end of file diff --git a/include/dawn/util/array.h b/include/dawn/util/array.h new file mode 100644 index 00000000..67fa65d7 --- /dev/null +++ b/include/dawn/util/array.h @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "../libs.h" + +/** + * Definition of a callback that is used to sort an array. + * + * @param left The left element in the array. + * @param right The right element in the array. + * @return -1 for Left priority, 1 for Right and 0 for Equal. + */ +typedef int32_t *arraysort_t(void const*, void const*); \ No newline at end of file diff --git a/src/game/game.c b/src/game/game.c index 47077894..2fd2575c 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -13,7 +13,7 @@ bool gameInit(game_t *game) { // Init the engine and the rendering pipeline engineInit(&game->engine, game); - + // Hand off to the poker logic. pokerInit(&game->poker, &game->engine); diff --git a/src/game/game.h b/src/game/game.h index 81c32e87..8c542dfa 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -8,6 +8,9 @@ #include "../engine/engine.h" #include "../poker/poker.h" +#include "../poker/card.h" +#include "../util/array.h" + #include "../display/camera.h" #include "../display/shader.h" #include "../display/gui/font.h" diff --git a/src/poker/card.c b/src/poker/card.c index 00cc7908..c1033ee8 100644 --- a/src/poker/card.c +++ b/src/poker/card.c @@ -21,4 +21,8 @@ void cardDeal(card_t *source, uint8_t *sourceSize, card_t *dest, i = *destSize; dest[i] = card; *destSize = i+1; +} + +void cardHandSort(card_t *cards, uint8_t length) { + arraySortUint8(cards, length); } \ No newline at end of file diff --git a/src/poker/card.h b/src/poker/card.h index a78bceb5..791088f0 100644 --- a/src/poker/card.h +++ b/src/poker/card.h @@ -6,6 +6,21 @@ */ #pragma once #include +#include "../util/array.h" + +/** + * Returns the suit of a given card. + * @param card to get the suit for. + * @returns The suit. + */ +#define cardGetSuit(card) (card / CARD_COUNT_PER_SUIT) + +/** + * Returns the number of a given card. + * @param card The card to get the number for. + * @returns The card number. + */ +#define cardGetNumber(card) (card % CARD_COUNT_PER_SUIT) /** * Deals a card from an array source to an array destination. Pointers to array @@ -18,4 +33,12 @@ */ void cardDeal(card_t *source, uint8_t *sourceSize, card_t *dest, uint8_t *destSize -); \ No newline at end of file +); + +/** + * Sort a hand of cards. + * + * @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 diff --git a/src/poker/player.c b/src/poker/player.c index 1d83f1da..df9d328d 100644 --- a/src/poker/player.c +++ b/src/poker/player.c @@ -19,4 +19,21 @@ bool pokerPlayerIsAlive(pokerplayer_t *player) { bool pokerPlayerIsHuman(poker_t *poker, pokerplayer_t *player) { return (poker->players + POKER_PLAYER_HUMAN_INDEX) == player; +} + +void pokerPlayerDeal(poker_t *poker, pokerplayer_t *player) { + cardDeal(poker->deck, &poker->deckSize, player->cards, &player->cardCount); +} + +void pokerPlayerDealAll(poker_t *poker, uint8_t count) { + uint8_t x, y; + pokerplayer_t *player; + + for(y = 0; y < count; y++) { + for(x = 0; x < POKER_PLAYER_COUNT; x++) { + player = poker->players + x; + if(!pokerPlayerIsAlive(player)) continue; + pokerPlayerDeal(poker, player); + } + } } \ No newline at end of file diff --git a/src/poker/player.h b/src/poker/player.h index 10b87750..a7a22f88 100644 --- a/src/poker/player.h +++ b/src/poker/player.h @@ -25,7 +25,6 @@ void pokerPlayerBet(poker_t *poker, pokerplayer_t *player, int32_t chips); */ bool pokerPlayerIsAlive(pokerplayer_t *player); - /** * Returns true if the player provided is human or not. * @@ -33,4 +32,20 @@ bool pokerPlayerIsAlive(pokerplayer_t *player); * @param player Player instance. * @returns True if the player is human. */ -bool pokerPlayerIsHuman(poker_t *poker, pokerplayer_t *player); \ No newline at end of file +bool pokerPlayerIsHuman(poker_t *poker, pokerplayer_t *player); + +/** + * Deal a card to a player. + * + * @param poker Poker game instance. + * @param player Poker player to deal to. + */ +void pokerPlayerDeal(poker_t *poker, pokerplayer_t *player); + +/** + * Deal card(s) to every active player. + * + * @param poker Poker game instance. + * @param count Count of cards to deal. + */ +void pokerPlayerDealAll(poker_t *poker, uint8_t count); \ No newline at end of file diff --git a/src/poker/render/player.c b/src/poker/render/player.c index 01308ac2..df7a23da 100644 --- a/src/poker/render/player.c +++ b/src/poker/render/player.c @@ -14,8 +14,8 @@ void pokerPlayerInit(poker_t *poker) { for(i = 0; i < POKER_PLAYER_COUNT; i++) { player = poker->players + i; - assetTextureLoad(&player->bodyTexture, "characters/penny/body.png"); - assetTextureLoad(&player->faceTexture, "characters/penny/face.png"); + assetTextureLoad(&player->bodyTexture, "characters/penny/textures/body.png"); + assetTextureLoad(&player->faceTexture, "characters/penny/textures/face.png"); w = 0.6; h=((float)player->faceTexture.width)/((float)player->faceTexture.height)*w; diff --git a/src/poker/round/deal.c b/src/poker/round/deal.c index a29c5154..a8b619ed 100644 --- a/src/poker/round/deal.c +++ b/src/poker/round/deal.c @@ -7,35 +7,16 @@ #include "deal.h" void pokerDealInit(poker_t *poker) { - uint8_t x, y; - card_t temporary; - pokerplayer_t *player; - poker->round = POKER_ROUND_DEAL; // Hard look at the dealer pokerLookAtPlayer(&poker->cameraWorld, POKER_SEAT_DEALER); // Shuffle the deck - for(x = 0; x < CARD_DECK_SIZE - 1; x++) { - // Select random element from remaining elements. - y = u8randRange(x, CARD_DECK_SIZE); - temporary = poker->deck[y];// Take out other card - poker->deck[y] = poker->deck[x];// Move my card there - poker->deck[x] = temporary;// Put other card here. - } + arrayShuffle(sizeof(card_t), poker->deck, CARD_DECK_SIZE); // Deal 2 card to each player - for(y = 0; y < POKER_DEAL_CARD_EACH; y++) { - for(x = 0; x < POKER_PLAYER_COUNT; x++) { - player = poker->players + x; - if(!pokerPlayerIsAlive(player)) continue; - cardDeal( - poker->deck, &poker->deckSize, - player->cards, &player->cardCount - ); - } - } + pokerPlayerDealAll(poker, POKER_DEAL_CARD_EACH); printf("Cards Dealt\n"); pokerBetInit(poker); diff --git a/src/poker/round/deal.h b/src/poker/round/deal.h index d02c6e5f..1946da66 100644 --- a/src/poker/round/deal.h +++ b/src/poker/round/deal.h @@ -7,6 +7,7 @@ #pragma once #include +#include "../../util/array.h" #include "../render/look.h" #include "../player.h" #include "../card.h" diff --git a/src/poker/round/winner.c b/src/poker/round/winner.c index c2c19cf1..c3f1217d 100644 --- a/src/poker/round/winner.c +++ b/src/poker/round/winner.c @@ -27,25 +27,11 @@ void pokerWinnerInit(poker_t *poker) { poker->round = POKER_ROUND_WINNER; printf("Winner Winner Chicken Dinner\n"); - // Find the player(s) who have a royal flush? + // Kill me for(uint8_t i = 0; i < POKER_PLAYER_COUNT; i++) { pokerplayer_t *player = poker->players + i; if(!pokerPlayerIsAlive(player)) continue; - - card_t fullHand[POKER_DEALER_HAND+POKER_PLAYER_HAND]; - pokerWinnerGetFullHand(poker, player, fullHand); - uint8_t cardCount = poker->cardsFacing + player->cardCount; - - uint8_t suit; - - // Find the ace and get it's suit - for(uint8_t j = 0; j < cardCount; j++) { - card_t card = fullHand[j]; - if((card % CARD_COUNT_PER_SUIT) != CARD_ACE) continue; - suit = card / CARD_COUNT_PER_SUIT; - break; - } - - // Now we have a suit, check we have each of that + + // Create a fll ahnd } } \ No newline at end of file diff --git a/src/poker/round/winner.h b/src/poker/round/winner.h index 6cc5be83..30c698d0 100644 --- a/src/poker/round/winner.h +++ b/src/poker/round/winner.h @@ -8,6 +8,7 @@ #pragma once #include #include "../player.h" +#include "../card.h" void pokerWinnerGetFullHand(poker_t *poker,pokerplayer_t *player,card_t *cards); void pokerWinnerInit(poker_t *poker); \ No newline at end of file diff --git a/src/util/array.c b/src/util/array.c new file mode 100644 index 00000000..b36757d0 --- /dev/null +++ b/src/util/array.c @@ -0,0 +1,77 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "array.h" + +void * arrayGet(size_t size, void *array, int32_t index) { + // Cast to char array + char *cArray = (char *)array; + return cArray + (index * (size/sizeof(char))); +} + +void arrayShuffle(size_t size, void *array, int32_t arrayLength) { + int32_t x, y; + void *itemDest, *itemSource; + void *temporary = malloc(size); + + for(x = 0; x < arrayLength-1; x++) { + // Select random element from remaining elements. + y = u8randRange(x, arrayLength); + + itemDest = arrayGet(size, array, y); + itemSource = arrayGet(size, array, x); + + // Copy the data into temporary variable + memcpy(temporary, itemDest, size); + + // Take the item at this current index, and store from where we just took + memcpy(itemDest, itemSource, size); + + // Store the thing from where we just took into here. + memcpy(itemSource, temporary, size); + } + + // Cleanup + free(temporary); +} + +int32_t arrayFind(size_t size, void *array, int32_t length, void *value) { + int32_t i; + void *item; + + for(i = 0; i < length; i++) { + // Get the item + item = arrayGet(size, array, i); + + // Compare the bits directly. + if(memcmp(item, &value, size) == 0) return i; + } + + // No find. + return -1; +} + + +void arraySort(size_t size, void *array, int32_t length, arraysort_t *sort) { + qsort(array, length, size, sort); +} + +void arraySortInt32(int32_t *array, int32_t length) { + arraySort(sizeof(int32_t), array, length, &_arraySorterInt32); +} + +int32_t _arraySorterInt32(const void* left, const void* right) { + return *((int32_t *)left) - *((int32_t *)right); +} + +void arraySortUint8(uint8_t *array, int32_t length) { + arraySort(sizeof(uint8_t), array, length, &_arraySorterUint8); +} + +int32_t _arraySorterUint8(const void* left, const void* right) { + return *((uint8_t *)left) - *((uint8_t *)right); +} \ No newline at end of file diff --git a/src/util/array.h b/src/util/array.h new file mode 100644 index 00000000..b84fb486 --- /dev/null +++ b/src/util/array.h @@ -0,0 +1,69 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include + +/** + * Retreive the pointer to an elment within the array of unknown type. + * + * @param size Size of each element within the array. + * @param array Array to get from. + * @param index Index to get + * @return Pointer to the item in the array. + */ +void * arrayGet(size_t size, void *array, int32_t index); + +/** + * Randomizes the contents of an array. + * + * @param size Size of each element within the array. + * @param array Array to shuffle. + * @param arrayLength Length of the array. + */ +void arrayShuffle(size_t size, void *array, int32_t arrayLength); + +/** + * Find the index within the array that matches the given value. + * + * @param size Size of each element within the array. + * @param array Array to get from. + * @param length Max length to check to. + * @param value Value to look for. This is a literal compare of value == value. + * @returns The index within the array that matches, or -1 if no match. + */ +int32_t arrayFind(size_t size, void *array, int32_t length, void *value); + +/** + * Sort an array based on the result of an array sort callback. + * + * @param size Size of each element within the array. + * @param array Array to sort. + * @param length Max length to sort to. + * @param sort Callback to use to sort. + */ +void arraySort(size_t size, void *array, int32_t length, arraysort_t *sort); + +/** + * Sort an int32 array. + * + * @param array Array to sort. + * @param length Max length to sort. + */ +void arraySortInt32(int32_t *array, int32_t length); +/** Internal int32_t array sorter. */ +int32_t _arraySorterInt32(void *left, void*right); + +/** + * Sort a uint8_t array. + * + * @param array Array to sort. + * @param length Max Length to sort. + */ +void arraySortUint8(uint8_t *array, int32_t length); +/** Internal uint8_t array sorter. */ +int32_t _arraySorterUint8(const void* left, const void* right);