Added Full House

This commit is contained in:
2021-06-23 09:42:13 -07:00
parent 0611b59715
commit c6fd56816c
9 changed files with 132 additions and 45 deletions

View File

@ -76,10 +76,10 @@
////////////////////////////////////////////////////////////////////////////////
// Suits
////////////////////////////////////////////////////////////////////////////////
#define CARD_SUIT_CLUBS 0x0D
#define CARD_SUIT_DIAMONDS 0x1A
#define CARD_SUIT_HEARTS 0x27
#define CARD_SUIT_SPADES 0x34
#define CARD_SUIT_CLUBS 0x00
#define CARD_SUIT_DIAMONDS 0x01
#define CARD_SUIT_HEARTS 0x02
#define CARD_SUIT_SPADES 0x03
////////////////////////////////////////////////////////////////////////////////
// Card numbers
@ -101,6 +101,9 @@
/** Count of cards in each suit */
#define CARD_COUNT_PER_SUIT 13
/** Count of suits */
#define CARD_SUIT_COUNT 4
/** Standard Card Deck Size */
#define CARD_DECK_SIZE 52

View File

@ -15,4 +15,4 @@
* @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*);
typedef int32_t *arraysort_t(void*, void*);

View File

@ -11,12 +11,6 @@
#include "../poker/card.h"
#include "../util/array.h"
#include "../display/camera.h"
#include "../display/shader.h"
#include "../display/gui/font.h"
#include "../display/primitives/quad.h"
#include "../display/primitives/cube.h"
/**
* Initialize the game context.
*

View File

@ -27,7 +27,7 @@ void cardHandSort(card_t *cards, uint8_t length) {
arraySort(sizeof(card_t), cards, (int32_t)length, (arraysort_t *)&_cardSorter);
}
int32_t _cardSorter(const void* left, const void* right) {
int32_t _cardSorter(void* left, void* right) {
card_t cardL = *((card_t *)left);
card_t cardR = *((card_t *)right);
@ -43,11 +43,7 @@ int32_t _cardSorter(const void* left, const void* right) {
}
int32_t cardContains(card_t *hand, uint8_t length, card_t card) {
int32_t i;
for(i = 0; i < length; i++) {
if(hand[i] == card) return i;
}
return -1;
return arrayFind(sizeof(card_t), hand, (int32_t)length, &card);
}
int32_t cardContainsNumber(card_t *hand, uint8_t length, uint8_t number) {
@ -56,4 +52,20 @@ int32_t cardContainsNumber(card_t *hand, uint8_t length, uint8_t number) {
if(cardGetNumber(hand[i]) == number) return i;
}
return -1;
}
uint8_t cardCountPairs(
card_t *in, uint8_t inCount, uint8_t number, int32_t out[CARD_SUIT_COUNT]
) {
uint8_t i, count;
int32_t index;
count = 0;
for(i = 0; i <= CARD_SUIT_COUNT; i++) {// "For each suit"
index = cardContains(in, inCount, cardGet(number, i));
if(index == -1) continue;
out[count++] = index;
}
return count;
}

View File

@ -26,7 +26,7 @@
* Returns the card number for a given suit.
* @param number The number to get.
* @param suit The suit to get.
* @returns The card number for.
* @returns The card for this suit and number combo.
*/
#define cardGet(number, suit) ((suit * CARD_COUNT_PER_SUIT) + number)
@ -50,7 +50,7 @@ void cardDeal(card_t *source, uint8_t *sourceSize, card_t *dest,
* @param length Length of the array of cards.
*/
void cardHandSort(card_t *cards, uint8_t length);
int32_t _cardSorter(const void* left, const void* right);
int32_t _cardSorter(void* left, void* right);
/**
* Check if an array of cards contains a specific card.
@ -70,4 +70,17 @@ int32_t cardContains(card_t *hand, uint8_t length, card_t card);
* @param number The number to look for.
* @returns The index within the array that the first card is. -1 if not found.
*/
int32_t cardContainsNumber(card_t *hand, uint8_t length, uint8_t number);
int32_t cardContainsNumber(card_t *hand, uint8_t length, uint8_t number);
/**
* Counts the amount of times a card's number appears within the given hand.
*
* @param in The hand to check
* @param inCount How big the hand being checked is
* @param number The number to look for.
* @param out The indexes within the hand array for each card matching.
* @return How many cards in the pair, e.g. 3 for Three of a kind, 4 for full.
*/
uint8_t cardCountPairs(
card_t *in, uint8_t inCount, uint8_t number, int32_t out[4]
);

View File

@ -30,7 +30,8 @@ winning_t pokerWinnerGetStatus(poker_t *poker, pokerplayer_t *player) {
uint8_t i, j, l;
int32_t index;
card_t card;
uint8_t number, suit;
uint8_t number, suit, pairCount;
int32_t pairs[CARD_SUIT_COUNT];
// Get the full poker hand (should be a 7 card hand)
winning.size = poker->cardsFacing + player->cardCount;
@ -43,12 +44,14 @@ winning_t pokerWinnerGetStatus(poker_t *poker, pokerplayer_t *player) {
// TESTING HAND
winning.full[0] = CARD_CLUBS_ACE;
winning.full[1] = CARD_CLUBS_TWO;
winning.full[2] = CARD_CLUBS_THREE;
winning.full[3] = CARD_SPADES_FOUR;
winning.full[4] = CARD_CLUBS_FOUR;
winning.full[5] = CARD_CLUBS_FIVE;
winning.full[6] = CARD_CLUBS_JACK;
winning.full[1] = CARD_SPADES_ACE;
winning.full[2] = CARD_HEARTS_TWO;
winning.full[3] = CARD_DIAMONDS_TWO;
winning.full[4] = CARD_SPADES_TWO;
winning.full[5] = CARD_HEARTS_QUEEN;
winning.full[6] = CARD_HEARTS_JACK;
//////////////////////// Now look for the winning set ////////////////////////
@ -74,7 +77,7 @@ winning_t pokerWinnerGetStatus(poker_t *poker, pokerplayer_t *player) {
if(winning.count < POKER_WINNING_SET_SIZE) continue;
// Add self to array
winning.set[0] = card;
winning.set[0] = i;
winning.type = (
number == CARD_ACE ? POKER_WINNING_TYPE_ROYAL_FLUSH :
POKER_WINNING_TYPE_STRAIGHT_FLUSH
@ -87,8 +90,43 @@ winning_t pokerWinnerGetStatus(poker_t *poker, pokerplayer_t *player) {
return winning;
}
// High card (worst)
// Four of a kind.
winning.count = 0;
for(i = 0; i < winning.size; i++) {
card = winning.full[i];
number = cardGetNumber(card);
pairCount = cardCountPairs(winning.full, winning.size, number, pairs);
if(pairCount < CARD_SUIT_COUNT) continue;
winning.count = pairCount;
arrayCopy(sizeof(int32_t), pairs, pairCount, winning.set);
winning.type = POKER_WINNING_TYPE_FOUR_OF_A_KIND;
printf("Four of a kind\n");
return winning;
}
// Full House
winning.count = 0;
for(i = 0; i < winning.size; i++) {
index = i;// Check we haven't already added this card.
if(arrayContains(sizeof(int32_t),winning.set,winning.count,&index))continue;
card = winning.full[i];
number = cardGetNumber(card);
pairCount = cardCountPairs(winning.full, winning.size, number, pairs);
// Did we find either two pair or three pair?
if(pairCount != 2 && pairCount != 3) continue;
if(winning.count == 3) pairCount = 2;//Clamp to 5 max.
arrayCopy(sizeof(int32_t), pairs, pairCount, winning.set+winning.count);
winning.count += pairCount;
if(winning.count != POKER_WINNING_SET_SIZE) continue;
winning.type = POKER_WINNING_TYPE_FULL_HOUSE;
printf("Full House\n");
return winning;
}
// High card (worst)
return winning;
}

View File

@ -12,6 +12,8 @@
#define POKER_WINNING_TYPE_ROYAL_FLUSH 0x01
#define POKER_WINNING_TYPE_STRAIGHT_FLUSH 0x02
#define POKER_WINNING_TYPE_FOUR_OF_A_KIND 0x03
#define POKER_WINNING_TYPE_FULL_HOUSE 0x04
#define POKER_WINNING_SET_SIZE 5

View File

@ -8,9 +8,7 @@
#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)));
return (char *)array + (index * (size/sizeof(char)));
}
void arrayShuffle(size_t size, void *array, int32_t arrayLength) {
@ -44,34 +42,38 @@ int32_t arrayFind(size_t size, void *array, int32_t length, void *value) {
void *item;
for(i = 0; i < length; i++) {
// Get the item
// Get the item and compare.
item = arrayGet(size, array, i);
// Compare the bits directly.
if(memcmp(item, &value, size) == 0) return i;
if(memcmp(item, value, size) == 0) return i;
}
// No find.
return -1;
}
bool arrayContains(size_t size, void *array, int32_t length, void *value) {
int32_t i = arrayFind(size, array, length, value);
return i != -1;
}
void arrayCopy(size_t size, void *source, int32_t length, void *dest) {
memcpy(dest, source, size * length);
}
void arraySort(size_t size, void *array, int32_t length, arraysort_t *sort) {
qsort(array, length, size, sort);
qsort(array, length, size, (_CoreCrtNonSecureSearchSortCompareFunction)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) {
int32_t _arraySorterInt32(void* left, 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) {
int32_t _arraySorterUint8(void* left, void* right) {
return *((uint8_t *)left) - *((uint8_t *)right);
}

View File

@ -28,16 +28,39 @@ void * arrayGet(size_t size, void *array, int32_t index);
void arrayShuffle(size_t size, void *array, int32_t arrayLength);
/**
* Find the index within the array that matches the given value.
* Find the index within the array that matches the given pointer.
*
* @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.
* @param value Pointer to the value to look for. This is memory compared.
* @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);
/**
* Compares each item in an array with the specified value, and returns true if
* found within the array. This is a shorthand for arrayFind without an int32_t
* being returned.
*
* @param size Size of each element within the array.
* @param array Array to get from.
* @param length Max length to check to.
* @param value Pointer to the value to look for. This is memory compared.
* @returns True if the array contains the value, otherwise false.
*/
bool arrayContains(size_t size, void *array, int32_t length, void *value);
/**
* Copies the raw contents from the source array to the destination array.
*
* @param size Size of each element within the array.
* @param source Source array.
* @param length Length of the source array.
* @param dest Destination array.
*/
void arrayCopy(size_t size, void *source, int32_t length, void *dest);
/**
* Sort an array based on the result of an array sort callback.
*
@ -56,7 +79,7 @@ void arraySort(size_t size, void *array, int32_t length, arraysort_t *sort);
*/
void arraySortInt32(int32_t *array, int32_t length);
/** Internal int32_t array sorter. */
int32_t _arraySorterInt32(const void *left, const void* right);
int32_t _arraySorterInt32(void *left, void* right);
/**
* Sort a uint8_t array.
@ -66,4 +89,4 @@ int32_t _arraySorterInt32(const void *left, const void* right);
*/
void arraySortUint8(uint8_t *array, int32_t length);
/** Internal uint8_t array sorter. */
int32_t _arraySorterUint8(const void* left, const void* right);
int32_t _arraySorterUint8(void* left, void* right);