Survival Royale 1.0.0
A very simple yet funny card game.
Loading...
Searching...
No Matches
main.h
Go to the documentation of this file.
1// Copyright (C) 2025 Giulio Salvi, Jacopo Paradisi
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
20
21#include "config.h"
22#include "includes.h"
23#include "logs_configuration.h"
24
26#define Ace 1
28#define Two 2
30#define Three 3
32#define Four 4
34#define Five 5
36#define Six 6
38#define Seven 7
40#define Jack 8
42#define Queen 9
44#define King 10
45
47#define Clubs 11
49#define Spades 12
51#define Diamonds 13
53#define Hearts 14
54
55#ifndef _MAIN_H_
56 #define _MAIN_H_
58 typedef struct Card {
60 unsigned int suit;
62 unsigned int rank;
64
66 typedef struct Player {
68 unsigned int id;
70 unsigned int lifePoints;
78
92
94 void checkTerminal();
96 void setupTerminal();
99 bool checkTerminalHost();
102 bool checkTerminalSize();
107 int randomInt(const int min, const int max);
110 int askPlayerNumber();
113 Card buildCard();
116 Card* buildDeck();
123 Game buildGame(int playersCounter);
126 void freeDeck(Card* deck);
129 void freePlayer(Player* player);
133 void freePlayers(Player** players, int playersCounter);
136 void freeGame(Game* game);
137
146 Player* preparePlayer(int id, Card* deck, GameConfiguration configuration);
153 Game prepareGame(int playersCounter, Card* deck, GameConfiguration gameConfiguration, LogsConfiguration logsConfiguration);
156 void shuffleDeck(Card* deck);
160 bool handleGamePhase(Game* game);
164 bool isGameEnded(Game* game);
170 bool applyEffect(Game* game, int playerPosition, bool facedUpCard);
174 bool revealFacedDownCard(Card card);
178 void giveCards(Game* game, Card* deck);
181 void announceDeadPlayers(Game* game);
185 int countDeadPlayers(Game* game);
188 void removeDeadPlayers(Game* game);
191 void withdrawCards(Game* game);
194 void announceWinner(Player player);
195
200 bool deckHasCard(Card* deck, Card card);
205 bool cardAreEqual(Card card1, Card card2);
211 bool cardsWereGiven(Player** players, int playersCounter, Player player);
212
218 void revealCard(Card card, int playerId, bool facedUp, bool newLine);
222 void printCardEffect(Card card, bool newLine);
226 void tellFacedDownCard(Card card, int playerId);
227#endif
Definition of functions which retrieves the game configuration from the possible sources.
Preprocessor file used for including library for modules.
Definition of logger configuration structure.
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 s...
Definition main.c:532
Card * prepareCardDeck()
Generates a deck with cards ordered randomly. It is guaranteed that no duplicated cards occurs in the...
Definition main.c:295
bool cardAreEqual(Card card1, Card card2)
Checks if two cards are equal.
Definition main.c:608
bool isGameEnded(Game *game)
Determines whether the game is ended.
Definition main.c:417
void shuffleDeck(Card *deck)
Shuffles the deck using Fisher - Yates algorithm.
Definition main.c:354
Card * buildDeck()
Generates a deck.
Definition main.c:239
void printCardEffect(Card card, bool newLine)
Prints user-friendly the card's effect.
Definition main.c:621
bool handleGamePhase(Game *game)
Handles the current game phase.
Definition main.c:364
Player * buildPlayer()
Generates a player.
Definition main.c:250
bool cardsWereGiven(Player **players, int playersCounter, Player player)
Checks if the cards of a given player have been already given to other players.
Definition main.c:612
void freeGame(Game *game)
Frees a game structure.
Definition main.c:291
Card buildCard()
Generates a card.
Definition main.c:230
bool revealFacedDownCard(Card card)
Asks to the player if he wants to reveal the faced down card.
Definition main.c:512
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 con...
Definition main.c:312
void freePlayer(Player *player)
Frees a player.
Definition main.c:281
bool checkTerminalSize()
Checks if the terminal size is suitable for the TUI (Terminal User Interface).
Definition main.c:94
void setupTerminal()
On Windows, configures that the terminal such that it supports UTF-8 encoded chars.
Definition main.c:88
void revealCard(Card card, int playerId, bool facedUp, bool newLine)
Prints user-friendly the card revealed from a player.
Definition main.c:639
Game buildGame(int playersCounter)
Generates a game with the given number of players.
Definition main.c:263
int randomInt(const int min, const int max)
Generates a random number in the range (inclusive).
Definition main.c:144
void announceWinner(Player player)
Announces the winner.
Definition main.c:596
bool applyEffect(Game *game, int playerPosition, bool facedUpCard)
Applies the effect of the player at the given index in the players vector.
Definition main.c:427
bool checkTerminalHost()
On Windows, it checks if the terminal's host process is supported.
Definition main.c:101
void freePlayers(Player **players, int playersCounter)
Free all the players in the vector.
Definition main.c:285
void checkTerminal()
Checks if the terminal size is suitable for the TUI (Terminal User Interface); if the terminal it is ...
Definition main.c:77
int askPlayerNumber()
Asks the number of players to the user.
Definition main.c:212
void removeDeadPlayers(Game *game)
Removes the dead players from the players vector.
Definition main.c:568
void withdrawCards(Game *game)
Generates via the function buildCard new cards that are given to the players.
Definition main.c:589
void tellFacedDownCard(Card card, int playerId)
Prints user-friendly the faced down card revealed of the player.
Definition main.c:649
bool deckHasCard(Card *deck, Card card)
Checks if a card is present in a deck.
Definition main.c:600
void announceDeadPlayers(Game *game)
Announces the dead players.
Definition main.c:552
int countDeadPlayers(Game *game)
Counts how many dead players there are in the players vector.
Definition main.c:558
void freeDeck(Card *deck)
Frees the cards deck.
Definition main.c:277
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 config...
Definition main.c:343
Defines a structure for the cards.
Definition main.h:58
unsigned int rank
The rank of the card.
Definition main.h:62
unsigned int suit
The suit of the card.
Definition main.h:60
The game configuration structure.
Defines a structure for the game.
Definition main.h:80
unsigned int lifePointsOnTheField
The amount of LPs currently on the playing field.
Definition main.h:88
GameConfiguration gameConfiguration
The configuration for the game.
Definition main.h:82
unsigned int playersCounter
The amount of players currently alive.
Definition main.h:86
Player ** players
The currently alive players vector.
Definition main.h:90
LogsConfiguration logsConfiguration
The configuration for the logs, if the game has to be verbose.
Definition main.h:84
The configuration for the logger.
Defines a structure for the player's info.
Definition main.h:66
Card facedDownCard
The faced down card.
Definition main.h:76
bool revealedFacedDownCard
Flag for recording if the faced down card has been revealed; it can be toggled even if the faced down...
Definition main.h:72
unsigned int lifePoints
The current player LPs.
Definition main.h:70
unsigned int id
The player ID.
Definition main.h:68
Card facedUpCard
The faced up card.
Definition main.h:74