Survival Royale 1.0.0
A very simple yet funny card game.
Loading...
Searching...
No Matches
main.c File Reference

Implementation of game-related functions. More...

#include "tui.h"
#include "ansi.h"
#include "main.h"
#include "logs.h"
#include "ansi_const.h"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
void checkTerminal ()
 Checks if the terminal size is suitable for the TUI (Terminal User Interface); if the terminal it is too small, it exits with EXIT_TERMINAL_TOO_SMALL exit code. On Windows, it checks if the terminal's host process is supported, otherwise it exits with EXIT_WINDOWS_TERMINAL_HOST_NOT_SUPPORTED exit code.
void setupTerminal ()
 On Windows, configures that the terminal such that it supports UTF-8 encoded chars.
bool checkTerminalSize ()
 Checks if the terminal size is suitable for the TUI (Terminal User Interface).
bool checkTerminalHost ()
 On Windows, it checks if the terminal's host process is supported.
int randomInt (const int min, const int max)
 Generates a random number in the range (inclusive).
GameConfiguration askConfigurationOptionsViaTerminal ()
 Asks to the user via terminal the game configuration.
int askPlayerNumber ()
 Asks the number of players to the user.
Card buildCard ()
 Generates a card.
CardbuildDeck ()
 Generates a deck.
PlayerbuildPlayer ()
 Generates a player.
Game buildGame (int playersCounter)
 Generates a game with the given number of players.
void freeDeck (Card *deck)
 Frees the cards deck.
void freePlayer (Player *player)
 Frees a player.
void freePlayers (Player **players, int playersCounter)
 Free all the players in the vector.
void freeGame (Game *game)
 Frees a game structure.
CardprepareCardDeck ()
 Generates a deck with cards ordered randomly. It is guaranteed that no duplicated cards occurs in the same deck.
Game prepareGame (int playersCounter, Card *deck, GameConfiguration gameConfiguration, LogsConfiguration logsConfiguration)
 Generates a game structure with the given players number and with the given data gave by the game configuration.
PlayerpreparePlayer (int id, Card *deck, GameConfiguration configuration)
 Generates a player structure with the given player id and with the given data gave by the game configuration.
void shuffleDeck (Card *deck)
 Shuffles the deck using Fisher - Yates algorithm.
bool handleGamePhase (Game *game)
 Handles the current game phase.
bool isGameEnded (Game *game)
 Determines whether the game is ended.
bool applyEffect (Game *game, int playerPosition, bool facedUpCard)
 Applies the effect of the player at the given index in the players vector.
bool revealFacedDownCard (Card card)
 Asks to the player if he wants to reveal the faced down card.
void giveCards (Game *game, Card *deck)
 Assigns new cards to the player. It is not guaranteed that the old cards might be reassigned to the same player.
void announceDeadPlayers (Game *game)
 Announces the dead players.
int countDeadPlayers (Game *game)
 Counts how many dead players there are in the players vector.
void removeDeadPlayers (Game *game)
 Removes the dead players from the players vector.
void withdrawCards (Game *game)
 Generates via the function buildCard new cards that are given to the players.
void announceWinner (Player player)
 Announces the winner.
bool deckHasCard (Card *deck, Card card)
 Checks if a card is present in a deck.
bool cardAreEqual (Card card1, Card card2)
 Checks if two cards are equal.
bool cardsWereGiven (Player **players, int playersCounter, Player player)
 Checks if the cards of a given player have been already given to other players.
void printCardEffect (Card card, bool newLine)
 Prints user-friendly the card's effect.
void revealCard (Card card, int playerId, bool facedUp, bool newLine)
 Prints user-friendly the card revealed from a player.
void tellFacedDownCard (Card card, int playerId)
 Prints user-friendly the faced down card revealed of the player.

Detailed Description

Implementation of game-related functions.

Definition in file main.c.

Function Documentation

◆ announceDeadPlayers()

void announceDeadPlayers ( Game * game)

Announces the dead players.

Parameters
gamePointer to the game struct.

Definition at line 552 of file main.c.

◆ announceWinner()

void announceWinner ( Player player)

Announces the winner.

Parameters
playerThe player who won.

Definition at line 596 of file main.c.

◆ applyEffect()

bool applyEffect ( Game * game,
int playerPosition,
bool facedUpCard )

Applies the effect of the player at the given index in the players vector.

