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

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Generate Chess960 starting position step by step in the J 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 J programming language

Source code in the j programming language

row0=: u: 9812+2}.5|i.10
king=: u:9812
rook=: u:9814
bish=: u:9815
pos=: I.@e.
bishok=: 1=2+/ .| pos&bish
rookok=: pos&rook -: (<./,>./)@pos&(rook,king)
ok=: bishok*rookok
perm=: A.&i.~ !
valid=: (#~ ok"1) ~.row0{"1~perm 8
gen=: valid {~ ? bind 960


   gen''
♘♗♖♔♗♕♖♘
   gen''
♗♘♘♗♖♔♖♕
   gen''
♖♗♔♘♘♕♗♖
   gen''
♖♔♕♗♗♘♖♘


  

You may also check:How to resolve the algorithm Array concatenation step by step in the Groovy programming language
You may also check:How to resolve the algorithm Multisplit step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the 11l programming language
You may also check:How to resolve the algorithm Enumerations step by step in the Picat programming language
You may also check:How to resolve the algorithm Find the missing permutation step by step in the 11l programming language