Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Search from all Lessons


LoginGet Started

Register to 4Geeks

← Back to Projects

Battleship with Vanilla.js

Difficulty

  • intermediate

Average duration

8 hrs

Technologies

Difficulty

  • intermediate

Average duration

8 hrs

Technologies

Battleship In Vanilla.js

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.

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

1$ 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:
1// 0 = empty 2// 1 = part of a ship 3// 2 = a sunken part of a ship 4// 3 = a missed shot 5let gameBoard = [ 6 [1,1,1,1,1,0,0,0,0,1], 7 [0,0,0,0,0,0,0,0,0,1], 8 [0,0,0,0,0,0,0,0,0,1], 9 [0,0,0,0,0,0,0,0,0,1], 10 [0,0,0,0,0,0,0,0,0,0], 11 [1,0,0,1,1,0,0,0,0,0], 12 [1,0,0,0,0,0,0,0,0,0], 13 [1,0,0,0,0,0,0,0,0,0], 14 [0,0,0,0,0,0,0,0,0,0], 15 [1,1,1,1,0,0,0,0,0,0] 16];
  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.

Sign up and get access to solution files and videos

We will use it to give you access to your account.
Already have an account? Login here.

By signing up, you agree to the Terms and conditions and Privacy policy.

Difficulty

  • intermediate

Average duration

8 hrs

Technologies

Difficulty

  • intermediate

Average duration

8 hrs

Technologies

Difficulty

  • intermediate

Average duration

8 hrs

Technologies

Difficulty

  • intermediate

Average duration

8 hrs

Technologies

Sign up and get access to solution files and videos

We will use it to give you access to your account.
Already have an account? Login here.

By signing up, you agree to the Terms and conditions and Privacy policy.

Difficulty

  • intermediate

Average duration

8 hrs

Technologies

Difficulty

  • intermediate

Average duration

8 hrs

Technologies

Supplementary Material

In order to prepare better for completing this exercises, we suggest the following materials

Lesson

What is DOM: Document Object Model

Exercise

Learn Javascript Arrays and Loops Interactive

Exercise

Learn how to manipulate The DOM with JS