Small refactorings/cleanups.

This commit is contained in:
Murilo Pereira 2011-06-03 02:48:26 -03:00
parent b5811c5d6d
commit 261950f133
14 changed files with 115 additions and 118 deletions

View File

@ -1,8 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <malloc.h> #include <malloc.h>
#include <string.h>
#include <errno.h> #include <errno.h>
#include "card.h" #include "card.h"
#include "common.h" #include "common.h"
@ -11,7 +11,6 @@ void allocate_card(struct card **card) {
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
exit(errno); exit(errno);
} }
allocate_frame(&((*card)->frame)); allocate_frame(&((*card)->frame));
} }
@ -22,24 +21,8 @@ void initialize_card(struct card *card) {
card->face = NO_FACE; 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) { void free_card(struct card *card) {
if (card) {
free_frame(card->frame); free_frame(card->frame);
}
free(card); free(card);
} }
@ -62,3 +45,17 @@ void expose_card(struct card *card) {
void cover_card(struct card *card) { void cover_card(struct card *card) {
card->face = COVERED; 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);
}

View File

@ -31,10 +31,10 @@ struct card {
void allocate_card(struct card **); void allocate_card(struct card **);
void initialize_card(struct card *); void initialize_card(struct card *);
struct card *duplicate_card(struct card *);
void free_card(struct card *); void free_card(struct card *);
void set_card(struct card *, enum value, enum suit, enum face, int, int); void set_card(struct card *, enum value, enum suit, enum face, int, int);
void expose_card(struct card *); void expose_card(struct card *);
void cover_card(struct card *); void cover_card(struct card *);
struct card *duplicate_card(struct card *);
#endif #endif

View File

@ -1,34 +1,30 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <malloc.h> #include <malloc.h>
#include <string.h>
#include <errno.h> #include <errno.h>
#include <ncurses.h> #include <ncurses.h>
#include "display.h"
#include "game.h"
#include "cursor.h" #include "cursor.h"
#include "game.h"
#include "common.h" #include "common.h"
void allocate_cursor(struct cursor **cursor) { void allocate_cursor(struct cursor **cursor) {
if (!(*cursor = malloc(sizeof(**cursor)))) { if (!(*cursor = malloc(sizeof(**cursor)))) {
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
exit(errno); exit(errno);
} else {
(*cursor)->window = NULL;
} }
(*cursor)->window = newwin(1, 1, CURSOR_BEGIN_Y, CURSOR_BEGIN_X);
} }
void initialize_cursor(struct cursor *cursor) { void initialize_cursor(struct cursor *cursor) {
cursor->window = newwin(1, 1, cursor->y, cursor->x); mvwin(cursor->window, CURSOR_BEGIN_Y, CURSOR_BEGIN_X);
cursor->x = CURSOR_BEGIN_X;
cursor->y = CURSOR_BEGIN_Y; cursor->y = CURSOR_BEGIN_Y;
cursor->x = CURSOR_BEGIN_X;
cursor->marked = false; cursor->marked = false;
} }
void free_cursor(struct cursor *cursor) { void free_cursor(struct cursor *cursor) {
if (cursor) {
delwin(cursor->window); delwin(cursor->window);
}
free(cursor); free(cursor);
} }
@ -88,7 +84,7 @@ void move_cursor(struct cursor *cursor, enum movement movement) {
} }
break; break;
case UP: case UP:
if (cursor->y > 1) { if (cursor->y > CURSOR_BEGIN_Y) {
cursor->y = CURSOR_BEGIN_Y; cursor->y = CURSOR_BEGIN_Y;
} }
break; break;

View File

@ -1,8 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <malloc.h> #include <malloc.h>
#include <string.h>
#include <errno.h> #include <errno.h>
#include "deck.h" #include "deck.h"
#include "common.h" #include "common.h"
@ -11,13 +11,12 @@ void allocate_deck(struct deck **deck) {
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
exit(errno); exit(errno);
} }
allocate_stack(&((*deck)->stock)); allocate_stack(&((*deck)->stock));
allocate_stack(&((*deck)->waste_pile)); 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])); 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])); allocate_stack(&((*deck)->maneuvre[i]));
} }
} }
@ -25,24 +24,22 @@ void allocate_deck(struct deck **deck) {
void initialize_deck(struct deck *deck) { void initialize_deck(struct deck *deck) {
initialize_stack(deck->stock); initialize_stack(deck->stock);
initialize_stack(deck->waste_pile); 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]); 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]); initialize_stack(deck->maneuvre[i]);
} }
} }
void free_deck(struct deck *deck) { void free_deck(struct deck *deck) {
if (deck) {
free_stack(deck->stock); free_stack(deck->stock);
free_stack(deck->waste_pile); 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]); 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_stack(deck->maneuvre[i]);
} }
}
free(deck); free(deck);
} }

View File

@ -3,11 +3,14 @@
#include "stack.h" #include "stack.h"
#define FOUNDATION_STACKS_NUMBER 4
#define MANEUVRE_STACKS_NUMBER 7
struct deck { struct deck {
struct stack *stock; struct stack *stock;
struct stack *waste_pile; struct stack *waste_pile;
struct stack *foundation[4]; struct stack *foundation[FOUNDATION_STACKS_NUMBER];
struct stack *maneuvre[7]; struct stack *maneuvre[MANEUVRE_STACKS_NUMBER];
}; };
void allocate_deck(struct deck **); void allocate_deck(struct deck **);

View File

@ -3,8 +3,9 @@
#include <string.h> #include <string.h>
#include <ncurses.h> #include <ncurses.h>
#include "game.h"
#include "display.h" #include "display.h"
#include "deck.h"
#include "game.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] = {
@ -69,6 +70,7 @@ void draw_stack(struct stack *stack) {
for (struct stack *i = reversed_stack; i; i = i->next) { for (struct stack *i = reversed_stack; i; i = i->next) {
draw_card(i->card); draw_card(i->card);
} }
free_stack(reversed_stack);
} else { } else {
draw_card(stack->card); draw_card(stack->card);
} }
@ -78,10 +80,10 @@ void draw_stack(struct stack *stack) {
void draw_deck(struct deck *deck) { void draw_deck(struct deck *deck) {
draw_stack(deck->stock); draw_stack(deck->stock);
draw_stack(deck->waste_pile); 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]); 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]); draw_stack(deck->maneuvre[i]);
} }
} }

View File

@ -1,8 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <malloc.h> #include <malloc.h>
#include <string.h>
#include <errno.h> #include <errno.h>
#include "frame.h" #include "frame.h"
#include "common.h" #include "common.h"
@ -19,19 +19,8 @@ void initialize_frame(struct frame *frame) {
frame->begin_x = 0; 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) { void free_frame(struct frame *frame) {
if (frame) {
delwin(frame->window); delwin(frame->window);
}
free(frame); free(frame);
} }
@ -40,3 +29,12 @@ void set_frame(struct frame *frame, int begin_y, int begin_x) {
frame->begin_x = begin_x; frame->begin_x = begin_x;
mvwin(frame->window, begin_y, 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);
}

View File

