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

Published on 12 May 2024 09:40 PM

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

Source code in the objeck programming language

class Chess960 {
  function : Main(args : String[]) ~ Nil {
    Generate(10);
  }
  
  function : Generate(c : Int) ~ Nil {
    for(x := 0; x < c; x += 1;) {
      StartPos()->PrintLine();
    };
  }
  
  function : StartPos() ~ String {
    p := Char->New[8];
    
    # bishops
    b1 : Int; b2 : Int;
    while(true) {
      b1 := GetPosition(); b2 := GetPosition(); 
      
      b1c := b1 and 1; b2c := b2 and 1;
      c := b1c = 0 & b2c <> 0;
      if(c) {
        break;
      };
    };
    p[b1] := 0x2657; p[b2] := 0x2657;

    # queen, knight, knight
    q := false;
    for(x := 0; x < 3; x += 1;) {
      do { 
        b1 := GetPosition(); 
      } while( p[b1] <> '\0');
      
      if(<>q) { 
        p[b1] := 0x2655; q := true; 
      }
      else { 
        p[b1] := 0x2658;
      };
    };

    # rook king rook
    q := false;
    for(x := 0; x < 3; x += 1;) {
      a := 0;
      while(a < 8) {
        if(p[a] = '\0') {
          break;
        };
        a += 1;
      };

      if(<>q) { 
        p[a] := 0x2656; q := true; 
      }
      else { 
        p[a] := 0x2654; q := false; 
      };
    };

    s := "";
    for(x := 0; x < 8; x += 1;) { s->Append(p[x]); };
    return s;
  }

  function : GetPosition() ~ Int {
    return (Float->Random() * 1000)->As(Int) % 8;
  }
}

  

You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the Standard ML programming language
You may also check:How to resolve the algorithm ABC problem step by step in the Ruby programming language
You may also check:How to resolve the algorithm Doubly-linked list/Definition step by step in the Nim programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Lobster programming language
You may also check:How to resolve the algorithm Shortest common supersequence step by step in the zkl programming language