2011-02-06 05:44:45 +00:00
|
|
|
#include <stdio.h>
|
2010-04-20 17:11:16 +00:00
|
|
|
#include <stdlib.h>
|
2010-04-03 07:11:27 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
2011-02-06 05:44:45 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <ncurses.h>
|
2011-02-20 21:45:15 +00:00
|
|
|
#include "game.h"
|
2010-04-03 07:11:27 +00:00
|
|
|
#include "display.h"
|
2011-05-08 23:20:22 +00:00
|
|
|
#include "common.h"
|
2010-04-05 01:48:55 +00:00
|
|
|
|
2011-02-06 06:45:53 +00:00
|
|
|
static char *card_suit(enum suit suit) {
|
2010-04-03 07:11:27 +00:00
|
|
|
char *card_suit;
|
|
|
|
|
2011-05-28 03:44:16 +00:00
|
|
|
if (!(card_suit = malloc(4 * sizeof(*card_suit)))) {
|
2011-05-08 23:20:22 +00:00
|
|
|
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
2011-02-06 05:44:45 +00:00
|
|
|
exit(errno);
|
|
|
|
}
|
2010-04-03 07:11:27 +00:00
|
|
|
|
|
|
|
switch(suit) {
|
2011-02-06 06:14:17 +00:00
|
|
|
case DIAMONDS: strcpy(card_suit, DIAMONDS_SYMBOL); break;
|
|
|
|
case SPADES: strcpy(card_suit, SPADES_SYMBOL); break;
|
|
|
|
case HEARTS: strcpy(card_suit, HEARTS_SYMBOL); break;
|
|
|
|
case CLUBS: strcpy(card_suit, CLUBS_SYMBOL); break;
|
|
|
|
default: strcpy(card_suit, "?");
|
2010-04-03 07:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(card_suit);
|
|
|
|
}
|
2010-04-03 18:49:57 +00:00
|
|
|
|
2011-02-06 06:45:53 +00:00
|
|
|
static char *card_value(enum value value) {
|
2010-04-03 18:49:57 +00:00
|
|
|
char *card_value;
|
|
|
|
|
2011-02-06 05:44:45 +00:00
|
|
|
if (!(card_value = malloc(2 * sizeof(*card_value)))) {
|
2011-05-08 23:20:22 +00:00
|
|
|
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
2011-02-06 05:44:45 +00:00
|
|
|
exit(errno);
|
|
|
|
}
|
2010-04-03 18:49:57 +00:00
|
|
|
|
|
|
|
switch(value) {
|
2011-02-06 06:14:17 +00:00
|
|
|
case TWO: card_value = "2"; break;
|
|
|
|
case THREE: card_value = "3"; break;
|
|
|
|
case FOUR: card_value = "4"; break;
|
|
|
|
case FIVE: card_value = "5"; break;
|
|
|
|
case SIX: card_value = "6"; break;
|
|
|
|
case SEVEN: card_value = "7"; break;
|
|
|
|
case EIGHT: card_value = "8"; break;
|
|
|
|
case NINE: card_value = "9"; break;
|
|
|
|
case TEN: card_value = "10"; break;
|
|
|
|
case JACK: card_value = "J"; break;
|
|
|
|
case QUEEN: card_value = "Q"; break;
|
|
|
|
case KING: card_value = "K"; break;
|
|
|
|
case ACE: card_value = "A"; break;
|
|
|
|
default: card_value = "?";
|
2010-04-03 18:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(card_value);
|
|
|
|
}
|
2010-04-03 19:34:45 +00:00
|
|
|
|
2011-05-15 23:39:22 +00:00
|
|
|
void erase_card(struct card *card) {
|
|
|
|
werase(card->frame->window);
|
|
|
|
wbkgd(card->frame->window, WHITE_ON_GREEN);
|
|
|
|
wrefresh(card->frame->window);
|
|
|
|
}
|
|
|
|
|
2010-04-21 07:14:39 +00:00
|
|
|
void erase_stack(struct stack *stack) {
|
2011-05-15 23:39:22 +00:00
|
|
|
if (maneuvre_stack(stack)) {
|
|
|
|
for (; stack; stack = stack->next) {
|
|
|
|
erase_card(stack->card);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
erase_card(stack->card);
|
2011-05-15 18:02:34 +00:00
|
|
|
}
|
2010-04-21 07:14:39 +00:00
|
|
|
}
|
|
|
|
|
2010-04-03 19:34:45 +00:00
|
|
|
void draw_value(struct card *card) {
|
2011-05-09 03:50:56 +00:00
|
|
|
mvwprintw(card->frame->window, 0, 0, card_value(card->value));
|
|
|
|
mvwprintw(card->frame->window,
|
2011-02-14 02:24:19 +00:00
|
|
|
4,
|
|
|
|
6 - (strlen(card_value(card->value)) - 1),
|
|
|
|
card_value(card->value));
|
2010-04-03 19:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void draw_suit(struct card *card) {
|
|
|
|
if (card->suit % 2 == 0) {
|
2011-05-09 03:50:56 +00:00
|
|
|
wattron(card->frame->window, COLOR_PAIR(RED_ON_WHITE));
|
2010-04-03 19:34:45 +00:00
|
|
|
} else {
|
2011-05-09 03:50:56 +00:00
|
|
|
wattron(card->frame->window, COLOR_PAIR(BLACK_ON_WHITE));
|
2010-04-11 23:11:40 +00:00
|
|
|
}
|
2011-05-09 03:50:56 +00:00
|
|
|
mvwprintw(card->frame->window, 0, 1 + (strlen(card_value(card->value) + 1)),
|
2010-04-11 23:11:40 +00:00
|
|
|
card_suit(card->suit));
|
2011-05-09 03:50:56 +00:00
|
|
|
mvwprintw(card->frame->window, 4, 5 - (strlen(card_value(card->value) + 1)),
|
2010-04-11 23:11:40 +00:00
|
|
|
card_suit(card->suit));
|
|
|
|
if (card->suit % 2 == 0) {
|
2011-05-09 03:50:56 +00:00
|
|
|
wattroff(card->frame->window, COLOR_PAIR(RED_ON_WHITE));
|
2010-04-11 23:11:40 +00:00
|
|
|
} else {
|
2011-05-09 03:50:56 +00:00
|
|
|
wattroff(card->frame->window, COLOR_PAIR(BLACK_ON_WHITE));
|
2010-04-03 19:34:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void draw_front(struct card *card) {
|
2011-05-09 03:50:56 +00:00
|
|
|
wbkgd(card->frame->window, COLOR_PAIR(BLACK_ON_WHITE));
|
2010-04-03 19:34:45 +00:00
|
|
|
draw_value(card);
|
|
|
|
draw_suit(card);
|
|
|
|
}
|
|
|
|
|
|
|
|
void draw_back(struct card *card) {
|
2011-05-09 03:50:56 +00:00
|
|
|
wbkgd(card->frame->window, COLOR_PAIR(WHITE_ON_BLUE));
|
2010-04-03 19:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void draw_card(struct card *card) {
|
|
|
|
if (card->face == EXPOSED) {
|
|
|
|
draw_front(card);
|
|
|
|
} else {
|
|
|
|
draw_back(card);
|
|
|
|
}
|
2011-05-09 03:50:56 +00:00
|
|
|
wrefresh(card->frame->window);
|
2010-04-11 20:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void draw_stack(struct stack *stack) {
|
2011-05-08 02:09:39 +00:00
|
|
|
erase_stack(stack);
|
|
|
|
if (empty(stack)) {
|
2011-05-15 23:39:22 +00:00
|
|
|
box(stack->card->frame->window, 0, 0);
|
|
|
|
wrefresh(stack->card->frame->window);
|
2011-05-08 02:09:39 +00:00
|
|
|
} else {
|
2011-05-01 05:05:35 +00:00
|
|
|
if (maneuvre_stack(stack)) {
|
|
|
|
struct stack *reversed_stack = reverse(stack);
|
|
|
|
for (struct stack *i = reversed_stack; i; i = i->next) {
|
|
|
|
draw_card(i->card);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
draw_card(stack->card);
|
2010-04-11 20:11:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-06 02:22:16 +00:00
|
|
|
void draw_deck(struct deck *deck) {
|
2010-04-11 20:11:45 +00:00
|
|
|
draw_stack(deck->stock);
|
|
|
|
draw_stack(deck->waste_pile);
|
2011-05-29 20:12:52 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
draw_stack(deck->foundation[i]);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 7; i++) {
|
|
|
|
draw_stack(deck->maneuvre[i]);
|
|
|
|
}
|
2010-04-03 19:34:45 +00:00
|
|
|
}
|
2011-02-17 01:50:29 +00:00
|
|
|
|
|
|
|
void draw_cursor(struct cursor *cursor) {
|
2011-05-09 05:04:38 +00:00
|
|
|
mvwaddch(cursor->window, cursor->y, cursor->x, '*');
|
|
|
|
wrefresh(cursor->window);
|
2011-02-17 01:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void erase_cursor(struct cursor *cursor) {
|
2011-05-09 05:04:38 +00:00
|
|
|
mvwdelch(cursor->window, cursor->y, cursor->x);
|
2011-02-17 01:50:29 +00:00
|
|
|
}
|