Skip to content

sarahbohr/battleship

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Battleship

Write the logic to make the game of battleship.

Usage

  • Clone the repo
  • Run locally on your browser (I recommend running a server locally like lite-server or http-server)
  • Navigate to / or /index.html to play the game
  • Navigate to /test or /test/index.html to view tests

Task

Pass the tests in /test by implementing the following functions in /scripts/logic.js:

  • fireMissle: takes a display board, a board with the ship positions and the coordinates for the missle. The function should use the coordinates to determine a hit or a miss based on the ship board. It should add either hit or miss to the display board and return the display board.
const c = carrier = 5
const b = battleship = 4
const s = submarine = 3
const d = destroyer = 3
const p = patrolBoat = 2
const m = miss = 1
const e = empty = 0
const h = hit = -1

const boardExample = [
  [e,e,d,d,d,e,e,m,e,e],
  [e,e,e,e,e,e,e,e,e,e],
  [e,e,e,e,s,e,e,e,e,e],
  [e,e,e,e,s,e,e,e,e,s],
  [b,e,e,e,s,e,e,e,e,s],
  [b,e,e,e,e,p,h,e,e,s],
  [b,e,e,m,e,e,e,e,e,e],
  [b,e,e,e,e,e,e,e,e,e],
  [e,e,e,e,c,c,c,c,c,e],
  [e,e,e,e,e,e,e,e,e,e]
]
  • generateComputerMove: it should take a board and return the coordinates of a new location to send a missle. The location should not be a hit or a miss.
const coordinates = {
  x: 1,
  y: 3
}
  • isWon: it should take a board and return true if all the ships are hit, otherwise return false.

Stretch

  • Generate random boards for the player and computer
  • Implement smart computer moves
    • After a hit, a computer will fire at adjacent locations

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 68.6%
  • HTML 19.5%
  • CSS 11.9%