-
Notifications
You must be signed in to change notification settings - Fork 2
/
pogemon.js
89 lines (78 loc) · 2.59 KB
/
pogemon.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
// the main wrapper function
export function pogemon(inputTable) {
let sacha = inputTable[1].split(" ")
let mine = inputTable[2].split(" ")
// brute forcing all possibilities
const minePermuted = permutator(mine, sacha)
for (const minePermutedElement of minePermuted) {
if (Iwin(sacha, minePermutedElement))
return minePermutedElement.join(" ")
}
return -1
}
//type1 & type2 are strings
export function rules(type1, type2) {
const data = {
//case plante
'plante': {"poison": 'plante', 'sol': 'sol', 'vol': 'vol', 'plante': 'n'},
//case eau
'eau': {"plante": 'plante', 'sol': 'sol', 'eau': 'n'},
//case feu
'feu': {"eau": 'eau', 'plante': 'feu', 'feu': 'n',}
}
const listType = ['feu', 'eau', 'plante', 'glace', 'poison', 'sol', 'vol']
let dataout;
// skip if at least one type is invalid (not in the listType)
if (!listType.includes(type1) || !listType.includes(type2)) {
dataout = 'n'
} else {
if (Object.keys(data).includes(type1) && typeof data[type1][type2] !== "undefined")
dataout = data[type1][type2]
else if (Object.keys(data).includes(type2) && typeof data[type2][type1] !== "undefined")
dataout = data[type2][type1]
else dataout = 'n'
}
//replace all undefined result to "n"
return (typeof dataout == "undefined") ? 'n' : dataout;
}
const permutator = (inputArr) => {
let result = [];
const permute = (arr, m = []) => {
if (arr.length === 0) {
result.push(m)
} else {
for (let i = 0; i < arr.length; i++) {
let curr = arr.slice();
let next = curr.splice(i, 1);
permute(curr.slice(), m.concat(next))
}
}
}
permute(inputArr)
return result;
}
//take parameter as table of data
export function Iwin([...sacha], [...mine]) {
while (true) {
if (sacha.length === 0 || mine.length === 0)
break
//if I win remove sacha's thirst element
let ruleResult = rules(sacha[0], mine[0]);
if (ruleResult === mine[0]) {
sacha.shift()
}
//both has the same type
else if (ruleResult === 'n') {
sacha.shift()
mine.shift()
}
//sacha win
else
mine.shift()
}
if (sacha.length < mine.length)
return true
if (sacha.length >= mine.length)
return false
}
//todo: fix timeout issue