Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  • Thread Tools
  • Rate This Thread
  1. #1
    New to the CF scene
    Join Date
    May 2016
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question JavaScript only works on initial page load

    Hi,

    I've been adding features to the JavaScript on this page. When the page loads, you can move the pieces and play the game. When the New Game link is clicked the game board resets but the pieces can no longer be moved. My goal is to be able to reset the board and play again without reloading the page.

    I think it has something to do with this line.
    Code:
    function $(i){return document.getElementById(i);}
    Because if I comment out the line then the board doesn't work even on the first page load.

    Is there a way I can call or rewrite this function so it will run at the end of the resetBoard function?

    Or is there some other problem/solution that I'm missing?

    I'm not anywhere near being a JavaScript guru so the answer to this may be simple. Thanks for the help.

  2. #2
    Regular Coder
    Join Date
    Feb 2016
    Posts
    128
    Thanks
    0
    Thanked 32 Times in 32 Posts
    After passing a beautifier the line where function P() is defined looks like this:
    Code:
    		 function P(w, c, h, e, S, s) {
    		     var t, o, L, E, d, O = e,
    		         N = -H * H,
    		         K = 78 - h << X,
    		         p, g, n, m, A, q, r, C, J, a = y ? -X : X;
    		     y ^= 8;
    		     G++;
    		     d = w || s && s >= h && P(0, 0, 0, 21, 0, 0) > H;
    		     do {
    		         if (o = I[p = O]) {
    		             q = o & Q ^ y;
    		             if (q < 7) {
    		                 A = q-- & 2 ? 8 : 4;
    		                 C = o - 9 & Q ? [53, 47, 61, 51, 47, 47][q] : 57;
    		                 do {
    		                     r = I[p += l[C]];
    		                     if (!w | p == w) {
    		                         g = q | p + a - S ? 0 : S;
    		                         if (!r & (!!q | A < 3 || !!g) || (r + 1 & Q ^ y) > 9 && q | A > 2) {
    		                             if (m = !(r - 2 & 7)) return y ^= 8, I[G--] = O, K;
    		                             J = n = o & Q;
    		                             E = I[p - a] & Q;
    		                             t = q | E - 7 ? n : (n += 2, 6 ^ y);
    		                             while (n <= t) {
    		                                 L = r ? l[r & 7 | 32] - h - q : 0;
    		                                 if (s) L += (1 - q ? l[(p - p % X) / X + 37] - l[(O - O % X) / X + 37] + l[p % X + 38] * (q ? 1 : 2) - l[O % X + 38] + (o & 16) / 2 : !!m * 9) + (!q ? !(I[p - 1] ^ n) + !(I[p + 1] ^ n) + l[n & 7 | 32] - 99 + !!g * 99 + (A < 2) : 0) + !(E ^ y ^ 9);
    		                                 if (s > h || 1 < s & s == h && L > Q | d) {
    		                                     I[p] = n, I[O] = m ? (I[g] = I[m], I[m] = 0) : g ? I[g] = 0 : 0;
    		                                     L -= P(s > h | d ? 0 : p, L - N, h + 1, I[G + 1], J = q | A > 1 ? 0 : p, s);
    		                                     if (!(h || s - 1 | B - O | i - n | p - b | L < -H)) return F(), G--, u = J;
    		                                     J = q - 1 | A < 7 || m || !s | d | r | o < Q || P(0, 0, 0, 21, 0, 0) > H;
    		                                     I[O] = o;
    		                                     I[p] = r;
    		                                     m ? (I[m] = I[g], I[g] = 0) : g ? I[g] = 9 ^ y : 0
    		                                 }
    		                                 if (L > N || s > 1 && L == N && !h && M() < .5) {
    		                                     I[G] = O;
    		                                     if (s > 1) {
    		                                         if (h && c - L < 0) return y ^= 8, G--, L;
    		                                         if (!h) i = n, B = O, b = p
    		                                     }
    		                                     N = L
    		                                 }
    		                                 n += J || (g = p, m = p < O ? g - 3 : g + 2, I[m] < Q | I[m + O - p] || I[p += p - O]) ? 1 : 0
    		                             }
    		                         }
    		                     }
    		                 } while (!r & q > 2 || (p = O, q | A > 2 | o > Q & !r && ++C * --A))
    		             }
    		         }
    		     } while (++O > 98 ? O = 20 : e - O);
    		     return y ^= 8, G--, N + H * H && N > -K + 1924 | d ? N : 0
    		 }
    		 B = i = y = u = 0;
    		 while (B++ < 120) I[B - 1] = B % X ? B / X % X < 2 | B % X < 2 ? 7 : B / X & 4 ? 0 : l[i++] | 16 : 7;
    It's obvious that this code:
    Code:
    		 B = i = y = u = 0;
    		 while (B++ < 120) I[B - 1] = B % X ? B / X % X < 2 | B % X < 2 ? 7 : B / X & 4 ? 0 : l[i++] | 16 : 7;
    is not located inside that function. Instead it is executed when the page is loaded but not when function resetBoard() is called. You have to delete this code here and insert it into function resetBoard:
    Code:
    		 function resetBoard() {
    		    String.prototype.U=String.prototype.charCodeAt;M=Math.random;W=setTimeout;X=10;Q=15;H=1e4;
    			
    			// aliases and constants
                var strHTML = '<tr><td onclick="V(21)" id="21">♜</td><td onclick="V(22)" id="22">♞</td><td onclick="V(23)" id="23">♝</td><td onclick="V(24)" id="24">♛</td><td onclick="V(25)" id="25">♚</td><td onclick="V(26)" id="26">♝</td><td onclick="V(27)" id="27">♞</td><td onclick="V(28)" id="28">♜</td></tr>'
    		       + '<tr><td onclick="V(31)" id="31">♟</td><td onclick="V(32)" id="32">♟</td><td onclick="V(33)" id="33">♟</td><td onclick="V(34)" id="34">♟</td><td onclick="V(35)" id="35">♟</td><td onclick="V(36)" id="36">♟</td><td onclick="V(37)" id="37">♟</td><td onclick="V(38)" id="38">♟</td></tr>'
    		       + '<tr><td onclick="V(41)" id="41">&nbsp;</td><td onclick="V(42)" id="42">&nbsp;</td><td onclick="V(43)" id="43">&nbsp;</td><td onclick="V(44)" id="44">&nbsp;</td><td onclick="V(45)" id="45">&nbsp;</td><td onclick="V(46)" id="46">&nbsp;</td><td onclick="V(47)" id="47">&nbsp;</td><td onclick="V(48)" id="48">&nbsp;</td></tr>'
    		       + '<tr><td onclick="V(51)" id="51">&nbsp;</td><td onclick="V(52)" id="52">&nbsp;</td><td onclick="V(53)" id="53">&nbsp;</td><td onclick="V(54)" id="54">&nbsp;</td><td onclick="V(55)" id="55">&nbsp;</td><td onclick="V(56)" id="56">&nbsp;</td><td onclick="V(57)" id="57">&nbsp;</td><td onclick="V(58)" id="58">&nbsp;</td></tr>'
    		       + '<tr><td onclick="V(61)" id="61">&nbsp;</td><td onclick="V(62)" id="62">&nbsp;</td><td onclick="V(63)" id="63">&nbsp;</td><td onclick="V(64)" id="64">&nbsp;</td><td onclick="V(65)" id="65">&nbsp;</td><td onclick="V(66)" id="66">&nbsp;</td><td onclick="V(67)" id="67">&nbsp;</td><td onclick="V(68)" id="68">&nbsp;</td></tr>'
    		       + '<tr><td onclick="V(71)" id="71">&nbsp;</td><td onclick="V(72)" id="72">&nbsp;</td><td onclick="V(73)" id="73">&nbsp;</td><td onclick="V(74)" id="74">&nbsp;</td><td onclick="V(75)" id="75">&nbsp;</td><td onclick="V(76)" id="76">&nbsp;</td><td onclick="V(77)" id="77">&nbsp;</td><td onclick="V(78)" id="78">&nbsp;</td></tr>'
    		       + '<tr><td onclick="V(81)" id="81">♙</td><td onclick="V(82)" id="82">♙</td><td onclick="V(83)" id="83">♙</td><td onclick="V(84)" id="84">♙</td><td onclick="V(85)" id="85">♙</td><td onclick="V(86)" id="86">♙</td><td onclick="V(87)" id="87">♙</td><td onclick="V(88)" id="88">♙</td></tr>'
    		       + '<tr><td onclick="V(91)" id="91">♖</td><td onclick="V(92)" id="92">♘</td><td onclick="V(93)" id="93">♗</td><td onclick="V(94)" id="94">♕</td><td onclick="V(95)" id="95">♔</td><td onclick="V(96)" id="96">♗</td><td onclick="V(97)" id="97">♘</td><td onclick="V(98)" id="98">♖</td></tr>';
    			
    		    // 2K chess (1683 bytes)
                b=0;
    		    I=[];
    		    G=120;
    		    l=[5,3,4,6,2,4,3,5,1,1,1,1,1,1,1,1,9,9,9,9,9,9,9,9,13,11,12,14,10,12,11,13,0,99,0,306,297,495,846,-1,0,1,2,2,1,0,-1,-1,1,-10,10,-11,-9,9,11,10,20,-9,-11,-10,-20,-21,-19,-12,-8,8,12,19,21];
    		    B = i = y = u = 0;
    		    while (B++ < 120) I[B - 1] = B % X ? B / X % X < 2 | B % X < 2 ? 7 : B / X & 4 ? 0 : l[i++] | 16 : 7;			
    			
    			// Set the board to the starting position for a new game
    		    document.getElementById("tblBoard").innerHTML = strHTML;
    		 }

  3. Users who have thanked Sempervivum for this post:

    dpleo (05-14-2016)

  4. #3
    New to the CF scene
    Join Date
    May 2016
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Thanks!

    That did the trick. I'm not sure how to mark the question resolved.

    Enjoy!


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •