Blog Post 4: Battleship Version 1 Complete

Hello and it’s good to be back.  Well, the weekly updates were well intentioned, but once I entered the Summer quarter back at Oregon State University, that became more of a pipe dream.  To help you all follow my blog easier, I’ve added an RSS Link to the Blog page so you can drop by once I add a new post.  Regarding school work, I’m about to head into Fall quarter, but it should be a lot less stressful so I can continue to work on my projects this Fall.

Now that that’s out of the way, time for some game updates!

As you may have seen from the blog post title, I finally have a solid version for you guys to try out for my Battleship game!  This includes play modes: 2-Player and VS the computer.  The computer may be a touch dull now, but I think he has improved much over my initial build.  Below, I’ll break down my improvements I’ve made with this game last week.


July/August 2017 drop-off:

Back in June, was the last time I looked at my project, so getting back up to speed took a little time.

My original plan, as documented in my previous blog post, was to restructure how I used “while” loops to check for button presses from the player.  The way I figured to do this was to use the built-in action listeners for key presses and click locations provided by GridWorld.  Once the user pressed the correct key or clicked their location for their battleship shot guess, the game would continue.

This was good in theory, but I ended up having to tear my code open and restructure a lot of the logic.  This took a LONG time to get everything working again and after taking my CS 162 course on C++ programming, I will fully document my pseudocode and work through most problems before writing any code.  This was a big lesson learned and anyone reviewing my code can see my troubles.


Event Dispatch Thread (EDT) Problem:

After restructuring my code, all key presses and clicks worked immediately and my game was no longer hemorrhaging memory (yay!).  However, my messages, which worked on BlueJ wouldn’t respond.  Once a message would pop need to pop up, my program would freeze and then continue as if nothing had happened.  There was something behind the scenes I needed to figure out…

It turned out that the Event Dispatch Thread (EDT), which runs all the game’s logic on one thread, is affected by anything you do to it.  In this case, I was attempting to show messages for a few seconds to the viewer.  To accomplish this, I would pause the game.  This worked in BlueJ, but in Eclipse, this pause would freeze the graphics window as well causing my problem.

To fix this, I created a separate thread to handle the text and image displays, which would pause and start the game with no graphical issues.


Trapped Ship Problem:

When playing the game, you notice that your ship spawns in the far right of the screen.  I picked this instead of a click-place method because of time and engine constraints at the time of my assignment.

This method was all good an dandy until I realized if a player wanted to stack ships on the right side of the arena, they could possibly trap their next spawned ship from entering the play area!  The result would make the game unplayable and very angry messages to me about how this should never have gotten through QA (which is me) before release and such.  But I digress…

I initially though rotating the ship to the left would be able to squeeze the ship out of the problem, but it did not solve every iteration of my testing.  My next step was to add an extra row for ship spawning.  Should the column be filled, the ship will spawn in the last column instead (see below).




Through even more testing, I found I could still trap myself should 5 ships be chosen at the beginning!  Frustrated with this, my last attempt to save the game was to spawn the ship in the very first column.  Should the player choose to line their ships around the border, there was no way possible they had any ships located in the first two boxes (see below).




Let me know if you can trap your ship though.  I’ve been trying to trap myself over multiple attempts and think I have the set-up figured out.


Computer Logic Problem:

After tearing apart my code for optimization purposes, I just had to update my computer logic to mesh with this new code.  This took some time, but nothing large to note from it.

While updating my code, I was also able to squash a couple of well-hidden bugs in my computer’s logic.  It now performs as expected, so you shouldn’t run into any problem when playing!  If you do, please let me know what you were doing and what error occurred.  This will make it easier for me to fix the issue.


General Clean-up:

After confirming my game worked, I began performing simple clean-up operations like changing window pop-up locations and making the game playable again after one person/computer wins.


Executable File Problem:

Just when I thought everything was ready to be packed into an executable .jar file and ready to play, nothing worked.  Running it through the command prompt, I realized I had two problems:


  1. The executable not find image/text file assests
  2. The text file could not be read


To fix the first issue, I needed to pack all my resources into sources file.  This way the executable would know where to look when accessing them.

Fixing the second problem proved more difficult.  In Java, I was using what was called a Scanner object and reading them in using a File object.  However, these cannot be read with executable .jar files.  A quick search through Stack Overflow showed me that many a person has trodden down this path before.

To fix this problem, I needed to use a couple of objects called a BufferedReader and an InputStreamReader.  This effectively gave me the ability to stream my text document (for the Computer’s map selection) via characters.  By changing back into integers to read my row, column values, I was able to get the information my program needed.  After that, my program runs as it should.


And that’s it for changes!  After only a couple weeks of work, I was able to change over from BlueJ to Eclipse, optimize my project, and submit a first version.  Please let me know what you think (and please be brutally honest.  That helps me more than anything.)


Looking to the future:

At the moment, I will post this first version of my project to the site and take a break from it.  Now, I know I promised to work on my “The Quest” project, but over the past Summer, I have gained much experience using C++ (whereas “The Quest” is written in C#).

I would like to spread my wings (so to speak) with this language and experiment with the Unreal engine on a new secret project.  This is inline with the language and engine I would like to excel in since they are not only used in a lot of large game companies, but the lighting system in the game looks oh so damn beautiful.  I know a game isn’t all about its graphics, I get that, but there are so many tools available in the engine that it makes my mouth salivate with the possibilities and directions I can head in.

I will keep you all in the loop more often as the Fall Quarter will take less time for coursework than Summer or Winter, but I’ll keep most of what I do under wraps for the moment.

Until then, I hope you are all enjoying your week….no, that’s too formal.  How about, I hope many of you reading this have thought of at least six impossible things to do before breakfast*.


* Lewis Carroll (Oh boy, references in a blog post?!)


Leave a Reply

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

You are commenting using your 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