-
-
Save DC3/b13b9f308ac4830c5cc4b972d1f9d81d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var a = "60o0749460o07134482"; | |
| //var b= parseInt(a, 10); | |
| G = function(a, b) { | |
| var c = function(a) { | |
| for (var b, c = [], d = 3, e = function(a) { | |
| return a >= "0" && "9" >= a ? parseInt(a, 10) : a.charCodeAt(0) - "a".charCodeAt(0) + 10 | |
| }, f = function(a, b) { | |
| var c, d, f, g, h; | |
| //c =e 函数 | |
| return c = e(a[b]), d = e(a[b + 1]), d = 15 & d, f = (12 & c) >> 2, g = (2 & c) > 0, h = (1 & c) > 0, { | |
| value: d, //d=e 函数 | |
| winner: f, // f = (12 & c) | |
| playerPair: g, //g=(2&c) | |
| bankerPair: h // h = (1 & c) > 0 | |
| } | |
| }, | |
| g = 0; g < a.length - 1; g += 2) | |
| b = f(a, g), b.winner !== d ? c.push(b) : c = []; | |
| return c | |
| }; | |
| if (!a || void 0 === b) return []; | |
| switch (a) { | |
| case "7bal": | |
| case "bal": | |
| return c(b); | |
| case "rol": | |
| case "rofl": | |
| return b.split(";").map(function(a) { | |
| return { | |
| result: a | |
| } | |
| }); | |
| default: | |
| return b | |
| } | |
| } | |
| console.log(G("bal", a))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 不想写代码了,我直接阐述原理吧。 | |
| 'bal'作为算法的决策条件,没什么可说的。选择的算法如下: | |
| ** 把最外层字符串变量 a 的值按 2 个字符拆分,然后解析到最后结果数组的每个对象。 | |
| 先说下 for 循环里 e 函数 | |
| 这个函数接受一个字符,把字符转换数字,内部会作如下判断 | |
| * 如果字符是 0-10 的数字字符串,转到原数字 | |
| * 如果不是 0-10 的数字字符串,取字符的 ASSCI 码减去 a 的 ASSCI 码,再加上 10 返回,公式 | |
| ``` | |
| // ASSCI(str) - 87 -- a 的 ASSCI 码是 97 | |
| ASSCI(str) - ASSCI(a) + 10 | |
| ``` | |
| 回到刚才说到的 2 个字符解析。 for 循环会把 2 个字符的依次传给上面提到的函数 e ,把值保存到两个变量 c,d | |
| ``` | |
| c = 第 1 个字符转换的数字 | |
| d = 第 2 个字符转换的数字 | |
| ``` | |
| 接着,用变量 d 与 15 做二进制与运算, 15 的二进制是 4 个 1(1111),也就是取 d 的最后 4 个二进制位。 | |
| 把这个 4 个二进制代表的值赋值给最后结果对象的 value 属性 | |
| 用变量 c 与 12 , 2 , 1 这 3 个数字作二进制与运算,这 3 个数字的二进制分别是'1100'、 '10'、'1'。 | |
| * 用 12 和 c 作与运算后又把数字向右偏移了 2 位,其实就是取数字的第 3 、 4 两个二进制位, | |
| 把这两个二进制位表示的值赋值给最后对象的 winner 属性 | |
| * 用 2 和 c 作与运算后,其实取出了数字的第 2 个二进制位,然后判断是否大于 0(其实就是第 2 个二进制位有值就表示 true),结果赋值给最后对象的 playerPair | |
| * 用 1 和 c 作运算,原理同上面的 2 一样,只是取得是数字的第 1 个二进制位,结果赋值给对面的 bankerPair 属性 | |
| 上面的对象属性解析对应如下: | |
| value: 取 2 个字符的第 2 个字符的数值的第 1 到第 4 的二进制位 | |
| winner: 取 2 个字符的第 1 个字符的数值的第 3 、 4 两个二进制位 | |
| playerPair: 取 2 个字符的第 1 个字符的数值的第 2 个二进制位,如果有值就是 true | |
| bankerPair :同 playerPair 取值很相似,区别在于取值位置是第 1 个二进制位 | |
| 在 for 循环里,对象创建完成后,用对象的 winner 属性和数字 3 作比较。 | |
| * 如果等于 3 ,就重置最后的结果数组 | |
| * 如果不等于 3 ,就把对象压栈到最后的结果数组的最后一位 | |
| 循环完后,返回对象数组。 | |
| 总算写完了,想的时候没猜到写这么多。希望了帮到露珠 :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment