Make "--colors" be "four-color-deck".

Also, some automatic file formatting with LSP via LLVM.
This commit is contained in:
Murilo Pereira 2020-06-01 16:51:26 +02:00
parent 87619ed80a
commit 2fe7075946
5 changed files with 105 additions and 94 deletions

2
README
View File

@ -84,7 +84,7 @@
-v, --version Show version
-h, --help Show this message
-p, --passes Number of passes through the deck (default: 3)
-c, --colors Unique colors for each suit (default: false)
--four-color-deck Draw unique card suit colors (default: false)
--no-background-color Don't draw background color (default: false)
#+end_src

View File

@ -1,24 +1,28 @@
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <time.h>
#include <assert.h>
#include "game.h"
#include "card.h"
#include "stack.h"
#include "deck.h"
#include "gui.h"
#include "cursor.h"
#include "common.h"
#include "cursor.h"
#include "deck.h"
#include "game.h"
#include "gui.h"
#include "stack.h"
static int foundation_begin_x(int x) {
switch (x) {
case 0: return(FOUNDATION_0_BEGIN_X);
case 1: return(FOUNDATION_1_BEGIN_X);
case 2: return(FOUNDATION_2_BEGIN_X);
case 3: return(FOUNDATION_3_BEGIN_X);
case 0:
return (FOUNDATION_0_BEGIN_X);
case 1:
return (FOUNDATION_1_BEGIN_X);
case 2:
return (FOUNDATION_2_BEGIN_X);
case 3:
return (FOUNDATION_3_BEGIN_X);
default:
endwin();
game_end();
@ -28,13 +32,20 @@ static int foundation_begin_x(int x) {
static int maneuvre_begin_x(int x) {
switch (x) {
case 0: return(MANEUVRE_0_BEGIN_X);
case 1: return(MANEUVRE_1_BEGIN_X);
case 2: return(MANEUVRE_2_BEGIN_X);
case 3: return(MANEUVRE_3_BEGIN_X);
case 4: return(MANEUVRE_4_BEGIN_X);
case 5: return(MANEUVRE_5_BEGIN_X);
case 6: return(MANEUVRE_6_BEGIN_X);
case 0:
return (MANEUVRE_0_BEGIN_X);
case 1:
return (MANEUVRE_1_BEGIN_X);
case 2:
return (MANEUVRE_2_BEGIN_X);
case 3:
return (MANEUVRE_3_BEGIN_X);
case 4:
return (MANEUVRE_4_BEGIN_X);
case 5:
return (MANEUVRE_5_BEGIN_X);
case 6:
return (MANEUVRE_6_BEGIN_X);
default:
endwin();
game_end();
@ -43,61 +54,61 @@ static int maneuvre_begin_x(int x) {
}
static bool waste_pile_stack(struct stack *stack) {
return((stack->card->frame->begin_y == WASTE_PILE_BEGIN_Y) &&
(stack->card->frame->begin_x == WASTE_PILE_BEGIN_X));
return ((stack->card->frame->begin_y == WASTE_PILE_BEGIN_Y) &&
(stack->card->frame->begin_x == WASTE_PILE_BEGIN_X));
}
static bool foundation_stack(struct stack *stack) {
return(stack->card->frame->begin_y == FOUNDATION_BEGIN_Y &&
(stack->card->frame->begin_x == FOUNDATION_0_BEGIN_X ||
stack->card->frame->begin_x == FOUNDATION_1_BEGIN_X ||
stack->card->frame->begin_x == FOUNDATION_2_BEGIN_X ||
stack->card->frame->begin_x == FOUNDATION_3_BEGIN_X));
return (stack->card->frame->begin_y == FOUNDATION_BEGIN_Y &&
(stack->card->frame->begin_x == FOUNDATION_0_BEGIN_X ||
stack->card->frame->begin_x == FOUNDATION_1_BEGIN_X ||
stack->card->frame->begin_x == FOUNDATION_2_BEGIN_X ||
stack->card->frame->begin_x == FOUNDATION_3_BEGIN_X));
}
bool stock_stack(struct stack *stack) {
return((stack->card->frame->begin_y == STOCK_BEGIN_Y) &&
(stack->card->frame->begin_x == STOCK_BEGIN_X));
return ((stack->card->frame->begin_y == STOCK_BEGIN_Y) &&
(stack->card->frame->begin_x == STOCK_BEGIN_X));
}
bool maneuvre_stack(struct stack *stack) {
return(stack->card->frame->begin_y >= MANEUVRE_BEGIN_Y &&
(stack->card->frame->begin_x == MANEUVRE_0_BEGIN_X ||
stack->card->frame->begin_x == MANEUVRE_1_BEGIN_X ||
stack->card->frame->begin_x == MANEUVRE_2_BEGIN_X ||
stack->card->frame->begin_x == MANEUVRE_3_BEGIN_X ||
stack->card->frame->begin_x == MANEUVRE_4_BEGIN_X ||
stack->card->frame->begin_x == MANEUVRE_5_BEGIN_X ||
stack->card->frame->begin_x == MANEUVRE_6_BEGIN_X));
return (stack->card->frame->begin_y >= MANEUVRE_BEGIN_Y &&
(stack->card->frame->begin_x == MANEUVRE_0_BEGIN_X ||
stack->card->frame->begin_x == MANEUVRE_1_BEGIN_X ||
stack->card->frame->begin_x == MANEUVRE_2_BEGIN_X ||
stack->card->frame->begin_x == MANEUVRE_3_BEGIN_X ||
stack->card->frame->begin_x == MANEUVRE_4_BEGIN_X ||
stack->card->frame->begin_x == MANEUVRE_5_BEGIN_X ||
stack->card->frame->begin_x == MANEUVRE_6_BEGIN_X));
}
bool valid_move(struct stack *origin, struct stack *destination) {
if (origin->card->face == EXPOSED) {
if (stock_stack(origin) && waste_pile_stack(destination)) {
return(true);
return (true);
} else if (foundation_stack(destination)) {
if (stack_empty(destination)) {
if (origin->card->value == ACE) {
return(true);
return (true);
}
} else if (origin->card->suit == destination->card->suit &&
origin->card->value == destination->card->value + 1) {
return(true);
origin->card->value == destination->card->value + 1) {
return (true);
}
} else if (maneuvre_stack(destination)) {
if (stack_empty(destination)) {
if (origin->card->value == KING) {
return(true);
return (true);
}
} else if (destination->card->face == EXPOSED &&
(origin->card->suit + destination->card->suit) % 2 == 1 &&
origin->card->value + 1 == destination->card->value) {
return(true);
(origin->card->suit + destination->card->suit) % 2 == 1 &&
origin->card->value + 1 == destination->card->value) {
return (true);
}
}
}
return(false);
return (false);
}
void move_card(struct stack **origin, struct stack **destination) {
@ -113,7 +124,8 @@ void move_card(struct stack **origin, struct stack **destination) {
}
}
void move_block(struct stack **origin, struct stack **destination, int block_size) {
void move_block(struct stack **origin, struct stack **destination,
int block_size) {
struct stack *tmp;
stack_malloc(&tmp);
stack_init(tmp);
@ -175,7 +187,8 @@ static void deal_cards(struct deck *deck) {
}
}
void game_init(struct game *game, int passes_through_deck, int color_mode) {
void game_init(struct game *game, int passes_through_deck,
int four_color_deck) {
cursor_malloc(&cursor);
cursor_init(cursor);
deck_malloc(&deck);
@ -183,15 +196,18 @@ void game_init(struct game *game, int passes_through_deck, int color_mode) {
/* Setting initial stacks' coordinates. */
frame_set(deck->stock->card->frame, STOCK_BEGIN_Y, STOCK_BEGIN_X);
frame_set(deck->waste_pile->card->frame, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
frame_set(deck->waste_pile->card->frame, WASTE_PILE_BEGIN_Y,
WASTE_PILE_BEGIN_X);
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
frame_set(deck->foundation[i]->card->frame, FOUNDATION_BEGIN_Y, foundation_begin_x(i));
frame_set(deck->foundation[i]->card->frame, FOUNDATION_BEGIN_Y,
foundation_begin_x(i));
}
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
frame_set(deck->maneuvre[i]->card->frame, MANEUVRE_BEGIN_Y, maneuvre_begin_x(i));
frame_set(deck->maneuvre[i]->card->frame, MANEUVRE_BEGIN_Y,
maneuvre_begin_x(i));
}
game->color_mode = color_mode;
game->four_color_deck = four_color_deck;
fill_deck(deck);
shuffle_deck(deck);
@ -211,17 +227,17 @@ void game_end() {
bool game_won() {
// If any card in the maneuvre stacks is covered, game is not won.
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
for (struct stack *j = deck->maneuvre[i]; j!= NULL; j = j->next) {
for (struct stack *j = deck->maneuvre[i]; j != NULL; j = j->next) {
if (j->card->face == COVERED) {
return(false);
return (false);
}
}
}
// If the stock pile or the waste pile aren't empty, game is not won.
if (!stack_empty(deck->stock) || !stack_empty(deck->waste_pile)) {
return(false);
return (false);
}
return(true);
return (true);
}

View File

@ -32,7 +32,7 @@
struct game {
int passes_through_deck_left;
int color_mode;
int four_color_deck;
};
struct deck *deck;

View File

@ -1,28 +1,25 @@
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#include "gui.h"
#include "card.h"
#include "deck.h"
#include "game.h"
#include "card.h"
#include "gui.h"
static const char *card_suits[4] = { "\u2666", "\u2660", "\u2665", "\u2663" };
static const char *card_values[13] = {
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
};
static const char *card_suits[4] = {"\u2666", "\u2660", "\u2665", "\u2663"};
static const char *card_values[13] = {"A", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "J", "Q", "K"};
static void draw_value(struct card *card) {
mvwprintw(card->frame->window, 0, 0, card_values[card->value]);
mvwprintw(card->frame->window,
4,
7 - strlen(card_values[card->value]),
mvwprintw(card->frame->window, 4, 7 - strlen(card_values[card->value]),
card_values[card->value]);
}
static void draw_suit(struct card *card) {
if (game.color_mode == 0) {
if (game.four_color_deck == 0) {
if (card->suit % 2 == 0) {
wattron(card->frame->window, COLOR_PAIR(RED_ON_WHITE));
} else {
@ -30,28 +27,24 @@ static void draw_suit(struct card *card) {
}
} else {
switch (card->suit) {
case SPADES:
wattron(card->frame->window, COLOR_PAIR(GREEN_ON_WHITE));
break;
case DIAMONDS:
wattron(card->frame->window, COLOR_PAIR(YELLOW_ON_WHITE));
break;
case CLUBS:
wattron(card->frame->window, COLOR_PAIR(BLACK_ON_WHITE));
break;
case HEARTS:
default:
wattron(card->frame->window, COLOR_PAIR(RED_ON_WHITE));
break;
case SPADES:
wattron(card->frame->window, COLOR_PAIR(GREEN_ON_WHITE));
break;
case DIAMONDS:
wattron(card->frame->window, COLOR_PAIR(YELLOW_ON_WHITE));
break;
case CLUBS:
wattron(card->frame->window, COLOR_PAIR(BLACK_ON_WHITE));
break;
case HEARTS:
default:
wattron(card->frame->window, COLOR_PAIR(RED_ON_WHITE));
break;
}
}
mvwprintw(card->frame->window,
0,
strlen(card_values[card->value]),
mvwprintw(card->frame->window, 0, strlen(card_values[card->value]),
card_suits[card->suit]);
mvwprintw(card->frame->window,
4,
6 - strlen(card_values[card->value]),
mvwprintw(card->frame->window, 4, 6 - strlen(card_values[card->value]),
card_suits[card->suit]);
if (card->suit % 2 == 0) {
wattroff(card->frame->window, COLOR_PAIR(RED_ON_WHITE));

View File

@ -23,13 +23,13 @@ int main(int argc, char *argv[]) {
int option;
int option_index;
int passes_through_deck = 3;
int color_mode = 0;
static int four_color_deck;
static int no_background_color;
static const struct option options[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{"passes", required_argument, NULL, 'p'},
{"colors", no_argument, NULL, 'c'},
{"four-color-deck", no_argument, &four_color_deck, 1},
{"no-background-color", no_argument, &no_background_color, 1},
{0, 0, 0, 0}};
@ -44,12 +44,13 @@ int main(int argc, char *argv[]) {
case 'p':
passes_through_deck = atoi(optarg);
break;
case 'c':
color_mode = 1;
break;
case 'h':
case '?':
usage(program_name);
exit(0);
case 0:
/* If this option set a "no_argument" flag, do nothing else now. */
printf("options[option_index].name: %s\n", options[option_index].name);
if (options[option_index].flag != 0)
break;
default:
@ -106,7 +107,7 @@ int main(int argc, char *argv[]) {
if (key == KEY_SPACEBAR) {
clear();
refresh();
game_init(&game, passes_through_deck, color_mode);
game_init(&game, passes_through_deck, four_color_deck);
break;
}
} else if (key == KEY_RESIZE) {
@ -142,7 +143,8 @@ void usage(const char *program_name) {
printf(" -h, --help Show this message\n");
printf(" -p, --passes Number of passes through the deck "
"(default: 3)\n");
printf(" -c, --colors Unique colors for each suit\n");
printf(" --four-color-deck Draw unique card suit colors "
"(default: false)\n");
printf(" --no-background-color Don't draw background color "
"(default: false)\n");
}