Skip to content

Instantly share code, notes, and snippets.

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

Battleship

Variables

  • carrier_hits=0

  • battleship_hits=0

  • cruiser_hits=0

  • submarine_hits=0

  • destroyer_hits=0

##Setup Game

Repeat for each player

  • Give player a fleet of ships containing a carrier, battleship, cruiser, submarine, and destroyer

  • Place each ship in the fleet on a location on the ocean grid, either horizontally or vertically

end

To determine who goes first, generate a random integer that's either 1 or 2 and save that in a variable called active_player

if active_player==1

  • active_player= "player1"

  • passive_player= "player2"

else

  • active_player= "player2"

  • passive_player= "player1"

end

puts "#{active_player} goes first"

Playing the game

while fleet_sunk== false

  • active_player should choose a hole on the ocean grid

  • if the hole that they chose is the same as a ship location

    • puts "It's a hit"

    • active_player places a red peg in target_hole

    • passive_player places a red peg in cruiser_hole

    • ship_type= Ask passive_player what type of ship it is ("carrier", "battleship", "cruiser", "submarine" or "destroyer")

      • if ship_type="carrier"

        • number_of_holes= 5

        • carrier_hits +=1

        • if number_of_holes== carrier_hits

          • carrier_sunk=true

          • puts "You've sunk #{active_player}'s carrier!"

        • end

  • elsif ship_type= "battleship"

    • number_of_holes= 4

    • battleship_hits +=1

    • if number_of_holes== battleship_hits

      • battleship_sunk=true

      • puts "You've sunk #{active_player}'s battleship!"

    • end

  • elsif ship_type= "cruiser"

    • number_of_holes= 3

    • cruiser_hits +=1

    • if number_of_holes == cruiser_hits

      • cruiser_sunk=true

      • puts "You've sunk #{active_player}'s cruiser!"

    • end

  • elsif ship_type= "submarine"

    • number_of_holes= 3

    • submarine_hits +=1

    • if number_of_holes== submarine_hits

      • submarine_sunk=true

      • puts "You've sunk #{active_player}'s submarine!"

    • end

  • elsif ship_type= "destroyer"

    • number_of_holes= 2

    • destroyer_hits +=1

    • if number_of_holes == destroyer_hits

      • destroyer_sunk=true

      • puts "You've sunk #{active_player}'s destroyer!"

    • end

  • else

    • puts "It's a miss"
    • active_player puts a white peg in the location that they guessed
  • end

  • if carrier_sunk && battleship_sunk && cruiser_sunk && submarine_sunk && destroyer_sunk

    • fleet_sunk=true

    • puts "Game over " passive_player "had their fleet sunk"

  • end

  • if active_player was equal to player 1

    • change active_player to player 2
  • else

    • change active_player to player1
  • end

end

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