-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcardstack.c
More file actions
135 lines (113 loc) · 2.66 KB
/
cardstack.c
File metadata and controls
135 lines (113 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "solitaire.h"
#include "cardstack.h"
#include "carddeck.h"
cardstack_t *new_cardstack(int type) {
cardstack_t *stack = malloc(sizeof(cardstack_t));
stack->top = NULL;
stack->type = type;
return stack;
}
cardstack_t *new_cardstack_fed_by(int type, cardstack_t *feed) {
cardstack_t *stack = new_cardstack(type);
stack->feed = feed;
return stack;
}
void put_helper(card_t *c) {
if (c != NULL) {
if (c->below != NULL) {
put_helper(c->below);
printf(" ");
}
putCard(c);
}
}
void put_cardstack(cardstack_t *stack, int reveal) {
card_t *c = stack->top;
if (reveal) {
put_helper(c);
} else {
while (c != NULL) {
printf("*");
c = c->below;
}
}
}
char *send_helper(card_t *c) {
char tempPile[1024];
tempPile[0] = '\0';
if (c != NULL) {
if (c->below != NULL) {
strcat(tempPile, send_helper(c->below));
strcat(tempPile, " ");
}
strcat(tempPile, sendCard(c));
}
char *pile = malloc(1024);
strcpy(pile, tempPile);
return pile;
}
char *send_cardstack(cardstack_t *stack, int reveal) {
card_t *c = stack->top;
char tempPile[2048];
tempPile[0] = '\0';
if (reveal) strcat(tempPile, send_helper(c));
else
{
while (c != NULL)
{
strcat(tempPile, "*");
c = c->below;
}
}
char *pile = malloc(2048);
strcpy(pile, tempPile);
return pile;
}
int is_empty(cardstack_t *stack) {
return stack->top == NULL;
}
void push(card_t *card, cardstack_t *stack) {
card->below = stack->top;
stack->top = card;
card->stack = stack;
}
card_t *pop(cardstack_t *stack) {
card_t *card = stack->top;
stack->top = card->below;
card->below = NULL;
card->stack = NULL;
return card;
}
card_t *top(cardstack_t *stack) {
return stack->top;
}
void feed(cardstack_t *stack) {
push(pop(stack->feed),stack);
}
void move_onto(card_t *card, cardstack_t *dest_stack) {
// Keep track of the source stack, the stack that holds
// the card to be moved.
//
cardstack_t *srce_stack = card->stack;
// Reassign the stack of the cards at and above the card
// being moved; reassign them to the destination stack.
//
card_t *top_card = top(srce_stack);
for (card_t *c = top_card; c != card; c = c->below) {
c->stack = dest_stack;
}
card->stack = dest_stack;
// Move them onto the destination stack.
//
srce_stack->top = card->below;
card->below = dest_stack->top;
dest_stack->top = top_card;
// Perhaps flip the top card of the source stack's
// feeder pile onto the source stack.
if (is_empty(srce_stack) && srce_stack->feed != NULL) {
feed(srce_stack);
}
}