Added Unit Tests
This commit is contained in:
@ -72,4 +72,10 @@ uint8_t cardCountPairs(card_t *in, uint8_t inCount, uint8_t number,
|
||||
|
||||
void cardShuffle(card_t *hand, uint8_t length) {
|
||||
arrayShuffle(sizeof(card_t), hand, length);
|
||||
}
|
||||
|
||||
uint8_t cardWriteDeck(card_t *hand) {
|
||||
uint8_t i;
|
||||
for(i = 0; i < CARD_DECK_SIZE; i++) hand[i] = i;
|
||||
return CARD_DECK_SIZE;
|
||||
}
|
@ -194,4 +194,12 @@ uint8_t cardCountPairs(
|
||||
* @param hand Hand to shuffle.
|
||||
* @param length Length of the hand to shuffle.
|
||||
*/
|
||||
void cardShuffle(card_t *hand, uint8_t length);
|
||||
void cardShuffle(card_t *hand, uint8_t length);
|
||||
|
||||
/**
|
||||
* Write an entire deck of cards (in order) to a given hand.
|
||||
*
|
||||
* @param hand Hand to write the deck to.
|
||||
* @return The count of cards in the deck.
|
||||
*/
|
||||
uint8_t cardWriteDeck(card_t *hand);
|
115
src/poker2/poker.c
Normal file
115
src/poker2/poker.c
Normal file
@ -0,0 +1,115 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "poker.h"
|
||||
|
||||
void pokerInit(poker2_t *poker) {
|
||||
pokerResetRound(poker);
|
||||
poker->playerCount = 0;
|
||||
}
|
||||
|
||||
void pokerResetRound(poker2_t *poker) {
|
||||
poker->deckSize = cardWriteDeck(poker->deck);
|
||||
poker->graveSize = 0;
|
||||
poker->communitySize = 0;
|
||||
poker->potCount = 0;
|
||||
pokerPotAdd(poker);
|
||||
}
|
||||
|
||||
// Pot functions
|
||||
uint8_t pokerPotAdd(poker2_t *poker) {
|
||||
poker2pot_t *pot;
|
||||
uint8_t i = poker->potCount++;
|
||||
pot = poker->pots + i;
|
||||
pot->chips = 0;
|
||||
pot->playerCount = 0;
|
||||
return i;
|
||||
}
|
||||
|
||||
void pokerPotAddPlayer(poker2pot_t *pot, uint8_t playerIndex) {
|
||||
if(!arrayContains(
|
||||
sizeof(uint8_t), pot->players, pot->playerCount, &playerIndex
|
||||
)) return;
|
||||
pot->players[pot->playerCount++] = playerIndex;
|
||||
}
|
||||
|
||||
// Dealer Functions
|
||||
void pokerTurn(poker2_t *poker, uint8_t count) {
|
||||
uint8_t i;
|
||||
for(i = 0; i < count; i++) {
|
||||
cardDeal(
|
||||
poker->deck, &poker->deckSize, poker->community, &poker->communitySize
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void pokerBurn(poker2_t *poker, uint8_t count) {
|
||||
uint8_t i;
|
||||
for(i = 0; i < count; i++) {
|
||||
cardDeal(poker->deck, &poker->deckSize, poker->grave, &poker->graveSize);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Player Functions
|
||||
uint8_t pokerPlayerAdd(poker2_t *poker) {
|
||||
poker2player_t *player;
|
||||
uint8_t i = poker->playerCount++;
|
||||
player = poker->players + i;
|
||||
player->cardCount = 0;
|
||||
player->chips = 0;
|
||||
player->state = 0x00;
|
||||
return i;
|
||||
}
|
||||
|
||||
void pokerPlayerDeal(poker2_t *poker, poker2player_t *player, uint8_t count) {
|
||||
uint8_t i;
|
||||
for(i = 0; i < count; i++) {
|
||||
cardDeal(poker->deck, &poker->deckSize, player->cards, &player->cardCount);
|
||||
}
|
||||
}
|
||||
|
||||
void pokerPlayerChipsAdd(poker2player_t *player, int32_t chips) {
|
||||
player->chips += chips;
|
||||
if(player->chips > 0) {
|
||||
flagOff(player->state, POKER_PLAYER_STATE_OUT);
|
||||
} else {
|
||||
player->state |= POKER_PLAYER_STATE_OUT;
|
||||
}
|
||||
}
|
||||
|
||||
void pokerPlayerDealAll(poker2_t *poker, uint8_t count) {
|
||||
uint8_t i;
|
||||
poker2player_t *player;
|
||||
for(i = 0; i < poker->playerCount; i++) {
|
||||
player = poker->players + i;
|
||||
|
||||
// Can't deal to a player who is folded or out
|
||||
if(player->state & (POKER_PLAYER_STATE_FOLDED|POKER_PLAYER_STATE_OUT)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
pokerPlayerDeal(poker, player, count);
|
||||
}
|
||||
}
|
||||
|
||||
// Betting
|
||||
void pokerPlayerBetPot(
|
||||
poker2_t *poker, poker2pot_t *pot, uint8_t playerIndex, int32_t chips
|
||||
) {
|
||||
poker2player_t *player;
|
||||
player = poker->players + playerIndex;
|
||||
player->chips -= chips;
|
||||
pot->chips += chips;
|
||||
pokerPotAddPlayer(pot, playerIndex);
|
||||
}
|
||||
|
||||
void pokerPlayerBet(poker2_t *poker, uint8_t playerIndex, int32_t chips) {
|
||||
pokerPlayerBetPot(
|
||||
poker, poker->pots + (poker->potCount - 1), playerIndex, chips
|
||||
);
|
||||
}
|
72
src/poker2/poker.h
Normal file
72
src/poker2/poker.h
Normal file
@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "../util/flags.h"
|
||||
#include "../util/array.h"
|
||||
#include "../poker/card.h"
|
||||
|
||||
#define POKER_PLAYER_COUNT_MAX 5
|
||||
#define POKER_PLAYER_HAND_SIZE_MAX 2
|
||||
#define POKER_PLAYER_STATE_FOLDED flagDefine(0)
|
||||
#define POKER_PLAYER_STATE_OUT flagDefine(1)
|
||||
|
||||
#define POKER_GRAVE_SIZE_MAX CARD_DECK_SIZE
|
||||
|
||||
#define POKER_COMMUNITY_SIZE_MAX 5
|
||||
#define POKER_POT_COUNT_MAX POKER_PLAYER_COUNT_MAX
|
||||
|
||||
typedef struct {
|
||||
int32_t chips;
|
||||
card_t cards[POKER_PLAYER_HAND_SIZE_MAX];
|
||||
uint8_t cardCount;
|
||||
uint8_t state;
|
||||
} poker2player_t;
|
||||
|
||||
typedef struct {
|
||||
int32_t chips;
|
||||
uint8_t players[POKER_PLAYER_COUNT_MAX];
|
||||
uint8_t playerCount;
|
||||
} poker2pot_t;
|
||||
|
||||
typedef struct {
|
||||
card_t deck[CARD_DECK_SIZE];
|
||||
uint8_t deckSize;
|
||||
|
||||
card_t grave[POKER_GRAVE_SIZE_MAX];
|
||||
uint8_t graveSize;
|
||||
|
||||
card_t community[POKER_COMMUNITY_SIZE_MAX];
|
||||
uint8_t communitySize;
|
||||
|
||||
poker2pot_t pots[POKER_POT_COUNT_MAX];
|
||||
uint8_t potCount;
|
||||
|
||||
poker2player_t players[POKER_PLAYER_COUNT_MAX];
|
||||
uint8_t playerCount;
|
||||
} poker2_t;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void pokerInit(poker2_t *poker);
|
||||
void pokerResetRound(poker2_t *poker);
|
||||
uint8_t pokerPotAdd(poker2_t *poker);
|
||||
void pokerPotAddPlayer(poker2pot_t *pot, uint8_t playerIndex);
|
||||
void pokerTurn(poker2_t *poker, uint8_t count);
|
||||
void pokerBurn(poker2_t *poker, uint8_t count);
|
||||
uint8_t pokerPlayerAdd(poker2_t *poker);
|
||||
void pokerPlayerDeal(poker2_t *poker, poker2player_t *player, uint8_t count);
|
||||
void pokerPlayerChipsAdd(poker2player_t *player, int32_t chips);
|
||||
void pokerPlayerDealAll(poker2_t *poker, uint8_t count);
|
||||
void pokerPlayerBetPot(
|
||||
poker2_t *poker, poker2pot_t *pot, uint8_t playerIndex, int32_t chips
|
||||
);
|
Reference in New Issue
Block a user