From ENGR005 2008
Revision as of 13:17, 12 December 2008 by Yalade1 (Talk | contribs) (Closing Thoughts)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Project Purpose

To bring to virtual life an American Classic...Battleship (the best Navel-inspired board game) using the language of technical computing...MATLAB!

Two adolescents bonding over he popular board game.

Team Members

Jack Nicoludis

Yewande Alade

(Pictures coming soon!....maybe!)


When attempting to come up with a program idea, we both sat down to brainstorm. Though E005 had been a relatively short course, we realized that we had gone over A LOT. So, it was pretty hard to come up with one single idea...and stick to it. While both enjoy working with robotics, we decided to work with MATLAB. Though working with hardware is important aspect of engineering, software plays just as much as an integral role. In the instance of robotics, metal machines are useless until they are made to move by programming. MATLAB is regularly used to do this sort of programming, so we thought it was important to familiarize ourselves with the MATLAB as much as possible. In class we had learned about matrix mathematics and displays via MATLAB, we viewed our final project as an opportunity to take this understanding further (and explore other MATLAB functions/capabilities).

So, exactly how were we going to do this? First of all, it was are aim to learn while having fun. After some research, we learned that it was in fact possible to make games with MATLAB, especially board games. While there are many MATLAB board game adaptations currently available, we were immediately drawn to Battleship, a Milton Bradley creation. Not only was it a game we both enjoyed playing as kids (and now as "adults"), it was a game that allowed us to be very creative in its putting together. It is game that we initially felt was within our programming abilities, yet also difficult enough challenge us.

It was final. Battleship was going to be our game. As for the format of the game, we decided to go with a user v. computer version. In this setup, the computer doesn't play against the user, it merely choose five ships, each of a different length, randomly makes them horizontal or vertical, and places them on a random part of the board. The player has the task of finding all the ships in the fewest tries possible. We decided that considering the time restraints, this format seemed to most feasible to execute effectively.

As for the Cold War-themed design...this was just a random implementation of creative license. (Also, it seemed amusing and ingenious at 2 am in the morning...)

Game Plan

What Program Entails

  • 1. Implement Top-Down Design (like the good little programmers we are)
  • 2. Commence Writing Script Using MATLAB
  • 3. Refine Initial Design During Programming Process
  • 4. Test, Test, TEST As We Go
  • 5. Debug (If Program Encounters Errors)
  • 6. Rewrite/Make Revisions (Based On Discoveries From Debugging)
  • 7. Play game!


November 20: Choose game

November 23: Decide on game's operation/function (putting into programming feasibility)

November 25: Meet to put together Top-Down Design and begin programming

November 25 - December 5: Programming/ Testing Period / Start on Powerpoint / Expound upon project proposal formulate final presentation

December 4: Practice Oral Presentation

December 6-7: Debugging / Continue Powerpoint

December 8: Practice Presentation / Finishing touches on Powerpoint

December 9: Presentation

Creation of Game


Majority of MATLAB programming relies heavily upon the creation and mutation of matrices. So when we were figuring how to start our program, we knew that we would need a matrix to represent a battleship board:

Empty board

Matrix interpretation of battleship board
  • First we decided to represent an empty board by using an 10 by 10 matrix composed entirely of zeros.

Orienting/Placing Ships

Placed Ships
  • Then we had to figure out a way to create five different ships, randomly place them on the board, then orient them all in a completely random fashion. While creating the ships(by using loops/iterations to repeat a series of steps five times) and choosing their orientation (horizontal or vertical) was relatively easy, placing the ships on the board was quite difficult. We often ran into the problem of placing ships "on top" of each other or placing them off the board. We eventually were able to correct this by setting really restrictive guidelines for the placement of ships.

Creation of GUI (Graphical User Interface)

What is a GUI?

A GUI is a pictorial interface to a program. It makes a program easier to use by providing a consistent appearance and with intuitive controls (like pushbuttons and menus!). A successful GUI behaves in an understandable and predictable manner (in other words, user friendly!)

  • It is for aesthetic appeal and ease of gameplay that we decided to use a GUI for our project. It MATLAB, there exists a special program called "guide" that allows MATLAB users to create GUI with relative ease Like a paint program, a programmer merely pastes the different features s/he wants to be included in his/her GUI and saves it as a MATLAB .fig file:
Guide window

In our particular GUI, we used pushbuttons to represent the different cells in our "matrix board" from earlier. We also used them as options for a player to press to "quit" or to "reset" a game. We used static text object for labels (like the title "Battleship"), instructions, and for the counter at the bottom of the window that tracks how many times a user as clicked a button. The last major component we used was a panel, which was the background of the window, and where we placed all of our static and pushbutton objects.

After this is done, MATLAB automatically (even though some later updates had to be done on our own) generates an M-file representation of your GUI. This m-file is composed of some "gimme" functions that the programmer does not have to worry about writing (or even understanding) in order to have the gui do what he wishes:

"Given" functions that compose GUI

The next step now was to add additional functions to work in conjunction with our GUI script.

Creation of Game (Part 2)

After programming the preliminary steps (setting up the board), we had to start programming the game for actual game play. This would prove to be the most time consuming of our project. Conceptualizing the playing of battleship by using matrices proved to be initially difficult, but finally we figured out a relatively simple method. Every time the player pressed a button on the board (representing an element in the "board matrix", if the value in the matrix was 0, this meant that there was no ship there. Since we wanted to keep track of where the user guessed, as opposed to leaving this cell as a 0, a miss would be turned into a 9. On the other hand, if the player picked a cell in the matrix that was greater that was a 1, 2, 3, 4,or 5, this meant that s/he had hit a ship. To keep track of the player's "hit's", a clicked 1, 2, 3, 4, or 5 cell was changed into an 8. This was not all that was needed to be done. We also wanted keep track of what type of ship a user sunk. To do this, in our program, we included a condition that stated that when all 1's, all 2's, all 3's, all 4's, or all 5's had been converted to 8, this the type of ship that each set of numbers represented was sunk. When all sets of numbers have been converted to 8s,the game ends.

Also, every time a player clicks a button, we added 1 to a counter (initialized before game play) to keep track of the player's number of guesses.

Finally, we had to write functions that permitted the player to close out of the game or reset a game. To perform both of these functions, we utilized dialogue boxes (another GUI function). Upon clicking the pushbuttons that represented either of these actions, a question dialogue box pops up to ask the player if s/he is sure she wants to follow through. The player has the option to choose 'Yes' or 'No'. For the close window option, we implemented GUI function that is widely available to programmers. For our reset option, however, was one we had to figure out on our own. Upon pressing the 'Reset' button, the board matrix was re-initialized to a zeroes matrix and new ships were chosen, placed in a random fashion, and hidden from user to start a new game.

Below is a GUI run of our and an actual representation of what is going on in our board matrix

Start of game
Player guesses once; a miss
Player guesses again; a hit!
Player sinks complete ship!; aircraft carrier
Player sinks all the ships; game over!


Making sure ships didn’t go off the board or overwrite locations of other ships

Like, stated before, we constantly ran into the problem ships overwriting other ships and ships running off the board (resulting in incomplete lengths of ships). Strict conditions were put into place to correct this program.

Trying to “streamline” program (loops/iterations for GUI)

Currently, each “board” pushbutton function has 50 lines of (repetitive) code to process the information from the player's click. After our calculations, we found that these 50 lines x 100 pushbuttons = 5000 lines of code out of 5435 lines in our main script.!!! Placeship()(our placing ship function) had only 90 lines and playgame() (our game play function) had only has 34 lines!!


We began programming by using local variables and passing them through functions, but this created input/output and definition of variable errors. Global variables were the solution, but required a lot of research to use them correctly. Global variables are variables that carry through ALL the functions within a script. However, they must be called BEFORE (and only before) they are defined/redefined. We did not know this before and went through a lot (5 hours) of grief to figure this out thanks to an extremely help webpage from the University College of London.

Closing Thoughts

  • If we were ever going to work on this program in the future, we would:

Condense code----- Import image via axes object vs. placing on button(We couldn't figure out how to put to import an image from an outside source into our GUI using 2-D objects (like axes). Instead we had to "cheat" by placing the picture on a pushbutton *which was surprisingly easy*)------ Insert single quote in a string (When we displayed to a user that s/he sunk a certain ship, all we could do was type “enemys battleship” instead of "enemy's battleship")


  • MATLAB is important tool in engineering, math, and other practical fields
  • Teamwork and positivity are essential
  • It’s okay to take breaks (sometimes that is really the best course of action, answers to difficult problems sometimes do just come to you
  • Timelines are set in place for a reason (We now understand why in the world of engineering, it is so essential to follow a timeline. It makes reaching a certain deadline feasible, and not as stressful as procrastination would have made it. Because of our relative diligence, doing this project was not as agonizing and it could have been....)
  • This project definitely did not take 6 - 10 hours. On the last day we spent 10 hours in Hicks alone. Many other hours were spent for programming late at night, brainstorming at lunch, and reading books (especially for GUI) to establish knowledge and understanding. It was a MAJOR time commitment, but worth the final product.


Contact us! We will be more that happy to e-mail it to you. HAVE FUN!!!!