Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: 960 FRC start positions derived from numbers - cpp method

Author: Reinhard Scharnagl

Date: 02:20:43 06/09/04

Go up one level in this thread


Now commented program is generating complete FEN strings:


// FRC 960 Starting-Position FEN string Generator
// identifying each position to a special number
// (C) 2004 Reinhard Scharnagl, Munich, Germany

#include <iostream>

using namespace std;

// position sequence synthesize working array
static char piece[9];
// knight distributions over 5 free squares
static const int knight_pos[10] = {
   3, // xx---
   5, // x-x--
   9, // x--x-
  17, // x---x
   6, // -xx--
  10, // -x-x-
  18, // -x--x
  12, // --xx-
  20, // --x-x
  24  // ---xx
};

// placing a piece at a counted free position
void place_at(int nr_free, char fig)
{
  for (int pos_q = 0; ; ++pos_q) {
    if (piece[pos_q] != '\0') {
      ++nr_free;
    } else if (nr_free == pos_q) {
      piece[pos_q] = fig;
      break;
    }
  }
}

int main(void)
{
  cout << "960 FRC positions (0..959):" << endl << endl;

  // generate all 960 positions
  for (int nr_cnt = 0; nr_cnt < 960; ++nr_cnt) {
    int pos, nr = nr_cnt;
    // clear pieces
    for (pos = 9; --pos >= 0; )
      piece[pos] = '\0';
    // set the bright bishop by dividing 4
    piece[2*(nr % 4) + 1] = 'b';
    nr /= 4;
    // set the dark bishop by dividing 4
    piece[2*(nr % 4) + 0] = 'b';
    nr /= 4;
    // set the queen in her free field dividing 6
    place_at((nr % 6), 'Q');
    nr /= 6;
    // calculate the knight positions by nr and set them
    pos = knight_pos[nr];
    for (int bit = 5; --bit >= 0; )
      if (pos & (1 << bit))
        place_at(bit, 'n');
    // set the remaining pieces, rooks and king
    place_at(2, 'r');
    place_at(1, 'k');
    place_at(0, 'r');

    // print out the resulting line
    cout << piece << "/pppppppp/8/8/8/8/PPPPPPPP/";
    for (pos = 8; --pos >= 0; )
      piece[pos] ^= 'A'^'a';
    cout << piece << " w KQkq - 0 1" << endl;
  }

  return 0;
}



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.