How I built a simple 'word guessing game' using Javascript ( for Beginners )
fawazsullia
Posted on May 14, 2021
I wanted to build something fun for my portfolio. After spending sometime thinking, I decided to code a game that was popular in my childhood.
Hangman
Players will have to fill the dashes with letters, until they guess the word. The game ends if the player guesses the correct word or if he chooses the wrong letter 5 times.
Here’s how the game looks:
Before we start building, let's see what the game needs to do.
- Generate a random word
- Generate dashes for the players to fill letters
- Provide letters for the players to choose
- End the game when the player loses or wins
Now, with that sorted, lets start building.
Also, you might want to keep the game and the source code open for reference.
HTML:
The basic structure of the game is quite simple.
A section for the title : This section contains the title and the number of chances a player has left
A section for the word: The blank space for the word is displayed here. We will dynamically display this, depending on the word length
A section for the letters: English alphabet to select will be displayed here. You can create a span tag for each letter, but it is much easier to do this with Javascript.
Styling
Styling entirely depends on your preferences, but to get started, here’s the link to my stylesheet.
The dynamics of the game ( Javascript )
As I said earlier, we need to create a section of alphabets that the players can select.
Steps:
- Create an array of alphabets, from a - z
- Map through this array with array.map
- For each alphabet on the array, create a span node, set the id to the alphabet and set a common classname ("alphabet")
- Create a text node with the letter as the inner text
- Append the textnode to the span and the span to our div ( alphabet section)
It should look something like this ( after styling )
Fetching a random word.
I’m sure you have heard about the acronym API before. We will be using an API called ‘Random word Api’ by M C Naveen, to generate a random word.
The endpoint we are going to use is https://random-words-api.vercel.app/word.
Steps:
- Fetch request at endpoint
- Resolve the promise with .then.
- Resolve the json promise returned
- Inside the callback, invoke a function that adds functionality to the game. By doing this, we make sure that the game becomes functional only after the fetch is successful
The game function:
Let’s split the fetched word into an array and store it.
To create empty spaces for the players to fill in, we use a similar method we used to create alphabets, but instead of an alphabet, the textnode will be an empty string.
Let’s then append it the div called word section.
Also, using foreach, we add an event listener to each alphabet.
We now define a call back for the event listener (click) we added in each letter, so that whenever a player clicks a letter, the call back is executed.
Inside the callback
- Select the letter with event.taget.innertext
- Check if the letter is in the word we fetched with array.includes
- Mark the letter as selected in the DOM ( for styling )
Now, the conditional. We have 2 conditions. \
a. The selected letter is in the word fetched.
b. The selected letter isn’t.
Selected letter is in the word
- Check where the selected letter is in the word and store it in an array(indexes). This lets us store multiple values.
- With DOM manipulation, add the letter in position stored in the array indexes
- Store the number of elements in the indexes array in a variable.
- If the variable becomes equal to word length, the player wins the game.
Selected letter is not in the word
- Increase the false count by 1
- When false count is 5, game over
That’s it! You successfully created a Word Game.
Feel free to build this on your own and modify it as you see fit.
If you have questions or improvements, feel free to ping me here.
Posted on May 14, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 8, 2024