← Back to Projects

Battleship with Vanilla.js

Goal

4Geeks Coding Projects tutorials and exercises for people learning to code or improving their coding skills

Difficulty

intermediate

Repository

Click to open

Video

Not available

Live demo

Not available

Average duration

8 hrs

Technologies

Battleship In Vanilla.js

Write a battleship game using Vanilla.js

Demonstration of Battleship

🌱 How to start this project

Do not clone this repository.

The first step to start coding is cloning the vanillajs boilerplate on your local computer or opening it using gitpod.

a) If using Gitpod (recommended) you can clone the boilerplate by clicking here.

b) If working locally type the following command from your command line:

$ git clone https://github.com/4GeeksAcademy/vanillajs-hello`.

💡 Important: Remember to create a new repository, update the remote (git remote set-url origin <your new url>), and upload the code to your new repository using add, commit and push.

📝Instructions

  1. Create your HTML/CSS. First, we recomend using a parent div with display:flex and flex-wrap: wrap (to allow multiple rows), the container must have 100 divs inside.
  2. After your HTML/CSS looks good, start thinking on how to make it dynamic using JS.
  3. To represent the gameBoard you can use a JS matrix like this:
// 0 = empty // 1 = part of a ship // 2 = a sunken part of a ship // 3 = a missed shot let gameBoard = [ [1,1,1,1,1,0,0,0,0,1], [0,0,0,0,0,0,0,0,0,1], [0,0,0,0,0,0,0,0,0,1], [0,0,0,0,0,0,0,0,0,1], [0,0,0,0,0,0,0,0,0,0], [1,0,0,1,1,0,0,0,0,0], [1,0,0,0,0,0,0,0,0,0], [1,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [1,1,1,1,0,0,0,0,0,0] ];
  1. Add one onClick to every <div> and call the fireTorpedo function with the coordinates of the div.
  2. Replace the value on the gameBoard and render the board again.

😎 Feeling confident?

  • Try implementing a "showShips" function that shows the ship positions when clicked.

Goal

4Geeks Coding Projects tutorials and exercises for people learning to code or improving their coding skills

Difficulty

intermediate

Repository

Click to open

Video

Not available

Live demo

Not available

Average duration

8 hrs

All rights reserved, 4Geeks LLC 2021. Read more about 4Geeks and what we are going here.