Computer Chess Club Archives


Search

Terms

Messages

Subject: My solution in 43 lines

Author: Mathieu Pagé

Date: 08:32:44 02/22/05

Go up one level in this thread


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?

Hi,

here is my solution. I has 43 lines.

I hope it will not look ugly due to lines break.

Mathieu P.

<code>

#include <string>
#include <iostream>

using namespace std;

#define GetLine(pos)				((pos) / 8)
#define GetColumn(pos)				((pos) % 8)
#define GetWhite(x)					((x) >> 6)
#define GetBlack(x)					((x) & 63)
#define IsValid(x)					(GetBlack(x) != GetWhite(x))
#define HowManyOnLine(x, line)		(GetLine(GetBlack(x)) == line ? 1 : 0) +
(GetLine(GetWhite(x)) == line ? 1 : 0)
#define GetColumnUnique(x, line)	(GetLine(GetBlack(x)) == line ?
GetColumn(GetBlack(x)) : GetColumn(GetWhite(x)))
#define GetColumnFirst(x)			(GetColumn(GetBlack(x)) < GetColumn(GetWhite(x)) ?
GetColumn(GetBlack(x)) : GetColumn(GetWhite(x)))
#define GetColumnSecond(x)			(GetColumn(GetBlack(x)) < GetColumn(GetWhite(x)) ?
GetColumn(GetWhite(x)) : GetColumn(GetBlack(x)))
#define GetBetween(x)				(GetColumnSecond(x) - GetColumnFirst(x) - 1)
#define PrintDigitNotZero(x)		(x != 0 ? string(1, '0' + (x)) : string(""))

int _tmain(int argc, _TCHAR* argv[])
{
	for (int i = 0; i < 4096; i++)
		if (IsValid(i))
		{
			for (int line = 0; line < 8; line++)
			{
				switch (HowManyOnLine(i, line))
				{
				case 0:
					cout <<"0";
					break;
				case 1:
					cout <<PrintDigitNotZero(GetColumnUnique(i, line)) <<(GetLine(GetBlack(i))
== line ? 'N' : 'n') <<PrintDigitNotZero(7 - GetColumnUnique(i, line));
					break;
				case 2:
					cout <<PrintDigitNotZero(GetColumnFirst(i)) <<(GetColumn(GetBlack(i)) ==
GetColumnFirst(i)? 'N' : 'n') <<PrintDigitNotZero(GetBetween(i))
<<(GetColumn(GetBlack(i)) == GetColumnFirst(i)? 'n' : 'N') <<PrintDigitNotZero(7
- GetColumnSecond(i));
				}
				if (line != 7)	cout <<'/';
			}
			cout <<" w - - 0" <<endl;
		}

	int cpt;
	cin >>cpt;
	return 0;
}

</code>



This page took 0.01 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.