Make "--colors" be "four-color-deck".
Also, some automatic file formatting with LSP via LLVM.
This commit is contained in:
parent
87619ed80a
commit
2fe7075946
2
README
2
README
@ -84,7 +84,7 @@
|
|||||||
-v, --version Show version
|
-v, --version Show version
|
||||||
-h, --help Show this message
|
-h, --help Show this message
|
||||||
-p, --passes Number of passes through the deck (default: 3)
|
-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)
|
--no-background-color Don't draw background color (default: false)
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
126
src/game.c
126
src/game.c
@ -1,24 +1,28 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
#include "game.h"
|
|
||||||
#include "card.h"
|
#include "card.h"
|
||||||
#include "stack.h"
|
|
||||||
#include "deck.h"
|
|
||||||
#include "gui.h"
|
|
||||||
#include "cursor.h"
|
|
||||||
#include "common.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) {
|
static int foundation_begin_x(int x) {
|
||||||
switch (x) {
|
switch (x) {
|
||||||
case 0: return(FOUNDATION_0_BEGIN_X);
|
case 0:
|
||||||
case 1: return(FOUNDATION_1_BEGIN_X);
|
return (FOUNDATION_0_BEGIN_X);
|
||||||
case 2: return(FOUNDATION_2_BEGIN_X);
|
case 1:
|
||||||
case 3: return(FOUNDATION_3_BEGIN_X);
|
return (FOUNDATION_1_BEGIN_X);
|
||||||
|
case 2:
|
||||||
|
return (FOUNDATION_2_BEGIN_X);
|
||||||
|
case 3:
|
||||||
|
return (FOUNDATION_3_BEGIN_X);
|
||||||
default:
|
default:
|
||||||
endwin();
|
endwin();
|
||||||
game_end();
|
game_end();
|
||||||
@ -28,13 +32,20 @@ static int foundation_begin_x(int x) {
|
|||||||
|
|
||||||
static int maneuvre_begin_x(int x) {
|
static int maneuvre_begin_x(int x) {
|
||||||
switch (x) {
|
switch (x) {
|
||||||
case 0: return(MANEUVRE_0_BEGIN_X);
|
case 0:
|
||||||
case 1: return(MANEUVRE_1_BEGIN_X);
|
return (MANEUVRE_0_BEGIN_X);
|
||||||
case 2: return(MANEUVRE_2_BEGIN_X);
|
case 1:
|
||||||
case 3: return(MANEUVRE_3_BEGIN_X);
|
return (MANEUVRE_1_BEGIN_X);
|
||||||
case 4: return(MANEUVRE_4_BEGIN_X);
|
case 2:
|
||||||
case 5: return(MANEUVRE_5_BEGIN_X);
|
return (MANEUVRE_2_BEGIN_X);
|
||||||
case 6: return(MANEUVRE_6_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:
|
default:
|
||||||
endwin();
|
endwin();
|
||||||
game_end();
|
game_end();
|
||||||
@ -43,61 +54,61 @@ static int maneuvre_begin_x(int x) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool waste_pile_stack(struct stack *stack) {
|
static bool waste_pile_stack(struct stack *stack) {
|
||||||
return((stack->card->frame->begin_y == WASTE_PILE_BEGIN_Y) &&
|
return ((stack->card->frame->begin_y == WASTE_PILE_BEGIN_Y) &&
|
||||||
(stack->card->frame->begin_x == WASTE_PILE_BEGIN_X));
|
(stack->card->frame->begin_x == WASTE_PILE_BEGIN_X));
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool foundation_stack(struct stack *stack) {
|
static bool foundation_stack(struct stack *stack) {
|
||||||
return(stack->card->frame->begin_y == FOUNDATION_BEGIN_Y &&
|
return (stack->card->frame->begin_y == FOUNDATION_BEGIN_Y &&
|
||||||
(stack->card->frame->begin_x == FOUNDATION_0_BEGIN_X ||
|
(stack->card->frame->begin_x == FOUNDATION_0_BEGIN_X ||
|
||||||
stack->card->frame->begin_x == FOUNDATION_1_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_2_BEGIN_X ||
|
||||||
stack->card->frame->begin_x == FOUNDATION_3_BEGIN_X));
|
stack->card->frame->begin_x == FOUNDATION_3_BEGIN_X));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool stock_stack(struct stack *stack) {
|
bool stock_stack(struct stack *stack) {
|
||||||
return((stack->card->frame->begin_y == STOCK_BEGIN_Y) &&
|
return ((stack->card->frame->begin_y == STOCK_BEGIN_Y) &&
|
||||||
(stack->card->frame->begin_x == STOCK_BEGIN_X));
|
(stack->card->frame->begin_x == STOCK_BEGIN_X));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool maneuvre_stack(struct stack *stack) {
|
bool maneuvre_stack(struct stack *stack) {
|
||||||
return(stack->card->frame->begin_y >= MANEUVRE_BEGIN_Y &&
|
return (stack->card->frame->begin_y >= MANEUVRE_BEGIN_Y &&
|
||||||
(stack->card->frame->begin_x == MANEUVRE_0_BEGIN_X ||
|
(stack->card->frame->begin_x == MANEUVRE_0_BEGIN_X ||
|
||||||
stack->card->frame->begin_x == MANEUVRE_1_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_2_BEGIN_X ||
|
||||||
stack->card->frame->begin_x == MANEUVRE_3_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_4_BEGIN_X ||
|
||||||
stack->card->frame->begin_x == MANEUVRE_5_BEGIN_X ||
|
stack->card->frame->begin_x == MANEUVRE_5_BEGIN_X ||
|
||||||
stack->card->frame->begin_x == MANEUVRE_6_BEGIN_X));
|
stack->card->frame->begin_x == MANEUVRE_6_BEGIN_X));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool valid_move(struct stack *origin, struct stack *destination) {
|
bool valid_move(struct stack *origin, struct stack *destination) {
|
||||||
if (origin->card->face == EXPOSED) {
|
if (origin->card->face == EXPOSED) {
|
||||||
if (stock_stack(origin) && waste_pile_stack(destination)) {
|
if (stock_stack(origin) && waste_pile_stack(destination)) {
|
||||||
return(true);
|
return (true);
|
||||||
} else if (foundation_stack(destination)) {
|
} else if (foundation_stack(destination)) {
|
||||||
if (stack_empty(destination)) {
|
if (stack_empty(destination)) {
|
||||||
if (origin->card->value == ACE) {
|
if (origin->card->value == ACE) {
|
||||||
return(true);
|
return (true);
|
||||||
}
|
}
|
||||||
} else if (origin->card->suit == destination->card->suit &&
|
} else if (origin->card->suit == destination->card->suit &&
|
||||||
origin->card->value == destination->card->value + 1) {
|
origin->card->value == destination->card->value + 1) {
|
||||||
return(true);
|
return (true);
|
||||||
}
|
}
|
||||||
} else if (maneuvre_stack(destination)) {
|
} else if (maneuvre_stack(destination)) {
|
||||||
if (stack_empty(destination)) {
|
if (stack_empty(destination)) {
|
||||||
if (origin->card->value == KING) {
|
if (origin->card->value == KING) {
|
||||||
return(true);
|
return (true);
|
||||||
}
|
}
|
||||||
} else if (destination->card->face == EXPOSED &&
|
} else if (destination->card->face == EXPOSED &&
|
||||||
(origin->card->suit + destination->card->suit) % 2 == 1 &&
|
(origin->card->suit + destination->card->suit) % 2 == 1 &&
|
||||||
origin->card->value + 1 == destination->card->value) {
|
origin->card->value + 1 == destination->card->value) {
|
||||||
return(true);
|
return (true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return(false);
|
return (false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_card(struct stack **origin, struct stack **destination) {
|
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;
|
struct stack *tmp;
|
||||||
stack_malloc(&tmp);
|
stack_malloc(&tmp);
|
||||||
stack_init(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_malloc(&cursor);
|
||||||
cursor_init(cursor);
|
cursor_init(cursor);
|
||||||
deck_malloc(&deck);
|
deck_malloc(&deck);
|
||||||
@ -183,15 +196,18 @@ void game_init(struct game *game, int passes_through_deck, int color_mode) {
|
|||||||
|
|
||||||
/* Setting initial stacks' coordinates. */
|
/* Setting initial stacks' coordinates. */
|
||||||
frame_set(deck->stock->card->frame, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
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++) {
|
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++) {
|
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);
|
fill_deck(deck);
|
||||||
shuffle_deck(deck);
|
shuffle_deck(deck);
|
||||||
@ -211,17 +227,17 @@ void game_end() {
|
|||||||
bool game_won() {
|
bool game_won() {
|
||||||
// If any card in the maneuvre stacks is covered, game is not won.
|
// If any card in the maneuvre stacks is covered, game is not won.
|
||||||
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
|
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) {
|
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 the stock pile or the waste pile aren't empty, game is not won.
|
||||||
if (!stack_empty(deck->stock) || !stack_empty(deck->waste_pile)) {
|
if (!stack_empty(deck->stock) || !stack_empty(deck->waste_pile)) {
|
||||||
return(false);
|
return (false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return(true);
|
return (true);
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@
|
|||||||
|
|
||||||
struct game {
|
struct game {
|
||||||
int passes_through_deck_left;
|
int passes_through_deck_left;
|
||||||
int color_mode;
|
int four_color_deck;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct deck *deck;
|
struct deck *deck;
|
||||||
|
53
src/gui.c
53
src/gui.c
@ -1,28 +1,25 @@
|
|||||||
|
#include <ncurses.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ncurses.h>
|
|
||||||
|
|
||||||
#include "gui.h"
|
#include "card.h"
|
||||||
#include "deck.h"
|
#include "deck.h"
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "card.h"
|
#include "gui.h"
|
||||||
|
|
||||||
static const char *card_suits[4] = { "\u2666", "\u2660", "\u2665", "\u2663" };
|
static const char *card_suits[4] = {"\u2666", "\u2660", "\u2665", "\u2663"};
|
||||||
static const char *card_values[13] = {
|
static const char *card_values[13] = {"A", "2", "3", "4", "5", "6", "7",
|
||||||
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
|
"8", "9", "10", "J", "Q", "K"};
|
||||||
};
|
|
||||||
|
|
||||||
static void draw_value(struct card *card) {
|
static void draw_value(struct card *card) {
|
||||||
mvwprintw(card->frame->window, 0, 0, card_values[card->value]);
|
mvwprintw(card->frame->window, 0, 0, card_values[card->value]);
|
||||||
mvwprintw(card->frame->window,
|
mvwprintw(card->frame->window, 4, 7 - strlen(card_values[card->value]),
|
||||||
4,
|
|
||||||
7 - strlen(card_values[card->value]),
|
|
||||||
card_values[card->value]);
|
card_values[card->value]);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void draw_suit(struct card *card) {
|
static void draw_suit(struct card *card) {
|
||||||
if (game.color_mode == 0) {
|
if (game.four_color_deck == 0) {
|
||||||
if (card->suit % 2 == 0) {
|
if (card->suit % 2 == 0) {
|
||||||
wattron(card->frame->window, COLOR_PAIR(RED_ON_WHITE));
|
wattron(card->frame->window, COLOR_PAIR(RED_ON_WHITE));
|
||||||
} else {
|
} else {
|
||||||
@ -30,28 +27,24 @@ static void draw_suit(struct card *card) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
switch (card->suit) {
|
switch (card->suit) {
|
||||||
case SPADES:
|
case SPADES:
|
||||||
wattron(card->frame->window, COLOR_PAIR(GREEN_ON_WHITE));
|
wattron(card->frame->window, COLOR_PAIR(GREEN_ON_WHITE));
|
||||||
break;
|
break;
|
||||||
case DIAMONDS:
|
case DIAMONDS:
|
||||||
wattron(card->frame->window, COLOR_PAIR(YELLOW_ON_WHITE));
|
wattron(card->frame->window, COLOR_PAIR(YELLOW_ON_WHITE));
|
||||||
break;
|
break;
|
||||||
case CLUBS:
|
case CLUBS:
|
||||||
wattron(card->frame->window, COLOR_PAIR(BLACK_ON_WHITE));
|
wattron(card->frame->window, COLOR_PAIR(BLACK_ON_WHITE));
|
||||||
break;
|
break;
|
||||||
case HEARTS:
|
case HEARTS:
|
||||||
default:
|
default:
|
||||||
wattron(card->frame->window, COLOR_PAIR(RED_ON_WHITE));
|
wattron(card->frame->window, COLOR_PAIR(RED_ON_WHITE));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mvwprintw(card->frame->window,
|
mvwprintw(card->frame->window, 0, strlen(card_values[card->value]),
|
||||||
0,
|
|
||||||
strlen(card_values[card->value]),
|
|
||||||
card_suits[card->suit]);
|
card_suits[card->suit]);
|
||||||
mvwprintw(card->frame->window,
|
mvwprintw(card->frame->window, 4, 6 - strlen(card_values[card->value]),
|
||||||
4,
|
|
||||||
6 - strlen(card_values[card->value]),
|
|
||||||
card_suits[card->suit]);
|
card_suits[card->suit]);
|
||||||
if (card->suit % 2 == 0) {
|
if (card->suit % 2 == 0) {
|
||||||
wattroff(card->frame->window, COLOR_PAIR(RED_ON_WHITE));
|
wattroff(card->frame->window, COLOR_PAIR(RED_ON_WHITE));
|
||||||
|
@ -23,13 +23,13 @@ int main(int argc, char *argv[]) {
|
|||||||
int option;
|
int option;
|
||||||
int option_index;
|
int option_index;
|
||||||
int passes_through_deck = 3;
|
int passes_through_deck = 3;
|
||||||
int color_mode = 0;
|
static int four_color_deck;
|
||||||
static int no_background_color;
|
static int no_background_color;
|
||||||
static const struct option options[] = {
|
static const struct option options[] = {
|
||||||
{"help", no_argument, NULL, 'h'},
|
{"help", no_argument, NULL, 'h'},
|
||||||
{"version", no_argument, NULL, 'v'},
|
{"version", no_argument, NULL, 'v'},
|
||||||
{"passes", required_argument, NULL, 'p'},
|
{"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},
|
{"no-background-color", no_argument, &no_background_color, 1},
|
||||||
{0, 0, 0, 0}};
|
{0, 0, 0, 0}};
|
||||||
|
|
||||||
@ -44,12 +44,13 @@ int main(int argc, char *argv[]) {
|
|||||||
case 'p':
|
case 'p':
|
||||||
passes_through_deck = atoi(optarg);
|
passes_through_deck = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
case 'c':
|
|
||||||
color_mode = 1;
|
|
||||||
break;
|
|
||||||
case 'h':
|
case 'h':
|
||||||
|
case '?':
|
||||||
|
usage(program_name);
|
||||||
|
exit(0);
|
||||||
case 0:
|
case 0:
|
||||||
/* If this option set a "no_argument" flag, do nothing else now. */
|
/* 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)
|
if (options[option_index].flag != 0)
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -106,7 +107,7 @@ int main(int argc, char *argv[]) {
|
|||||||
if (key == KEY_SPACEBAR) {
|
if (key == KEY_SPACEBAR) {
|
||||||
clear();
|
clear();
|
||||||
refresh();
|
refresh();
|
||||||
game_init(&game, passes_through_deck, color_mode);
|
game_init(&game, passes_through_deck, four_color_deck);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (key == KEY_RESIZE) {
|
} else if (key == KEY_RESIZE) {
|
||||||
@ -142,7 +143,8 @@ void usage(const char *program_name) {
|
|||||||
printf(" -h, --help Show this message\n");
|
printf(" -h, --help Show this message\n");
|
||||||
printf(" -p, --passes Number of passes through the deck "
|
printf(" -p, --passes Number of passes through the deck "
|
||||||
"(default: 3)\n");
|
"(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 "
|
printf(" --no-background-color Don't draw background color "
|
||||||
"(default: false)\n");
|
"(default: false)\n");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user