-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepsearch.js
More file actions
104 lines (100 loc) · 2.1 KB
/
deepsearch.js
File metadata and controls
104 lines (100 loc) · 2.1 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
const tab = [2, 4, 5, 3, 1];
const cards = [
{
"number": 1,
"id": -1,
"location": 0,
"type": 0,
"ccm": 2,
"power": 3,
"toughness": 1,
"abilities": [],
"cardDraw": 0
},
{
"number": 2,
"id": -1,
"location": 0,
"type": 2,
"ccm": 4,
"power": -1,
"toughness": -1,
"abilities": [],
"cardDraw": 0
},
{
"number": 3,
"id": -1,
"location": 0,
"type": 1,
"ccm": 5,
"power": 4,
"toughness": 0,
"abilities": [
"W"
],
"cardDraw": 0
},
{
"number": 4,
"id": -1,
"location": 0,
"type": 1,
"ccm": 3,
"power": 4,
"toughness": 0,
"abilities": [
"W"
],
"cardDraw": 0
},
{
"number": 5,
"id": -1,
"location": 0,
"type": 1,
"ccm": 1,
"power": 4,
"toughness": 0,
"abilities": [
"W"
],
"cardDraw": 0
},
];
/**
* Returns the Sets of all combinations of cards
* @param rest
* @param active
* @param res
* @returns {Array}
*/
function search(rest, active = [], res = []){
if (rest.length === 0){
res.push(active);
return active;
} else {
search(rest.slice(1), [...active, rest[0]], res);
search(rest.slice(1), active, res);
}
return res;
}
/**
* Returns only the sets of cards that are playable with the available mana
* @param cards
* @param mana
* @returns {*[]}
*/
function getPlayableSets(cards, mana) {
return res
.filter(tab => tab.map(a => a.ccm).reduce((a, b) => a + b, 0) <= mana)
.sort((t1, t2) => t2.map(a => a.ccm).reduce((a, b) => a + b, 0) - t1.map(a => a.ccm).reduce((a, b) => a + b, 0));
}
const mana = 4;
const res = search(cards);
const playable = getPlayableSets(res, mana);
const readable = playable.map(tab => tab.map(e => {
return {id: e.number, ccm: e.ccm};
}));
console.log(playable);
console.log(readable);