Skip to content

Instantly share code, notes, and snippets.

@maebeale
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maebeale/be6c86d885b33eab3d67 to your computer and use it in GitHub Desktop.
Save maebeale/be6c86d885b33eab3d67 to your computer and use it in GitHub Desktop.
battleship game

hash-happy pseudocode for text-based battleship game

based on rules from http://www.hasbro.com/common/instruct/battleship.pdf


  • start_game

setup variables to simulate a grid

  • grid_value = 8

  • computer_squares = an empty hash

  • player_squares = an empty hash

  • grid_letter = a

  • grid_number = 1

  • square_status = 'no_ship'

  • hit = 'hit'

###set contender to be player || computer #later can be adapted easily to allow more than 2 players. for now, contender & not_contender.

  • setup contender_squares hashes, which requires you to pass in contender
    • unless grid_value is 0
      • create a two char key of square_name (aka square_name) with a value of square_status
      • push the item into the #{contender}_squares hash
      • add/increment 1 grid_letter to grid_letter
      • add 1 grid_number to grid_number
      • decrease grid_value by 1
    • end
  • end

stock players with ships

there is a set of ships for each player, with key of ship_name and value of ship_length

  • ships = {'carrier' => 5, 'battleship' => 4, 'cruiser' => 3, 'submarine' => 3, 'destroyer' => 2}

  • create hashes for ship sets: #{contender}_ships = ships, and then #{not_contender}_ships = ships

place #{contender}_ships on #{contender}_squares

  • for each ship
    • randomly find a placement, which is #rules out diagonal && non-connecting squares from being played
      • ship_length computer_squares in the list where either all ship_length grid_letter or
        • ship_length grid_numbers are in a row (computer will need more instructions on this, aka grid_check) and
          • each square has a square_status of square_status
    • after placement, change each square_status to ship_name
  • end

draw squares for the human as a grid, called grid_graphic with argument of contender

  • make grid graphic that varies based on status of each square
    • puts contender.capitalize + ''s ' + 'Ocean'
    • arrange the grid from contender + '_' + 'squares' hash with letters incremented horizontally and numbers incremented vertically
    • if grid_status == 'no_ship'
      • show square_name on each contender + '_' + 'square'
    • elsif grid_status like '%_hit'
      • show 'x' on each contender + '_' + 'square'
    • else
      • show ship_name on each contender + '_' + 'square'
    • end

prompt human to place player_ships on player_squares

  • show grid_graphic(player)
  • until there are no more player_ships
    • show the human their list of player_ships keys
    • ask the human to type the name of the ship from player_ships they'd like to place on the board and name it selected_ship
    • remove selected_ship from player_ships
    • show grid_graphic(player)
    • ask the human to type in square_name ship_length number of player_squares
    • if grid_check of ship_length player_squares
      • change each player_square's square_status to ship_name
    • else
      • prompt the human to give you ship_length player_squares that will pass a grid check
    • end
  • end

###set contender #will this interfere w above use of contender?

  • randomly select player or computer to be the contender and go first
  • contender = player || computer

###game play. need to pass in contender

  • show grid_graphic(not_contender)

  • if contender == 'player'

    • ask human to enter square_name they'd like to hit, aka selected_#{not_contender}_square #not_contender is pretty negative!
  • else

    • have computer randomly select item from player_squares where square_status is not like '%_hit'
  • end

  • if selected_#{not_contender}_square square_status is like '%_hit'

    • puts 'Too bad. You already played that square! Start your turn over.' #this isn't in the rules...
  • else

    • if square_status for selected_#{not_contender}_square is like '%no_ship'
      • puts 'Miss.'
    • else
      • puts "Hit. #{square_status}" #this will be ship_name
      • change square_status to square_status + '_' + 'hit'
    • if #{not_contender}_squares has any value ship_name for ship_length number of times #can you do this on a value?
      • puts 'You sunk computer's #{ship_name}' for that computer_ship
    • else
    • end
  • if #{not_contender}_squares has any value ship_name for ship_length number of times for all #{not_contender}_ships

    • if contender == player
      • puts 'Congratulations! You sunk all of #{not_contender.capitalize}'s ships!' #could get fancy and ask name of player and set name of computer
    • else
      • puts "Sorry, #{not_contender.capitalize} sunk all your ships!"
    • end
    • show grid_graphic(computer)
    • show grid_graphic(player)
    • ask player 'Want to play again? Enter Yes or No.'
      • if player_input == like '%y' #need to ck on capitalization issues? need to deal with non-alpha chars?
        • start_game
      • else
        • 'Thanks for playing Battleship! Come on back now, ya hear?'
      • end
  • else

  • end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment