The idea it was named after

Thursday, October 13, 2005

Sudokizer-first-release disclosed

As I told you in my last entry, Sudokizer is a program that generates and solves sudokus thhat I'm developing as a pet project in order to strengthen my programming skills in orders to tackle a bigger project, AVD (Aeromodels Virtual Designer).

Today I have completed the minimum functionality so I have decided to unveil the first version of this program so that some of you sudoku-addicts can test it. WARNING: If you are not a sudoku addict, then don't even try it! Sudokus are much like smoking; you cannot stop doing it after some tries, but in this case the addiction is nearly instantaneuos and no initial cough is needed.

Introduction

The algorithm used to solve sudokus is very elemental and can only solve those of them containing strictly Single Candidates and/or Unique Candidates. My aim is not to become a sudoku guru but rather to improve my C++ programming skills. But don't worry, because most of the sudokus found in the newspapers are of those types ;-).

The algorithm to create Sudokus uses ctime, srand() and rand() functions for the pseudo-random number generator, and it creates sudokus with the requested number of filled cells, and the rest in blank. When it find one it can solve, then returns it, so it will return relatively easy to solve sudokus.

Both things -the solver and the generator - are encapsulated in a pair of cpp + h files, and the executable by now is only a show case; it creates a random game, shows it, and if you press <Enter> shows the solved one. Finally, if you press <Enter> one more time the program exits. Of course, each time it you execute it sudokizer shows you a different game!

Installation

Currently it's only available in SVN and it has only been tested in Linux so far. It doesn't compile in Windows because I don't even have it installed in my system and I call to clear command meanwhile Windows has cls, but probably once sduokizer gets a nice Qt user interface I will make it compile in that platform. It should compile and install in any system with a compiler and with scons installed. The download command is:

svn checkout svn://svn.berlios.de/avd/trunk/others/sudokizer

Read the INSTALL file for installation instructions, and enjoy it ;-).

Sunday, October 09, 2005

Yakuake, a terminal a key far away

I tend to use the command line all the time, for building my small programs, installing software, doing small checks, etc. Most of that daily task could be accomplished by a GUI replacement, but I feel more comfortable using the cmd because it turn to be usually faster to do it, if you already know how to.

Because of that, I used to have a konsole instance in each virtual desktop I was working on, each one with a couple of tabs or more. But now I don't use konsole anymore, because I have found a replacement which feels much natural to my patter usage of the command line. It's called Yakuake.

Yakuake is similar to konsole because it's an X terminal for KDE, which supports multiple tabs, etc. But it's the particuliar way to work with it what is appealing to me. When you launch it, it keeps hidden, and a popup tells you that you can show/hide it pressing F12. When you press that key, a terminal appears from the top of the screen covering 50% of it. It's very similar to Quake terminal, and hence it's name.

Now only have one terminal window, that doesn't eat space in the taskbar, always just one key away, and available in any virtual desktop! I recommend you to give it a try :-).

Tuesday, October 04, 2005

References vs Pointers and the Double Edged Sword

In order to test some nice things like doxygen, scons and to improve my C++ skills, I'm developing a pet project called "sudokizer" that will resolve sudoku games. I'm aware that there are already many libraries that achieve that goal, but as I alreadytsaid, it's just a pet project. I don't know about many standard libraries and I wanted to develop things by myself, so I created my own List interface, Linked list implementation (i.e. for the cells/squares in the Board), Iterators (i.e. to go through a list of cells), etc.

