Originally a final project for the introduction to Java course I took at Monterey Penninsula College (MPC), I expanded on the functionality of the game to turn it into a fully playable game outside of the IDE.
When beginning to think of a Final Project idea for my class, I thought the GridWorld (from the AP® Case Study) engine we were using throughout class was a good base to build from. When thinking of ideas of what project to build, I thought of Battleship right away. This seemed like a good-sized game to tackle, with a little challenge to learn over our previous taught course material.
Another reason I chose this game was because it was one of my favorite to play as a kid with my sisters. What interested me even more at the time was that it was Star Wars themed! I was, and still am, a big Star Wars fan. Being able to set up space battles with the pieces and sound effects and trying to outsmart my sisters was a lot of fun! You can see the box art of the original game below:
After making that decision, I needed to begin thinking how to change from this:
A majority of the work required to implement these changes actually cropped up with the setup of the ships for the players rather than playing the actual game itself. The reason for this being many things:
- Ships had to move across the screen as a single cohesive piece
- Moving/Rotating ships had to be limited to the play area and when not contacting other placed ships
- Rotating ship images had to be in correct locations and image orientations
- Placing ships could not block the user’s ability to place other ships
- Errors needed to be displayed to inform the player what they could or could not do
However, after dividing out each problem into a sub-problem, I could conquer each simple problem and began building my project up from there. If you would like to see my source code, you can view it on my GitHub here:
If you would like to play my game, you can find the download link here:
If you cannot get the game running, please see the troubleshooting section at the bottom of this page.
When you select your game type, a new screen will pop up and Player 1 can begin placing their ships. Do not place ships in the last column or row as this is where ships spawn into the environment.
Move your ship with the WASD buttons (W = up / A = left / S = down / D = right) and rotate it clockwise using the ‘Q’ key and counter-clockwise using the ‘E’ key. When the ship is rotated with one of the keys, use that same key to rotate it back to the vertical position.
Once you pick a place to set your ship, press <SPACE>. A new ship will spawn for you to place. This will continue until you have placed all your ships. If you chose a 2-Player game, another screen will pop up for the second player to place their ships.
When play begins, look at the prompt in the yellow box above the play area. It will indicate whose turn it is. When it is your turn, click on the square you think your opponent’s ship is located. Be careful; one click is all you get to decide your shot.
Once you have selected, play will continue onto the next player. This will continue until one player has sunk all the other’s battleships.
The computer has 2 different ship configurations for each ship quantity giving you a total of six unique games you can play with it. The logic for the computer is good enough to have a fun match with it, but don’t think he’s the sharpest tool in the shed yet. I’m working on continuing to hone that edge in the future.
Make sure you have the most up-to-date version of Java Runtime Environment. If you own a Mac, I know they stopped installing this on their newer computers. Go to the link below to check if you have yours up-to-date: