1 -- jumpsaround - jumper game 2 -- (c) 2021-2021 Alexander Kulbartsch 3 4 Size_x = 6 5 Size_y = 5 6 7 Jump = {{-1,-2}, {1,-2}, {-2,-1}, {2,-1}, {-2,1}, {2,1}, {-1,2}, {1,2}} 8 9 solutions = 0 10 roundtrips = 0 11 12 print('Hello, Jumper!') 13 14 -- initialize the board 15 board = {} 16 for x = 1, Size_x do 17 board[x] = {} 18 for y = 1, Size_y do 19 board[x][y] = 0 20 end 21 end 22 23 24 function print_board(round) 25 print() 26 print("==== Solution #" .. solutions .. " - Roundtrips #" .. roundtrips .. " ====") 27 if round then print('*** Roundtrip ***') end 28 for y = Size_y, 1, -1 do 29 for x = 1, Size_x do 30 local val = board[x][y] 31 if val < 10 then io.write(' ') end 32 io.write(val .. ' ') 33 end 34 io.write('\n') 35 end 36 end 37 38 39 function jump_to(x, y, iter) 40 -- store position 41 board[x][y] = iter 42 43 -- check for roundtrip 44 if iter == Size_x * Size_y then 45 solutions = solutions + 1 46 -- print_board(false) 47 for i = 1, 8 do 48 local nx = x + Jump[i][1] 49 local ny = y + Jump[i][2] 50 if nx >= 1 and nx <= Size_x and ny >= 1 and ny <= Size_y and board[nx][ny] == 1 then 51 roundtrips = roundtrips + 1 52 print_board(true) 53 board[x][y] = 0 54 return 55 end 56 end 57 end 58 59 -- try next jumps 60 for i = 1, 8 do 61 local nx = x + Jump[i][1] 62 local ny = y + Jump[i][2] 63 if nx >= 1 and nx <= Size_x and ny >= 1 and ny <= Size_y and board[nx][ny] == 0 then 64 jump_to(nx, ny, iter + 1) 65 end 66 end 67 68 board[x][y] = 0 69 end 70 71 72 jump_to(1, 1, 1) 73 print() 74 print("==== Solution #" .. solutions .. " - Roundtrips #" .. roundtrips .. " ====") 75 print('I am done.') 76 77 -- EOF