How to resolve the algorithm Generate Chess960 starting position step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Generate Chess960 starting position step by step in the jq programming language

Table of Contents

Problem Statement

Chess960 is a variant of chess created by world champion Bobby Fischer. Unlike other variants of the game, Chess960 does not require a different material, but instead relies on a random initial position, with a few constraints:

With those constraints there are 960 possible starting positions, thus the name of the variant.

The purpose of this task is to write a program that can randomly generate any one of the 960 Chess960 initial positions. You will show the result as the first rank displayed using either the chess symbols in Unicode (♔♕♖♗♘), the letters King Queen Rook Bishop kNight, or the corresponding letters in a language other than English.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Generate Chess960 starting position step by step in the jq programming language

Source code in the jq programming language

### Utilities
# The glyphs in .
def chars: explode[] | [.] | implode;

# input: an array
# $keys : an array of strings
def objectify($keys):
  with_entries(.key = $keys[.key]) ;

def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;


def Symbols: ["k", "q", "r", "b", "n"];

def A: ["K", "Q", "R", "B", "N"]     | objectify(Symbols);
def W: ["♔", "♕", "♖", "♗", "♘"]  | objectify(Symbols);
def B: ["♚", "♛", "♜", "♝", "♞"]  | objectify(Symbols);

def krn: [
    "nnrkr", "nrnkr", "nrknr", "nrkrn",
    "rnnkr", "rnknr", "rnkrn",
    "rknnr", "rknrn",
    "rkrnn"
];

# $sym specifies the Symbols
# $id specifies the position
def chess960($sym):
  . as $id
  | { q:   (($id/4)|floor),
      r:   ($id % 4)
    }
  | .pos[.r*2+1] = $sym.b
  | .t = .q
  | .q |= ((./4)|floor)
  | .r = (.t % 4)
  | .pos[.r*2] = $sym.b
  | .t = .q
  | .q |= ((./6)|floor)
  | .r = .t % 6
  | .i = 0
  | .break = false
  | until( .break; 
      if .pos[.i] == null
      then if .r == 0
           then .pos[.i] = $sym.q
           | .break = true
           else .r += -1
           end
      else .
      end
      | .i += 1 )
  | .i = 0      
  | reduce (krn[.q]|chars) as $f (.;
      # find next insertion point
      until(.pos[.i] == null; .i += 1)
      | if  $f | IN("k", "r", "n")
        then .pos[.i] = $sym[$f]
        else .
        end )
  | .pos
  | join(" ") ;

def display960($sym):
 "\(lpad(3))  \(chess960($sym))";
 
" ID  Start position",
( 0, 518, 959 |  display960(A) ),
"\nPseudo-random starting positions:",
(699, 889, 757, 645, 754 | display960(W))

  

You may also check:How to resolve the algorithm Loops/Break step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bogosort step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Constrained genericity step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm 24 game/Solve step by step in the Quackery programming language
You may also check:How to resolve the algorithm N-queens problem step by step in the Kotlin programming language