While doing that, I run into some C++ annoyances using references. References are not suitable for doing as many things as I supposed to. Apparently the problems go all around lvalues. An lvalue is "anything that can be on the left side of an assignment". Sometimes, it's better just to use a pointer, even if at first sight a reference could theoretically do the job. The main problems I found are:

  • You cannot assign a reference to zero

  • This is a major drawback to me. Of course that behaviour is well justified and in fact it's one of the main advantages of references because it generally makes code less error prone. But it is also a double edged sword.

    Imagine you are developing a class for linked lists called List. As private members, you have a reference to the current object (TIP: use templates for that ;-) and a pointer to the next element (which is of the type List so that you can continue indefinitely the list). You might think that you could use a reference instead of that pointer, but in reality that would make things harder. The standard method in linked lists is that the last element has the next private member set to 0, so to check if the list ends in the current element is as easy as comparing that pointer with zero. You can't do that with a reference, so you would need to find other solutions, being having a bool flag to control that the most obvious approach. This and other solutions just add complexity to the problem and the benefits of using a reference are just not worth the effort.

  • You cannot initialize a reference by calling its type constructor in a single sentence


  • Basically, you can't do this:


    Car& myCar = Car("Ford T"); // compile error


    But you would need to write:


    Car temp = Car("Ford T");
    Car& myCar = temp;


    This also means that you cannot initialize the reference in a constructor initializer list, so instead of writting the constructor as usual:


    class A {
    public:
              A(int i) {}
    };

    class B {
              char* myString;
              int myFlag;
              A& myA;
    public:
              B(int j) : myString(""), myFlag(0), myA(A(j)) {} // compile time error
    };


    It makes the constructor larger, more dirty because you have members intialization in two different places (you could not use the constructor initializer list, a method which is not very appealing either):


    class B {
              char* myString;
              int myFlag;
              A& myA;
    public:
              B(int j) : myString(""), myFlag(0) {
                        A temp = A(j);
                        myA = temp;
              }
    };


    This is also because of the magical lvalues as happened in the previous problem, and it has its technical justification as well, but it's still something strange for those learning the language. Using pointers, you could just do:


    Car* myCar = &Car("Ford T"); // works fine!

    class B {
              char* myString;
              int myFlag;
              A* myA;
    public:
              B(int j) : myString(""), myFlag(0), myA(&A(j)) {} // works fine!
    };



Conclusion: be careful when using references, because perhaps that's not what you really want to use ;-).

Saturday, October 01, 2005

Egoism theory

My brother FĂ©lix once formulated a theory around egoism. The theory states that everything we do have an egoist side.

An easy way to test theories is trying to find cases that invalidate them. One could argue that mother Teresa of Calcut could be a well-known proof where this theory would fail, because she did her labours for charity, clearly in a non-egoist way. Nevertheless, it turns out that Teresa of Calcut not only doesn't invalidate the theory, but she represents actually a non-critical and easy case where it's confirmed. I will explain this in a later in this entry, but now we'll try to understand better the reasoning behind this theory.

If you offer help your little sister in mathemathics, it makes you feel better with yourself or at least it's supposed to. It can be generally stated that there's a reason behind every human act. So actually you do it because of something. If instead in such situation you don't offer help to her, you wouldn't have your conscience clear. The reality is that when you perform an altruistic action it's because you need or want to achieve an inner goal, even if you finally don't manage to achieve it. The altruistic task is not an end for itself, it's a way to achieve mental sucess, a mental state. You do it for yourself. It's actually, in that sense, an egoist action but in a deeper level of your mind.

The case of Teresa of Calcut is a simple example because the reasons behind her behaviour were religious. Some people do a Yihad in order to go to Paradise - an atrocious simplicity for the sake of the argument -. Others provide help to people who need it also because it's the task God gave them. Probably Teresa decided that it was better to perform that task among many other easier things she could have done despite she thought it was god will, in any case there lies also a type/level of egoism in the reasons behind the decisions, as was argued in the previous paragraph.

A curious finding that comes from the egoism theory is that selfishness is not always bad - apply here your favourite definition of the concept of wickedness -, because otherwise everything would be bad and if so, it's not likely that we would have a definition for good. So of course there are measurable differences between doing the Yihad and helping people, even though both activities are driven by - different levels of - egoist ideas, just as all of human actions according to this theory.