Introduction: Arduino Battleship Game

About: Old dog still learning new tricks.

I remember playing a paper and pencil version of the Battleship Game as a kid. In fact, it has been around since about World War 1. I also had an “electronic” variant in the early 1960’s called “Sonar Sub Hunt” that had lights and sounds and hidden mines. By today’s video game standards Battleship is pretty boring but I thought I would make one anyway just to see what the grandkids thought about it. After all, sometimes retro can be cool.

There are bits and pieces of this project that would be useful elsewhere even if you aren’t interested in making the Battleship game. It has a simple 4-bit 1602 LCD interface that includes a routine for setting a specific character location. There is also an interface for decoding a 4x4 switch matrix. These two interfaces are available as separate include files so they are easily transportable. The game also has various sound effect routines and a simple one-transistor audio amplifier circuit.

Step 1: Hardware

The schematic presented here is for a single player so two units would need to be made. The units communicate using a 3-wire interface that includes the UART TX and RX lines and a ground wire. I chose to use a standard 1/8 inch stereo headphone jack and a standard cable with male plugs on both ends. The RX from one box goes to the TX of the other box and vice-versa. You can do that by swapping the wires soldered to the headphone jack inside the box or swapping them on the circuit board when you build it.

The classic game was laid out as a matrix but I decided to do a simple implementation using a 1602 LCD for the display. The first line shows the player’s ship locations and the opponent’s shots. The second line shows the player’s shots and any hits on the opponent’s ships. That provides 16 possible locations for ships. The number of ships is set in the software and I arbitrarily chose 5.

The 16 ship locations fit nicely with the switch requirements because it allows for a 4x4 matrix. There are 4x4 matrix switch pads available but I chose to use individual switches in a single line to match the linear display. I did, however, wire the switches as a 4x4 matrix so that only eight Arduino pins are needed. Refer to the switch wiring diagram and the schematic for the proper connections.

The LCD display is wired for a 4-bit interface. I also added an external LED on pin D13 to indicate which player should be taking a shot. Initially, the players decide who takes the first shot and then the software automatically controls the back and forth of the game.

I wanted to add simple sound effects for the shot, the explosions, the game ready, and the winner/loser. I tried using a simple piezo buzzer but ended up with a small speaker instead. The current required by the speaker exceeds what the Arduino can handle so a simple transistor amplifier was added. The sound still isn’t great but it’s better than with the buzzer. My speaker is 4 ohms but if you have an 8-ohm one then change the resistor in the schematic from 39 ohms to 33 ohms. If you use a piezo buzzer you should be able to drive it directly from the Arduino pin with the other side of the buzzer connected to ground.

An external reset switch is also included and is wired directly between ground and the “reset” pin on the Arduino. That provides the means for restarting the game.

Step 2: Software

The software has the include file for my LCD interface and I also created an include file for the 4x4 switch matrix scanning. The initialization prompts the player to select the locations for his ships and then goes to a “Ready” state. When both players are ready one of them starts the game by pressing a switch.

The shot location is transmitted via the UART to the other player and the appropriate result is transmitted back to the player who fired the shot. As mentioned earlier, once the first shot is taken, the software controls who takes the next shot. Before a shot is transmitted, it is checked against the locations of the previous shots. If that location has already been used, then the shot will not be transmitted. The “My_Shot” LED determines whose turn it is. There is also a shot sound generated for a valid selection and an explosion sound if a ship is hit. The sound effects are derived from examples found online with modifications to suit the game.

Once an opponent’s ships have all been hit, a message is displayed on each LCD – one as the winner, and one as the loser. The message also specifies that the game can be restarted by pressing the reset button. There are also separate sound effects for the winner and loser.

Step 3: Screen Shots

Here are some screen shots from the game. That’s it for this post. Check out my other Instructables and also my website at: www.boomerrules.wordpress.com