Computer Chess Club Archives


Search

Terms

Messages

Subject: Fen generator

Author: Steffen Jakob

Date: 00:34:58 02/24/05


Hi!

Uri mentioned that a general Fen generator would be useful for him:

> This idea seems to be good also for generalization to write a function that gets
> all the fen of 3 and 4 and 5 pieces(I think to use it for debugging the nalimov
> tablebases and also debugging my function to detect correct fen)

> Uri

The following should do the work. This code is also able to handle multiple
pieces of the same type (e.g. "KPPk"). Oh, and for those who judge the quality
of code by counting generated assembler lines: this code probably sucks bigtime!

Greetings,
Steffen.


#include <iostream>
#include <string>
#include <algorithm>

void GenerateFens(const std::string& pieces) {
	char board[64] = {
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0
	};

	// Initial setting of pieces in the empty board.
	std::copy(pieces.begin(), pieces.end(), &board[0]);
	// Prepare for permutating.
	std::sort(&board[0], &board[64]);
	do {
		int first_sq = 0;
		for(int sq = 0; sq < 64; ++sq) {
			if(board[sq] != 0) { // The square is not empty
				if(sq - first_sq > 0) {
					// Flush number of empty squares.
					std::cout << sq - first_sq;
				}
				std::cout << board[sq];
				first_sq = sq + 1;
			}
			if(sq % 8 == 7) { // End of a rank
				if(sq - first_sq >= 0) {
					// Flush number of empty squares.
					std::cout << 1 + sq - first_sq;
				}
				if(sq != 63) {
					std::cout << "/";
				}
				first_sq = sq + 1;
			}
		}
		std::cout << " w - - 0 1" << std::endl;
	}
	while(std::next_permutation(&board[0], &board[64]));
}

int main(int argc, char** argv) {
	GenerateFens("Nn");
	// GenerateFens("KPk");
	// GenerateFens("PP");
	// GenerateFens("PPPPPPPPRNBQKBNRpppppppprnbqkbnr");

	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.