Small refactorings/cleanups.
This commit is contained in:
parent
b5811c5d6d
commit
261950f133
33
src/card.c
33
src/card.c
@ -1,8 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "card.h"
|
||||
#include "common.h"
|
||||
|
||||
@ -11,7 +11,6 @@ void allocate_card(struct card **card) {
|
||||
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
||||
exit(errno);
|
||||
}
|
||||
|
||||
allocate_frame(&((*card)->frame));
|
||||
}
|
||||
|
||||
@ -22,24 +21,8 @@ void initialize_card(struct card *card) {
|
||||
card->face = NO_FACE;
|
||||
}
|
||||
|
||||
struct card *duplicate_card(struct card *card) {
|
||||
struct card *new_card;
|
||||
|
||||
allocate_card(&new_card);
|
||||
set_card(new_card,
|
||||
card->value,
|
||||
card->suit,
|
||||
card->face,
|
||||
card->frame->begin_y,
|
||||
card->frame->begin_x);
|
||||
|
||||
return(new_card);
|
||||
}
|
||||
|
||||
void free_card(struct card *card) {
|
||||
if (card) {
|
||||
free_frame(card->frame);
|
||||
}
|
||||
free(card);
|
||||
}
|
||||
|
||||
@ -62,3 +45,17 @@ void expose_card(struct card *card) {
|
||||
void cover_card(struct card *card) {
|
||||
card->face = COVERED;
|
||||
}
|
||||
|
||||
struct card *duplicate_card(struct card *card) {
|
||||
struct card *new_card;
|
||||
|
||||
allocate_card(&new_card);
|
||||
set_card(new_card,
|
||||
card->value,
|
||||
card->suit,
|
||||
card->face,
|
||||
card->frame->begin_y,
|
||||
card->frame->begin_x);
|
||||
|
||||
return(new_card);
|
||||
}
|
||||
|
@ -31,10 +31,10 @@ struct card {
|
||||
|
||||
void allocate_card(struct card **);
|
||||
void initialize_card(struct card *);
|
||||
struct card *duplicate_card(struct card *);
|
||||
void free_card(struct card *);
|
||||
void set_card(struct card *, enum value, enum suit, enum face, int, int);
|
||||
void expose_card(struct card *);
|
||||
void cover_card(struct card *);
|
||||
struct card *duplicate_card(struct card *);
|
||||
|
||||
#endif
|
||||
|
16
src/cursor.c
16
src/cursor.c
@ -1,34 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <ncurses.h>
|
||||
#include "display.h"
|
||||
#include "game.h"
|
||||
|
||||
#include "cursor.h"
|
||||
#include "game.h"
|
||||
#include "common.h"
|
||||
|
||||
void allocate_cursor(struct cursor **cursor) {
|
||||
if (!(*cursor = malloc(sizeof(**cursor)))) {
|
||||
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
||||
exit(errno);
|
||||
} else {
|
||||
(*cursor)->window = NULL;
|
||||
}
|
||||
(*cursor)->window = newwin(1, 1, CURSOR_BEGIN_Y, CURSOR_BEGIN_X);
|
||||
}
|
||||
|
||||
void initialize_cursor(struct cursor *cursor) {
|
||||
cursor->window = newwin(1, 1, cursor->y, cursor->x);
|
||||
cursor->x = CURSOR_BEGIN_X;
|
||||
mvwin(cursor->window, CURSOR_BEGIN_Y, CURSOR_BEGIN_X);
|
||||
cursor->y = CURSOR_BEGIN_Y;
|
||||
cursor->x = CURSOR_BEGIN_X;
|
||||
cursor->marked = false;
|
||||
}
|
||||
|
||||
void free_cursor(struct cursor *cursor) {
|
||||
if (cursor) {
|
||||
delwin(cursor->window);
|
||||
}
|
||||
free(cursor);
|
||||
}
|
||||
|
||||
@ -88,7 +84,7 @@ void move_cursor(struct cursor *cursor, enum movement movement) {
|
||||
}
|
||||
break;
|
||||
case UP:
|
||||
if (cursor->y > 1) {
|
||||
if (cursor->y > CURSOR_BEGIN_Y) {
|
||||
cursor->y = CURSOR_BEGIN_Y;
|
||||
}
|
||||
break;
|
||||
|
17
src/deck.c
17
src/deck.c
@ -1,8 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "deck.h"
|
||||
#include "common.h"
|
||||
|
||||
@ -11,13 +11,12 @@ void allocate_deck(struct deck **deck) {
|
||||
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
||||
exit(errno);
|
||||
}
|
||||
|
||||
allocate_stack(&((*deck)->stock));
|
||||
allocate_stack(&((*deck)->waste_pile));
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
|
||||
allocate_stack(&((*deck)->foundation[i]));
|
||||
}
|
||||
for (int i = 0; i < 7; i++) {
|
||||
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
|
||||
allocate_stack(&((*deck)->maneuvre[i]));
|
||||
}
|
||||
}
|
||||
@ -25,24 +24,22 @@ void allocate_deck(struct deck **deck) {
|
||||
void initialize_deck(struct deck *deck) {
|
||||
initialize_stack(deck->stock);
|
||||
initialize_stack(deck->waste_pile);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
|
||||
initialize_stack(deck->foundation[i]);
|
||||
}
|
||||
for (int i = 0; i < 7; i++) {
|
||||
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
|
||||
initialize_stack(deck->maneuvre[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void free_deck(struct deck *deck) {
|
||||
if (deck) {
|
||||
free_stack(deck->stock);
|
||||
free_stack(deck->waste_pile);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
|
||||
free_stack(deck->foundation[i]);
|
||||
}
|
||||
for (int i = 0; i < 7; i++) {
|
||||
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
|
||||
free_stack(deck->maneuvre[i]);
|
||||
}
|
||||
}
|
||||
free(deck);
|
||||
}
|
||||
|
@ -3,11 +3,14 @@
|
||||
|
||||
#include "stack.h"
|
||||
|
||||
#define FOUNDATION_STACKS_NUMBER 4
|
||||
#define MANEUVRE_STACKS_NUMBER 7
|
||||
|
||||
struct deck {
|
||||
struct stack *stock;
|
||||
struct stack *waste_pile;
|
||||
struct stack *foundation[4];
|
||||
struct stack *maneuvre[7];
|
||||
struct stack *foundation[FOUNDATION_STACKS_NUMBER];
|
||||
struct stack *maneuvre[MANEUVRE_STACKS_NUMBER];
|
||||
};
|
||||
|
||||
void allocate_deck(struct deck **);
|
||||
|
@ -3,8 +3,9 @@
|
||||
#include <string.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
#include "game.h"
|
||||
#include "display.h"
|
||||
#include "deck.h"
|
||||
#include "game.h"
|
||||
|
||||
static const char *card_suits[4] = { "\u2666", "\u2660", "\u2665", "\u2663" };
|
||||
static const char *card_values[13] = {
|
||||
@ -69,6 +70,7 @@ void draw_stack(struct stack *stack) {
|
||||
for (struct stack *i = reversed_stack; i; i = i->next) {
|
||||
draw_card(i->card);
|
||||
}
|
||||
free_stack(reversed_stack);
|
||||
} else {
|
||||
draw_card(stack->card);
|
||||
}
|
||||
@ -78,10 +80,10 @@ void draw_stack(struct stack *stack) {
|
||||
void draw_deck(struct deck *deck) {
|
||||
draw_stack(deck->stock);
|
||||
draw_stack(deck->waste_pile);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
|
||||
draw_stack(deck->foundation[i]);
|
||||
}
|
||||
for (int i = 0; i < 7; i++) {
|
||||
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
|
||||
draw_stack(deck->maneuvre[i]);
|
||||
}
|
||||
}
|
||||
|
22
src/frame.c
22
src/frame.c
@ -1,8 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "frame.h"
|
||||
#include "common.h"
|
||||
|
||||
@ -19,19 +19,8 @@ void initialize_frame(struct frame *frame) {
|
||||
frame->begin_x = 0;
|
||||
}
|
||||
|
||||
struct frame *duplicate_frame(struct frame *frame) {
|
||||
struct frame *new_frame;
|
||||
|
||||
allocate_frame(&new_frame);
|
||||
set_frame(new_frame, frame->begin_y, frame->begin_x);
|
||||
|
||||
return(new_frame);
|
||||
}
|
||||
|
||||
void free_frame(struct frame *frame) {
|
||||
if (frame) {
|
||||
delwin(frame->window);
|
||||
}
|
||||
free(frame);
|
||||
}
|
||||
|
||||
@ -40,3 +29,12 @@ void set_frame(struct frame *frame, int begin_y, int begin_x) {
|
||||
frame->begin_x = begin_x;
|
||||
mvwin(frame->window, begin_y, begin_x);
|
||||
}
|
||||
|
||||
struct frame *duplicate_frame(struct frame *frame) {
|
||||
struct frame *new_frame;
|
||||
|
||||
allocate_frame(&new_frame);
|
||||
set_frame(new_frame, frame->begin_y, frame->begin_x);
|
||||
|
||||
return(new_frame);
|
||||
}
|
||||
|
@ -14,8 +14,8 @@ struct frame {
|
||||
|
||||
void allocate_frame(struct frame **);
|
||||
void initialize_frame(struct frame *);
|
||||
struct frame *duplicate_frame(struct frame *);
|
||||
void free_frame(struct frame *);
|
||||
void set_frame(struct frame *, int, int);
|
||||
struct frame *duplicate_frame(struct frame *);
|
||||
|
||||
#endif
|
||||
|
36
src/game.c
36
src/game.c
@ -5,12 +5,16 @@
|
||||
#include <time.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "game.h"
|
||||
#include "card.h"
|
||||
#include "stack.h"
|
||||
#include "deck.h"
|
||||
#include "display.h"
|
||||
#include "cursor.h"
|
||||
#include "curses.h"
|
||||
#include "common.h"
|
||||
#include "game.h"
|
||||
|
||||
int foundation_begin_x(int x) {
|
||||
static int foundation_begin_x(int x) {
|
||||
switch (x) {
|
||||
case 0: return(FOUNDATION_0_BEGIN_X); break;
|
||||
case 1: return(FOUNDATION_1_BEGIN_X); break;
|
||||
@ -23,7 +27,7 @@ int foundation_begin_x(int x) {
|
||||
}
|
||||
}
|
||||
|
||||
int maneuvre_begin_x(int x) {
|
||||
static int maneuvre_begin_x(int x) {
|
||||
switch (x) {
|
||||
case 0: return(MANEUVRE_0_BEGIN_X); break;
|
||||
case 1: return(MANEUVRE_1_BEGIN_X); break;
|
||||
@ -39,21 +43,18 @@ int maneuvre_begin_x(int x) {
|
||||
}
|
||||
}
|
||||
|
||||
bool stock_stack(struct stack *stack) {
|
||||
return(stack && stack->card && stack->card->frame &&
|
||||
(stack->card->frame->begin_y == STOCK_BEGIN_Y) &&
|
||||
static bool stock_stack(struct stack *stack) {
|
||||
return((stack->card->frame->begin_y == STOCK_BEGIN_Y) &&
|
||||
(stack->card->frame->begin_x == STOCK_BEGIN_X));
|
||||
}
|
||||
|
||||
bool waste_pile_stack(struct stack *stack) {
|
||||
return(stack && stack->card && stack->card->frame &&
|
||||
(stack->card->frame->begin_y == WASTE_PILE_BEGIN_Y) &&
|
||||
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));
|
||||
}
|
||||
|
||||
bool foundation_stack(struct stack *stack) {
|
||||
return(stack && stack->card && stack->card->frame &&
|
||||
stack->card->frame->begin_y == FOUNDATION_BEGIN_Y &&
|
||||
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 ||
|
||||
@ -61,8 +62,7 @@ bool foundation_stack(struct stack *stack) {
|
||||
}
|
||||
|
||||
bool maneuvre_stack(struct stack *stack) {
|
||||
return(stack && stack->card && stack->card->frame &&
|
||||
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_1_BEGIN_X ||
|
||||
stack->card->frame->begin_x == MANEUVRE_2_BEGIN_X ||
|
||||
@ -135,20 +135,16 @@ static void shuffle_deck(struct deck *deck) {
|
||||
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
||||
exit(errno);
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||
card[i] = pop(&(deck->stock));
|
||||
}
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS - 1; i++) {
|
||||
random = i + (rand() % (NUMBER_OF_CARDS) - i);
|
||||
tmp = *card[i];
|
||||
*card[i] = (*card[random]);
|
||||
*card[random] = tmp;
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||
push(&(deck->stock), card[i]);
|
||||
}
|
||||
@ -182,10 +178,10 @@ void initialize_game() {
|
||||
/* Setting initial stacks' coordinates. */
|
||||
set_frame(deck->stock->card->frame, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
set_frame(deck->waste_pile->card->frame, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
|
||||
set_frame(deck->foundation[i]->card->frame, FOUNDATION_BEGIN_Y, foundation_begin_x(i));
|
||||
}
|
||||
for (int i = 0; i < 7; i++) {
|
||||
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
|
||||
set_frame(deck->maneuvre[i]->card->frame, MANEUVRE_BEGIN_Y, maneuvre_begin_x(i));
|
||||
}
|
||||
|
||||
|
@ -31,10 +31,8 @@
|
||||
struct deck *deck;
|
||||
struct cursor *cursor;
|
||||
|
||||
int foundation_begin_x(int);
|
||||
int maneuvre_begin_x(int);
|
||||
bool valid_move(struct stack *, struct stack *);
|
||||
bool maneuvre_stack(struct stack *);
|
||||
bool valid_move(struct stack *, struct stack *);
|
||||
void move_card(struct stack **, struct stack **);
|
||||
void greet_player();
|
||||
void initialize_game();
|
||||
|
@ -1,10 +1,12 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "keyboard.h"
|
||||
#include "stack.h"
|
||||
#include "game.h"
|
||||
#include "cursor.h"
|
||||
#include "display.h"
|
||||
#include "curses.h"
|
||||
#include "keyboard.h"
|
||||
|
||||
static struct stack **cursor_stack(struct cursor *cursor) {
|
||||
if (cursor->y == CURSOR_BEGIN_Y) {
|
||||
@ -101,6 +103,8 @@ static void handle_card_movement(struct cursor *cursor) {
|
||||
break;
|
||||
case KEY_SPACEBAR:
|
||||
destination = cursor_stack(cursor);
|
||||
/* As 'destination' can be NULL if the cursor is at the invalid spot we
|
||||
* check it before dereferencing */
|
||||
if (destination && valid_move(*origin, *destination)) {
|
||||
erase_stack(*origin);
|
||||
move_card(origin, destination);
|
||||
|
39
src/stack.c
39
src/stack.c
@ -2,9 +2,10 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "stack.h"
|
||||
#include "card.h"
|
||||
#include "common.h"
|
||||
|
||||
void allocate_stack(struct stack **stack) {
|
||||
@ -12,7 +13,6 @@ void allocate_stack(struct stack **stack) {
|
||||
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
||||
exit(errno);
|
||||
}
|
||||
|
||||
allocate_card(&((*stack)->card));
|
||||
}
|
||||
|
||||
@ -21,6 +21,16 @@ void initialize_stack(struct stack *stack) {
|
||||
stack->next = NULL;
|
||||
}
|
||||
|
||||
void free_stack(struct stack *stack) {
|
||||
struct stack *tmp;
|
||||
|
||||
for (; stack; stack = tmp) {
|
||||
tmp = stack->next;
|
||||
free_card(stack->card);
|
||||
free(stack);
|
||||
}
|
||||
}
|
||||
|
||||
struct stack *duplicate_stack(struct stack *stack) {
|
||||
struct stack *iterator = stack;
|
||||
struct stack *tmp_stack, *new_stack;
|
||||
@ -40,16 +50,6 @@ struct stack *duplicate_stack(struct stack *stack) {
|
||||
return(new_stack);
|
||||
}
|
||||
|
||||
void free_stack(struct stack *stack) {
|
||||
struct stack *tmp;
|
||||
|
||||
for (; stack; stack = tmp) {
|
||||
tmp = stack->next;
|
||||
free_card(stack->card);
|
||||
free(stack);
|
||||
}
|
||||
}
|
||||
|
||||
bool empty(struct stack *stack) {
|
||||
return(stack->card->value == NO_VALUE &&
|
||||
stack->card->suit == NO_SUIT &&
|
||||
@ -107,16 +107,21 @@ struct card *pop(struct stack **stack) {
|
||||
}
|
||||
|
||||
struct stack *reverse(struct stack *stack) {
|
||||
if (length(stack) > 1) {
|
||||
struct stack *tmp_stack, *iterator;
|
||||
|
||||
allocate_stack(&tmp_stack);
|
||||
initialize_stack(tmp_stack);
|
||||
if (length(stack) > 1) {
|
||||
for (iterator = stack; iterator; iterator = iterator->next) {
|
||||
push(&tmp_stack, iterator->card);
|
||||
push(&tmp_stack, duplicate_card(iterator->card));
|
||||
}
|
||||
} else {
|
||||
set_card(tmp_stack->card,
|
||||
stack->card->value,
|
||||
stack->card->suit,
|
||||
stack->card->face,
|
||||
stack->card->frame->begin_y,
|
||||
stack->card->frame->begin_x);
|
||||
}
|
||||
return(tmp_stack);
|
||||
} else {
|
||||
return(stack);
|
||||
}
|
||||
}
|
||||
|
@ -10,8 +10,8 @@ struct stack {
|
||||
|
||||
void allocate_stack(struct stack **);
|
||||
void initialize_stack(struct stack *);
|
||||
struct stack *duplicate_stack(struct stack *);
|
||||
void free_stack(struct stack *);
|
||||
struct stack *duplicate_stack(struct stack *);
|
||||
bool empty(struct stack *);
|
||||
int length(struct stack *);
|
||||
void push(struct stack **, struct card *);
|
||||
|
@ -1,8 +1,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <ncurses.h>
|
||||
#include "curses.h"
|
||||
|
||||
#include "game.h"
|
||||
#include "keyboard.h"
|
||||
#include "curses.h"
|
||||
|
||||
const char *program_name;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user