Perl: normalizador de ficheros / files normalizer

espa?ol Perl, normalizador de ficheros: Un script en Perl para normalizar el formato del contenido de ficheros de texto: saltos de línea (windows/unix), tabulaciones, espacios al final de línea y saltos de línea al final de fichero.
(más)

espa?ol Perl, files normalizer: A Perl script for normalization the format of the content of text files: new lines (windows/unix), tabulations, ending line white spaces and ending file new lines.
(read more)

Hanoi Towers 64 Gadget


Add to Google

Adaptation of Hanoi of Towers with 64 discs to gadget.

Customizable, in elements and appearance.

As a curiosity, if you set the gadtet with an interval of 989 milliseconds and 15 discs, the completion takes 9 hours, that is, the duration of a normal job day of 8 hours (plus one hour for lunch).

Hanoi Towers with 64 disks

There are several versions of the legend, but all of them say that the end of the world will be reached as soon as all the disks are moved from the source peg to the destination one. Then, the following Java implementation of the Hanoi Towers in an applet with 64 disks can be considered a count down to the end of the world, :).

The puzzle consists in moving all the disks from the source peg to the destination one. Only one disk can be moved in each movement, and a bigger disk can’t ever moved on a smaller one.

The minimum number of movements needed for n disks are 2n-1, the, with 64 disks and moving one disk each second, the puzzle solution would last 585.442 billions years (calculus fromWikipedia).

This would be the actual position of the disks if the execution would have started on 1883, when the puzzle was ivented by Édouard Lucas.










Oh Mummy gadget



Add to Google

Adaptation of Oh Mummy! to gadget.

The classic Oh Mummy in a gadget. Use the arrow keys for walking all around the blocks until finding the 2 green blocks and then pass to the next level coming back to the initial position. Add it easily to your iGoogle with the Add to Google. button.

It has been changed from the original one. The mummies AI and velocities have been addapted. The game is divided in phases, and each phase consists of 5 miniphases; any time you continue the game, you will start from the beginning of the phase you are.

Subset

Given a set S, what are their subset?, what are their k-subset?. I’ve compiled several Java methods which iterate over these subset. Due to its low efficiency, this technique only should be used in brute force algorithms.

Contents

Notation
All the subset
All the k-subset
Auxiliar methods
References

Notation

S is the set with elements {n1, n2, n3, ... nn}. The number of elements of S is |S|=n.

K is the subset of S {k1, k2, k3, ... kk}, where ki belongs to S and ki doesn’t represent the same element S that kj does, with 1<=i,j<=k. The number of elements of K is |K|=k and takes values from 0 to n.

The subset of S will be represented byt set of n elements of {0, 1}, where 1 means belonging and 0 means not belonging. The number of elements of the suset |K|=k is the number of 1's that it has.

(contents)

All the subset

All the subset of S have subset from 0 elements to n. There are 2n of subset.

All the subset.

|S|=3

K0={0,0,0}
K1={0,0,1}
K2={0,1,0}
K3={0,1,1}
K4={1,0,0}
K5={1,0,1}
K6={1,1,0}
K7={1,1,1}

allSubsetBit(int) (iterative)

/**
* Iterates over all the subset of a given set of n elements.
*
* The maximun number of elements of the given set is 63.
* Subset are represented at bit level, 1 means belonging,
* and 0 not belonging.
* @param n Number of elements of the set, 0<=n<=63.
*/
public static void allSubsetBit(int n) {
for (long i = 0, lim = 1L << n; i < lim; ++i) {
printSubset(n, i);
}
}


allSubsetArrayIterative(int) (iterative)

/**
* Iterates over all the subset of a given set of n elements.
*
* The size of the set doesn't have any restriction, but
* the memory heap size.
* The subset is represented by an array of booleans, where
* true means belonging, and false means not belonging.
* @param n Number of elements of the set, n<=0.
*/
public static void allSubsetArrayIterative(int n) {

boolean[] subset = new boolean[n];

while (true) {

printSubset(n, subset);

// Add 1
int i=0;
do {
subset[i] = !subset[i];
++i;
} while (i<n && !subset[i-1]);

if (i>=n && !subset[i-1]) break;
}
}

