Archive for the ‘chessbox’ Category

C to Java

Tuesday, May 10th, 2005

I’ve almost finished the mostly-mechanical first pass of converting the C GNU Chess code to Java syntax. I haven’t tried to compile yet, but there’s about six files out of thirty-plus that IDEA (world’s greatest IDE) says contain syntax errors. Most of them are complicated printf() calls that I put off, thinking I might just start using Java 5, which comes with a printf() of its own. Also remaining are some threading issues like the use of the signal() function.

The most common C features in C GNU Chess that needed converting to Java:

primitive typedefs
better readability and type-checking by using BitBoard instead of long. I started using Java classes for these typedefs, but BitBoard operations much be fast, and I’ve have to deal with translating between the different semantics for assignment of primitives and objects.
macros
most were just constants; I made functions for most of the others, except a few like CLEARBIT that seemed easier to inline.
primitive reference parameters
I used the return value if possible; otherwise I passed the params in an array.
printf and scanf
only a few of these we hard to convert; Java 5’s printf will help
enum
something else that’s available in Java 5
implicit bools
I can’t count how many times I converted “if (c)” to “if (c!=0)
goto
Java doesn’t have goto, but it does allow labels on break and continue, which sometimes resulted in creating dummy loops just so I could use a labeled break instead of a goto.