Author: Steffen Jakob
Date: 07:12:55 02/24/05
Go up one level in this thread
On February 24, 2005 at 04:29:11, Uri Blass wrote:
>On February 24, 2005 at 03:34:58, Steffen Jakob wrote:
>
>>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;
>>}
>
>Thanks
>
>I had no chance to get to this code by myself for the simple reason of missing
>knowledge about library functions.
>
>If I understand correctly
>
>std::copy(pieces.begin(), pieces.end(), &board[0]);
>
>Is it the same as
>board[0]='N'
>board[1]='n'
>
>std::sort(&board[0], &board[64]);
>sort board in lexisographic order so you have the first permutation
>board[0]=0;
>...
>board[61]=0;
>board[62]='n'
>board[63]='N'
>
>std::next_permutation(
>calculate the next permutation in lexixographic order
Yes, these algorithms are part of the standard template library (STL).
http://www.sgi.com/tech/stl/copy.html
http://www.sgi.com/tech/stl/sort.html
http://www.sgi.com/tech/stl/next_permutation.html
http://www.sgi.com/tech/stl/
Greetings,
Steffen.
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.