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

Published on 12 May 2024 09:40 PM

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

Source code in the wren programming language

import "random" for Random
import "./dynamic" for Tuple
import "./fmt" for Fmt

var Symbols = Tuple.create("Symbols", ["k", "q", "r", "b", "n"])

var A = Symbols.new("K", "Q", "R", "B", "N")
var W = Symbols.new("♔", "♕", "♖", "♗", "♘")
var B = Symbols.new("♚", "♛", "♜", "♝", "♞")

var krn = [
    "nnrkr", "nrnkr", "nrknr", "nrkrn",
    "rnnkr", "rnknr", "rnkrn",
    "rknnr", "rknrn",
    "rkrnn"
]

var NUL = "\0"

var chess960 = Fn.new { |sym, id|
    var pos = List.filled(8, NUL)
    var q = (id/4).floor
    var r = id % 4
    pos[r*2+1]= sym.b
    var t = q
    q = (q/4).floor
    r = t % 4
    pos[r*2] = sym.b
    t = q
    q = (q/6).floor
    r = t % 6
    var i = 0
    while (true) {
        if (pos[i] == NUL) {
            if (r == 0) {
                pos[i] = sym.q
                break
            }
            r = r - 1
        }
        i = i + 1
    }
    i = 0
    for (f in krn[q]) {
        while (pos[i] != NUL) i = i + 1
        pos[i] = (f == "k") ? sym.k :
                 (f == "r") ? sym.r :
                 (f == "n") ? sym.n : pos[i]
    }
    return pos.join(" ")
}

System.print(" ID  Start position")
for (id in [0, 518, 959]) Fmt.print("$3d  $s", id, chess960.call(A, id))
System.print("\nRandom")
var rand = Random.new()
for (i in 0..4) System.print(chess960.call(W, rand.int(960)))


  

You may also check:How to resolve the algorithm Department numbers step by step in the AWK programming language
You may also check:How to resolve the algorithm Last Friday of each month step by step in the Pascal programming language
You may also check:How to resolve the algorithm Sum to 100 step by step in the REXX programming language
You may also check:How to resolve the algorithm Align columns step by step in the HicEst programming language
You may also check:How to resolve the algorithm Continued fraction/Arithmetic/Construct from rational number step by step in the Tcl programming language