Iterators, threads, linked lists & states.

Wow! It has been a minute since my last post, but have no fear faithful readers of the neillmorgan wordpress blog for the latest CIS 211 chronicles have arrived.  I have learned more in the past couple weeks in this class then I think I have all year.  It all started with the iterator pattern.  I read  chapter 9 in full and felt fairly competent about collections and the iterator and composite patterns.  Then lab 5 came around and let me know exactly how much I didn’t know about Linked Lists.  I think I might have been at a little disadvantage when it came to this assignment because I did not take the previous class in the sequence at UO, I took 133G at lane which is game programming with C++ and we did not cover linked lists.  I now feel like I understand singly linked lists very well but I confusion started at the head (literally).  If you check out this site herehttp://www.brpreiss.com/books/opus5/html/page97.html you will notice that a singly linked list can be designed in many ways as far as the way the head and tail function.  Since we weren’t shown very much in the way of linked lists in class I had no real direction while trying to design my linked list and my mind really wandered with how the head should function for what I wanted to do.  After annotating every line of the key to the linked list I got a pretty good feel for it.  Anyway,  I said I was going to resubmit my lab 5 since I didn’t do very well on it, but I my lab 6 includes the use of the linked lists and the iterator pattern so I figure that would be an exercise of utility at this point. I’m using the linked lists and iterator pattern to cycle through my animation graphics.  My linked lists consist of nodes filled with string references to the individual graphics themselves and then I use an iterator to run through them.

Another thing I learned on my ventures was threads.  I was struggling with the idea of how I was going to make my enemy characters move in the game without them moving at a million miles a minute and with an interface that wasn’t totally ridiculous.  Since I am using and abusing the draw function in my driver It would have been extremely tedious  to try and modify the way that function behaves while I’m calling it so much in my keyEvents listener that controls my main character (link).  So before I stumbled upon a solid solution to my enemy character animation problem I of course went through some trials and tribulations.  One thing I learned once and ended up learning twice is that when it comes to animation timers are not what u want.  I made a pong game in visual basic  in class a few years back and used timers to control the animation of the paddles and the ball that bounced between them.  After about 30 minutes of running the game the animation slowed to a sloth like speed and you would have to restart the program.  After a little investigation into timers and java, I came to the conclusion that they have a similar effect.  The bottom line is timers are ok if you only need one or two and you don’t depend on them for very many function calls, otherwise your processor is in bog down city.

So after investigating some source code to a few other java games I found online, I noticed they all used threads for doing animations as well as many other things.  I checked out java’s thread capabilities and was bummed to find out that to use just a basic thread you had to extend a thread class.  This was disheartening because I know I will need to eventually extend the observable class in my enemy soldier class, but  I found out that you don’t have to extend thread to use a thread.  Java provides an interface called Runnable that basically creates a giant loop that allows you to call any function you like within it and control the speed at which the thread executes with the sleep function.  So long story short, everything seems to be going well with animating my other characters in the game with threads.

To get completely up to date I added in the state pattern as lab 6 required to control the states of my enemy soldier character.  Right now he has two states, a scanning state in which he is just mulling about the room looking around for intruders and an attack state where he charges the main character if you get to close to him.  At this point it just switches once from the scanning to attack state, but eventually you will be able out run the soldier and he will resume the scanning state.  Bare with me faithful neilmorgan wordpress blog readers, I know my AI needs work, but I think what I’ve done suffices the lab 6 requirements and is a step in the right direction.  My next post will be up real soon so watch out!!  Until next time…………….. keep learning, keep coding and keep it REAL!!!

-morgan neill

Advertisements

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: