1 // jumpsaround - jumper game
    2 // (c) 2020 Alexander Kulbartsch
    3 
    4 const SIZEX = 6
    5 const SIZEY = 5
    6 
    7 const jump = [[-1, -2], [1, -2], [-2, -1], [2, -1], [-2, 1], [2, 1], [-1, 2], [1, 2]];
    8 
    9 let board = new Array;
   10 let solutions = 0;
   11 let roundtrips = 0;
   12 
   13 
   14 function main() {
   15     console.log("Hello, Jumper!\n");
   16     
   17     // init
   18     for (i = 0; i < SIZEX; i++) {
   19         board[i] = [];  // same as "new Array"
   20         for (j = 0; j < SIZEY; j++) {
   21             board[i][j] = 0;
   22         }
   23     }
   24     jumpto(0, 0, 1, 0);
   25     console.log("I am done.");  
   26     return 0;
   27 }
   28 
   29 
   30 function jumpto(x, y, iter) {
   31 
   32     // ok - store position
   33     board[x][y] = iter;
   34     
   35     // finished ?
   36     if (iter >= SIZEX*SIZEY ) {
   37         // check for jumper roundtrips
   38         var round = false;
   39         for (var i = 0; i < 8; i++) {
   40             nx = x + jump[i][0]; 
   41             ny = y + jump[i][1]; 
   42             // check if position is on the board
   43             if ( nx<0 || nx>(SIZEX-1) || ny<0 || ny>(SIZEY-1) ) continue;
   44             // detect roundtrip
   45             if ( board[nx][ny] == 1 ) { round = true; roundtrips++; break; } // True
   46         }
   47         solutions++;
   48         if(round) printboard(round);
   49         board[x][y] = 0;        
   50         return;
   51     }
   52     
   53     // try next jump
   54     for (i = 0; i < 8; i++) {   
   55         // new position
   56         nx = x + jump[i][0]; 
   57         ny = y + jump[i][1]; 
   58         // check if new position is on the board
   59         if ( nx<0 || nx>(SIZEX-1) || ny<0 || ny>(SIZEY-1) ) continue;
   60         //  check if new field position is still empty
   61         if ( board[nx][ny] != 0 ) continue; 
   62         jumpto(nx, ny, iter + 1, i);
   63     }
   64     
   65     // this iteration is done
   66     board[x][y] = 0;
   67 }
   68 
   69 function printboard(round) {
   70     let rt = "";
   71     if (round) rt = " *** Roundtrip *** "; 
   72     console.log("===== Solution #", solutions, " - Roundtripps:", roundtrips, " ====== ", rt);
   73     for (i = 0; i < SIZEY; i++) {
   74         let line = ""
   75         for (j = 0; j < SIZEX; j++) {
   76             if(board[j][i] < 10) line = line + " ";
   77             line = line + board[j][i] + ":";
   78         }
   79         console.log(line);
   80     }
   81     console.log("");
   82 }
   83 
   84 main();
   85 
   86 // EOF