Erasing stacks when empty. Also moved some event-related code to the keyboard object.

This commit is contained in:
Murilo Soares Pereira 2010-04-21 04:14:39 -03:00
parent 87909b5075
commit 7b39ec5cc6
11 changed files with 50 additions and 16 deletions

View File

@ -1,7 +1,5 @@
#include <ncurses.h> #include <ncurses.h>
#include <malloc.h> #include <malloc.h>
#include "stack.h"
#include "deck.h"
#include "display.h" #include "display.h"
#include "game.h" #include "game.h"
#include "cursor.h" #include "cursor.h"

View File

@ -2,6 +2,7 @@
#define CURSOR_H #define CURSOR_H
#include <stdbool.h> #include <stdbool.h>
#include "deck.h"
#define CURSOR_STARTING_X 4 #define CURSOR_STARTING_X 4
#define CURSOR_STARTING_Y 7 #define CURSOR_STARTING_Y 7

View File

@ -46,6 +46,22 @@ char *card_value(enum value value) {
return(card_value); return(card_value);
} }
void erase_stack(struct stack *stack) {
WINDOW *empty_stack = NULL;
wbkgd(stack->card->frame->shape, WHITE_ON_GREEN);
wrefresh(stack->card->frame->shape);
empty_stack = newwin(FRAME_HEIGHT,
FRAME_WIDTH,
stack->card->frame->start_y,
stack->card->frame->start_x);
box(empty_stack, 0, 0);
wrefresh(empty_stack);
delwin(empty_stack);
return;
}
void draw_empty_stacks() { void draw_empty_stacks() {
WINDOW **empty_stack; WINDOW **empty_stack;

View File

@ -13,9 +13,11 @@
#define BLACK_ON_WHITE 1 #define BLACK_ON_WHITE 1
#define RED_ON_WHITE 2 #define RED_ON_WHITE 2
#define WHITE_ON_BLUE 3 #define WHITE_ON_BLUE 3
#define WHITE_ON_GREEN 4
char *card_suit(enum suit); char *card_suit(enum suit);
char *card_value(enum value); char *card_value(enum value);
void erase_stack(struct stack *);
void draw_empty_stacks(); void draw_empty_stacks();
void draw_value(struct card *); void draw_value(struct card *);
void draw_suit(struct card *); void draw_suit(struct card *);

View File

@ -220,14 +220,3 @@ void end_game(struct deck *deck) {
return; return;
} }
void handle_stock_event() {
if (!empty(deck->stock)) {
move_card(&(deck->stock), &(deck->waste_pile));
expose_card(deck->waste_pile->card);
draw_stack(deck->stock);
draw_stack(deck->waste_pile);
}
return;
}

View File

@ -36,6 +36,5 @@ void greet_player();
void initialize_game(); void initialize_game();
void prepare_game(struct deck **); void prepare_game(struct deck **);
void end_game(); void end_game();
void handle_stock_event();
#endif #endif

View File

@ -1,6 +1,22 @@
#include <stdio.h> #include <stdio.h>
#include "display.h"
#include "keyboard.h" #include "keyboard.h"
void handle_stock_event() {
if (!empty(deck->stock)) {
/* erase the stack before emptying it */
if (length(deck->stock) == 1) {
erase_stack(deck->stock);
}
move_card(&(deck->stock), &(deck->waste_pile));
expose_card(deck->waste_pile->card);
draw_stack(deck->stock);
draw_stack(deck->waste_pile);
}
return;
}
int key_event() { int key_event() {
int pressed_key; int pressed_key;

View File

@ -1,8 +1,13 @@
#ifndef KEYBOARD_H #ifndef KEYBOARD_H
#define KEYBOARD_H #define KEYBOARD_H
#include "deck.h"
#define KEY_SPACEBAR ' ' #define KEY_SPACEBAR ' '
extern struct deck *deck;
void handle_stock_event();
int key_event(); int key_event();
#endif #endif

View File

@ -1,6 +1,7 @@
#include <malloc.h> #include <malloc.h>
#include <stdbool.h> #include <stdbool.h>
#include "stack.h" #include "stack.h"
#include "game.h"
void allocate_stack(struct stack **stack) { void allocate_stack(struct stack **stack) {
*stack = malloc(sizeof(**stack)); *stack = malloc(sizeof(**stack));
@ -79,7 +80,12 @@ struct stack *pop(struct stack **stack) {
} }
bool maneuvre_stack(struct stack *stack) { bool maneuvre_stack(struct stack *stack) {
return(stack->card->frame->start_y >= MANEUVRE_STACKS_START_Y); return(stack->card->frame->start_y >= MANEUVRE_STACKS_STARTING_Y);
}
bool waste_pile_stack(struct stack *stack) {
return((stack->card->frame->start_x == WASTE_PILE_STARTING_X) &&
(stack->card->frame->start_y == WASTE_PILE_STARTING_Y));
} }
void refresh_card_coordinates(struct stack *origin, struct stack *destination) { void refresh_card_coordinates(struct stack *origin, struct stack *destination) {

View File

@ -3,7 +3,7 @@
#include "card.h" #include "card.h"
#define MANEUVRE_STACKS_START_Y 7 #define MANEUVRE_STACKS_STARTING_Y 7
struct stack { struct stack {
struct card *card; struct card *card;
@ -18,6 +18,7 @@ int length(struct stack *);
void push(struct stack **, struct card *); void push(struct stack **, struct card *);
struct stack *pop(struct stack **); struct stack *pop(struct stack **);
bool maneuvre_stack(struct stack *); bool maneuvre_stack(struct stack *);
bool waste_pile_stack(struct stack *);
void refresh_card_coordinates(struct stack *, struct stack *); void refresh_card_coordinates(struct stack *, struct stack *);
void move_card(struct stack **, struct stack **); void move_card(struct stack **, struct stack **);

View File

@ -14,6 +14,7 @@ void initialize_curses() {
init_pair(1, COLOR_BLACK, COLOR_WHITE); init_pair(1, COLOR_BLACK, COLOR_WHITE);
init_pair(2, COLOR_RED, COLOR_WHITE); init_pair(2, COLOR_RED, COLOR_WHITE);
init_pair(3, COLOR_WHITE, COLOR_BLUE); init_pair(3, COLOR_WHITE, COLOR_BLUE);
init_pair(4, COLOR_WHITE, COLOR_GREEN);
return; return;
} }