Parameters
gamePointer to the game struct.
playerPositionThe index of the player in the players vector.
facedUpCardFlag for determining if the effect of the faced up card has to be applied, if set to true.
Returns
If the game is using TUI, returns true if the function has printed something, otherwise false. If it game is not in TUI, returns false.

Definition at line 427 of file main.c.

◆ askConfigurationOptionsViaTerminal()

GameConfiguration askConfigurationOptionsViaTerminal ( )

Asks to the user via terminal the game configuration.

Returns
The game configuration asked via terminal.

Definition at line 148 of file main.c.

◆ askPlayerNumber()

int askPlayerNumber ( )

Asks the number of players to the user.

Returns
The number of players.

Definition at line 212 of file main.c.

◆ buildCard()

Card buildCard ( )

Generates a card.

Returns
Generates a card with seed and number set to -1.

Definition at line 230 of file main.c.

◆ buildDeck()

Card * buildDeck ( )

Generates a deck.

Returns
Generates a deck of 40 cards, generated by buildCard().

Definition at line 239 of file main.c.

◆ buildGame()

Game buildGame ( int playersCounter)

Generates a game with the given number of players.

Parameters
playersCounterThe number of players.
Returns
Generates a game with the given number of players. Players are built by buildPlayer. lifePointsOnTheField field is unset.

Definition at line 263 of file main.c.

◆ buildPlayer()

Player * buildPlayer ( )

Generates a player.

Returns
Generates a player with player's id to -1. Player's cards are built by buildCard. lifePoints property is unset.

Definition at line 250 of file main.c.

◆ cardAreEqual()

bool cardAreEqual ( Card card1,
Card card2 )

Checks if two cards are equal.

Parameters
card1A card to compare.
card2A card to compare.
Returns
True if the cards have the same suit and rank, false otherwise.

Definition at line 608 of file main.c.

◆ cardsWereGiven()

bool cardsWereGiven ( Player ** players,
int playersCounter,
Player player )

Checks if the cards of a given player have been already given to other players.

Parameters
playersThe players vector. It does not contain player.
playersCounterThe number of players in the vector.
playerThe player whose cards are to look for.
Returns
True if a player in the vector has at least one card equal to the player's one, false otherwise.

Definition at line 612 of file main.c.

◆ checkTerminal()

void checkTerminal ( )

Checks if the terminal size is suitable for the TUI (Terminal User Interface); if the terminal it is too small, it exits with EXIT_TERMINAL_TOO_SMALL exit code. On Windows, it checks if the terminal's host process is supported, otherwise it exits with EXIT_WINDOWS_TERMINAL_HOST_NOT_SUPPORTED exit code.

Definition at line 77 of file main.c.

◆ checkTerminalHost()

bool checkTerminalHost ( )

On Windows, it checks if the terminal's host process is supported.

Returns
True if the terminal's host process is supported, false otherwise.

Definition at line 101 of file main.c.

◆ checkTerminalSize()

bool checkTerminalSize ( )

Checks if the terminal size is suitable for the TUI (Terminal User Interface).

Returns
True if the terminal is not too small, false otherwise.

Definition at line 94 of file main.c.

◆ countDeadPlayers()

int countDeadPlayers ( Game * game)

Counts how many dead players there are in the players vector.

Parameters
gamePointer to the game struct.
Returns
The amount of dead players in the players vector.

Definition at line 558 of file main.c.

◆ deckHasCard()

bool deckHasCard ( Card * deck,
Card card )

Checks if a card is present in a deck.

Parameters
deckThe deck to check into.
cardThe card to look for.
Returns
True if the card is present in the deck, false otherwise.

Definition at line 600 of file main.c.

◆ freeDeck()

void freeDeck ( Card * deck)

Frees the cards deck.

Parameters
deckThe deck to free.

Definition at line 277 of file main.c.

◆ freeGame()

void freeGame ( Game * game)

Frees a game structure.

Parameters
gameThe game structure to free.

Definition at line 291 of file main.c.

◆ freePlayer()

void freePlayer ( Player * player)

Frees a player.

Parameters
playerThe player to free.

Definition at line 281 of file main.c.

◆ freePlayers()

void freePlayers ( Player ** players,
int playersCounter )

Free all the players in the vector.

Parameters
playersThe players vector.
playersCounterThe number of players.

Definition at line 285 of file main.c.

◆ giveCards()

