Week 3, Thursday

April 9, 2009 at 9:04 pm (Java, Week 3) (, , , , , , , )

Micah led me to a particularly big realization today about how subclassing and field overriding / hiding work in Java. I was in the process of changing my newly-created Board interface into an abstract class, since I realized that the implementations could be used, with very few changes, in both the 2-D and 3-D board implementation classes. The differences would be in the fields: an int[] squares and int[][] winSets. I was having a problem where I couldn’t get the abstract class’s methods to use the subclasses’ fields, and it turned out that the reason was that I’d declared the fields AGAIN in the subclasses. I wasn’t overriding the fields, as I thought I was; I was literally creating new fields with the same names and types, which were accessible only to the subclass.

And what I should have done (what Micah showed me to do) was to declare the fields only once, in the abstract class (as protected so the subclasses get access), and set the values in the subclasses’ constructors. This change worked fine and made me feel kind of dumb for not realizing it. But feeling dumb for something I didn’t already know isn’t such a bad thing: if I didn’t feel that way, I wouldn’t know that information now. I got better! If I feel like code I wrote weeks ago is bad, that means I learned something in the meantime. It doesn’t mean I should try to write bad code to make myself feel good later, but I think it’s a good metric of improvement to look at months-old code and see how bad you think it is. I’m sure once you get to a certain level it gets more difficult, but if it’s anything like trumpet playing, the real masters will see problems that less skilled artists don’t.

I got 3-D Tic-Tac-Toe working for two human players, which was not as big of a challenge as I assumed it would be. However, the AI for the computer player is slow enough as to be basically useless, for the time being. I spent several hours (with tips from Micah) trying to track down bottlenecks in my recursive algorithm, but the more time I spend on it, the more convinced I am that the algorithm itself is going to have to change, to check fewer nodes of the game tree. I was looking into alpha-beta pruning, in particular, as it’s one of the speed strategies I see consistently mentioned for the minimax algorithm. Micah, on the other hand, is convinced that I’ve got at least one method call that’s bottlenecking in the recursion, so I think I’ll benefit from stepping away from the problem tonight and getting fresh eyes on it in the morning.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: