Thursday, April 19, 2007

Rendering chess positions


Chess, 3-D graphics and FOSS are three areas I like. I have been using the POV-Ray program for sometime now and prepared files to render individual chess pieces, which can be included in a POV-Ray scene. I tested the include files with a board and a few lights. This way, I could include the shape definition for a piece and place it on a given position on the board by translating it.

Next, I studied the CPAN Chess libraries in Perl and figured how to read in a file saved in FEN with a Perl script. The script would then generate a POV-Ray file with necessary translate instructions to place different pieces in different positions, as specified in the input FEN file. Now, I simply used POV-Ray to render the file generated above and I have a 3-D view of the board position. I have included a sample image. I got this position by editing the start position in Xboard using the "Edit Game" feature. I saved the position as an FEN file and then ran the perl script and the povray program on my ubuntu powered laptop.

Thursday, April 12, 2007

Varsity project on sourceforge.net

In the penultimate year of my bachelors course in Information Science engineering, my classmate, Vineeth and I developed a handy little piece of software. Back then, we were learning UML and were amused with how new applications could be designed using it's methodology. Why not develop software using which we could design UML diagrams, we thought. This idea stuck as our curricular mini-project for the 6th semester.

The UML design suite, which was the result of our efforts, finally had 7 modules. You could develop
  • Use-case diagrams
  • Sequence diagrams
  • Activity diagrams
  • Class diagrams
  • Component diagrams
  • Deployment diagrams
We implemented two options for saving work. First, an open XML based file format which we developed. Second, any of a set of well known image formats including JPEG and PNG.

The project was not publically available for long and we knew we could not leave something of it's kind stored somewhere on a CD or computer without any positives coming out of it. So recently, I submitted the online and requested sourceforge to have our project up on their archives.

On sourceforge, our project has a homepage, screenshots, a subversion repository, forums for development and open discussion. All these can be accessed by clicking:

http://sourceforge.net/projects/umldesignsuite

Tuesday, February 20, 2007

deepOfix public release

Many things are happening around the world. People are working for a living, children are studying in schools, youth are looking for employment, cities, especially here in south India are growing. Today is a very special day because I am part of something unique. If I were to read a newspaper and ask myself, what does this newspaper have in it for me to be happy about, for example, a fellow Indian - Ratan Tata's work through the years in bringing a grand old company to this age when it has acquired both quality and size, the events culminating in his TATA's takeover of the Corus group. This kind of news makes me take pride in a fellow countryman and in many ways, a man I look up to personally as one who, having had wealth, nevertheless worked and with genuine sincerity and constancy through the years, has brought us here in this massive subcontinent a smile.

Sometimes, days pass us by and we wonder what has changed. Today, I believe something that has been in the laboratory of deeprootlinux is being unveiled - released publicly. Today, DeepRoot Linux releases deepOfix Mail Server, a one-of-its-kind product. deepOfix has been developed without using any propreitary software development platform or software development tool. Our network runs completely on Free Software products. We use FOSS (Free and Open Source) products for testing, linking, interpreting and compiling code, version management, hosting of our servers, managing mail withing our company. I am pleased to know of the many institutions using our product, many of who have good things to say about it. The DeepOfix page on sourceforge.net is:

http://sourceforge.net/projects/deepofix

Update: The deepOfix project is now at http://deepofix.org

Thursday, February 08, 2007

Men, Ideas, Computers and Society

Thomas Jefferson, third President of the United States of America wrote a letter to Isaac McPherson in 1813 regarding an 'idea'. He lived in a time of many intellectual inventions. Inventors often sought patents and Jefferson wanted to communicate to McPherson that an idea should be available to all, for mutual benefit. This is (part of) what he had to write:

"If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it. Its peculiar character, too, is that no one possesses the less, because every other possesses the whole of it. He who receives an idea from me, receives instruction himself without lessening mine; as he who lights his taper at mine, receives light without darkening me."

Today, the computer has been long since invented, streamlined and brought to many desks. People in countries all over the earth use computers - many have prepared documents using word processors, presentation software and so forth. Many programmers develop software and many organizations use software and also earn revenue from developing software.

In this article, I wish to write about my moral perspective on free and open source software and proprietary software. The human mind is the place where every invention starts. A person works very hard for a period of time and tries many methods, until finally (s)he comes upon a solution. Where does this solution come from and how is it shared fairly with all? This article is not a final word, or even one I expect the reader to accept. Yet, I hope it makes for interesting reading.

What makes us human? Many animals live on our planet and have many capabilities. But we are social by nature - we share and live together in families. What we own is not entirely ours. For example, if I wear a shirt, the idea of making thread was given to one person. He shared it and many know how to make thread. Similarly, many know how to weave thread into cloth. This idea too came from some person. Cloth making and computer manufacturing may have started not centuries, but millenia apart. Yet, we remain people in need of mutual help.

Now, if every person in the world believed in free software, believed that free software was going to solve many problems, this is positive, but not ultimate. From a moral perspective, we ask ourselves whether we have given to poorer people dignity and opportunity to grow. If we give complete liberty to our children, for example, would they learn and live by principle? How are we to know whether they would use freedom justly and fairly and be fair and just for others? People in a ravaged country would be conscious of the possible ills of a morally deficient society. People who have not felt the pain of certain kinds of deprivation may not grow up with this social sensitivity.

Ultimately, proprietary and open source software models are in the hands of people. If a software developer is sensitive to the poor man near his house, he wants to help him. The poor man has a great dignity as a man, he can have other treasures than wealth - virtue. When justice is not yet established in a land or society, its people suffer and there is need to change. If reform is initiated by people willing to take the risk and offer themselves for it, there may be improvement until possibly justice and peace reigns. If a land or a society enjoys justice and peace, it can be a nursery of mutual respect and growth. Yet, this can stagnate a society because the social interdependence which brought the people to this good time can be forgotten. People can become aloof to the needs of the underprivileged.

Virtues - charity, peace, desire to share, all stem from man's goodness. Social evil comes because of man's passion - greed, prejudice, selfishness. Yet, this is not only a societal problem, but a personal one too. Morality of a society is a necessary precursor of all good things which come later. History has shown that where there were greedy or power hungry people - those who caused many to die, these could not last the test of time. Men may have been feared because of their brutality - even if they were geniuses at military conflict, they did not leave a moral quality that can remain the strength of a society. Morality and ethics therefore are facets of human development that, though different from culture to culture, are a common need for all of us. Only in this way, I can trust you when we work together, or even play a game of football together. Only in this way can we build something that we cannot build individually.

Wednesday, January 31, 2007

Spiral using ncurses

I tried to write a program to draw a spiral using the ncurses library - one which draws slowly in a way which can be seen noticeably. I first analyzed the number of characters to be drawn in each direction - down, right, up and left.

I prepared a table having the number of characters in the outer spiral of the spiral design. I used a variable, m, to hold the number of characters in a particular direction. I programmed m to change according to the mathematical pattern which we can notice in the table.


+----------+----+-----+------+--------+
| n | 1 | 2 | 3 | 4 |
| down | 4 | 8 | 12 | 16 |
| right | 3 | 7 | 11 | 15 |
| up | 2 | 6 | 10 | 14 |
| left | 1 | 5 | 9 | 13 |
+----------+----+-----+------+--------+

I leave the code for the interested reader here. I implemented this using C with the help of the ncurses library for moving the cursor in the relevant directions before printing the character.

#include <curses.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
int x, y, n, i, m, dx, dy, j;
int ay[] = {1, 0, -1, 0};
int ax[] = {0, 1, 0, -1};
int am[] = {1, 1, 1, 1};

char s[80];

n = atoi(argv[1]);
m = n * 4;

initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);

x = 0;
y = 0;

while (m > 0)
{
for (i = 0; i < 4; ++i)
{
dx = ax[i];
dy = ay[i];
for (j = 0; j < m; ++j)
{
x += dx; y += dy;
mvaddstr(y, x, "X");
}
system("sleep 1");
refresh();
--m;
}
}

system("sleep 1");

endwin();

return 0;
}


The program, when run, should draw the pattern arm by arm or the spiral, i.e, first downward, then rightward, then upward and then leftward. The following is the output for n = 3:



X
X XXXXXXXXXX
X X X
X X XXXXXX X
X X X X X
X X X XX X X
X X X X X X
X X XXXX X X
X X X X
X XXXXXXXX X
X X
XXXXXXXXXXXX

Tuesday, December 13, 2005

Cows and Bulls

Cows n Bulls is a game played by any number of players. In the game, a player chooses a 4-digit number (digits may repeat). The chosen number is written down and placed aside. The other player tries to guess the number which the first player has chosen. The player 2 makes attempts (guesses) and the player 1 should give feedback in terms of number of hits and misses.
  1. A hit means that the number is in the correct position.

  2. A miss means that the number is present but in a different position.

The goal of the game is for player 2 to guess the number which player 1 thought of.

Throwing in the computer

Now, we bring the computer into the picture. The computer should ask the player to guess a 4-digit number. The computer should guess the number using the information of hits and misses. The question is how can we program the computer to guess the correct number using only the information provided by way of the hits and misses counts.

Monday, October 24, 2005

Windows Command Prompt

Windows Tip :: Add Command Prompt Option to Every Folder

Open your registry and find the key below.

Create a new sub key called 'Command' under the main key (i.e. [HKEY_CLASSES_ROOT\Directory\shell\Command]). Change the value of '(Default)' within the key to equal the text you would like on the right-click menu, for example 'Command Prompt'.

Create another new sub-key under the key created above, named 'command' (i.e. [HKEY_CLASSES_ROOT\...\Command\command]). Change the value of '(Default)' within this key depending on your operating system to equal either:

Windows 95, 98 or Me
command.com /k cd "%1"

or

Windows NT, 2000 or XP
cmd.exe /k cd %1

I got this from this link:

http://www.winguides.com/registry/display.php/260/

Monday, October 17, 2005

Two Challenging Database problems

Here are two interesting problems using databases. I have given the table structures and the requirements too.

1. Cumulative Statistics Problem

In the game of cricket, we have several statistics. One of them is the runs scored per over. We have a table in a database in which we store two pieces of information.
  1. Over
  2. Runs
Now, we need to display 2 pieces of information
a. Runs scored per over
b. Runs scored in the last 5 overs.
We need data in 3 columns
  1. Over
  2. Runs this over
  3. Runs last 5 overs
How do we do this?
It is possible using only one SQL SELECT command. You may call the table Stats.

2. Insert cum update requirement

In the same situation above, 2 people do the task of recording the game statistics. One person manages the main database. The other person gives online information, which is stored in a temporary database (can be a temporary table instead - implementation is arbitrary).

For example, the DB manager stores information about 7 overs - overs 1-7. The recorder collects information about last few overs. The recorder may submit this information at any point of time, even between an over. But once he submits the runs of that over, he starts afresh.

For example, if 3 runs were scored in the first 3 balls of the 7th over, he sends this information - 3 runs in the 7th over.

Now, there is a drinks break. What he has already sent is of the over till now. Next, he starts collecting information again for the same (seventh) over.

Now, there are 5 runs in the last 3 balls of the 7th over. He notes down this information - 5 runs in the 7th over (the first 3 balls are already sent so he ignores the runs scored then). Next, he may start recording the 8th over. Now, if 6 runs are scored in this over, he will have this stat - 6 runs in the 8th over.

So he sends this information to the database manager - 6 runs in the 8th over. At this point, he sends information of the latest collection since the last sending. This is 5 runs in the remainder of the 7th over + 6 runs in the 8th over.

The database manager should be able to keep the database up to date. New records may be added for each new over. For the overs already recorded, the remainder scores may be aggregated and updated in the main database.

How would you manage this?

Tables in both database have the same format - (Over, Runs)

This is possible using the standard SQL commands like SELECT, INSERT, UPDATE

Answers:

1. The problem seems deceptively simple because it is stated so easily. The challenge is to do it in just one statement. In my solution, I have taken the same table twice, joining it with itself.

SELECT
s1.Over,
s1.Runs,
SUM(s2.Runs)
FROM
Stats s1
INNER JOIN Stats s2
ON s2.Over <= s1.Over AND s2.Over > s1.Over - 5
GROUP BY
s1.Over,
s1.Runs

2. Suppose the database names were DBMain and DBAux and the table names were Stats, one concept I would use is to aggregate the Runs values using SUM and GROUP BY Over to find the resultant table.

BEGIN TRANSACTION

INSERT INTO Temp(Over, Runs)
SELECT
Main.Over,
Main.Runs + Aux.Runs
FROM
DBAux.Stats Aux
INNER JOIN DBMain.Stats Main
ON Main.Over = Aux.Over

DELETE FROM DBMain.Stats
WHERE
Over
IN (
SELECT
Over
FROM
Temp
)

INSERT INTO DBMain.Stats
SELECT
Over,
Runs
FROM
Temp

END TRANSACTION

Of course, it may be possible that there is a less expensive way of doing the above. I tested the above using a MySQL database.