-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstack.c
74 lines (64 loc) · 1.37 KB
/
stack.c
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
#include <stdio.h>
#include "random.h"
#include "tile.h"
#define NUM_TILES_DOMINO 28
#define FALSE 0
#define TRUE 1
typedef struct
{
int n_tiles;
t_tile f[NUM_TILES_DOMINO];
} t_stack;
void print_stack(t_stack pi, int visible);
void initialize_stack(t_stack *pi);
int is_stack_empty(t_stack pi);
t_tile pick_from_stack(t_stack *pi);
void initialize_stack(t_stack *pi)
{
int i = 0, j, k;
pi->n_tiles = NUM_TILES_DOMINO; // At the start all the tiles belong to the stack.
for (j = 0; j < 7; j++)
for (k = j; k < 7; k++)
{
initialize_tile(&pi->f[i], j, k); // Initialize the 28 tiles.
i++;
}
}
void print_stack(t_stack pi, int visible)
{
int i;
if (visible == TRUE)
{ // Omniscience on.
printf("Pila:\t");
for (i = 0; i < pi.n_tiles; i++)
print_tile(pi.f[i], visible);
}
else
{ // Omnisciencia off.
printf("Pila:\t");
for (i = 0; i < pi.n_tiles; i++)
printf("?:?|");
}
if (pi.n_tiles == 0)
printf("La pila esta vacia");
}
int is_stack_empty(t_stack pi)
{
if (pi.n_tiles == 0)
return (TRUE);
else
return (FALSE);
}
t_tile pick_from_stack(t_stack *pi)
{
int i, pos;
t_stack aux;
pos = random_number(pi->n_tiles); // Picks a random tile.
aux.f[pos] = pi->f[pos];
for (i = pos; i < (pi->n_tiles - 1); i++)
{ // Deletes the picked tile from the stack.
pi->f[i] = pi->f[i + 1];
}
pi->n_tiles--;
return (aux.f[pos]); // Returns the picked tile.
}