24 "Ace",
"Two",
"Three",
"Four",
"Five",
25 "Six",
"Seven",
"Jack",
"Queen",
"King"
29 "Clubs",
"Spades",
"Diamonds",
"Hearts"
32void navigatePages(
PageData* pagesData,
int totalPages,
int maxRows,
int maxColumns,
int bestStartColumn,
int playerIndex,
Game* game) {
33 int id = pagesData->
players[playerIndex]->
id;
40 displayPage(&pagesData[currentPage], maxRows, maxColumns, bestStartColumn);
48 displayPage(&pagesData[currentPage], maxRows, maxColumns, bestStartColumn);
55 printgr(
"Do you want reveal it?");
64 input = tolower(getchar());
71 currentPage = (currentPage + 1) % totalPages;
72 displayPage(&pagesData[currentPage], maxRows, maxColumns, bestStartColumn);
73 }
else if(input ==
's') {
77 currentPage = (currentPage - 1 + totalPages) % totalPages;
78 displayPage(&pagesData[currentPage], maxRows, maxColumns, bestStartColumn);
81 bool applyEffectHasPrinted =
applyEffect(game, playerIndex,
false);
82 displayPage(&pagesData[currentPage], maxRows, maxColumns, bestStartColumn);
84 if(!applyEffectHasPrinted) {
89 }
else if(input ==
'n')
90 printgr(
"#b#The card has not been revealed#r#.");
92 if(input ==
'y' || input ==
'n') {
115 drawLine(1, 1, maxColumns,
'h',
'u');
117 drawLine(2, 1, maxRows - 1,
'v',
'n');
118 drawLine(2, maxColumns, maxRows - 1,
'v',
'n');
120 drawLine(maxRows, 1, maxColumns,
'h',
'd');
123 drawLine(logSeparatorRow, 2, maxColumns - 2,
'h',
'n');
131 int currentRow = startX;
134 int currentCol = bestStartColumn + (i % page->
playerPerRow) * columnSpacing;
137 drawCard(currentRow, currentCol, player->facedUpCard.suit, player->facedUpCard.rank);
150void drawLine(
int startingX,
int startingY,
int length,
char direction,
char borders) {
154 for(
int i = 1; i < length - 1; i++) {
160 }
else if(borders ==
'd') {
163 for(
int i = 1; i < length - 1; i++) {
169 }
else if(borders ==
'n') {
170 for(
int i = 0; i < length; i++) {
171 if(direction ==
'h') {
174 }
else if(direction ==
'v') {
182void drawCard(
int startingX,
int startingY,
int suit,
int rank) {
197 cursorPosition(startingX + 2, centerCol - strlen(cardSuitString) / 2);
232 int availableRows = upperSectionHeight / rowSpacing;
235 int usableColumns = maxColumns - 2;
236 int displayablePlayersPerRow = usableColumns / columnSpacing;
239 int totalCardWidth = displayablePlayersPerRow * columnSpacing;
240 int remainingSpace = usableColumns - totalCardWidth;
241 *bestStartColumn = 3 + (remainingSpace / 2);
244 int playersInEachFilledPage = availableRows * displayablePlayersPerRow;
245 *totalPages = (totalPlayers + playersInEachFilledPage - 1) / playersInEachFilledPage;
252 int playersArrayIndex = 0;
254 for(
int i = 0; i < *totalPages; i++) {
255 int playersOnThisPage = (playersArrayIndex + playersInEachFilledPage > totalPlayers)
256 ? totalPlayers - playersArrayIndex
257 : playersInEachFilledPage;
266 if(!pagesData[i].players)
269 for(
int j = 0; j < playersOnThisPage; j++)
270 pagesData[i].players[j] = players[playersArrayIndex++];
277 for(
int i = 0; i < totalPages; i++)
278 free(pages[i].players);
288 for(
int i = 0; i < totalPages; i++)
290 if(pagesData[i].players[j]->
id ==
id)
return i;
void Pause(bool clear)
Waits for the user to press a key without waiting for 'Enter' to be pressed.
void printgr(const char *text)
Prints a text with the graphic rendition specified with custom graphic rendition format specifiers.
void eraseInDisplay(int n)
Clears part of the screen.
void eraseInLine(int n)
Erases part of the line.
void graphicReset()
Resets the graphic rendition.
void defaultForegroundColor()
Resets the foreground color.
void cursorUp(int n)
Moves the cursor up by n rows.
void cursorPosition(int n, int m)
Moves the cursor at n-th row and at the m-th column.
void clearScreen()
Clears the entire terminal's window's content.
void cursorBack(int n)
Moves the cursor to the left by n columns.
void printfgr(char *text,...)
Prints text as printgr but handles standard C format specifiers %, d, i, u, x, X, f,...
#define FgYellow
ANSI standard yellow foreground color.
#define FgBlue
ANSI standard blue foreground color.
#define FgWhite
ANSI standard white foreground color.
#define FgMagenta
ANSI standard magenta foreground color.
#define FgBrightRed
ANSI standard bright red foreground color.
#define FgRed
ANSI standard red foreground color.
#define FgGreen
ANSI standard green foreground color.
#define EXIT_ALLOC_FAILURE
Exit code when dynamic memory allocation fails.
#define Seven
Integer const to represent rank seven.
#define Jack
Integer const to represent rank jack.
#define Ace
Integer const to represent rank ace.
#define Clubs
Integer const to represent suit clubs.
bool applyEffect(Game *game, int playerPosition, bool facedUpCard)
Applies the effect of the player at the given index in the players vector.
void tellFacedDownCard(Card card, int playerId)
Prints user-friendly the faced down card revealed of the player.
#define Queen
Integer const to represent rank queen.
#define King
Integer const to represent rank king.
Defines a structure for the game.
unsigned int lifePointsOnTheField
The amount of LPs currently on the playing field.
Player ** players
The currently alive players vector.
Struct to represent the layout and data of a page containing player information.
int playerPerRow
The number of players displayed per row.
int playerRows
The number of rows used to display players on the page.
int playerCount
The total number of players on the page.
Player ** players
An array of pointers to the players displayed on the page.
int rowSpacing
The vertical spacing between rows, including card height and player details.
Defines a structure for the player's info.
Card facedDownCard
The faced down card.
bool revealedFacedDownCard
Flag for recording if the faced down card has been revealed; it can be toggled even if the faced down...
unsigned int id
The player ID.
Defines a struct for representing a generic color.
void drawPageFrame(int maxRows, int maxColumns)
Draws the frame for a page, including borders and separators.
int getPageContainingPlayer(PageData *pagesData, int totalPages, int id)
Determines which page contains a player by their ID.
PageData * getPageData(int maxRows, int maxColumns, Player **players, int totalPlayers, int *totalPages, int *bestStartColumn)
Computes the page layout for players and allocates memory for the page data.
void drawCard(int startingX, int startingY, int suit, int rank)
Draws a card at a specified position.
const char * CARD_SUIT_STRINGS[]
const char * CARD_RANK_STRINGS[]
void freePageData(PageData *pages, int totalPages)
Frees the memory allocated for page data.
void drawLine(int startingX, int startingY, int length, char direction, char borders)
Draws a line in the terminal.
int getCardColor(int cardRank)
Retrieves the color associated with a card rank.
void displayPage(PageData *page, int maxRows, int maxColumns, int bestStartColumn)
Displays a page of cards and player information.
void drawCardsForPage(PageData *page, int startX, int bestStartColumn)
Draws all cards for the current page.
bool isTerminalSizeValid(int maxRows, int maxColumns)
Checks if the terminal size meets the minimum requirements.
void navigatePages(PageData *pagesData, int totalPages, int maxRows, int maxColumns, int bestStartColumn, int playerIndex, Game *game)
Navigates between pages and handles player actions during their turn.
Terminal User Interface (TUI) implementation for card game display.
#define CARD_HEIGHT
Integer const to represent the height of a card.
#define MIN_ROWS
Integer const to represent the minimum rows of the terminal in order to start the game.
#define MIN_COLUMNS
Integer const to represent the minimum columns of the terminal in order to start the game.
#define LOG_SECTION_HEIGHT
Integer const to represent the height of the logs section.
#define CARD_WIDTH
Integer const to represent the width of a card.