Sun Microsystems released Java to the public in 1995 when Netscape released a web browser capable of running Java Applets. James Gosling led a team to develop a new language called Oak. The name had to be changed and Java was born. In 1996, Java was all the rave and I wanted to be part of it. Netscape was the Java Runtime Environment then, and java.applet.Applet was the library object allowed your Netscape browser to run code stored on the server. Brendan Eich was working with a team at Netscape to adapt Scheme into client-side programming when Netscape made an agreement with Sun. Thus came JavaScript, considered to be a companion to Java. Sun Microsystems, now bought out by Oracle, has made Java the scalable monster it is today. JavaScript took a separate path, but remains supported by the remains of Netscape and the Mozilla Foundation today.

I wrote two Java Applets in 1996, in which a printout for one of them still exists. Much of this code is likely deprecated today and since Google Chrome stopped supporting Java Applets, there is no effort to rehash this in Java. But there is PyGame, hence an opportunity to translate it and make it work again.

The game is the classic Concentration, where two tiles are turned over to reveal objects. If the objects match, they remain turned over and points are added, but if they don’t match the objects are hidden and points are taken away. The objects were circles and crosses in red, green and blue. The program constructed all of the images, but today I would use PNG graphics instead. My old website offered this game and a tile slide puzzle for anyone who wished to play, perhaps a forerunner to today’s tablet apps that wastes away the time every day. I was proud of these games, but wished to not seek Java programming at the time. Less than ten years later, I’d be writing Java for a living. Today, I get opportunities to be a Java Developer again, but this time I wish to work with Python, PHP and JavaScript only.

Looking at the code, nothing has changed in how Java is structured. I haven’t worked with the Graphics library since then and don’t know if it still exists. Here’s the code I wrote to display the game:

public void paintNew(Graphics g) {
    fm = g.getFontMetrics();
    g.setFont(f);
    width = size().width;
    height = size().height;
    s = "2f4f4f";
    w = size().width / 6;
    h = size().height / 6;
    c = 1;
    color = toColor(s);
    g.setColor(color);
    g.fillRect(0, 0, width, height);
    for(int i = 0; i <= 5; i++) {
        for( int j = 0; j <= 5; j++) {
            x = j * w;
            y = i * h;
            square(x, y, w, h, g);
            drawProportionedString(c, x, y, w, h, g);
            c++;
        }
    }
}

As you can tell, my code disregarded making it legible for someone else. Today, my variable names would reflect the variable’s purpose and I would comment throughout. This is just a sample of a game surface with 36 square areas. There is a local method call to drawProportionedString that I’ll list next:

public void drawProportionedString(int psc, int psx, int psy, int psw, int psh, Graphics g) {
    avg = ((psw + psh) / 2) / 2;
    xc = (psw - fm.stringWidth("" + psc)) / 2;
    yc = (fm.getAscent() + (psh - (fm.getAscent() + fm.getDescent())) / 2);
    sx = psx + xc;
    sy = psy + yc;
    g.drawString("" + psc, sx, sy);
}

The variable c is the number between 1 and 36 that is printed in each of the 36 squares, proportionately. That’s really what all this code does. From this, though it’s revealed how the game works graphically. All of the objects that are assigned these x, y coordinates are drawn when needed, using the same x, y, w and h values, making it look like they are hidden by the numbers. Although the class is called Animate, nothing is really animated here.

The Concentrate class is where all of the logic exists. This is the engine of the game where Animate is an extension. An init() method displays the game and the run() method acts as a listener, waiting for mouse events in order to do something. The local method tableCreate() generates the randomness of the hidden objects for each game:

public void tableCreate() {
    int seedArray[];
    int newPos;
    int i;
    r = (Math.random() * 1000) % 35;
    int seed = (int) r + 1;
    seedArray = new int[36];
    table = new int[36];
    for(i = 0; i <= 17; i++) {
        r = (Math.random() * 1000) % 6;
        ran = (int)r;
        seedArray[i] = ran;
        seedArray[35 - i] = ran;
    }
    for(i = 0; <= 35; i++) {
        newPos = calcSeed(seed, i);
        table[newPos] = seedArray[i];
    }
}

If you’ve noticed, I had to insure that an there were always a pair of objects to match. This is accomplished by using the random seed number between 0 and 17 to place similar objects in the first half and the second half of 36 objects (i and 35 – i). variable r is a global double and ran is a global integer. Today, I’d refrain from using global variables. A local method calcSeed(int seed, int ran) is called to assure that the random number remains between 0 and 35:

public int calcSeed(int seed, int ran) {
    int solution = ran + seed;
    if(solution > 35) {
        solution -= 36;
    }
    return solution;
}

This is an example of my earliest Java coding. It’s not pretty, but it worked well and I miss those days. I have a lot of respect for game developers; my stuff barely scratches the surface and perhaps I was meant to be a web developer. Now to make this work again. I’ll write about it when it’s ready. If I can, I’ll make it web-friendly. I must write about another topic on Sunday. This blog was meant for web development, but for the purpose of the challenge, it’s about all of me. Four more to go…

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.