Chess 960 position generator

Hi All,

I wanted to build a chess960 position generator, and I wanted to get some guidance on the way to store the data. The output would be a char array like: 'RNBQKBNR' that I generatte from the basic rules.

Here is my code, but I don't know how to handle the data. I thought I could shove it all into an array[8]. Like
{R, N, B, Q, K, B, N, R}.

int idn = random(960) ;         //Divide idn by 4, yielding quotient 'bishop_intermediate' and remainder 'bishop light'
int bishop_light = idn % 4;    // place a bishop on light square (b, d, f, h) 
int bishop_light_pos[] = {1, 3, 5, 7};
int a = bishop_light_pos[bishop_light];
int bishop_intermediate = idn/4; // Divide 'bishop_intermediate' by 4 again, yielding quotient N3 and remainder 'bishop_dark'.
int N3 = bishop_intermediate/4;
int bishop_dark = bishop_intermediate % 4; // place a bishop on dark square (a, c, e, g)
int bishop_dark_pos[] = {0, 2, 4, 6};
int b = bishop_dark_pos[bishop_dark];
int N4 = N3/6; 
int queen = N3 % 6;  //  Divide N3 by 6, yielding quotient N4 and remainder queen.
                                   //  Place the Queen according to Q, where 0 is the first free square starting from a, 1 is the second, etc.
                                 //  N4 will be a single digit, 0 ... 9. 
                                //  Place the Knights according to its value by consulting the following table:
int knight_table[] = { 11000, 10100, 10010, 10001, 01100, 01010, 01001, 00110, 00101, 00011 };

void setup() {
  Serial.begin(9600);
}


void loop() {
  Serial.println(idn);
  Serial.print("First Bishop on ");
  Serial.println(a);
  
  Serial.print("Second Bishop on ");
  Serial.println(b);
  
  
  delay(10000);
  
}

Here is the link to the rules of the scheme: Fischer random chess numbering scheme - Wikipedia

And the text description of what I am trying to do:

Direct derivation[edit]
White's Chess960 starting array can be derived from its number N (0 ... 959) as follows:

a) Divide N by 4, yielding quotient N2 and remainder B1. Place a Bishop upon the bright square corresponding to B1 (0=b, 1=d, 2=f, 3=h).

b) Divide N2 by 4 again, yielding quotient N3 and remainder B2. Place a second Bishop upon the dark square corresponding to B2 (0=a, 1=c, 2=e, 3=g).

c) Divide N3 by 6, yielding quotient N4 and remainder Q. Place the Queen according to Q, where 0 is the first free square starting from a, 1 is the second, etc.

d) N4 will be a single digit, 0 ... 9. Place the Knights according to its value by consulting the following table:

Digit Knight positioning
0 N N - - -
1 N - N - -
2 N - - N -
3 N - - - N
4 - N N - -
5 - N - N -
6 - N - - N
7 - - N N -
8 - - N - N
9 - - - N N
e) There are three blank squares remaining; place a Rook in each of the outer two and the King in the middle one.

Thanks for any ideas about the best way I should try to do this.

Jimmy

The best method of storage depends on what you do with the data afterwards. That being said, an array should be fine. What are you trying to do?

I'm just going to print it on a pair of DL2416 LED displays, or some kind of 8-digit intelligent display. I have a few HDSP 2115 displays that would work nice. I'll have it print the 8 digits, wait a little time and then go into sleep/power down. The reset button will start it back up and display a new position.

I started messing with trying to shove the results char by char into an array called back_rank, but I was getting 'array bound is not an integer constant.'

Looking closer at your code, you should probably define the length of all of your arrays:
int bishop_light_pos[4] = {1, 3, 5, 7};
int bishop_dark_pos[4] = {0, 2, 4, 6};

The biggest downside to arrays is that in C they are not adjustable length. Generally This just means you need to plan ahead and make sure it is long enough to handle anything you intend on putting into it.

Thanks for this info, I'll declare the size of those arrays.

I'm having trouble working out where to put the queen. I have a variable 'queen' that would be 0 to 6, and I have to put the Q into the index of the array that corresponds to the first open index.

So if the 'queen is 4', and the array is {B, B, 0, 0, 0, 0, 0, 0}, I need to put Q at index 6 like { B, B, 0, 0, 0, 0, Q, 0}.

So do I iterate over the array and count both the index count, and the 0-count?

This works for the 0th open index:

if (queen == 0){
    for ( int i = 0; i < 8; i++ ){
        if (back_rank[i] == '0') {
            current_index = i;
            break;
        }
   } 
back_rank[current_index] = 'Q';

But I'm having a hard time figuring out how to count the index, and the open squares (represented by '0'), when it is more than 0.

Thanks for any help.

Jimmy

I don't know anything about chess programming. If the columns in the array have specific meanings (e.g. the queen is always in column 4 or something similar) then it may make the code easier to follow if you give the index numbers a name. For example

byte queenCol = 4;

and then you could refer to it like this

if (array[queenCol] == 0) { ....

Sorry if the specifics are stupid but I hope it illustrates my point.

...R

I got this done. I still have a few bugs, and need to clean up the code a little. It currently looks like it was written by honey badgers.
I've attached a picture to show what it looks like. Thanks to all who offered advice.