Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Chess programming puzzle

Author: Steffen Jakob

Date: 12:49:26 02/22/05

Go up one level in this thread


On February 22, 2005 at 15:43:44, Scott Gasch wrote:

>On February 22, 2005 at 09:20:31, Steffen Jakob wrote:
>
>>On February 22, 2005 at 08:35:07, Andrew Wagner wrote:
>>
>>>Hi all.
>>>I got sidetracked this morning by an interesting chess programming problem. It
>>>took me a couple hours, but I think I have a working algorithm -- haven't tested
>>>yet though. Anyway, I got to wondering if others would approach it the same way.
>>>So I thought I'd make a little competition of it. Post your code here, and I'll
>>>pick the program I like best and shower praise and adulation on its author. If
>>>people like this challenge, maybe I'll do one each month or something. Anyway,
>>>here's the one I did this morning:
>>>There are 64 x 63 = 4032 ways to put a black knight and white knoght both on a
>>>chess board. Write a program -- from scratch -- to generate FENs for each of
>>>these positions. The FENs should look something like: Nn6/8/8/8/8/8/8/8 w - - 0
>>>1.
>>>
>>>I think my code will wind up weighing in at around 60-70 lines of C. Can you do
>>>better?
>>
>>Here is a quick hack. Without comments, with place to optimize, also almost
>>untested, but shorter than yours ;-)
>>
>>
>>#include <iostream>
>>
>>int GetFile(int square) { return square & 7; }
>>int GetRank(int square) { return square >> 3; }
>>
>>int main(int argc, char** argv) {
>>	for(int white_knight = 0; white_knight < 64; ++white_knight) {
>>		for(int black_knight = 0; black_knight < 64; ++black_knight) {
>>			if(white_knight == black_knight) {
>>				continue;
>>			}
>>
>>			for(int rank = 7; rank >= 0; --rank) {
>>				int n_empty = 0;
>>				for(int file = 0; file <= 7; ++file) {
>>					if(rank == GetRank(white_knight) && file == GetFile(white_knight)) {
>>						if(n_empty > 0) {
>>							std::cout << n_empty;
>>						}
>>						std::cout << "N";
>>						n_empty = 0;
>>					}
>>					else if(rank == GetRank(black_knight) && file == GetFile(black_knight)) {
>>						if(n_empty > 0) {
>>							std::cout << n_empty;
>>						}
>>						std::cout << "n";
>>						n_empty = 0;
>>					}
>>					else {
>>						++n_empty;
>>					}
>>				}
>>				if(n_empty > 0) {
>>					std::cout << n_empty;
>>				}
>>				if(rank != 0) {
>>					std::cout << "/";
>>				}
>>			}
>>			std::cout << " w - - 0 1" << std::endl;
>>		}
>>	}
>>
>>	return 0;
>>}
>
>[ scott@wannabe:~/tmp ] % g++34 -O -S steffen.cpp ; wc -l steffen.s
>     557 steffen.s

Most of this code is because of using streams. If I replace them by printf,
define the GetXXX() functions inline and compile the code with gcc instead of
g++ I get 148 assembler lines.

Best wishes,
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.