Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Four-color deck mode #43

Merged
merged 3 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
-v, --version Show version
-h, --help Show this message
-p, --passes Number of passes through the deck (default: 3)
--four-color-deck Draw unique card suit colors (default: false)
--no-background-color Don't draw background color (default: false)
#+end_src

Expand Down
124 changes: 71 additions & 53 deletions src/game.c
Original file line number Diff line number Diff line change
@@ -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 "common.h"
#include "cursor.h"
#include "deck.h"
#include "game.h"
#include "gui.h"
#include "cursor.h"
#include "common.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();
Expand All @@ -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();
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -175,22 +187,28 @@ static void deal_cards(struct deck *deck) {
}
}

void game_init(struct game *game, int passes_through_deck) {
void game_init(struct game *game, int passes_through_deck,
int four_color_deck) {
cursor_malloc(&cursor);
cursor_init(cursor);
deck_malloc(&deck);
deck_init(deck);

/* 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->four_color_deck = four_color_deck;

fill_deck(deck);
shuffle_deck(deck);
deal_cards(deck);
Expand All @@ -209,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);
}
3 changes: 2 additions & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

struct game {
int passes_through_deck_left;
int four_color_deck;
};

struct deck *deck;
Expand All @@ -42,7 +43,7 @@ bool stock_stack(struct stack *);
bool valid_move(struct stack *, struct stack *);
void move_card(struct stack **, struct stack **);
void move_block(struct stack **, struct stack **, int);
void game_init(struct game *, int);
void game_init(struct game *, int, int);
bool game_won();
void game_end();

Expand Down
48 changes: 30 additions & 18 deletions src/gui.c
Original file line number Diff line number Diff line change
@@ -1,38 +1,50 @@
#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 "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 (card->suit % 2 == 0) {
wattron(card->frame->window, COLOR_PAIR(RED_ON_WHITE));
if (game.four_color_deck == 0) {
if (card->suit % 2 == 0) {
wattron(card->frame->window, COLOR_PAIR(RED_ON_WHITE));
} else {
wattron(card->frame->window, COLOR_PAIR(BLACK_ON_WHITE));
}
} else {
wattron(card->frame->window, COLOR_PAIR(BLACK_ON_WHITE));
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;
}
}
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));
Expand Down
10 changes: 6 additions & 4 deletions src/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
#include "deck.h"
#include "cursor.h"

#define BLACK_ON_WHITE 1
#define RED_ON_WHITE 2
#define WHITE_ON_BLUE 3
#define WHITE_ON_GREEN 4
#define BLACK_ON_WHITE 1
#define RED_ON_WHITE 2
#define GREEN_ON_WHITE 3
#define YELLOW_ON_WHITE 4
#define WHITE_ON_BLUE 5
#define WHITE_ON_GREEN 6

extern struct game game;

Expand Down
16 changes: 13 additions & 3 deletions src/ttysolitaire.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ int main(int argc, char *argv[]) {
int option;
int option_index;
int passes_through_deck = 3;
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'},
{"four-color-deck", no_argument, &four_color_deck, 1},
{"no-background-color", no_argument, &no_background_color, 1},
{0, 0, 0, 0}};

Expand All @@ -43,8 +45,12 @@ int main(int argc, char *argv[]) {
passes_through_deck = atoi(optarg);
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:
Expand All @@ -68,8 +74,10 @@ int main(int argc, char *argv[]) {
}
init_pair(1, COLOR_BLACK, COLOR_WHITE);
init_pair(2, COLOR_RED, COLOR_WHITE);
init_pair(3, COLOR_WHITE, COLOR_BLUE);
init_pair(4, COLOR_WHITE, COLOR_GREEN);
init_pair(3, COLOR_GREEN, COLOR_WHITE);
init_pair(4, COLOR_YELLOW, COLOR_WHITE);
init_pair(5, COLOR_WHITE, COLOR_BLUE);
init_pair(6, COLOR_WHITE, COLOR_GREEN);

int key;

Expand Down Expand Up @@ -99,7 +107,7 @@ int main(int argc, char *argv[]) {
if (key == KEY_SPACEBAR) {
clear();
refresh();
game_init(&game, passes_through_deck);
game_init(&game, passes_through_deck, four_color_deck);
break;
}
} else if (key == KEY_RESIZE) {
Expand Down Expand Up @@ -135,6 +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(" --four-color-deck Draw unique card suit colors "
"(default: false)\n");
printf(" --no-background-color Don't draw background color "
"(default: false)\n");
}
Expand Down