-
Notifications
You must be signed in to change notification settings - Fork 0
/
1000-sort_deck.c
110 lines (101 loc) · 1.78 KB
/
1000-sort_deck.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
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
#include "deck.h"
/**
* sort_deck - Sorts a deck of cards
*
* @deck: The card deck
*/
void sort_deck(deck_node_t **deck)
{
deck_node_t *current;
size_t len;
deck_node_t *one, *two, *three, *four;
len = list_len_deck(*deck);
if (!deck || !*deck || len < 2)
return;
current = *deck;
while (current)
{
if (current->prev && card_value(current) < card_value(
current->prev))
{
one = current->prev->prev;
two = current->prev;
three = current;
four = current->next;
two->next = four;
if (four)
four->prev = two;
three->next = two;
three->prev = one;
if (one)
one->next = three;
else
*deck = three;
two->prev = three;
current = *deck;
continue;
}
else
current = current->next;
}
}
/**
* card_value - Returns card value
*
* @node: The card node in the deck
* Return: value
*/
int card_value(deck_node_t *node)
{
char *valu[13] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen", "King"};
char *kinds[4] = {"SPADE", "HEART", "CLUB", "DIAMOND"};
int i, kinds_of_valu = 0;
for (i = 0; i <= 13; i++)
{
if (!_strcmp(node->card->value, valu[i - 1]))
kinds_of_valu = i;
}
for (i = 1; i <= 4; i++)
{
if (!_strcmp(kinds[node->card->kind], kinds[i - 1]))
kinds_of_valu = kinds_of_valu + (13 * i);
}
return (kinds_of_valu);
}
/**
* _strcmp - Compare strings
*
* @s1: First string
* @s2: Second string
* Return: value
*/
int _strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2)
{
if (*s1 == '\0')
{
return (0);
}
s1++;
s2++;
}
return (*s1 - *s2);
}
/**
* list_len_deck - Function for list length
*
* @list: List
* Return: length value
*/
size_t list_len_deck(deck_node_t *list)
{
size_t len = 0;
while (list)
{
len++;
list = list->next;
}
return (len);
}