void giveCards ( Game * game,
Card * deck )

Assigns new cards to the player. It is not guaranteed that the old cards might be reassigned to the same player.

Parameters
gamePointer to the game struct.
deckThe cards deck.

Definition at line 532 of file main.c.

◆ handleGamePhase()

bool handleGamePhase ( Game * game)

Handles the current game phase.

Parameters
gamePointer to the game struct.
Returns
Returns if the game can go on.

Definition at line 364 of file main.c.

◆ isGameEnded()

bool isGameEnded ( Game * game)

Determines whether the game is ended.

Parameters
gamePointer to the game struct.
Returns
True if only player is still alive, false otherwise.

Definition at line 417 of file main.c.

◆ main()

int main ( int argc,
char ** argv )

Definition at line 27 of file main.c.

◆ prepareCardDeck()

Card * prepareCardDeck ( )

Generates a deck with cards ordered randomly. It is guaranteed that no duplicated cards occurs in the same deck.

Returns
The deck generated randomly.

Definition at line 295 of file main.c.

◆ prepareGame()

Game prepareGame ( int playersCounter,
Card * deck,
GameConfiguration gameConfiguration,
LogsConfiguration logsConfiguration )

Generates a game structure with the given players number and with the given data gave by the game configuration.

Parameters
playersCounterThe number of players.
deckThe deck from which the cards are given.
gameConfigurationThe game configuration form which data, like default LPs on the playing field, is taken.
logsConfigurationThe logs configuration to append to the generated game structure.
Returns
The generated game. It is guaranteed that players do not have the same cards.

Definition at line 312 of file main.c.

◆ preparePlayer()

Player * preparePlayer ( int id,
Card * deck,
GameConfiguration configuration )

Generates a player structure with the given player id and with the given data gave by the game configuration.

Parameters
idThe ID of the player.
deckThe deck from which the cards are given.
configurationThe game configuration from which data, like default LPs, is taken.
Returns
The generated player. It may occur that this function, called several times, generates players with the same cards.

Definition at line 343 of file main.c.

◆ printCardEffect()

void printCardEffect ( Card card,
bool newLine )

Prints user-friendly the card's effect.

Parameters
cardThe card whose effect has to be printed.
newLineFlag for determining if the function has to go to a new line before exiting.

Definition at line 621 of file main.c.

◆ randomInt()

int randomInt ( const int min,
const int max )

Generates a random number in the range (inclusive).

Parameters
minThe minimum possibly generated value.
maxThe maximum possibly generated value.
Returns
A random value between min and max (inclusive).

Definition at line 144 of file main.c.

◆ removeDeadPlayers()

void removeDeadPlayers ( Game * game)

Removes the dead players from the players vector.

Parameters
gamePointer to the game struct.

Definition at line 568 of file main.c.

◆ revealCard()

void revealCard ( Card card,
int playerId,
bool facedUp,
bool newLine )

Prints user-friendly the card revealed from a player.

Parameters
cardThe card that has been revealed.
playerIdThe player ID.
facedUpFlag for determining if the card was faced up, if set to true.
newLineFlag for determining if the function has to go to a new line before exiting.

Definition at line 639 of file main.c.

◆ revealFacedDownCard()

bool revealFacedDownCard ( Card card)

Asks to the player if he wants to reveal the faced down card.

Parameters
cardThe card that could be revealed.
Returns
True if the player wants to reveal the faced down card, false otherwise.

Definition at line 512 of file main.c.

◆ setupTerminal()

void setupTerminal ( )

On Windows, configures that the terminal such that it supports UTF-8 encoded chars.

Definition at line 88 of file main.c.

◆ shuffleDeck()

void shuffleDeck ( Card * deck)

Shuffles the deck using Fisher - Yates algorithm.

Parameters
deckThe deck shuffled.

Definition at line 354 of file main.c.

◆ tellFacedDownCard()

void tellFacedDownCard ( Card card,
int playerId )

Prints user-friendly the faced down card revealed of the player.

Parameters
cardThe faced down card.
playerIdThe card's owner ID. If it is 0, it prints 'Your', otherwise it prints 'Player <playerID>'s'

Definition at line 649 of file main.c.

◆ withdrawCards()

void withdrawCards ( Game * game)

Generates via the function buildCard new cards that are given to the players.

Parameters
gamePointer to the game struct.

Definition at line 589 of file main.c.