Battleship

Battleship is a war simulation game that started out as a pencil and paper game. The principle is quite simple. Players mark off two grids of size 10 x 10. On one grid, a player places "ships" of width 1 and length { 2, 3, 4, 4, 5 }. After both players place each of their five ships, they take turns guessing cell by cell where ships are located. Hits and misses are charted/recorded on the second (blank) grid.

The basic components thus break down into:

Two pairs of 10 x 10 grids that use cells.
Two sets of "pieces" that contain
Markers for denoting Hits and Misses.

   The Order of Play is:
     I. (SETUP)
	 Setup boards.  Players simultaneously place ships on
	 their grids preparing for play.

    II. (PLAY)
      A. Player announces shot by coordinate tuple.
      B. Opponent looks up shot and returns hit or miss
	   - if hit, determine if hit ship has total damage
	     and if total damage, return ship type sunk
      C. Player records whether shot is hit or miss.
      D. If last Opponent ship is sunk, Player is winner.
      E. If Player is not winner, play continues with Opponent at A.

Okay, we have the basics now. First we should find a way to display the game board. I designed my board to be two 10x10 grids that are placed side by side, numbered along the far left and far right sides, with letter numbering along the top. Also I initialized my grids to 0 values throughout to help with bookkeeping. Displaying the grid is a simple iteration of for() loops interspersed with tabs, spaces and carriage returns. Try to construct two grids as I have described, before looking at my code.

bship1.c

Now, we want to place ships on the grid...after all, what are we supposed to shoot at? There are many, many ways to implement ship placement, in fact, while I was in college we discussed the theory of implementation of ship placement for battleship. Anywho, in MY opinion, giving the x,y location of the nose (Bow) of the ship and it's general facing (n, s, e, w) is sufficient for populating the grid. To place a ship, you just go to x, y, then fill in the ship grid with the number of the ship (1 - Destroyer, 2-Cruiser, ... and so on). Another way to fill the grid (the hard coded way), is to initialize the array with the ship positions. You can do the initialization any way you like. In my code, I did the x,y placement for the player's ships, and the hard coded approach for the opponents ships.

CHALLENGE: come up with your own way to place ships
CHALLENGE: allow two players to place ships independently
bship2.c

Great! Now all we need is some way to play and away we go! So, all we need is some way to input a tuple for a shot, and keep track of whose turn it is. So here is the basic code:

  1. Get shot.
  2. Check if shot hits or misses.
  3. Did shot win game?
  4. change players (by flag would be fine).
bship3.c

We now have a fully playable 2-player game of Battleship...but it depends alot on having a second player, particularly an honest one. Why not give ourselves a computer to play against from time to time? It should be relatively simple. All you have to do is make player 2 give random choices for it's shot. Go ahead, it won't bite (unless you program it to).

CHALLENGE: come up with an Artificially Intelligent computer player. Let it choose it's shots by what shots it has already made.

Now it is up to you. There are alot of improvements you can make from here. You have a fully functional game, but there are things you may want to do to make the game more fun to play. Here are some challenging suggestions: