Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: OT: C++ code question.

Author: Benny Antonsson

Date: 09:51:13 11/06/03

Go up one level in this thread


I use something like this to compile the ECO-book for Alarm.
The ECO source file is in the following text-format:

[A00 Polish: Outflank variation]
b2b4 c7c6
[A00 Polish: Outflank, wing defence]
b2b4 c7c6 c1b2 a7a5 a2a3 a5b4 a3b4 a8a1 b2a1 d8b6 c2c3
[A00 Sokolsky: 1...d5]
b2b4 d7d5 c1b2 c8f5

(Lines are not supposed to be longer than 1024 chars)


And here is the CreateECOBook-method from Alarm:


////////////////////////////////////////////////////////////////////////
//
// includes
//
#include <string.h>
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>


////////////////////////////////////////////////////////////////////////
//
// CreateECOBook
//
void Tools::CreateECOBook(char* theSourceFileName)
{
	ifstream in;
	ofstream out;
	char str[1024];
	char* moveStr;
	Board board;

	int numEcos		= 0;
	int fileSize	= 0;

	// Open input file
	in.open(theSourceFileName, ios::in | ios::nocreate);

	// Get filesize for percentage calculations
	if (in.fail() == false)
	{
		in.seekg(0, ios::end);
		fileSize = in.tellg();
		in.seekg(0, ios::beg);
	}
	else
	{
		cout << "Unable to open file: " << theSourceFileName << "!" << endl;

		// Exit this method if file wasn't found
		return;
	}

	// Open output file
	out.open("book.eco", ios::out | ios::binary);

	// Get first variation name
	if (in.eof() == false)
	{
		in.getline(str, 1024, '\n');
	}

	// Process all eco variations in the file
	while (in.eof() == false)
	{
		board.InitialPosition();
		ECOBOOKSLOT ebs;

		strcpy(ebs.name, str);

		// Get first string with moves from this variation
		in.getline(str, 1024, '\n');

		// Read all strings from variation
		while (str[0] != '[' && in.eof() == false)
		{
			// Get each move in variation
			moveStr = strtok(str, " ");

			while (moveStr != NULL)
			{
				Move move = IsLegalMove(board, moveStr);

				// Move is illegal
				if (move.flags & MOVE_ILLEGAL)
				{
					cout << "Illegal move! " << endl;
					cout << "Name: " << ebs.name << endl;
					cout << "Move: " << moveStr << endl;
					break;
				}
				else
				{
					// Make move on board
					board.MakeMove(move);
				}

				// Get next move from move string
				moveStr = strtok(NULL, " ");
			}

			// Get next move string from file
			in.getline(str, 1024, '\n');
		}

		// All moves are executed on the board, now calculate the hash values
		ebs.hashKey		= hashTable.CalcBookHashKey(board);
		ebs.hashLock	= hashTable.CalcBookHashLock(board);

		// Save this ECO to our output file
		out.write((char*)&ebs.hashKey, sizeof(ebs.hashKey));
		out.write((char*)&ebs.hashLock, sizeof(ebs.hashLock));
		out.write(ebs.name, sizeof(ebs.name));

		// Increase a statistical counter
		numEcos++;

		cout << numEcos << "\r";
		cout.flush();
	}

	// Close output file
	out.close();

	// Show statistics
	cout << "Number of eco codes: " << numEcos << endl;
}


/Benny



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.