Refactoring Complete

I had previously coded up a bunch of library code for my roguelike game engine. My first idea was to hold all items in a big array. When the items were picked up and them wielded, the item state would change. Then I had a bunch of custom code that switched on the state.

The problem was that most items get removed and regenerated during change of dungeon level. I did not want my players inventory and equipment to disappear when the dungeon level changed. So I needed a refactoring. I decided to create separate containers for the player inventory and the player equipment. Today I refactored all the code to use this new design.

I was pleased to see that much code was simplified. Many functions were no longer even needed. The code was getting really clean. This must mean the redesign was a move in the positive direction. This shall also allow easy changes of inventory/equipment rules in the future.

Not sure what I am going to do next. I am still riding a high of a good code refactoring. It is probably time to implement some more functionality in the dungeon.

Storing Items

I am building a library to help with the development of a roguelike game. Got the maze generation down pat. Next I added items such as weapon and armor to the maze. I implemented these items in a big array. The items knew where they started out in the dungeon.

As the player picks up and wields the items, the items change their location. This was working like a champ. Then I realized the items were tied to the dungeon. The dungeon gets regenerated on each level as the player walks up and down the stairs. Ooops.

I don't want the players to lose the items in their inventory or on their body when they navigate the dunegon levels. What I should have done is to separately manage the dungeon objects and the player inventory/equipment. Then I could move the items from each of these containers as the player picks up or discards the items.

This design change will require some refactoring. While I am at it, I might as well change the containers from arrays to lists. That will make the adding and removing a whole lot easier.

Maze Mechanics

Every year I enter a contest to see if I can create a game in seven days. It must be a roguelike game. Usually I write it in a language that I am comfortable in such as C++. I only tried writing my game in Java once. It was disappointing to say the least.

I learned a lot of Java over the past over the last few months. I am ready to try my hand at a Java roguelike again. But first I thought I would practice with some roguelike library code. You know. I need to knock out some mundane stuff to be proficient for the real game design.

Right now I am wrapping up the basics of maze drawing. I got some rooms connected by halls. Good stuff. The only problem is that the user can navigate around the maze. When I get too close ot the side of the maze, I have some coding problems.

The maze display is based on an array of BufferedString objects. I just index into them based on where the player is on the maze. I keep the player in the center of the screen. So when the player reaches the edge of the maze, we are faced with drawing cells that do not exist in the StringBuffer.

Now an elegant solution would be to detect this and somehow append some virtual walls outside the edge of the maze. I might still opt for that grand solution. For now I am trying to hurry up and get stuff done. So I extend the edges of the maze in all directions to create an outside buffer. This ensures you can use the same dumb code to index into the BufferedStrings even when at the edge of the navigatable maze. Nice trick huh?

The Outstanding Breakout

My advanced Java community college class did not have a final exam. Instead we needed to submit a final project. The instructor came up with a couple ideas for projects we could do. He also said he was open to other ideas upon his approvel.

Initially I thought I might develop an electronic football game. Nothing like Madden. This would be a clone of the old Mattel hand held electronic football games from the 70's and 80's. Then I saw the idea of writing the arcade game Breakout and I knew I had to do it.

The funny thing about the final is that most students opted to do the mortgage calculator. That has the advantage of being easy to do. The instructor provides the GUI and the equations to figure out mortgage payments. The student just needs to implement the equation and hook it up. Boring.

The basic breakout game was pretty easy to implement. I had already coded some exercises with balls bouncing around the screen. I concentrated on my intro and game over screens. I alsmot used this game as a place for some demo graphics on those auxiliary screens. I also concentrated on game play. Another student was excited to play my game and give me UI feedback. In the end I spent a lot of time developing this game. My instructor called is amazing and outstanding.

The real win from this game is the stuff I learned from doing it. It is now a part of my JAva portfolio. Yeah I could have done the mortgage calculator. But where's the glory in taking the easy way out and cheating myself?

Maze Design

I am struggling on how to internally represent my roguelike maze. Currently I maintain a 2D array as well as an array of StringBuffers. The array is used for easy lookup of objects in the maze. The StringBuffer is used to quickly generate a string to draw on the screen. The problem is that I need to update both structure every time something moves in the maze.

I could just keep the 2D array. Then I could generate the Strings on the fly as I need to display. However I do a lot of display updates (whenever the player moves). That might take a lot of time to generate an 80x25 worth of character Strings.

Or I could just keep everything in the String array. Then I could index into it when I need to check different cells of the maze. That just does not feel as natural as the separate 2D array though.

Breakout Performance

I coded up an epic game of Breakout. Most of my effort was spent on my intro screen. I also spent a good deal of time on special effects for the high score screen. The thing rocks on my development machine. But I ran the same game on a laptop with limited hardware, and the results were disappointing.

The intro screen draws a lot of random lines to produce a static effect. It also draws text in increasing font sizes that get really big. This screen works but is painfully slow on the laptop with limited hardware.

Luckily the main game mechanics adjust themselves according to the amount of time it takes to render the game screen. So it plays well whether there is high power hardware, or limited resources. I should have done that with the intro screen as well.

JDBC to the Rescue

I just read a chapter on the JDBC. This allows me to access databases from my Java code. Now I have the power to write CRUD apps. Yeah. So far I have written some code that uses Statements to do basic querying. I have also delved into PreparedStatements to insert and update.

To test out and play with this capability, I downloaded and installed the latest MySQL database. It is taking a while to get used to MySQL database administration. I also got MySQL Connector/J so that my JDBC can talk to the MySQL database. So far it works like a charm.

I have studied and modified a little bit of code that uses an AbstractTableModel and JTable. This seems to be quite the powerful combinatoin. It is also something that will take a while to learn. Now the world of databases it at my fingertips. Perhaps I shall try to connect to an Oracle database with JDBC as well.

Towers of Hanoi

In my Java class this semester, we went over the Towers of Hanoi problem during our study of recusion. The recusrive solution is elegant. It is also simpler than any iterative solution. We did not code a solution. We just studied one intently to understand the recursive nature of it.

Recently I saw one of the sample problems you need to solve during a Facebook interview. One of them is the Towers of Hanoi problem. Nice. Facebook even let's you code the thing in Java. It has to work correctly. And you need to complete the thing in 45 minutes.

Not sure if I can meet that metric. But at least I have seen the problem and solution before. I guess I could google the code. That would be cheating. I feel good about what my Java college class is teaching me.

Multithreading

I am just starting to learn multithreading. My second exercise was to put 20 balls on the screen. Each one is moved using a different thread. On the surface I was surprised what little computer resources the 20 threads used. My machine is dedicating 3 percent of its CPU to the bouncing balls app.

I have some nice and smooth animation too. Every frame update causes the whole screen to be repainted. Right now I am using the basic Thread class to start up and Runnable. My main thread, which I assume is the event dispatch thread, updates the GUI on a timer (which happens to have its own thread).

Next I should try a whole lot more threads. Or I could make the graphics more intense. I need to figure out where the performance bottleneck is, if any.

Eating the Keys

I am learning about the finer points of GUI development with Java this week. It turns out there are some GUI basics in Java that I don't know about. So I am studying them first. One topic of interest is catching events. To practive, I started coding up an event viewer. It is supposed to get and print out events that fire based on a bunch of different GUI components.

For the most part I can knock out the even viewer code. I am setting up listeners that log events to the screen. Then I try to do a KeyListener. But my event handlers are not getting called. What the heck? Initially my text area control was displaying the keys. Okay. I disabled it. Still no keyboard events.

I am a good debugger. I traced the issue down to the presence of a JButton on my GUI. If I remove the JButton, the keyboard events fire and I get the calls. What could be going on here? Is the JButton receiving and eating up the keyboard events? It looks like I am going to have to consult an expert here.

The Need for Wildcards

We are covering generics in my advanced Java community college class. The weak book has a tough time explaining concepts clearly. I get the main idea behind generics. Java gets to keep up with C++ templates. But then we get to something which does not seem right.

Since the Integer class extends Number, you can pass in an Integer object when a function requires a Number parameter. That is clear. But when you use those types with a generic class such as Stack, things get weird. You cannot pass in an object of type Stack when a function requires a Stack parameter. What the heck?

Integer extends Number. The Stack is a Stack. Why can't Stack act as a Stack. Makes no sense on the outside. The book cooked up some example to try to explain it away. I was still not convinced. Later I read about the use of wildcards to solve this problem. You get a Stack argument type that can then receive a Stack. But why do we need this new syntax in the first place. The textbook could not explain it. Time to dig deeper.

Collections and Maps

I have been studying up on the Java Collections Framework for the past week. It was hard to get a grasp on such a huge subject. Initially I had trouble figuring out what were the interfaces and what were the classes. With the help of Wikipedia and the Java API documentation, I think I am starting to understand it a little better.

There are really two main things to get here - collections and maps. Collections is the bigger piece. The grandfather interface is Collection (from java.util.Collection). A couple important interfaces extend Collection. They are List, Queue, and Set. You then get a bunch of classes that implement these interfaces. Do not get confused by some base classes which are then extended to the classes you use on a daily bases.

Maps are their own thing in that they store pairs of keys and values. Like the Collection interface, maps start with the Map interface. Then you have some classes that implement Map. Those classes are often extended to give you convenient classes to do your work.

One thing that is common to all this is that you need to write some code to get a feel for using the collections and maps. When in doubt of the hierarchy, take a look at the UML diagrams from Wikipedia on Collection and Map.

Collections Framework

This week my college class is doing the chapter on the Java Collections Framework. The textbook was very confusing. It looks like there are some interfaces to get familiar with. Then there are multiple classes that implement the interfaces. Other interfaces are involved as well. Initially I was overwhelmed. Now I am trying to focus on a few classes at a time.

I found that Wikipedia has a good overview on the collections. The first class I got familiar with was the HashSet. This thing is a generic class. You need to pass in the type of the items that are stored in the collection. HasSet implements the Set interface. As such, it can only hold unique items in the collection. You add() items to the set. You can also check whether they are already ikn the set with a call to contains().

The second class I played with is HashMap. Do you see a pattern forming here? This class implements the Map interface. You store key/value pairs with this thing. You need to specify the types of the key and value when you create the generic class. Then you put or get key/value pairs.

Most of these collection-related items seems to be in java.util. Let's see if I can figure out all the other classes and interfaces. I am studying it hard.

James Gosling Makes a Move

I just read a post about James Gosling quitting Google corporation. He has moved on to work at Liquid Robotics. What a name for a company. The guys is going to be chief architect there. This is surprising. I thought Google would have been keeping him happy and well paid. Perhaps sometimes you need to make a change, or work for the little guy.

In the rare instance that you do not know, James Gosling is the inventor of the Java programming language. He did that back when he worked for Sun Microsystems (which has since been acquired by Oracle). Poor Google. Gosling only lasted 6 months there. Is there more to this story?

Advanced Java

I have been away from the Java programming language for almost the whole summer. There was an app I wanted to write. But I needed it to work quick. So I fell back to using C++. Now I want to get back into the Java swing of things (no pun intended). This week I signed up for a community college course on Advanced Java.

I was a little shocked by the price of the class. It rang up to around $800. That does not even cover the cost of the book. I thought community college was supposed ot be inexpensive. It might be worth it though. We are supposed to cover topics such as collections, multithreading, generics, database programming, network programming, and Java Server Pages. Dang. That's quite a list.

To get my Java skills back, I decided to read the first chapter from the class textbook. It is on recusion. The Java syntax for recursion is simple enough. Understanding the technique of generating a recursive algorithm to solve a problem is hard. With the new Java class coming up at the end of the month, I think I will be more chatty on my blog about Java points of interest.

Deployment Options

I got a new idea for a computer program I want to write. First I need to walk the thing all the way through to completion. I need users to be able to run this program on their own machines. I am thinking about using Java for the coding, but want to ensure a smooth user experience.

To run the Java bytecode on their machines, the users will need the Java Runtime Environment. What if they do not have it on their machine? Or what if they have the wrong version? I need my software to work the very first time, and every time after that.

I did some research on the subject. I can package up my files as a jar. However this still requires the JRE to be installed. Or I could go the web way and use Java Web Start. That would require me to set up a web server to host the apps. Then they will get downloaded to the user machine. That might require too much infrastructure for me.

I could also distribute the program as an applet. Then the whole thing runs in the browser. This also requires some infrastructure. But it will just be a web page. I do not have a lot of experience with applets using Java. That is a consideration.

Finally there are some compilers that produce executables that run directly on the user's native machine. This is the next best thing to developing with a language like C++ and compiling a version for the target machine. I need to play with ohe of these programs that produces a stand alone Java executable before I make a final decision.

Back to Java

I have been spending time learning JavaScript over the past year or so. However now it is time to return to Java. Later this year I plan to take an advanced Java class in school. However for now I am writing some small utlities in Java.

I was surprised at how rusty I got. You need to use your skills to keep them current. I coded a simple switch statement. Being a good developer, I tried to use some constants in the labels of the switch statement. They did not work. Ooops. I forgot to make those constants "final".

There was something that surprised me a bit. I put together a switch statement that initialized a variable. I included a default label in the switch clause. So all paths through would get the variable initialized. However the compiler still gave me a warning that the variable might not be initialized.

It was a small thing. But I don't want any warnings in my code. So I had to blatantly initiallize my variable. Not so clean.

Java Micro Edition

Oracle seems to be standing behind Java Micro Edition (Java ME). The mobile market relies heavy on Java. It is good for developers too since Java lends itself to cross platform development.

There are three layers to Java ME: (1) configuration, (2) profile, and (3) packages. The configurations are further divided into two flavors. You have connected and connected limited configs. The difference is how powerful the target device is.

Java ME supports "over the air provisioning", or OTAP. This allows you to install Java apps over the wireless network. Java ME also provides things such as security, UIs, and offline support.

JavaScript Forms

I am working on development a web app game. It is being written in JavaScript. I had learned how to do forms, so I made the game use a form. The majority of the screen is a big select area. I was going crazy because my JavaScript code was generating errors. The browser kept complaining that document.forms[0].mySelect was either NULL or not an object.

This was troubling because I had coded successful projects that used this technique. Googling the web also showed many examples of this working. To get by I hacked some getElementById calls instead. It still troubled my. I was running Internet Explorer 8. This stuff should have been working.

Near the end of my game development cycle I had to add more items to the form such as a status message. That is when I found the root cause of the problem. The select element was not within the form tags in the HTML. Ouch. No wonder JavaScript could not access the select object. It was just not part of the form. Now I got an action item to go back and use the familiar technique to access the form data in JavaScript code.

Java Enterprise Edition is Dead

I am reading an opinion piece on Java EE. It states that that there were two winners for application servers. There was WebLogic which was best. And there was JBoss that was free. However web services and REST seemed to take away their steam.

Do you actually need an app server? Maybe you can just get by with plain old JDBC. Here is the killer stat which might prove Java EE is a dead end. Nobody implements Java EE 6. There is only open source GlassFish. Ouch.

Java is not dead. But Java Enterprise Edition may very well be. I am glad I did not spend too much time with Java EE.

Oracle Positioning Java for the Enterprise

I read a couple articles stating the Oracle is positioning Java for the enterprise market. Specifically they intend for it to be used mainly for enterprise middleware. Thus Java will be poised to run on the server side.

Oracle has the trademark to Java. They plan to use this influence to direct the future of Java. Their latest plans are in stark contrast to Sun Microsystems, who had a more broad perspective on the application of Java.

Personally I don't care what Oracle plans. Right now I am writing a program to run on my workstation (client). And it is being written in Java. The developers will truly shape how Java is going to be used. Don't worry where Oracle wants to take Java. They will come around.