Implemented game#valid_move.
This commit is contained in:
@@ -66,8 +66,6 @@ void erase_stack(struct stack *stack) {
|
||||
box(empty_stack, 0, 0);
|
||||
wrefresh(empty_stack);
|
||||
delwin(empty_stack);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void draw_empty_stacks() {
|
||||
@@ -198,7 +196,16 @@ void draw_card(struct card *card) {
|
||||
}
|
||||
|
||||
void draw_stack(struct stack *stack) {
|
||||
if (!empty(stack)) {
|
||||
erase_stack(stack);
|
||||
if (empty(stack)) {
|
||||
WINDOW *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);
|
||||
} else {
|
||||
if (maneuvre_stack(stack)) {
|
||||
struct stack *reversed_stack = reverse(stack);
|
||||
|
||||
|
||||
71
lib/game.c
71
lib/game.c
@@ -8,23 +8,82 @@
|
||||
#include "util.h"
|
||||
#include "game.h"
|
||||
|
||||
bool stock_stack(struct stack *stack) {
|
||||
return(stack && stack->card && stack->card->frame &&
|
||||
(stack->card->frame->start_y == STOCK_STARTING_Y) &&
|
||||
(stack->card->frame->start_x == STOCK_STARTING_X));
|
||||
}
|
||||
|
||||
bool waste_pile_stack(struct stack *stack) {
|
||||
return(stack && stack->card && stack->card->frame &&
|
||||
(stack->card->frame->start_y == WASTE_PILE_STARTING_Y) &&
|
||||
(stack->card->frame->start_x == WASTE_PILE_STARTING_X));
|
||||
}
|
||||
|
||||
bool foundation_stack(struct stack *stack) {
|
||||
return(stack && stack->card && stack->card->frame &&
|
||||
stack->card->frame->start_y == FOUNDATION_STARTING_Y &&
|
||||
(stack->card->frame->start_x == FOUNDATION_0_STARTING_X ||
|
||||
stack->card->frame->start_x == FOUNDATION_1_STARTING_X ||
|
||||
stack->card->frame->start_x == FOUNDATION_2_STARTING_X ||
|
||||
stack->card->frame->start_x == FOUNDATION_3_STARTING_X));
|
||||
}
|
||||
|
||||
bool maneuvre_stack(struct stack *stack) {
|
||||
return(stack->card->frame->start_y >= MANEUVRE_STACKS_STARTING_Y);
|
||||
return(stack && stack->card && stack->card->frame &&
|
||||
stack->card->frame->start_y >= MANEUVRE_STACKS_STARTING_Y &&
|
||||
(stack->card->frame->start_x == MANEUVRE_0_STARTING_X ||
|
||||
stack->card->frame->start_x == MANEUVRE_1_STARTING_X ||
|
||||
stack->card->frame->start_x == MANEUVRE_2_STARTING_X ||
|
||||
stack->card->frame->start_x == MANEUVRE_3_STARTING_X ||
|
||||
stack->card->frame->start_x == MANEUVRE_4_STARTING_X ||
|
||||
stack->card->frame->start_x == MANEUVRE_5_STARTING_X ||
|
||||
stack->card->frame->start_x == MANEUVRE_6_STARTING_X));
|
||||
}
|
||||
|
||||
bool valid_move(struct stack *origin, struct stack *destination) {
|
||||
if (stock_stack(origin)) {
|
||||
if (waste_pile_stack(destination)) {
|
||||
return(true);
|
||||
} else {
|
||||
return(false);
|
||||
}
|
||||
} else if (waste_pile_stack(origin)) {
|
||||
if (foundation_stack(destination) || maneuvre_stack(destination)) {
|
||||
return(true);
|
||||
} else {
|
||||
return(false);
|
||||
}
|
||||
} else if (foundation_stack(origin)) {
|
||||
if ((foundation_stack(destination) && origin != destination) || maneuvre_stack(destination)) {
|
||||
return(true);
|
||||
} else {
|
||||
return(false);
|
||||
}
|
||||
} else if (maneuvre_stack(origin)) {
|
||||
if ((maneuvre_stack(destination) && origin != destination) || foundation_stack(destination)) {
|
||||
return(true);
|
||||
} else {
|
||||
return(false);
|
||||
}
|
||||
} else {
|
||||
return(false);
|
||||
}
|
||||
}
|
||||
|
||||
void move_card(struct stack **origin, struct stack **destination) {
|
||||
struct stack *stack = NULL;
|
||||
struct stack *stack;
|
||||
|
||||
(*origin)->card->frame->start_x = (*destination)->card->frame->start_x;
|
||||
(*origin)->card->frame->start_y = (*destination)->card->frame->start_y;
|
||||
if (!empty(*origin)) {
|
||||
(*origin)->card->frame->start_x = (*destination)->card->frame->start_x;
|
||||
(*origin)->card->frame->start_y = (*destination)->card->frame->start_y;
|
||||
}
|
||||
if (!empty(*destination) && maneuvre_stack(*destination)) {
|
||||
(*origin)->card->frame->start_y++;
|
||||
}
|
||||
if ((stack = pop(origin))) {
|
||||
push(destination, stack->card);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static void set_stacks_initial_coordinates(struct deck *deck) {
|
||||
|
||||
@@ -34,6 +34,8 @@ extern const char *program_name;
|
||||
struct deck *deck;
|
||||
struct cursor *cursor;
|
||||
|
||||
bool valid_move(struct stack *, struct stack *);
|
||||
bool maneuvre_stack(struct stack *);
|
||||
void move_card(struct stack **, struct stack **);
|
||||
void greet_player();
|
||||
void initialize_game();
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <stdlib.h>
|
||||
#include "card.h"
|
||||
#include "game.h"
|
||||
#include "display.h"
|
||||
@@ -90,19 +91,19 @@ static void handle_card_movement(struct cursor *cursor) {
|
||||
move_cursor(cursor, RIGHT);
|
||||
break;
|
||||
case KEY_SPACEBAR:
|
||||
if (!cursor_on_invalid_spot(cursor) &&
|
||||
!cursor_on_stock(cursor) &&
|
||||
!cursor_on_stack(cursor, origin)) {
|
||||
destination = cursor_stack(cursor);
|
||||
destination = cursor_stack(cursor);
|
||||
if (valid_move(origin, destination)) {
|
||||
move_card(&origin, &destination);
|
||||
draw_stack(origin);
|
||||
draw_stack(destination);
|
||||
}
|
||||
return;
|
||||
case 'q':
|
||||
case 'Q':
|
||||
end_curses();
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void handle_keyboard_event(int key) {
|
||||
@@ -131,6 +132,4 @@ void handle_keyboard_event(int key) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user