Skip to content

Instantly share code, notes, and snippets.

@katiebuilds
Last active August 29, 2015 14:13
Show Gist options
  • Save katiebuilds/abcaa3be622de5ca4bce to your computer and use it in GitHub Desktop.
Save katiebuilds/abcaa3be622de5ca4bce to your computer and use it in GitHub Desktop.
Battleship Instructions

#Battleship Instructions ##General Vision I looked this up on Wikipedia first, and after reading that it started as a pen-and-paper game, I started visualizing this game as based on a grid of squares, and the different states they might be in. I was thinking of things along these lines:

  • Each side has the following:

    • Target Grid Squares
    • empty
    • hit enemy
    • missed enemy
    • Ocean Grid Squares
    • empty
    • with ship
      • Ship safe
      • Ship partially hit
      • Ship sunk
    • Game Status board
      • Game in progress: some squares empty
      • Game finished/someone won
  • And then, I had all manner of thoughts about rules for how the computer would place ships on this grid, but for now, that's not really relevant.

##Actual Instructions Addressed to the computer itself

  • OPENING SCREEN/END OF GAME
    • Wait for the user to click, "I'd like to play Battleship!" When they do, start with BOARD SETUP
  • BOARD SETUP
    • Randomly arrange all your ships on the Ocean Grid.
    • Ask the user: "Please choose positions for all of your ships."
    • Check if they have placed all 5 ships.
      • If yes, do GAMEPLAY.
      • If no, wait 5 minutes, then ask, "Did you still want to play? Please place all 5 ships on your grid."
      • Keep checking every 5 minutes until GAMEPLAY begins.
  • GAMEPLAY
    • PERSON'S TURN
      • Ask the user: "Take your best shot!"
      • Check the square with the coordinates they named
        • If the square contains a ship
          • (ship) = name of the appropriate ship
          • Tell them, "Oh no! My (ship) has been hit!"
          • If there are no more safe squares left on your ship
            • Tell them, "Curses! You sunk my (ship)!"
            • Give them a point on their Game Status board
              • If there are no more empty squares on their Game Status board
                • Tell them, "Alas! You've won!"
                • Ask them, "Do you want to play again?"
                  • If yes, start again with BOARD SETUP
                  • If no, do END OF GAME
        • If the square doesn't contain a ship, tell them, "Miss!"
    • COMPUTER'S TURN
      • Do not select a square that you've selected before.
      • Check if you have already hit any of their ships.
        • If the ships you've already hit are completely sunk || if you haven't gotten any hits
          • Randomly select a square to hit
        • If you have hit a ship before
          • Randomly select an adjoining square to hit
      • (coordinates) = Coordinates of the square you hit
      • Tell them, "(coordinates)!"
      • If you have hit their ship, record this as a hit in your Target Grid.
        • Check if there are still safe squares on their ship
          • If yes, repeat GAMEPLAY
          • Else
            • (ship) = name of the appropriate ship
            • Tell them, "What a delight! I've sunk your (ship)!"
            • Give yourself a point on your Game Status Board
              • If there are no more empty squares on your Game Status board
                • Tell them, "So there! I've won!"
                • Ask them, "Do you want to play again?"
                  • If yes, start again with BOARD SETUP
                  • If no, do END OF GAME
      • Else
        • Record this as a miss in your Target Grid
        • Repeat GAMEPLAY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment