-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
55 lines (49 loc) · 2.16 KB
/
index.php
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
<?php
$unsorted_deck = array('4C','1C','4H','QD', '10C','JC','4H','4S','QH', '1S', '10D','10H','10S');
$sorted_deck = sort_card_deck($unsorted_deck);
var_dump($sorted_deck);
function sort_card_deck( $deck_to_sort ) {
$cards = array('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', '1');
$suits = array('S'=>0, 'C'=>2, 'H'=>3, 'D'=>4);
$sorted_deck = array();
$deck_to_sort_length = count($deck_to_sort);
foreach ( $cards as $card ) {
for ( $i=0; $i < $deck_to_sort_length; $i++ ) {
if ( $card == $deck_to_sort[$i][0] && $deck_to_sort[$i][1] != '0' && $card != '10' ) {
$sorted_deck[$card][] = $deck_to_sort[$i];
} else if( $card == '10' && '0' == $deck_to_sort[$i][1] ) {
$sorted_deck[$card][] = $deck_to_sort[$i];
}
}
}
// show sperated cards;
// var_dump($sorted_deck);
foreach ($sorted_deck as $key => $card_with_suits ) {
// If count is less than 1 it is already sorted;
if ( count( $card_with_suits ) <= 1 ) {
continue ;
}
// get the current cards count
$cards_count = count( $card_with_suits );
// Selection Sort
for ($i=0; $i < $cards_count; $i++) {
$min[$i] = $card_with_suits[$i];
$changed = false;
for ($j=$i+1; $j < $cards_count; $j++) {
if ( $key != '10' && $suits[$min[$i][1]] > $suits[$card_with_suits[$j][1]] ) {
$min['min'] = $j;
$changed = true;
} else if ($key == '10' && $suits[$min[$i][2]] > $suits[$card_with_suits[$j][2]]){
$min['min'] = $j;
$changed = true;
}
}
if ( $changed == true ) {
$card_with_suits[$i] = $card_with_suits[$min['min']];
$card_with_suits[$min['min']] = $min[$i];
}
}
$sorted_deck[$key] = $card_with_suits;
}
return $sorted_deck;
}