allSubsetArrayRecursive(int) (recursive)

/**
* Iterates over all the subset of a given set of n elements,
* recursive version.
*
* The size of the set doesn't have any restriction, but
* the memory heap size.
* The subset is represented by an array of booleans, where
* true means belonging, and false means not belonging.
* @param n Number of elements of the set, n<=0.
*/
public static void allSubsetArrayRecursive(int n) {
boolean[] subset = new boolean[n];
allSubsetArrayRecursion(n, subset, 0);
}

private static void allSubsetArrayRecursion(int n, boolean[] subset, int i) {

if (i < n) {
subset[i] = true;
allSubsetArrayRecursion(n, subset, i + 1);

subset[i] = false;
allSubsetArrayRecursion(n, subset, i + 1);
} else {
printSubset(n, subset);
}
}


(contents)


All the k-subset

All the subset of S with k elements. The number of k-subset is:



n!

|k-subset|=
(n-k)!·k!

All the k-subset.

|S|=5
k=3

K0={0,0,1,1,1}
K1={0,1,0,1,1}
K2={0,1,1,0,1}
K3={0,1,1,1,0}
K4={1,0,0,1,1}
K5={1,0,1,0,1}
K6={1,0,1,1,0}
K7={1,1,0,0,1}
K8={1,1,0,1,0}
K9={1,1,1,0,0}

The following code has been taken from the book Hacker's Delight by Henry S. Warren Jr. It is a bitwise trick for getting the next greater number than one given with the same number of 1’s in binary representation.


allSubsetKBit(int,int)

/**
* Iterates over all the subset of k elements in a set of n
* elements.
*
* The maximun number of elements of the given set is 63.
* k must be 0<k<=n.
* Subset are represented at bit level, 1 means belonging,
* and 0 not belonging.
* REF: Warren, Hackers Delight, p.14.
* @param n Number of elements of the set, 0<=n<=63.
* @param k Number of element of the subset, 0<k<=n.
*/
public static void allSubsetKBit(int n, int k) {

long subset = 0;
// Set 1 k first bits
for (int i=0; i<k; ++i) {
subset |= 1<<i;
}

long limite = 1<<n;

long smallest, ripple, ones;
while (subset < limite) {
printSubset(n, subset);

// From Hacker's Delight
// Next greater number with the same number of 1's.
smallest = subset & -subset;
ripple = subset + smallest;
ones = subset ^ ripple;
ones = (ones>>2)/smallest;
subset = ripple | ones;
}
}



allSubsetKArrayIterative(int,int)

/**
* Iterates over all the subset of a given set of n elements,
* iterative version.
*
* The size of the set doesn't have any restriction, but
* the memory heap size.
* k must be 0<k<=n.
* The subset is represented by an array of booleans, where
* true means belonging, and false means not belonging.
* @param n Number of elements of the set. No restrictions.
* @param k Number of element of the subset, 0<=k<=n.
*/
public static void allSubsetKArrayIterative(int n, int k) {
boolean[] subset = new boolean[n];

while (true) {

int subsetCount = 0;
for (int i=0; i<n; ++i) {
if (subset[i]) {
++subsetCount;
}
}

if (subsetCount==k) {
printSubset(n, subset);
}

// Add 1
int i=0;
do {
subset[i] = !subset[i];
++i;
} while (i<n && !subset[i-1]);

if (i>=n && !subset[i-1]) break;
}
}

allSubsetKArrayRecursive(int,int)

/**
* Iterates over all the subset of a given set of n elements,
* recursive version.
*
* The size of the set doesn't have any restriction, but
* the memory heap size.
* k must be 0<k<=n.
* The subset is represented by an array of booleans, where
* true means belonging, and false means not belonging.
* @param n Number of elements of the set. No restrictions.
* @param k Number of element of the subset, 0<k<=n.
*/
public static void allSubsetKArrayRecursive(int n, int k) {
boolean[] subset = new boolean[n];
allSubsetKArrayRecursion(n, k, subset, 0, 0);
}

