diff --git a/src/js/lib/mathlib.js b/src/js/lib/mathlib.js index 48e9bedc..4981c499 100644 --- a/src/js/lib/mathlib.js +++ b/src/js/lib/mathlib.js @@ -863,11 +863,12 @@ var mathlib = (function() { var aface = adjFaces[face][i]; var ridx = adjFaces[aface].indexOf(face); if (wide == 0 || wide == 1) { - swaps[i].push(base + i); - swaps[i].push(base + i + 5); - swaps[i].push(aface * 11 + ridx % 5 + 5); - swaps[i].push(aface * 11 + ridx % 5); - swaps[i].push(aface * 11 + (ridx + 1) % 5); + swaps[i].push( + base + i, + base + i + 5, + aface * 11 + ridx % 5 + 5, + aface * 11 + ridx % 5, + aface * 11 + (ridx + 1) % 5); } if (wide == 1 || wide == 2) { swaps[i].push(aface * 11 + 10); @@ -881,12 +882,14 @@ var mathlib = (function() { var opp = oppFace[face]; var oaface = adjFaces[opp][ii]; var oridx = adjFaces[oaface].indexOf(opp); - swaps[i].push(opp * 11 + ii); - swaps[i].push(opp * 11 + ii + 5); - swaps[i].push(oaface * 11 + 10); + swaps[i].push( + opp * 11 + ii, + opp * 11 + ii + 5, + oaface * 11 + 10); for (var j = 0; j < 5; j++) { - swaps[i].push(oaface * 11 + (oridx + j) % 5 + 5); - swaps[i].push(oaface * 11 + (oridx + j) % 5); + swaps[i].push( + oaface * 11 + (oridx + j) % 5 + 5, + oaface * 11 + (oridx + j) % 5); } } } @@ -1024,11 +1027,7 @@ var mathlib = (function() { }; _.toStr = function(sol, move_map, power_map) { - var ret = []; - for (var i = 0; i < sol.length; i++) { - ret.push(move_map[sol[i][0]] + power_map[sol[i][1]]); - } - return ret.join(' ').replace(/ +/g, ' '); + return sol.map((move) => move_map[move[0]] + power_map[move[1]]).join(' ').replace(/ +/g, ' '); }; // ida search algorithm @@ -1123,89 +1122,6 @@ var mathlib = (function() { _ = gSolver.prototype; - /* - _.calcNumOfStates = function() { - var len = this.solvedStates[0].length; - var genMove = []; - for (var moveIdx = 0; moveIdx < this.movesList.length; moveIdx++) { - var state = []; - for (var i = 0; i < len; i++) { - state.push(i + 32); - } - var newState = this.doMove(String.fromCharCode.apply(null, state), this.movesList[moveIdx][0]); - if (!newState) { - continue; - } - for (var i = 0; i < len; i++) { - state[i] = newState.charCodeAt(i) - 32; - } - genMove.push(state); - } - console.log(genMove); - var sgsObj = new SchreierSims(genMove); - console.log(sgsObj.size()); - return sgsObj; - - var genColor = []; - var state = this.solvedStates[0]; - var e = []; - for (var i = 0; i < len; i++) { - e[i] = i; - } - var checked = []; - for (var i = 0; i < len; i++) { - if (checked[i]) { - continue; - } - for (var j = i + 1; j < len; j++) { - if (state[i] == state[j] && (i % 9 % 2) == (j % 9 % 2)) { - var perm = e.slice(); - perm[i] = j; - perm[j] = i; - checked[j] = 1; - genColor.push(perm); - } - } - } - - var sgsObj = new SchreierSims(genMove); - sgsObj.minkwitz(); - var perm = e.slice(); - var initMv = []; - for (var i = 0; i < 50; i++) { - var mv = rn(genMove.length); - perm = sgsObj.permMult(genMove[mv], perm); - initMv.push(sgsObj.invMap[mv]); - } - var sol = sgsObj.getGen(perm); - var move2str = function(v) { return "URFDLB"[~~(v/3)] + " 2'"[v%3]; }; - sol = $.map(Array.prototype.concat.apply([], sol).reverse(), move2str).join(' '); - console.log($.map(initMv.reverse(), move2str).join(' '), '\n', sol); - - var sgs0, sgs1, sgs01; - for (var r = 0; r < 100; r++) { - var shuffle = []; - for (var i = 0; i < len; i++) { - shuffle[i] = i; - } - for (var i = 0; i < len; i++) { - var j = ~~(Math.random() * (len - i)) + i; - var tmp = shuffle[i]; - shuffle[i] = shuffle[j]; - shuffle[j] = tmp; - } - sgs0 = new SchreierSims(genColor, shuffle); - sgs1 = new SchreierSims(genMove, shuffle); - sgs01 = sgs0.intersect(sgs1); - if (sgs01.cnt != -1) { - console.log(r); - break; - } - } - console.log(sgs01.cnt, sgs0.size(), sgs1.size(), sgs01.size(), sgs1.size() / sgs01.size()); - }; - */ - _.updatePrun = function(targetDepth) { targetDepth = targetDepth === undefined ? this.prunDepth + 1 : targetDepth; for (var depth = this.prunDepth + 1; depth <= targetDepth; depth++) { @@ -1304,7 +1220,7 @@ var mathlib = (function() { if (this.solvedStates.indexOf(state) == -1) { return false; } - var solArr = this.getSolArr(); + var solArr = this.sol.map((move) => this.movesList[move][0]); this.subOpt = true; if (solArr.join(',') == this.prevSolStr) { return false; @@ -1347,14 +1263,6 @@ var mathlib = (function() { return false; }; - _.getSolArr = function() { - var solArr = []; - for (var i = 0; i < this.sol.length; i++) { - solArr.push(this.movesList[this.sol[i]][0]); - } - return solArr; - } - var randGen = (function() { var rndFunc; var rndCnt; @@ -1504,11 +1412,7 @@ var mathlib = (function() { } function idxArray(arr, idx) { - var ret = []; - for (var i = 0; i < arr.length; i++) { - ret.push(arr[i][idx]); - } - return ret; + return arr.map((elem) => elem[idx]); } function permOriMult(p1, p2, prod, o1, o2, ori, oriMod) { diff --git a/src/js/lib/utillib.js b/src/js/lib/utillib.js index 6305cd01..02cd3ac2 100644 --- a/src/js/lib/utillib.js +++ b/src/js/lib/utillib.js @@ -225,6 +225,10 @@ ISCSTIMER && execMain(function() { }); }; + $.fn.reclk = function(handler) { + return this.unbind('click').click(handler); + }; + if ('serviceWorker' in navigator) { $(function() { navigator.serviceWorker.register('sw.js'); @@ -384,6 +388,10 @@ var DEBUGBL = false; // for debugging bluetooth return ret; }; + $.format = function(format, args) { + return format.replace(/{(\d+)}/g, (m, num) => args[~~num] || ''); + }; + $.UDPOLY_RE = "skb|m?pyr|prc|heli(?:2x2|cv)?|crz3a|giga|mgm|klm|redi|fto|ctico"; $.TWISTY_RE = "sq1|clk|udpoly|" + $.UDPOLY_RE; })(); diff --git a/src/js/scramble/pyraminx.js b/src/js/scramble/pyraminx.js index da559d81..17dd5aef 100644 --- a/src/js/scramble/pyraminx.js +++ b/src/js/scramble/pyraminx.js @@ -335,23 +335,23 @@ var mpyr = (function() { } MpyrCubie.MpyrMult = function() { - var prod = arguments[arguments.length - 1] || new MpyrCubie(); - for (var k = 0; k < arguments.length; k++) { - var a = arguments[arguments.length - 1 - k]; + var args = Array.from(arguments); + var prod = args.pop() || new MpyrCubie(); + return args.reduceRight((b, a) => { for (var i = 0; i < 4; i++) { - prod.ct[i] = k == 0 ? i : (a.ct[prod.ct[i]]); - prod.co[i] = k == 0 ? 0 : ((a.co[prod.cp[i]] + prod.co[i]) % 3); - prod.cp[i] = k == 0 ? i : (a.cp[prod.cp[i]]); + prod.ct[i] = a.ct[b.ct[i]]; + prod.co[i] = (a.co[b.cp[i]] + b.co[i]) % 3; + prod.cp[i] = a.cp[b.cp[i]]; } for (var i = 0; i < 6; i++) { - prod.eo[i] = k == 0 ? 0 : (a.eo[prod.ep[i]] ^ prod.eo[i]); - prod.ep[i] = k == 0 ? i : (a.ep[prod.ep[i]]); + prod.eo[i] = a.eo[b.ep[i]] ^ b.eo[i]; + prod.ep[i] = a.ep[b.ep[i]]; } for (var i = 0; i < 12; i++) { - prod.wp[i] = k == 0 ? i : (a.wp[prod.wp[i]]); + prod.wp[i] = a.wp[b.wp[i]]; } - } - return prod; + return prod; + }); } function initMoveCube() { @@ -546,18 +546,11 @@ var mpyr = (function() { var move2str = ["U", "U'", "Uw", "Uw'", "B", "B'", "Bw", "Bw'", "R", "R'", "Rw", "Rw'", "L", "L'", "Lw", "Lw'"] function prettyMoves(moves) { - var buf = []; - for (var i = 0; i < moves.length; i++) { - buf[i] = move2str[moves[i]]; - } - return buf.join(' '); + return moves.map((move) => move2str[move]).join(' '); } function applyMoves(mc, moves) { - for (var i = 0; i < moves.length; i++) { - mc = MpyrCubie.MpyrMult(mc, MpyrCubie.moveCube[moves[i]], null); - } - return mc; + return moves.reduce((mc, move) => MpyrCubie.MpyrMult(mc, MpyrCubie.moveCube[move], null), mc); } function solveMpyr(mc) { diff --git a/src/js/solver/ftocta.js b/src/js/solver/ftocta.js index 8dabb014..94dfa18f 100644 --- a/src/js/solver/ftocta.js +++ b/src/js/solver/ftocta.js @@ -139,20 +139,20 @@ var ftosolver = (function() { } FtoCubie.FtoMult = function() { - var prod = arguments[arguments.length - 1] || new FtoCubie(); - for (var k = 0; k < arguments.length; k++) { - var a = arguments[arguments.length - 1 - k]; + var args = Array.from(arguments); + var prod = args.pop() || new FtoCubie(); + return args.reduceRight((b, a) => { for (var i = 0; i < 6; i++) { - prod.co[i] = k == 0 ? 0 : (a.co[prod.cp[i]] ^ prod.co[i]); - prod.cp[i] = k == 0 ? i : a.cp[prod.cp[i]]; + prod.co[i] = a.co[b.cp[i]] ^ b.co[i]; + prod.cp[i] = a.cp[b.cp[i]]; } for (var i = 0; i < 12; i++) { - prod.ep[i] = k == 0 ? i : a.ep[prod.ep[i]]; - prod.uf[i] = k == 0 ? i : a.uf[prod.uf[i]]; - prod.rl[i] = k == 0 ? i : a.rl[prod.rl[i]]; + prod.ep[i] = a.ep[b.ep[i]]; + prod.uf[i] = a.uf[b.uf[i]]; + prod.rl[i] = a.rl[b.rl[i]]; } - } - return prod; + return prod; + }); } function initMoveCube() { diff --git a/src/js/stats/recons.js b/src/js/stats/recons.js index 4d2846ae..1ee636d8 100644 --- a/src/js/stats/recons.js +++ b/src/js/stats/recons.js @@ -2,12 +2,12 @@ var recons = execMain(function() { var isEnable; var titleStr = TOOLS_RECONS_TITLE.split('|'); - var div = $('
'); + var div = $('
'); var reconsClick = $('
').append(''); var table = $(''); var rangeSelect = $(''); - var requestBack = $(''); + var requestBack = $(''); var tableTh = $('').append($(''); @@ -183,14 +183,14 @@ var recons = execMain(function() { } var trTpl = - '' + - '' + - '' + - '' + - '' + - '' + + '' + + '' + + '' + + '' + + '' + + '' + ''; var str = []; @@ -207,7 +207,7 @@ var recons = execMain(function() { if (isSuperStep) { curSIdx = sDataIdx[i]; var sval = stepSData[curSIdx]; - var trsdata = [ + str.push($.format(trTpl, [ '', sval[0], sval[1] / maxSubt * 100, @@ -217,15 +217,9 @@ var recons = execMain(function() { Math.round(sval[3] * 10) / 10, sval[3] > 0 && sval[1] + sval[2] > 0 ? Math.round(sval[3] / (sval[1] + sval[2]) * 10000 ) / 10 : 'N/A', 'click sstep' - ]; - var curTr = trTpl; - for (var j = 0; j < 9; j++) { - curTr = curTr.replace(new RegExp('\\$' + j, 'g'), trsdata[j]); - } - str.push(curTr); + ])); } - - var trdata = [ + str.push($.format(trTpl, [ sDataIdx[i] == curSIdx ? 'display:none;' : '', val[0], val[1] / maxSubt * 100, @@ -235,12 +229,7 @@ var recons = execMain(function() { Math.round(val[3] * 10) / 10, val[3] > 0 && val[1] + val[2] > 0 ? Math.round(val[3] / (val[1] + val[2]) * 10000 ) / 10 : 'N/A', ['oll', 'pll'].indexOf(val[0]) != -1 ? 'click' : '' - ]; - var curTr = trTpl; - for (var j = 0; j < 9; j++) { - curTr = curTr.replace(new RegExp('\\$' + j, 'g'), trdata[j]); - } - str.push(curTr); + ])); } var endTr = $('').append(tidx ? $('' + @@ -538,7 +527,7 @@ var caseStat = execMain(function() { var isEnable; var titleStr = TOOLS_RECONS_TITLE.split('|'); - var div = $('
'); + var div = $('
'); var table = $('
').append(methodSelect), '' + titleStr[0] + '' + titleStr[1] + '' + titleStr[2] + '' + titleStr[3] + '
$1' + - ' ' + - ' 
$4$5$6$7
{1}' + + ' ' + + ' 
{4}{5}{6}{7}
').append(requestBack) : $('').append(rangeSelect), '' + (isPercent ? Math.round(totIns / sumSubt * 1000) / 10 + '%' : kernel.pretty(totIns)) + '
'); var methodSelect = $('').append( @@ -575,17 +564,17 @@ var caseStat = execMain(function() { } var trTpl = - '' + + '' + '' + - '' + + '' + '' + + ' ' + + ' ' + '' + - '' + - '' + - '' + - '' + + '' + + '' + + '' + + '' + ''; table.empty().append(tableTh.unbind('click').click(procHeaderClick)); @@ -626,11 +615,7 @@ var caseStat = execMain(function() { }); for (var row of trdata) { - var curTr = trTpl; - for (var j = 0; j < row.length; j++) { - curTr = curTr.replace(new RegExp('\\$' + j, 'g'), row[j]); - } - curTr = $(curTr); + var curTr = $($.format(trTpl, row)); var img = curTr.find('img'); img.css({ 'width': '2em', @@ -717,7 +702,7 @@ var caseStat = execMain(function() { }); var scatter = execMain(function() { - var canvas = $(''), ctx; + var canvas = $(''), ctx; var scatterDiv = $('
'); var methodSelect = $('
$1
{1}$2{2}' + - ' ' + - ' 
$5$6$7$8{5}{6}{7}{8}