-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcore-ai.js
130 lines (116 loc) · 2.78 KB
/
core-ai.js
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
//数组排序
function arraySort(array, asc) {
return array.sort(function (a, b) {
return asc === 'asc' ? a - b : b - a;
})
}
//数组去重
function arrayClearRepeat(array) {
var ret = [];
array.forEach(function (item) {
if (ret.indexOf(item) === -1) {
ret.push(item);
}
});
return ret;
}
//从一个数组中过滤掉 <= n 的成员
function removeItemLessOf(array, n) {
return array.filter(function (item) {
return item > n;
});
}
//筛选数组中累计出现过至少n次的成员
function getCardByCountOverOf(array, n) {
var ret = getGroupByCard(array);
var r = [];
for (var i in ret) {
if (ret[i] >= n) {
r.push(parseInt(i));
}
}
return r;
}
// 去除数组中指定的项
function removeItem(array, item) {
var ret = [];
array.forEach(function (i) {
if (i !== item) {
ret.push(i);
}
});
return ret;
}
function A(option, cards) {
cards = arrayClearRepeat(cards);
cards = removeItemLessOf(cards, option.key);
cards = arraySort(cards, 'asc');
return cards.map(function (card) {
return [card];
})
}
function AA(option, cards) {
cards = getCardByCountOverOf(cards, 2);
cards = arrayClearRepeat(cards);
cards = removeItemLessOf(cards, option.key);
cards = arraySort(cards, 'asc')
var ret = [];
cards.forEach(function (card) {
ret.push([card, card]);
})
return ret;
}
function AAA(option, cards) {
cards = getCardByCountOverOf(cards, 3);
cards = arrayClearRepeat(cards);
cards = removeItemLessOf(cards, option.key);
cards = arraySort(cards, 'asc');
var ret = [];
cards.forEach(function (card) {
ret.push([card, card, card]);
})
return ret;
}
function AAAA(option, cards) {
cards = getCardByCountOverOf(cards, 4);
cards = arrayClearRepeat(cards);
cards = removeItemLessOf(cards, option.key);
cards = arraySort(cards, 'asc');
var ret = [];
cards.forEach(function (card) {
ret.push([card, card, card, card]);
})
return ret;
}
function KING(option, cards) {
return cards.indexOf(16) > -1 && cards.indexOf(17) > - 1 ? [[16, 17]] : [];
}
// function AAAB(option, cards) {
// var cards_3 = getCardByCountOverOf(cards, 3);
// cards_3 = removeItemLessOf(cards_3, option.key)
// }
var mapping = {
A: [A, AAAA, KING],
AA: [AA, AAAA, KING],
AAA: [AAA, AAAA, KING],
AAAB: [AAAB, AAAA, KING],
AAAA: [AAAA, KING],
AAABB: [AAABB, AAAA, KING],
ABCDE: [ABCDE, AAAA, KING],
AABBCC: [AABBCC, AAAA, KING],
AAABBB: [AAABBB, AAAA, KING],
AAAABC: [AAAABC, AAAA, KING],
AAAABBCC: [AAAABBCC, AAAA, KING],
KING: []
}
function getSuggest(option, cards) {
var hooks = mapping[option.type];
var ret = [];
hooks.forEach(function (invoke) {
var retItem = invoke(option, cards);
if (retItem.length) {
ret = ret.concat(retItem);
}
})
return ret;
}