How to resolve the algorithm Knight's tour step by step in the 11l programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Knight's tour step by step in the 11l programming language

Table of Contents

Problem Statement

Problem: you have a standard 8x8 chessboard, empty but for a single knight on some square. Your task is to emit a series of legal knight moves that result in the knight visiting every square on the chessboard exactly once. Note that it is not a requirement that the tour be "closed"; that is, the knight need not end within a single move of its start position. Input and output may be textual or graphical, according to the conventions of the programming environment. If textual, squares should be indicated in algebraic notation. The output should indicate the order in which the knight visits the squares, starting with the initial position. The form of the output may be a diagram of the board with the squares numbered according to visitation sequence, or a textual list of algebraic coordinates in order, or even an actual animation of the knight moving around the chessboard. Input: starting square Output: move sequence

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Knight's tour step by step in the 11l programming language

Source code in the 11l programming language

V _kmoves = [(2, 1), (1, 2), (-1, 2), (-2, 1), (-2, -1), (-1, -2), (1, -2), (2, -1)]

F chess2index(=chess, boardsize)
   ‘Convert Algebraic chess notation to internal index format’
   chess = chess.lowercase()
   V x = chess[0].code - ‘a’.code
   V y = boardsize - Int(chess[1..])
   R (x, y)

F boardstring(board, boardsize)
   V r = 0 .< boardsize
   V lines = ‘’
   L(y) r
      lines ‘’= "\n"r.map(x -> (I @board[(x, @y)] {‘#2’.format(@board[(x, @y)])} E ‘  ’)).join(‘,’)
   R lines

F knightmoves(board, P, boardsize)
   V (Px, Py) = P
   V kmoves = Set(:_kmoves.map((x, y) -> (@Px + x, @Py + y)))
   kmoves = Set(Array(kmoves).filter((x, y) -> x C 0 .< @boardsize & y C 0 .< @boardsize & !@board[(x, y)]))
   R kmoves

F accessibility(board, P, boardsize)
   [(Int, (Int, Int))] access
   V brd = copy(board)
   L(pos) knightmoves(board, P, boardsize' boardsize)
      brd[pos] = -1
      access.append((knightmoves(brd, pos, boardsize' boardsize).len, pos))
      brd[pos] = 0
   R access

F knights_tour(start, boardsize, _debug = 0B)
   [(Int, Int) = Int] board
   L(x) 0 .< boardsize
      L(y) 0 .< boardsize
         board[(x, y)] = 0
   V move = 1
   V P = chess2index(start, boardsize)
   board[P] = move
   move++
   I _debug
      print(boardstring(board, boardsize' boardsize))
   L move <= board.len
      P = min(accessibility(board, P, boardsize))[1]
      board[P] = move
      move++
      I _debug
         print(boardstring(board, boardsize' boardsize))
         input("\n#2 next: ".format(move))
   R board

L(boardsize, start) [(5, ‘c3’), (8, ‘h8’), (10, ‘e6’)]
   print(‘boardsize: ’boardsize)
   print(‘Start position: ’start)
   V board = knights_tour(start, boardsize)
   print(boardstring(board, boardsize' boardsize))
   print()

  

You may also check:How to resolve the algorithm Find the intersection of a line with a plane step by step in the zkl programming language
You may also check:How to resolve the algorithm Text processing/1 step by step in the J programming language
You may also check:How to resolve the algorithm Tokenize a string with escaping step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Smith numbers step by step in the 11l programming language