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

This file contains implementations of the functions used for game-related operations. More...

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

Functions

struct Gamegame_createGame (const int pointsNumber)
 Allocates memory for and initializes a game. More...
 
int game_deleteGame (struct Game **game)
 Frees the memory of a game and makes the pointer NULL. More...
 
int game_addPlayer (struct Player *player, struct Game *game)
 Adds a player to a game. More...
 
int game_removePlayer (const struct Player *player, struct Game *game)
 Removes a player from a game. More...
 
int game_addTeam (struct Team *team, struct Game *game)
 Adds a team to a game. More...
 
int game_removeTeam (const struct Team *team, struct Game *game)
 Removes a team from a game. More...
 
struct Teamgame_winningTeam (struct Game *game)
 Searches the winning team of a game. More...
 
int maximumValue (struct Card *cards[], const int length, const enum Suit suit)
 Function to find the maximum value of a certain suit. More...
 
int game_checkCard (struct Player *player, const struct Game *game, struct Hand *hand, const int idCard)
 Function checks if the player can put a card down. More...
 
int findAllowedCard (const struct Player *player, const struct Game *game, struct Hand *hand, int currentCard, const int searchPattern)
 Helper for function that search for an allowed card. More...
 
int game_findNextAllowedCard (struct Player *player, struct Game *game, struct Hand *hand, int currentCard)
 Function to find the next allowed card after currentCard. Uses game_checkCard to check if a card is allowed. More...
 
int game_findPreviousAllowedCard (struct Player *player, struct Game *game, struct Hand *hand, int currentCard)
 Function to find the previous allowed card before currentCard. Uses game_checkCard to check if a card is allowed. More...
 
struct Teamgame_findTeam (const struct Game *game, struct Player *player)
 Function to find the team in which a player belongs. More...
 
int game_updateScore (const struct Game *game, struct Player *bidWinner)
 Function to update the score of teams and players after a round. More...
 
int game_arrangePlayersRound (struct Game *game, const int i)
 Function to add a round to a game and to arrange players into it, according to game rules. More...
 

Detailed Description

This file contains implementations of the functions used for game-related operations.

Function Documentation

int findAllowedCard ( const struct Player player,
const struct Game game,
struct Hand hand,
int  currentCard,
const int  searchPattern 
)

Helper for function that search for an allowed card.

Parameters
playerThe player who's hand is searched.
gameThe game in which the player and the hand belongs.
handThe hand where is supposed to put the card.
currentCardThe cardId to look for.
searchPatternFunction searches the player's hand using a pattern provided by this argument. Currently, there are allowed only 1 and -1 as search patterns, altough the function may be extended to use other patterns as well.
Returns
The first allowed card found.
int game_addPlayer ( struct Player player,
struct Game game 
)

Adds a player to a game.

Parameters
playerThe player to be added.
gameThe game where the player is to be added.
Returns
NO_ERROR on success, other value on failure.
int game_addTeam ( struct Team team,
struct Game game 
)

Adds a team to a game.

Parameters
teamThe team to be added.
gameThe game where the team is to be added to.
Returns
NO_ERROR on success, other value on failure.
int game_arrangePlayersRound ( struct Game game,
const int  i 
)

Function to add a round to a game and to arrange players into it, according to game rules.

Parameters
gameThe game to process.
iThe index of the first player in that round.
Returns
NO_ERROR or 0 on success, other value on failure.
int game_checkCard ( struct Player player,
const struct Game game,
struct Hand hand,
const int  idCard 
)

Function checks if the player can put a card down.

Parameters
playerThe player who wants to put the card down.
gameThe game where the player is located.
handThe hand in which should put the card.
idCardThe id of the card.
Returns
1 if the player may to put the card down 0 if the player can't to put the card down other value on failure.
struct Game* game_createGame ( const int  numberPoints)

Allocates memory for and initializes a game.

Parameters
numberPointsThe number of points required for winning the game.
Returns
Pointer to the new game on success or NULL on failure.
int game_deleteGame ( struct Game **  game)

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

Parameters
gamePointer to the game to be deleted.
Returns
NO_ERROR on success, other value on failure.
int game_findNextAllowedCard ( struct Player player,
struct Game game,
struct Hand hand,
int  currentCard 
)

Function to find the next allowed card after currentCard. Uses game_checkCard to check if a card is allowed.

Parameters
playerThe player who has the cards.
gameThe game where the player is located.
handThe hand in which should put the card.
currentCardFunction finds the first allowed card after currentCard.
Returns
The next allowed card's id on success (value between 0 and MAX_CARDS -1). Negative value on failure.
int game_findPreviousAllowedCard ( struct Player player,
struct Game game,
struct Hand hand,
int  currentCard 
)

Function to find the previous allowed card before currentCard. Uses game_checkCard to check if a card is allowed.

Parameters
playerThe player who has the cards.
gameThe game where the player is located.
handThe hand in which should put the card.
currentCardFunction finds the last allowed card before currentCard.
Returns
The next allowed card's id on success (value between 0 and MAX_CARDS -1). Negative value on failure.
struct Team* game_findTeam ( const struct Game game,
struct Player player 
)

Function to find the team in which a player belongs.

Parameters
gameThe game where to search.
playerThe player to find.
Returns
Pointer to the required team on success, NULL on failure.
int game_removePlayer ( const struct Player player,
struct Game game 
)

Removes a player from a game.

Parameters
playerThe player to be removed.
gameThe game from where the player is to be removed.
Returns
NO_ERROR on success, other value on failure.
int game_removeTeam ( const struct Team team,
struct Game game 
)

Removes a team from a game.

Parameters
teamThe team to be removed.
gameThe game from where the team is to be removed.
Returns
NO_ERROR on success, error code otherwise.
int game_updateScore ( const struct Game game,
struct Player bidWinner 
)

Function to update the score of teams and players after a round.

Parameters
gameGame where to update the scores.
bidWinnerPointer to the player that won the bid in the last round.
Returns
NO_ERROR or 0 on success, other value on failure.
struct Team* game_winningTeam ( struct Game game)

Searches the winning team of a game.

Parameters
gameThe game in which the winning team is to be search.
Returns
Pointer to the winner team on success or NULL on failure.
int maximumValue ( struct Card cards[],
const int  length,
const enum Suit  suit 
)

Function to find the maximum value of a certain suit.

Parameters
cardsThe card array where to look for.
lengthThe length of cards array.
suitThe suit to search for.
Returns
The maximum value of cards of suit indicated as parameter.