1 // jumpsaround - jumper game
    2 // (c) 2011-2020 Alexander Kulbartsch
    3 
    4 import std.stdio;
    5 
    6 enum byte SIZEX = 6;
    7 enum byte SIZEY = 5;
    8 
    9 /* static array */
   10 byte[2][8] jump = [[-1,-2], [1,-2], [-2,-1], [2,-1], [-2,1], [2,1], [-1,2], [1,2]];
   11 
   12 byte[SIZEY][SIZEX] board;
   13 byte[64] iters; // SIZEX*SIZEY
   14 
   15 long solutions;
   16 long roundtrips;
   17 
   18 int main () {
   19     writeln("Hello, Jumper!\n");
   20 
   21     // init
   22 
   23     for (byte i = 0; i < SIZEX; i++) {
   24         for (byte j = 0; j < SIZEY; j++) {
   25             board[i][j] = 0;
   26         }
   27     }
   28     for (byte i = 0; i < (SIZEX*SIZEY); i++) iters[i] = 0;
   29     iters[0] = ' ';
   30 
   31     jumpto(0, 0, 1, 0);
   32 
   33     writeln("I am done.\n");
   34 
   35     return 0;
   36 }
   37 
   38 
   39 void jumpto(byte x, byte y, byte iter, byte arity) {
   40 
   41     // ok - store position
   42     board[x][y] = iter;
   43     iters[iter] = arity;
   44 
   45     // finished?
   46     if (iter >= SIZEX*SIZEY ) {
   47 
   48         // check for jumper roundtrips
   49         byte round = 0;
   50         for (int i = 0; i < 8; i++) {
   51             byte nx = cast(byte)(x + jump[i][0]);
   52             byte ny = cast(byte)(y + jump[i][1]);
   53             // check if position is on the board
   54             if ( nx<0 || nx>(SIZEX-1) || ny<0 || ny>(SIZEY-1) ) continue;
   55             // detect roundtrip
   56             if ( board[nx][ny] == 1 ) { round = 1; roundtrips++; break; } // True
   57         }
   58 
   59         solutions++;
   60         printboard(round);
   61 
   62         board[x][y] = 0;
   63         iters[iter] = 0;
   64         return;
   65     }
   66 
   67     // try next jump
   68     for (byte i = 0; i < 8; i++) {
   69 
   70         // new position
   71         byte nx = cast(byte)(x + jump[i][0]);
   72         byte ny = cast(byte)(y + jump[i][1]);
   73 
   74         // check if new position is on the board
   75         if ( nx<0 || nx>(SIZEX-1) || ny<0 || ny>(SIZEY-1) ) continue;
   76 
   77         //  check if new field position is still empty
   78         if ( board[nx][ny] != 0 ) continue;
   79 
   80         jumpto(nx, ny, cast(byte)(iter + 1), i);
   81     }
   82 
   83     // this iteration is done
   84     board[x][y] = 0;
   85     iters[iter] = 0;
   86 
   87 }
   88 
   89 void printboard(int round) {
   90     printf("===== Solution # %ld - Roundtripps: %ld ====== ", solutions, roundtrips);
   91     if (round) printf(" *** Roundtrip *** ");
   92     printf("\n");
   93     for (int i = 0; i < SIZEY; i++) {
   94         for (int j = 0; j < SIZEX; j++) {
   95             printf("%2i:", board[j][i]);
   96         }
   97         printf("\n");
   98     }
   99     printf("\n");
  100 }
  101 
  102 // EOF