11.12.11
Posted in chess at 1:37 pm by javierruizblog
I have made some new changes to my chess engine, Aretar.
First of all, I have included the UCI protocol so now the engine can be used with a chess GUI such as Arena
As well as using it with Arena or any other UCI based GUI, you can play Aretar via command line, as in the previous version. Use ‘?’ to get a brief summary of accepted commands and their usage.
The chess engine is also more stable now, and faster thanks to the using of two threads. It is a fixed value for the moment, but I hope it could be configured in the near future.
Despite these new features, Aretar is not a very strong engine yet. I guess it is in the range of 1400-1600 points in an Elo rating, but I have made no test to support these values. Its main weakness are that the engine does not use any strategy but only tactics. It can process as much as 250000 moves per second, more or less (which is not a really high value for a chess engine nowadays), but although it uses some positional and mobility issues, comparison of number of pieces and their values carries on being the core of its brain.
Aretar Chess Engine
(Password: ‘aretar’)
If you find any bug, please, report it so I can fix it in the next release.
Regards,
Javier Ruiz
Permalink
08.28.11
Posted in chess at 11:38 am by javierruizblog
For a long time I wanted to make my own chess program, mainly because I am a fan of this game and also because it is a good exercise of programming.
Chess is one of the most ancient games in humanity, so it has been studied by a lot of people and there exist milliards of computer implementations.
Due to its nature, chess is quite easy to implement in a computer. The main idea to give the PC some ‘intelligence’ aims towards finding a good way to evaluate the available moves in a given position.
If we were be capable of evaluate all the possible moves until one of the two opponents had reached a winning position, it would be easy to determine the better move. But the more deep we go into a given combination, the more possibilities come to our path; it can be easily seen that the number of movements grows in a exponential way.
In order to give a quick answer, we have to avoid searching ‘ad infinitum’, so a given search depth is fixed when the program is being created. This depth can be adapted later if needed, but it is the main tool to get a response in a reasonable amount of time.
The second tool widely used to increase speed is ‘pruning’. It consists on stop searching a branch when the futility of carry on it has been determined. With this method we can get really fast answers.
The chess program I have made takes into account both principles. The evaluation function uses a static approach, basically counting pieces from both sides and giving a fixed value to each of them. The sum of all these values gives a rough idea of who is leading the game. Apart from this static approach, positional issues have been taken into account: passed pawns, king safety, early queen movement, central squares, and so on.
It is a command line based program in which you use ‘Coordinate Notation’ to enter the moves (downcase only without slashes).
Example:
e2e4
b1c3
I hope you enjoy the program, although it is not a very strong opponent yet.
Aretar Chess Engine
(Password: ‘aretar’)
If you want to start out in this fascinating world of chess programming I recommend reading these pages:
Beowulf Computer Chess Engine
Chessprogramming - Wikispaces
Cheers,
Javi
Permalink
02.18.11
Posted in MySQL at 11:00 am by javierruizblog
Recently I have had to manage a MySQL database with the factory inventory of my company.
For doing this I created a CLR Form in Microsoft Visual C++ 2008 using the MySQL C API, in order to get an easy way of manipulate the database. When configuring the API I met some problems which I will present here.
First of all I had to include the MySQL paths into the project environment. I went to the Project->Properties tab and in the Properties Pages window I selected Configuration Properties->C/C++->General
I wrote down the MySQL include directory into the Additional Include Directories box, which in my case was:
C:\wamp\bin\mysql\mysql5.5.8\include
Then I checked that the WIN32 Preprocessor definition was set in the corresponding place so I ran into the ..C/C++->Preprocessor box and look at the Preprocessor definitions. It was correctly set so I didn’t change anything here.
Next I went to the Properties->C/C++->Linker->General tab and I added the MySQL lib path into the Additional libraries directory box. In my case it was: C:\wamp\bin\mysql\mysql5.5.8\lib
Later on, and yet in the Linker tab, I selected the Input option and there the Additional dependencies box. I had to write here all the needed libraries: libmysql.lib mysqlclient.lib
If you prefer you can use the pragma directive to include the libraries:
#pragma comment(lib, “mysqlclient.lib”)
Finally I had to fix a bug in the “mysql_com.h” file:
#if defined(__WIN__) && !defined( _CUSTOMCONFIG_)
#define MYSQL_NAMEDPIPE "MySQL"
#define MYSQL_SERVICENAME "MySQL"
#include /* Added line For windows */
#endif /* __WIN__ */
At this point I was ready to write down a small test program to manage my database.The following program open the database and make a query.(Note: I use a CLR Form with a Button and a ListView control to do the test)
#include "stdafx.h"
#include "Form1.h"
#include "mysql.h”
MYSQL *ConPtr,Mysql;
MYSQL_RES *ResPtr;
System::Void Form1::TestB_Click(System::Object^ sender, System::EventArgs^ e)
{
const char *ch;
unsigned int num_fields;
unsigned int i;
char aux[100];
MYSQL_ROW myrow;
MYSQL_FIELD *field;
ConPtr = mysql_init(NULL);
if (!mysql_real_connect(ConPtr,"localhost","root","","mydatabase",0,NULL,0))
{
ch = mysql_error(&Mysql);
}
// Retrieve the 'materials' table
strcpy(Sqlvar,"SELECT * FROM materials");
mysql_query(ConPtr,Sqlvar);
// Query result saved into ResPtr
ResPtr = mysql_store_result(ConPtr);
// Read retrieved number of fields
num_fields = mysql_num_fields(ResPtr);
// Add fields to the ListView Columns
while((field = mysql_fetch_field(ResPtr)))
{
String ^systemstring = gcnew String((const char*)field->name);
listView1->Columns->Add(systemstring, 100, HorizontalAlignment::Center);
}
// Clear everything from the ListView
listView1->Items->Clear();
listView1->GridLines = true;
listView1->View = View::Details;
listView1->LabelEdit = true;
// Allow the user to rearrange columns.
listView1->AllowColumnReorder = true;
if (num_fields)
{
// Create an array of String* to hold the subitems
array^ subItems = gcnew array(num_fields);
while ((myrow = mysql_fetch_row(ResPtr)))
{
unsigned long *lengths;
lengths = mysql_fetch_lengths(ResPtr);
for(i = 0; i Items->Add(itm);
}
}
else
{
richTextBox1->AppendText("MySQL has returned an empty value");
}
listView1->AutoResizeColumns(ColumnHeaderAutoResizeStyle::HeaderSize);
mysql_close(ConPtr);
}
And this is the result:

Permalink
10.26.10
Posted in Uncategorized at 9:07 am by javierruizblog
Some time ago I wrote a small Tetris code as an exercise in style. It is nothing new, but it provided me some fun and the results were quite satisfactory, taking into account that it runs on a command shell and the ‘graphics’ are blocks of text.

The main code was made in a lazy morning and then I added the score functions. It is not a very worked out job, so you may expect some functions without any comments, but I hope they can be understood well.
Tetris code
For the compilation I used Visual Studio Project 2008 under Windows XP, but the code is so simple that I am sure it could be adapted easily for any other compiler, as GNU gcc, for example.
Regards,
Javi
Permalink
10.14.10
Posted in Algorithm at 2:56 pm by javierruizblog
Lately I’ve been playing around a little bit with the Goertzel algorithm. As you know, this algorithm is widely used in tone detection because it is a digital signal processing tool for identifiying frequency components of a signal; i.e., it can be seen as an IIR filter with a very high Q and a very narrow bandpass. I have programmed a small function to use the Goertzel algorithm and find the phase of a given signal which is embedded into a noisy carrier. Two main cases have been studied using this code. In the first case I have fixed the following parameters:


;
; 
;
; 

Instead of using an integer k I have decided to use the real value of the calculated k. This decission ended in a better accuracy on the results. The following graph shows the resulting phase, given by the Goertzel algorithm, versus the ratio
of two CW with the same frequency but different amplitude and two different and fixed phases.
is swept in the range of 0,1 to 100 which gives us a final range between -20 and 40dB.
![Phase given by Goertzel of two CW at the same frequency]()
In the second case I have used the Goertzel algorithm in a signal with noise.


;
;
; 

And the noise is AWGN with:
; 
The amplitude of the signal was multiplied by a factor of
because this made the calculation of the SNR easier:


Phase given by Goertzel of a sin with noise
Both graphs were very useful because they show in an experimental way that the Goertzel algorithm is unable to discriminate the signal from multipath effects. They provide also a SNR threshold which may be taken into account when designing with this filter.
Permalink