Added some array utils.
This commit is contained in:
		| @@ -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" | ||||
| @@ -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; | ||||
							
								
								
									
										18
									
								
								include/dawn/util/array.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								include/dawn/util/array.h
									
									
									
									
									
										Normal file
									
								
							| @@ -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*); | ||||
| @@ -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); | ||||
|  | ||||
|   | ||||
| @@ -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" | ||||
|   | ||||
| @@ -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); | ||||
| } | ||||
| @@ -6,6 +6,21 @@ | ||||
|  */ | ||||
| #pragma once | ||||
| #include <dawn/dawn.h> | ||||
| #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 | ||||
| ); | ||||
| ); | ||||
|  | ||||
| /** | ||||
|  * 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); | ||||
| @@ -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); | ||||
|     } | ||||
|   } | ||||
| } | ||||
| @@ -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); | ||||
| 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); | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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); | ||||
|   | ||||
| @@ -7,6 +7,7 @@ | ||||
|  | ||||
| #pragma once | ||||
| #include <dawn/dawn.h> | ||||
| #include "../../util/array.h" | ||||
| #include "../render/look.h" | ||||
| #include "../player.h" | ||||
| #include "../card.h" | ||||
|   | ||||
| @@ -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 | ||||
|   } | ||||
| } | ||||
| @@ -8,6 +8,7 @@ | ||||
| #pragma once | ||||
| #include <dawn/dawn.h> | ||||
| #include "../player.h" | ||||
| #include "../card.h" | ||||
|  | ||||
| void pokerWinnerGetFullHand(poker_t *poker,pokerplayer_t *player,card_t *cards); | ||||
| void pokerWinnerInit(poker_t *poker); | ||||
							
								
								
									
										77
									
								
								src/util/array.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								src/util/array.c
									
									
									
									
									
										Normal file
									
								
							| @@ -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); | ||||
| } | ||||
							
								
								
									
										69
									
								
								src/util/array.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								src/util/array.h
									
									
									
									
									
										Normal file
									
								
							| @@ -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 <dawn/dawn.h> | ||||
|  | ||||
| /** | ||||
|  * 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); | ||||
		Reference in New Issue
	
	Block a user