@ -14,8 +14,8 @@ struct frame {
void allocate_frame(struct frame **); void allocate_frame(struct frame **);
void initialize_frame(struct frame *); void initialize_frame(struct frame *);
struct frame *duplicate_frame(struct frame *);
void free_frame(struct frame *); void free_frame(struct frame *);
void set_frame(struct frame *, int, int); void set_frame(struct frame *, int, int);
struct frame *duplicate_frame(struct frame *);
#endif #endif

View File

@ -5,12 +5,16 @@
#include <time.h> #include <time.h>
#include <assert.h> #include <assert.h>
#include "game.h"
#include "card.h"
#include "stack.h"
#include "deck.h"
#include "display.h" #include "display.h"
#include "cursor.h"
#include "curses.h" #include "curses.h"
#include "common.h" #include "common.h"
#include "game.h"
int foundation_begin_x(int x) { static int foundation_begin_x(int x) {
switch (x) { switch (x) {
case 0: return(FOUNDATION_0_BEGIN_X); break; case 0: return(FOUNDATION_0_BEGIN_X); break;
case 1: return(FOUNDATION_1_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) { switch (x) {
case 0: return(MANEUVRE_0_BEGIN_X); break; case 0: return(MANEUVRE_0_BEGIN_X); break;
case 1: return(MANEUVRE_1_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) { static bool stock_stack(struct stack *stack) {
return(stack && stack->card && stack->card->frame && return((stack->card->frame->begin_y == STOCK_BEGIN_Y) &&
(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 waste_pile_stack(struct stack *stack) { static bool waste_pile_stack(struct stack *stack) {
return(stack && stack->card && stack->card->frame && return((stack->card->frame->begin_y == WASTE_PILE_BEGIN_Y) &&
(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));
} }
bool foundation_stack(struct stack *stack) { static bool foundation_stack(struct stack *stack) {
return(stack && stack->card && stack->card->frame && return(stack->card->frame->begin_y == FOUNDATION_BEGIN_Y &&
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 ||
@ -61,8 +62,7 @@ bool foundation_stack(struct stack *stack) {
} }
bool maneuvre_stack(struct stack *stack) { bool maneuvre_stack(struct stack *stack) {
return(stack && stack->card && stack->card->frame && return(stack->card->frame->begin_y >= MANEUVRE_BEGIN_Y &&
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 ||
@ -135,20 +135,16 @@ static void shuffle_deck(struct deck *deck) {
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
exit(errno); exit(errno);
} }
for (int i = 0; i < NUMBER_OF_CARDS; i++) { for (int i = 0; i < NUMBER_OF_CARDS; i++) {
card[i] = pop(&(deck->stock)); card[i] = pop(&(deck->stock));
} }
srand(time(NULL)); srand(time(NULL));
for (int i = 0; i < NUMBER_OF_CARDS - 1; i++) { for (int i = 0; i < NUMBER_OF_CARDS - 1; i++) {
random = i + (rand() % (NUMBER_OF_CARDS) - i); random = i + (rand() % (NUMBER_OF_CARDS) - i);
tmp = *card[i]; tmp = *card[i];
*card[i] = (*card[random]); *card[i] = (*card[random]);
*card[random] = tmp; *card[random] = tmp;
} }
for (int i = 0; i < NUMBER_OF_CARDS; i++) { for (int i = 0; i < NUMBER_OF_CARDS; i++) {
push(&(deck->stock), card[i]); push(&(deck->stock), card[i]);
} }
@ -182,10 +178,10 @@ void initialize_game() {
/* Setting initial stacks' coordinates. */ /* Setting initial stacks' coordinates. */
set_frame(deck->stock->card->frame, STOCK_BEGIN_Y, STOCK_BEGIN_X); 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); 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)); 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)); set_frame(deck->maneuvre[i]->card->frame, MANEUVRE_BEGIN_Y, maneuvre_begin_x(i));
} }

View File

@ -31,10 +31,8 @@
struct deck *deck; struct deck *deck;
struct cursor *cursor; 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 maneuvre_stack(struct stack *);
bool valid_move(struct stack *, struct stack *);
void move_card(struct stack **, struct stack **); void move_card(struct stack **, struct stack **);
void greet_player(); void greet_player();
void initialize_game(); void initialize_game();

View File

@ -1,10 +1,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include "keyboard.h"
#include "stack.h"
#include "game.h" #include "game.h"
#include "cursor.h"
#include "display.h" #include "display.h"
#include "curses.h" #include "curses.h"
#include "keyboard.h"
static struct stack **cursor_stack(struct cursor *cursor) { static struct stack **cursor_stack(struct cursor *cursor) {
if (cursor->y == CURSOR_BEGIN_Y) { if (cursor->y == CURSOR_BEGIN_Y) {
@ -101,6 +103,8 @@ static void handle_card_movement(struct cursor *cursor) {
break; break;
case KEY_SPACEBAR: case KEY_SPACEBAR:
destination = cursor_stack(cursor); 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)) { if (destination && valid_move(*origin, *destination)) {
erase_stack(*origin); erase_stack(*origin);
move_card(origin, destination); move_card(origin, destination);

View File

@ -2,9 +2,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <malloc.h> #include <malloc.h>
#include <string.h>
#include <errno.h> #include <errno.h>
#include "stack.h" #include "stack.h"
#include "card.h"
#include "common.h" #include "common.h"
void allocate_stack(struct stack **stack) { 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__)); fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
exit(errno); exit(errno);
} }
allocate_card(&((*stack)->card)); allocate_card(&((*stack)->card));
} }
@ -21,6 +21,16 @@ void initialize_stack(struct stack *stack) {
stack->next = NULL; 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 *duplicate_stack(struct stack *stack) {
struct stack *iterator = stack; struct stack *iterator = stack;
struct stack *tmp_stack, *new_stack; struct stack *tmp_stack, *new_stack;
@ -40,16 +50,6 @@ struct stack *duplicate_stack(struct stack *stack) {
return(new_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) { bool empty(struct stack *stack) {
return(stack->card->value == NO_VALUE && return(stack->card->value == NO_VALUE &&
stack->card->suit == NO_SUIT && stack->card->suit == NO_SUIT &&
@ -107,16 +107,21 @@ struct card *pop(struct stack **stack) {
} }
struct stack *reverse(struct stack *stack) { struct stack *reverse(struct stack *stack) {
if (length(stack) > 1) {
struct stack *tmp_stack, *iterator; struct stack *tmp_stack, *iterator;
allocate_stack(&tmp_stack); allocate_stack(&tmp_stack);
initialize_stack(tmp_stack); initialize_stack(tmp_stack);
if (length(stack) > 1) {
for (iterator = stack; iterator; iterator = iterator->next) { 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); return(tmp_stack);
} else {
return(stack);
}
} }

View File

@ -10,8 +10,8 @@ struct stack {
void allocate_stack(struct stack **); void allocate_stack(struct stack **);
void initialize_stack(struct stack *); void initialize_stack(struct stack *);
struct stack *duplicate_stack(struct stack *);
void free_stack(struct stack *); void free_stack(struct stack *);
struct stack *duplicate_stack(struct stack *);
bool empty(struct stack *); bool empty(struct stack *);
int length(struct stack *); int length(struct stack *);
void push(struct stack **, struct card *); void push(struct stack **, struct card *);

View File

@ -1,8 +1,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <ncurses.h> #include <ncurses.h>
#include "curses.h"
#include "game.h" #include "game.h"
#include "keyboard.h" #include "keyboard.h"
#include "curses.h"
const char *program_name; const char *program_name;