private static void allSubsetKArrayRecursion(int n, int k, boolean[] subset, int subsetElements, int i) {

if (i < n) {
subset[i] = true;
allSubsetKArrayRecursion(n, k, subset, subsetElements+1, i+1);

subset[i] = false;
allSubsetKArrayRecursion(n, k, subset, subsetElements, i + 1);
} else {

if (k==subsetElements) {
printSubset(n, subset);
}
}
}

(contents)


Auxiliar methods

These methods do a function with the subset given by the methods above. In this case, they print the subset. These are the methods that need to be changed for changing the behaviour.

printSubset(int,long)

/**
* Prints a subset represented with the bits of a long.
*
* @param n Maximum number of elements of the subset.
* @param subset Bit representation of the subset.
*/
private static void printSubset(int n, long subset) {
for (int j = n-1; j >= 0; --j) {
System.out.printf("%d", ((subset & (1L << j)) != 0) ? 1 : 0);
}
System.out.printf("%n");
}

printSubset(int,boolean[])

/**
* Prints a subset represented by an array of booleans.
*
* @param n Maximum number of elementos of the subset.
* @param subset Subser represented by an array of booleans.
*/
private static void printSubset(int n, boolean[] subset) {
for (int j = n-1; j >= 0; --j) {
System.out.printf("%d", (subset[j]) ? 1 : 0);
}
System.out.printf("%n");
}

(contents)

Referencess

"Hacker's Delight", by Henry S. Warren Jr.
"k-subconjuntos.".

(contents)


Hano Cup

It is my customized java framework based on and compatible to Java Cup 2007 competition. It provides an interface to simulate football (soccer) matches between programmed teams that implement 'Tactica' interface. I have added several useful characteristics to program new teams. Details about this project are here. It includes an auto-executable jar file and an Eclipse project with the sources.

OH MUMMY!

Play it here!

Classics are back, and I wanted to pay tribute to one of the first games I had, the mythical OH MUMMY. This is a new entry for the J4K 2007 Contest, so, its size is less than 4K.
Any comment or suggestion is always welcome.


Introduction

It is a long time ago when I had my first computer, an Amstrad CPC 464. Now, its 4Mhz and 64K of RAM seem to be tiny, but thanks to it, with BASIC as programming language, I started to like programming.

Then, games are loaded from a cassette, and loading last very long. Bugs were really annoying, because you had to load the complete program (about 30-60 minutes) to realize that it didn’t work

One of the first games I got was Oh mummy!. It was funny trying to deal with more and more mummyes. I’m sure that if you had played this game, you remember its song; you can find it here.

How to play

The objective is to find the two objects that allow you to pass to the next level. In the original game, the objects were a key and a kind of sarcophagus, but due to the size of the final jar file has to be under 4k, I had to substitute them by green blocks. For knowing what a block hides, you have to walk all around it.

For passing of level, after finding the two green blocks, you have to go back to the initial position.

Be careful with the mummies. They look slow at the beginning, but the are cleverer and cleverer in each new level.

Controls are the direction arrows.

Source code

Finally I submitted the source you can download it at ohmummy_src.zip.

The code is optimized for 4 Kbytes, and I had to apply my own compression format to the sprite image to mimimize its size. Compression:
  • 1 bit for transparency
    • 0 = transparent
    • 1 = colored
  • If colored, 2 bits for the color
    • 2 bits = index in the color palette 0-based


OriginalCompressed

6.966 bytes
418 bytes



Billard4K

The objective is to introduce all the red balls into the holes, hitting them with the white ball. To hit the white ball, we have to use the stick when all the balls on the table are stopped.

To start to play, there is to click on the screen. Each hit follows these steps:

  1. Direction: moving the mouse, we indicate where we hit the white ball from.
  2. Strength: when we have the direction, click and hold the button pressed, the strength is proportional to the distance between the mouse and the white ball; to hit the ball, release the button.
  3. Wait until all the balls are stopped.







This game was submitted to the J4K 2007 Contest.

Download the code here:
Source