CruceGame  v0.4.0
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
Functions | Variables
deck.c File Reference

In this file are defined functions used for deck-related operations, these functions are declared in the header file deck.h. More...

#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "deck.h"
#include "errors.h"
#include "constants.h"
#include <stdio.h>

Functions

struct Carddeck_createCard (const enum Suit suit, const int value)
 Allocates and initializes a card. More...
 
int deck_deleteCard (struct Card **card)
 Frees the memory of a card and makes the pointer NULL. More...
 
struct Deckdeck_createDeck ()
 Allocates and initializes a deck. More...
 
int deck_deleteDeck (struct Deck **deck)
 Frees the memory of a deck and sets the pointer to NULL. More...
 
void deck_swap (struct Card **a, struct Card **b)
 Swap 2 Cards. More...
 
int deck_deckShuffle (struct Deck *deck)
 Shuffles a deck. More...
 
int deck_compareCards (const struct Card *card1, const struct Card *card2, const enum Suit trump)
 Compare two cards. More...
 
int deck_cardsNumber (const struct Deck *deck)
 The function counts the cards from deck. More...
 

Variables

const int VALUES [] = {2, 3, 4, 0, 10, 11, -1}
 Game values for cards. More...
 

Detailed Description

In this file are defined functions used for deck-related operations, these functions are declared in the header file deck.h.

Function Documentation

int deck_cardsNumber ( const struct Deck deck)

The function counts the cards from deck.

Parameters
deckPointer to the deck from which it counts.
Returns
The cards number from deck.
int deck_compareCards ( const struct Card card1,
const struct Card card2,
const enum Suit  trump 
)

Compare two cards.

Parameters
card1The first card.
card2The second card.
trumpThe trump of the round.
Returns
0 If the cards are equal. 1 If the first card is winning. 2 If the second card is winning. Other value on failure.
struct Card* deck_createCard ( const enum Suit  suit,
const int  value 
)

Allocates and initializes a card.

Parameters
suitThe suit of the new card.
valueThe value of the new card.
Returns
Pointer to the new card on success or NULL on failure.
struct Deck* deck_createDeck ( )

Allocates and initializes a deck.

This function initializes a deck by iterating over all values and suits available. The deck will be always the same.

int deck_deckShuffle ( struct Deck deck)

Shuffles a deck.

This function randomly shuffles the deck. It uses rand function from stdlib with time as seed. The shuffle is performed by random swaps. The number of swaps is also random, but it is at least SWAP_MIN and smaller then SWAP_MAX.

int deck_deleteCard ( struct Card **  card)

Frees the memory of a card and makes the pointer NULL.

Parameters
cardPointer to the pointer to be freed.
Returns
NO_ERROR on success, other value on failure.
int deck_deleteDeck ( struct Deck **  deck)

Frees the memory of a deck and sets the pointer to NULL.

Parameters
deckPointer to the pointer to be freed.
Returns
NO_ERROR on success, other value on failure.
void deck_swap ( struct Card **  a,
struct Card **  b 
)

Swap 2 Cards.

Helper for deckShuffle

Parameters
a
b
Returns
void.

Variable Documentation

const int VALUES[] = {2, 3, 4, 0, 10, 11, -1}

Game values for cards.

Constants for game values