Build a Wordle Clone in JavaScript HTML CSS
Resumo
TLDRIn this video tutorial, viewers learn to create a web-based game called Wordle using HTML, CSS, and JavaScript. The game allows players to guess a daily word within six attempts, providing feedback through color-coded tiles. The tutorial covers setting up project files, writing HTML for the game structure, styling with CSS, and implementing game logic with JavaScript. Viewers learn to handle user input, display hints, and manage game states, making it beginner-friendly for those learning web development.
Conclusรตes
- ๐ฎ Create a simple web game: Wordle
- ๐ ๏ธ Use HTML, CSS, and JavaScript only
- ๐ Provide feedback with color-coded tiles
- ๐ Handle user input and game logic
- ๐ฑ Ensure responsiveness for different devices
- ๐ Allow six attempts to guess the word
- ๐ก Improve by adding a word database
- ๐ Implement a keyboard interface
- ๐ Manage game states effectively
- โ Make it beginner-friendly for web developers
Linha do tempo
- 00:00:00 - 00:05:00
The video introduces a tutorial on creating a popular word-guessing game called Wordle using basic web development technologies: HTML, CSS, and JavaScript. The game involves guessing a daily word within six attempts, with hints provided for incorrect guesses.
- 00:05:00 - 00:10:00
The presenter explains the structure of the game, including the use of HTML for content, CSS for styling, and JavaScript for functionality. They create three files: wordo.html, wordo.css, and wordo.js, and outline the roles of each technology in the game development process.
- 00:10:00 - 00:15:00
The HTML structure is built, including the head and body sections. The title is set to 'Wordle', and elements such as the game board and answer display are created. The presenter emphasizes the importance of IDs for styling and JavaScript access.
- 00:15:00 - 00:20:00
CSS styling is applied to the body, title, and game board. The presenter demonstrates how to center elements and style the game tiles, including colors for correct, present, and absent letters based on user guesses.
- 00:20:00 - 00:25:00
The JavaScript section begins with the creation of global variables for game dimensions and the target word. The presenter explains the logic for tracking user input and managing game state, including the current row and column for guesses.
- 00:25:00 - 00:30:00
The game board is populated with tiles using a loop, and the presenter discusses the importance of using JavaScript for dynamic content creation. They demonstrate how to create tiles with unique IDs and apply the tile class for styling.
- 00:30:00 - 00:36:12
User input handling is implemented, allowing players to type letters, use backspace, and submit guesses. The presenter explains the logic for processing guesses, providing feedback on correct letters, and managing game over conditions.
Mapa mental
Vรญdeo de perguntas e respostas
What is the game being created in this tutorial?
The game is called Wordle.
What technologies are used to create the game?
HTML, CSS, and JavaScript.
How many attempts do players have to guess the word?
Players have six attempts to guess the word.
What feedback do players receive for their guesses?
Players receive color-coded feedback: green for correct letters in the right position, yellow for correct letters in the wrong position, and gray for letters not in the word.
Is any external library or framework used?
No, the game is created using only basic HTML, CSS, and JavaScript.
Can the game be played on different devices?
Yes, the game is designed to scale properly on different devices.
What happens if a player runs out of attempts?
The game reveals the correct word if the player does not guess it within six attempts.
How can the game be improved?
Improvements could include adding a database of words and implementing a keyboard interface.
What is the purpose of the tutorial?
The tutorial aims to teach beginners web development by creating a simple game.
What is the final word used in the tutorial for guessing?
The final word used in the tutorial is 'squid.'
Ver mais resumos de vรญdeos
Elon Musk on danger of Artificial Intelligence | SXSW 2018
LHMC Abdominal Transplantation & Hepato-Pancreato-Biliary Surgery Fellowship Overview
๐จURGENTE! DANILO GENTILI EXPLODE E DETONA PATRICK MAIA
denied credit? flip the script with consumer law
The Chaos Of English Pronunciation by Gerard Nolst Trenitรฉ
IKUT NABI ATAU IKUT IMAM MADZHAB?? USTADZ NURUDIN & USTADZ ADI HIDAYAT
- 00:00:00hey there hope you're having a wonderful
- 00:00:02day so in this video we're going to
- 00:00:04create a popular game well uh it's
- 00:00:07popular for now uh it's been on the news
- 00:00:10actually and a lot of people seem to
- 00:00:12know about it and that is wordo and so I
- 00:00:16think this is a good game for beginners
- 00:00:18who are uh learning web development so
- 00:00:22basic HTML CSS JavaScript so in this
- 00:00:25video we're going to create this game
- 00:00:27from scratch using just HTML CS says
- 00:00:30JavaScript no libraries no Frameworks
- 00:00:32nothing if you've never played this
- 00:00:34before you can just go on this website
- 00:00:36and basically it's a daily game that
- 00:00:39takes like 2 minutes and uh there's a
- 00:00:42new word each day you have six attempts
- 00:00:45to guess what that new word is every
- 00:00:47time you guess a word uh if you don't
- 00:00:50get it right uh you're given some hints
- 00:00:53you can see that the first guess I put
- 00:00:55in was Apple and there are some gray
- 00:00:58tiles and yellow tiles yellow tiles mean
- 00:01:00that the letter exists in the word but
- 00:01:02it's not in the correct position so
- 00:01:05there's a p and E in here so my second
- 00:01:08guess is press and you can see there's a
- 00:01:10green tile this means that the first
- 00:01:12letter is a p so that's correct and
- 00:01:14there's an RN e that I did not guess the
- 00:01:17correct position of and then you can
- 00:01:19keep guessing and so if I guess print I
- 00:01:23press enter and it will submit my guess
- 00:01:26and you can see that wasn't the right
- 00:01:28guess I can put penny I can also
- 00:01:31backspace right so I'm given six tries
- 00:01:33and after six tries if I don't guess it
- 00:01:36they will tell me what the word is okay
- 00:01:38so we're going to create this game and
- 00:01:41to do that you want to have an editor so
- 00:01:43I'm going to create a new file and I'm
- 00:01:46just going to call it
- 00:01:48wordo
- 00:01:50HTML uh did it create it here no okay
- 00:01:53I'll do it here wo.
- 00:01:55HTML and then I'm going to create
- 00:01:57another one wo. CSS
- 00:02:00and word.
- 00:02:03JS okay so we have these three files and
- 00:02:07basically HTML is all the content on
- 00:02:10this page so it's the title it's this
- 00:02:13little horizontal line these icons the
- 00:02:16tiles the letters so that's HTML CSS is
- 00:02:20basically your styling so adding colors
- 00:02:23so adding this yellow tile this green
- 00:02:25tile this gray one adding the borders
- 00:02:28here this keyboard uh we're not going to
- 00:02:30add the keyboard or these little icons
- 00:02:33because you it's a little too
- 00:02:34complicated um for now we're just going
- 00:02:36to focus on the actual game itself where
- 00:02:39you're guessing the word and being able
- 00:02:42to process user input and then finally
- 00:02:45we have JavaScript so JavaScript is used
- 00:02:47to modify elements on the web page so
- 00:02:51JavaScript allows us to type in you know
- 00:02:54a guess and then we can submit the guess
- 00:02:56so if I press enter a popup appears
- 00:02:58that's JavaScript and it will compare
- 00:03:01your guess with the word that they have
- 00:03:04so that's pretty much it your HTML CSS
- 00:03:06JavaScript okay so after I created my
- 00:03:08files um I can just open up the HTML one
- 00:03:12in my web browser so I just double click
- 00:03:14on this right here and you can see this
- 00:03:16is my uh blank HTML file now let's start
- 00:03:21working on this so
- 00:03:23first we have
- 00:03:26HTML I'm assuming you know a little of
- 00:03:29the basics if not that's okay I'll try
- 00:03:30to make it as um beginner friendly as
- 00:03:33possible and try to explain as much as I
- 00:03:35can so first we have head and we have
- 00:03:39the
- 00:03:40body okay so head is basically this tiny
- 00:03:44stuff so it's your your title right so
- 00:03:48in this case it just has the file name
- 00:03:49we can change it we'll make the title
- 00:03:53Wordle and then we're going to add a
- 00:03:56little uh some other stuff So Meta car
- 00:04:00set is going to be
- 00:04:02utf8 and it's just standard character
- 00:04:05set so we're going to also have meta
- 00:04:08name equals viewport and this will be
- 00:04:14content with equals device withth
- 00:04:17initial scale equal zero user scalable
- 00:04:23no okay so what this basically does is
- 00:04:27it scales your web page so that it
- 00:04:29doesn't look look weird in other devices
- 00:04:32uh so whether using your phone or an
- 00:04:35iPad or your computer it will scale it
- 00:04:37so that you know it will look nice and
- 00:04:41uh we also want to link our CSS in
- 00:04:43JavaScript files so basically just
- 00:04:46link and then uh this is
- 00:04:50stylesheet and our hre is going to be
- 00:04:54word. CSS and in our JavaScript script
- 00:04:59source is going to be wo.
- 00:05:02JS and then we close that tag okay so
- 00:05:05that's it for head and now we have body
- 00:05:08so for our body let's add the title and
- 00:05:11we're going to just put
- 00:05:13word and then if I save this and refresh
- 00:05:18it you can see wordo and we're going to
- 00:05:20style It Center it and change the font
- 00:05:23let's go back here and I'm going to add
- 00:05:26that line that line is basically HR it's
- 00:05:28just that little Thin Line line and I'm
- 00:05:30also going to add a breake line so what
- 00:05:33this basically does is it just adds a
- 00:05:36new line just pushes things down a
- 00:05:38little so it will create this white
- 00:05:40space between this line and this board
- 00:05:42all right so let's go back here and I'm
- 00:05:45going to create the board so div and I'm
- 00:05:48going to set it an ID
- 00:05:51board and I'm going to close that and
- 00:05:55then I'm going to add one more BR and
- 00:05:57then in case the user um or the player
- 00:06:01does not guess correctly we want to show
- 00:06:03them the answer right so I'm going to
- 00:06:05have another
- 00:06:07header and I'm going to set the ID to
- 00:06:10answer and I'm not going to put in
- 00:06:12anything uh we're going to populate this
- 00:06:14later if the user uh does not uh get the
- 00:06:18correct answer and for here I'm just
- 00:06:21going to make the ID title so the I IDs
- 00:06:24are used for uh styling and for
- 00:06:27accessing uh the element in
- 00:06:30JavaScript okay and it's different from
- 00:06:32a class in that ID is it's Unique so
- 00:06:35when I try to get an element with title
- 00:06:38it's always going to be this
- 00:06:40tag and uh yeah all right so that's HTML
- 00:06:44now we have our line and our title all
- 00:06:47right so this looks uh pretty ugly so
- 00:06:49I'm going to style
- 00:06:53it all right so first let's style our
- 00:06:56body so this is everything within our
- 00:06:59web page whatever we put as a style here
- 00:07:01would apply to all the tags in uh the
- 00:07:04body so basically everything inside
- 00:07:09here so I'm just going to change the
- 00:07:11font
- 00:07:14oops and the font is Ariel altica
- 00:07:20sanserif and text align we're going to
- 00:07:22center
- 00:07:23it okay and basically you just copy and
- 00:07:27paste the title World from the website
- 00:07:29and then and put our Microsoft Word or
- 00:07:32uh Google Docs and you can see it's
- 00:07:33Ariel okay so let's go back and let's
- 00:07:37also shorten that line so this HR line
- 00:07:41is very long but we want it to be about
- 00:07:44this length
- 00:07:46so so we're just going to make the width
- 00:07:49uh 500
- 00:07:51pixels and then title so to access the
- 00:07:54ID um that you want to style you use
- 00:07:57this hash and for classes use the dot so
- 00:08:00I'm going to use hash
- 00:08:02title and I'm going to make the font
- 00:08:04size so the font size we saw in
- 00:08:07microsoftw it was 27 but this is not the
- 00:08:10correct uh unit I guess of measurement
- 00:08:14so in Microsoft whereare they use PT we
- 00:08:18want pixels so we put in 27 convert and
- 00:08:20we get 36 so we're going to change this
- 00:08:24to 36
- 00:08:25pixels and we're going to make it bold
- 00:08:28so font weight bold
- 00:08:30letter
- 00:08:31spacing and make it 2 PX or two pixels
- 00:08:35so letter spacing basically adds spacing
- 00:08:39between the letters so they're not
- 00:08:40scrunched up together all right and then
- 00:08:44let's have our board so for now you
- 00:08:46won't be able to see the board because
- 00:08:48we didn't really populate it yet but I
- 00:08:50I'll add the colors so I'm going to set
- 00:08:53the width to 350 PX height to
- 00:08:56420 and I'm going to set the back
- 00:08:59background color let's make it green for
- 00:09:01now and if I go back and refresh this
- 00:09:04you can see I have my board here this is
- 00:09:07where we'll put our tiles and we have
- 00:09:11our nicely centered title and shortened
- 00:09:14line here
- 00:09:17so now uh let's Center the board so I'm
- 00:09:22going to do margin zero
- 00:09:25Auto margin top I'm going to push the
- 00:09:28board down a little bit
- 00:09:30by three pixels and then display flex
- 00:09:33and flex wrap
- 00:09:36wrap all right so display Flex what this
- 00:09:39does is if we go back here if you notice
- 00:09:43we have this uh board which is 5X 6 or
- 00:09:476x5 and what that Flex is going to do is
- 00:09:50going to keep our tile elements within
- 00:09:53that board okay within that box so for
- 00:09:57tile we're going to have a tile class
- 00:09:59that we're going to create uh I guess 30
- 00:10:02of so we're going to have tile right
- 00:10:06because it's a class and we're going to
- 00:10:08make the well let's start with the
- 00:10:13box so the border right so each each uh
- 00:10:19box starts with a border this gray
- 00:10:22border so just going to add that it's
- 00:10:25going to be 2
- 00:10:26PX two pixels solid and let's make it
- 00:10:30light
- 00:10:31gray and then I'm going to make the
- 00:10:33width 60 pixels and the height 60
- 00:10:38pixels and we want some space in between
- 00:10:41the boxes so we're going to use
- 00:10:43margin and let's set the space between
- 00:10:47each box by 2.5 pixels but because it
- 00:10:51applies to each tile it's going to be
- 00:10:542.5 pixels away from one tile and next
- 00:10:57to that tile is going to be another 2.5
- 00:10:59pixels away so in total there's going to
- 00:11:01be five pixels in between uh two tiles
- 00:11:05and then now uh let's add some styling
- 00:11:07for the text so we want to style the
- 00:11:10text and we want to center it change the
- 00:11:13font so let's do
- 00:11:16that so let's see text and I'm going to
- 00:11:20make the color black to begin with and
- 00:11:24font size will be the same as the title
- 00:11:2636 pixels font weight will be both
- 00:11:31old and then display Flex again keep in
- 00:11:34the box and justify content
- 00:11:39Center and align items
- 00:11:42Center so justify with Center it I think
- 00:11:46horizontally or vertically I forgot
- 00:11:48which one but you need both of them so
- 00:11:50one of them censers it horizontally and
- 00:11:52the other censers it uh vertically all
- 00:11:55right so we got that down and then now
- 00:11:58we have
- 00:11:59three more things to add and that is
- 00:12:02changing the color based on the hints
- 00:12:04right so we have uh
- 00:12:08correct we have uh present and then we
- 00:12:12have
- 00:12:15absent okay so for correct we want to
- 00:12:18change the color to Green so background
- 00:12:22color uh so I already went ahead and
- 00:12:25figured out which colors correspond to
- 00:12:27which uh hint type
- 00:12:29so 6 a a
- 00:12:33a64 this should be the
- 00:12:35green and then we're going to change a
- 00:12:37font color to white and make the Border
- 00:12:40color white as well so that uh since our
- 00:12:44background is white it's just going to
- 00:12:45hide the Border uh if we made the Border
- 00:12:48if we took away the Border it's going to
- 00:12:50make the size of the tiles smaller but
- 00:12:51we don't want to do that we just want to
- 00:12:53uh hide the border so we're going to
- 00:12:55make it white so it Blends in with the
- 00:12:57background and let's do the same for for
- 00:12:59our present so that's yellow so
- 00:13:01background color so it's going to be
- 00:13:06c9b
- 00:13:09458 and the color will be White and The
- 00:13:12Border color will also be
- 00:13:15white all right and then we have the
- 00:13:17last one absent and this is a dark
- 00:13:20grayish color 787
- 00:13:25c7e same thing color white
- 00:13:29border color
- 00:13:33white okay so our CSS is done let's go
- 00:13:36back and refresh so you can see so far
- 00:13:39we just centered this now we're going to
- 00:13:41use JavaScript to populate the tiles for
- 00:13:44you uh for the player to guess all right
- 00:13:47so before we continue let's talk about
- 00:13:49how we might design this so I have paint
- 00:13:52here and basically uh you have this uh
- 00:13:55board right so you have five columns and
- 00:13:59six rows so each column represents the
- 00:14:03current position for the letters of the
- 00:14:05word that you're guessing and the rows
- 00:14:07represent um how many attempts you're
- 00:14:09allowed right so the columns basically
- 00:14:12represent the length of the word so you
- 00:14:14have five columns and you have six
- 00:14:16guesses and so what we're going to do is
- 00:14:19we're going to
- 00:14:20create um 30 tiles and we're going to
- 00:14:23give each one an ID so in this case
- 00:14:26because this one uh is a index 0 0 we're
- 00:14:30going to make this ID something like
- 00:14:320-0 and then over here like this L this
- 00:14:36would be
- 00:14:370-3 this e over here would be 1-2 and so
- 00:14:41on so each ID will correspond to its row
- 00:14:44column
- 00:14:46indices all right so let's add some
- 00:14:48Global variables so we have our height
- 00:14:50and that's going to be six so we said
- 00:14:53this is the number of
- 00:14:55guesses and then we have our width so
- 00:14:57this going to be five it's going to be
- 00:15:00length of the word and the reason I am
- 00:15:02using variables is because you can
- 00:15:04change these however you like you can
- 00:15:07reduce the number of guesses or add more
- 00:15:09or you can change the length of the word
- 00:15:11so we're just going to keep it
- 00:15:12consistent with what wordo has and just
- 00:15:14make uh six guesses and five letters for
- 00:15:18the
- 00:15:19word okay so we're going to have row
- 00:15:22equals z and column equal Zer uh this is
- 00:15:26the player's current uh guessing
- 00:15:28position currently after two guesses the
- 00:15:31user or the player is here right so the
- 00:15:34row is two and the column is zero and if
- 00:15:36they guess let's say a the user would
- 00:15:39now be here right so we bump up column
- 00:15:41to one and then as the user keeps
- 00:15:44guessing and submits their guas we
- 00:15:47increment row by one so now row is three
- 00:15:49and column is back to zero again this is
- 00:15:52the current guess or attempt
- 00:15:57number and
- 00:16:00Uh current letter for that attempt okay
- 00:16:04whenever you play games you want to have
- 00:16:06a bull for game over so we're going to
- 00:16:08set that to
- 00:16:09false and the word uh let's just make it
- 00:16:13uh squid okay so that's the word that
- 00:16:17they'll be guessing um you could have an
- 00:16:19array of words uh something like
- 00:16:23words and put squid or apple and then
- 00:16:28you could you know have your word be
- 00:16:32completely random so maybe index one or
- 00:16:36zero or just uh use math to uh get a
- 00:16:39random index but I'm just going to keep
- 00:16:41it simple we're just going to use the
- 00:16:43same word throughout this uh tutorial so
- 00:16:46when our page loads we want to call a
- 00:16:48function so we're going to do window
- 00:16:49onload uh it's pretty self-explanatory
- 00:16:53uh when your page
- 00:16:54loads call this function and we're going
- 00:16:57to uh create an initialize function so
- 00:17:01we're going to call
- 00:17:02initialize and here we'll Define
- 00:17:04initialize and basically this
- 00:17:07will create our tiles and uh create some
- 00:17:12event listeners and whatnot for our uh
- 00:17:16game so oops forgot the e here okay so
- 00:17:19let's create the
- 00:17:22board which is basically your tiles your
- 00:17:25six uh attempts so
- 00:17:29we're going to do for Ral z r less than
- 00:17:33height
- 00:17:35r++ and then let C equals
- 00:17:39zero C less than WID
- 00:17:43C++
- 00:17:44C++ and we're going to create a new uh
- 00:17:48HTML element so we're going to say let
- 00:17:51tile equal to document do uh create
- 00:17:56element and we're going to make a span
- 00:17:58so it's it's similar to a paragraph tag
- 00:18:01except that it doesn't end with a new
- 00:18:03line so so with span you can put tiles
- 00:18:07next to each other instead of right
- 00:18:09below each other let's modify some
- 00:18:11Fields so tile. ID is going to be R2
- 00:18:17string and we're just going
- 00:18:19to make the ID like this so we're going
- 00:18:24to do R2
- 00:18:27string Plus the dash and c.2
- 00:18:31string
- 00:18:33okay and then we're going to add the
- 00:18:36class list to the class list um the tile
- 00:18:40class right so what that does is it's
- 00:18:44going to add this class with all this
- 00:18:47styling so basically when you create uh
- 00:18:50an element like this you're basically
- 00:18:52creating a
- 00:18:55span like this and then second line adds
- 00:18:59the ID to it so it'll be 0 0 for the
- 00:19:02first one and then the class will have
- 00:19:05tile and let's make the inner
- 00:19:09text uh let's put uh P for now and then
- 00:19:14finally let's do document. getet element
- 00:19:17by ID our
- 00:19:21board and we're going
- 00:19:23to append child
- 00:19:27oops child
- 00:19:30right so we have our inner text it's
- 00:19:31going to put the P here uh our code is
- 00:19:34going to find the board right by ID and
- 00:19:37insert this document or this HTML tag
- 00:19:41inside that div so it's going to do this
- 00:19:45and then it's going to create another
- 00:19:46one in the next for Loop and then
- 00:19:48another one so you can kind of see why
- 00:19:51we use JavaScript because we can use for
- 00:19:53Loops otherwise I would have to copy and
- 00:19:54paste this uh 30 times so that's that's
- 00:19:59uh tedious to do so after that let's
- 00:20:02save and see uh our
- 00:20:06changes uh oh yeah let's undo that
- 00:20:09background color where is it
- 00:20:13here
- 00:20:15okay uh something's wrong let's see what
- 00:20:18happened console tell that class list is
- 00:20:22not a function what happened here let's
- 00:20:25see to. classist do aend
- 00:20:29oops or add I think it's add should be
- 00:20:33add okay so let's go back
- 00:20:36check um what happened
- 00:20:39here is not a
- 00:20:42function I think I had an S
- 00:20:45somewhere element by
- 00:20:48ID okay there you go here's our
- 00:20:52tile all right so we have our six uh six
- 00:20:57rows and for each row we have five
- 00:20:59letters that the user can guess and as
- 00:21:01you can see that everything is nice and
- 00:21:03centered so let me show you what happens
- 00:21:06if we did not have some parts of our
- 00:21:08styling so for instance in our board if
- 00:21:11I did not have this display Flex to keep
- 00:21:14the tiles within the Box the board box
- 00:21:18let's see and it'll be like this a
- 00:21:20single column of 30
- 00:21:24tiles and in our tiles if I did not have
- 00:21:27justify content
- 00:21:29Center you can see p is centered
- 00:21:33vertically but not horizontally so it's
- 00:21:35over here when it should be a little to
- 00:21:37the
- 00:21:39right and if I coment out align items
- 00:21:44you can see that it is centered
- 00:21:46horizontally but not vertically so we
- 00:21:48need both of them so that it centers
- 00:21:52horizontally and
- 00:21:54vertically okay so let's go back to our
- 00:21:57code
- 00:22:00and let's make this an empty string
- 00:22:04so there you go now let's add our logic
- 00:22:08so we're going to process user input so
- 00:22:12listen for key
- 00:22:15press and we have document. getet or add
- 00:22:19event
- 00:22:21listener key up so we're using key up
- 00:22:25basically um when you're typing into
- 00:22:27wordo when you put your finger down on a
- 00:22:30we want to wait till the player lifts
- 00:22:33their finger up and that's when we
- 00:22:35register the key that they pressed
- 00:22:37because if we did key down then the user
- 00:22:40can just hold down the key and then put
- 00:22:42in 5 A's without lifting up a finger so
- 00:22:45that's why we use key up we're going to
- 00:22:47use um an arrow function here
- 00:22:53oops and if you don't know what an arrow
- 00:22:55function is it's basically this is the
- 00:22:57parameter e which is the key event and
- 00:23:00you have this Arrow which is why it's
- 00:23:02called an arrow function and then you
- 00:23:03have your uh curly braces and in here
- 00:23:06you put everything you want in your
- 00:23:07function so first things uh first uh if
- 00:23:11game
- 00:23:12over basically if it's game over you
- 00:23:15just
- 00:23:16return right so this is going to make
- 00:23:20the page uh constantly listen uh for key
- 00:23:23uh for key presses and so the user can
- 00:23:27just type in Keys you don't want that to
- 00:23:29happen after they used up all all of
- 00:23:31their guesses or if they you know got
- 00:23:34the word correctly you shouldn't process
- 00:23:37the inputs anymore right so that would
- 00:23:38be game over so why don't I show you um
- 00:23:43by using alert I'll do alert uh e. code
- 00:23:47this will tell us what key was pressed
- 00:23:49right so e. code so if I refresh this
- 00:23:53you can see F5 was pressed if I type in
- 00:23:56a you can see key a was pressed rest I
- 00:23:59put backspace
- 00:24:01backspace
- 00:24:03enter uh
- 00:24:05p k so that's how you can uh figure out
- 00:24:10what key was pressed so let's comment
- 00:24:12that
- 00:24:13out all right so first we only allow uh
- 00:24:17certain keys to be pressed so A to Z the
- 00:24:21backspace and enter nothing else is
- 00:24:24allowed so to check to see if the user
- 00:24:28uh press an alphabet key we will use an
- 00:24:30if statement and we'll say key a as you
- 00:24:33saw before when I pressed a the code was
- 00:24:35ke not a but uh key a so key less than
- 00:24:42or equal to e. code and e. code is less
- 00:24:45than or equal to
- 00:24:47keyz so this less than or equal to this
- 00:24:51comparison is you're basically comparing
- 00:24:53the dictionary order right so if I put
- 00:24:56in like if I press a one or two or
- 00:24:59backspace uh they're not within the
- 00:25:01range of ke and keyz in you know lexical
- 00:25:06order uh within here I will check if
- 00:25:09column or the column that the user is
- 00:25:12currently uh inputting a letter into we
- 00:25:16need to check to see if it's less than
- 00:25:19uh five right because the indices go
- 00:25:22from 0 to four so if they are on let's
- 00:25:26say this column
- 00:25:29then that is less than the width which
- 00:25:31is five
- 00:25:33right so if they're on this column they
- 00:25:35can still guess this one this one and
- 00:25:38then after they guess this one we
- 00:25:40increment them to be on uh column five
- 00:25:43which is just temporary to say that oh
- 00:25:46uh you've input all five characters you
- 00:25:48can press enter now if they don't uh put
- 00:25:51in all five characters putting enter
- 00:25:53does nothing all right so if column is
- 00:25:55less than the width we're going to
- 00:25:59uh we're going to check again to make
- 00:26:00sure that the character at that tile is
- 00:26:04blank so that uh they just going on to
- 00:26:06the next one or they're pressing enter
- 00:26:08but they're not overwriting it so
- 00:26:11current
- 00:26:12tile or actually let's get the current
- 00:26:15tile first so let current
- 00:26:19tile go to be document. getet element by
- 00:26:23ID and it's going to be uh let's see row
- 00:26:28do to string plus the
- 00:26:31dash plus call. to
- 00:26:37string and then if C
- 00:26:40tile.
- 00:26:43inext is an empty string so it hasn't
- 00:26:46been filled we're going to update it so
- 00:26:50c.in text is going to be e. code and
- 00:26:54we're going to take uh index 3 right
- 00:26:57because it returns
- 00:26:59uh four character string key a key B Key
- 00:27:02C we just want the letter so we don't
- 00:27:05want the word key just we just want the
- 00:27:07letter and then we're going to increment
- 00:27:10column by one okay so let's save and
- 00:27:14let's go back now I can type some stuff
- 00:27:16in a p e and that's it right I'm stuck
- 00:27:22uh stuck at five I can't type anymore I
- 00:27:24can press enter and it should you know
- 00:27:26give me the results but we have an
- 00:27:27implement
- 00:27:29today now if the key press was not a
- 00:27:32letter key maybe it's backspace so we'll
- 00:27:35do else
- 00:27:37if e. code is equal to backspace in this
- 00:27:43case uh it's pretty
- 00:27:45straightforward um if
- 00:27:48Z call call and width so if the column
- 00:27:55uh that the user is currently on is
- 00:27:56between one and uh the width five then
- 00:28:01they can press back so zero does not
- 00:28:03count because uh if you're on zero that
- 00:28:05means you haven't uh started adding
- 00:28:08letters for that row you can only go
- 00:28:09back if you are within this area right
- 00:28:13so if you put in like CDE e f g then
- 00:28:19currently you're on a column five which
- 00:28:22is not a valid index then you can
- 00:28:25backspace and go back here backspace
- 00:28:28backspace backspace backspace and when
- 00:28:31you're on zero you can't backspace
- 00:28:33because there's nothing to backspace to
- 00:28:35okay so over here we're going to say if
- 00:28:40uh backspace was pressed we're going
- 00:28:43to do call minus equal
- 00:28:471 and then we're going to get the
- 00:28:50tile right because we did minus equal 1
- 00:28:53we need to get that updated ID so we're
- 00:28:56going to get copy this code
- 00:29:00and we're going to set it
- 00:29:02to uh set the inner text to A blank
- 00:29:08string okay so let's go back and try it
- 00:29:13out so I'm going to say AP I can
- 00:29:16backspace backspace backspace and that's
- 00:29:18it I can do
- 00:29:20APLE backspace backspace okay so that's
- 00:29:24nice now we just need to hit enter and
- 00:29:28have the game tell me some hints or tell
- 00:29:31me whether I got the word or
- 00:29:33not
- 00:29:36so what we're going to do is else if e.
- 00:29:40code is equal to
- 00:29:43enter we're going to call an update
- 00:29:46function that will oh what is that nope
- 00:29:50we're going to call an update function
- 00:29:52oh now come on
- 00:29:56update function
- 00:29:59and we're going to increment the row by
- 00:30:02one right so basically you use up your
- 00:30:05attempt for this one go on to the next
- 00:30:08attempt and set the column to zero start
- 00:30:12new
- 00:30:13row start at zero for new row and then
- 00:30:18so before I write my update function I
- 00:30:20also want to do one more check here and
- 00:30:22that is if not game
- 00:30:26over and row is equal to
- 00:30:30height that basically means you used up
- 00:30:32all your attempts if the row is equal to
- 00:30:34height that means you used up all six
- 00:30:35attempts we're going to say game over is
- 00:30:38equal to
- 00:30:39true and then we're going to reveal the
- 00:30:43answer in here so we're going to
- 00:30:45populate this okay so document. getet
- 00:30:49element by ID
- 00:30:51answer and we're going to set the inner
- 00:30:54text to
- 00:30:56word okay let's write our update
- 00:31:01function so in our update function we
- 00:31:03want to count the number of letters we
- 00:31:06get correct so we're going to set that
- 00:31:09to zero so first we're going to iterate
- 00:31:11through the letters of the word that the
- 00:31:14player guessed so for let c = z c is
- 00:31:20less than width
- 00:31:23C++ we're going to get that tile so it's
- 00:31:27this
- 00:31:29so let's just copy and paste
- 00:31:32this and we're going to say letter is
- 00:31:36going to be C tile.
- 00:31:40interex and then so
- 00:31:45if uh if the letter or actually let's
- 00:31:50phrase it as a question is it in the
- 00:31:53correct
- 00:31:55position so if word at index C is equal
- 00:32:00to the
- 00:32:01letter then we're going to say tile.
- 00:32:04class list.
- 00:32:06add correct so it's going to apply that
- 00:32:09styling with the green
- 00:32:12background and we're going to increment
- 00:32:14correct by
- 00:32:16one now if it's not in the correct
- 00:32:18position we'll ask is it in the word
- 00:32:22then so else if word. includes letter
- 00:32:29and then we're going to say tile. class
- 00:32:31list.
- 00:32:33add
- 00:32:36present and then finally not in the word
- 00:32:40so we're we're just going to say else
- 00:32:44tile. class list. add absent okay and
- 00:32:50finally we also want to update game over
- 00:32:52in case the user happens to guess it
- 00:32:55before this condition where row equals
- 00:32:57height before they uh use up all their
- 00:33:00attempts so another way for game over to
- 00:33:03happen is when they they guess it
- 00:33:04correctly so if correct is equal to the
- 00:33:10width then game
- 00:33:12over is
- 00:33:15true all right and I think that's it for
- 00:33:19the implementation if we refresh we can
- 00:33:22put
- 00:33:24Apple uh okay so there's a bug somewhere
- 00:33:27see
- 00:33:29inspect cannot read inner text so line
- 00:33:3374 Aero current tile.in text ah this
- 00:33:38should be instead of column or call it
- 00:33:41should be
- 00:33:42C all right so let's change that to C
- 00:33:47and let's see if that worked so let's
- 00:33:50refresh and I put in
- 00:33:53apple um oh I guess guess didn't work
- 00:33:57let's see what happened
- 00:34:00here uh tile is not defined yeah you got
- 00:34:04to be careful with these
- 00:34:06namings you got to be careful with the
- 00:34:09variable names so s tile we're going to
- 00:34:12put Cur
- 00:34:14tile and CTI
- 00:34:17here okay so that should be it let's
- 00:34:21refresh I'm going to put Apple you can
- 00:34:24see there are no uh letters in apple
- 00:34:27that appearance squid so I'm going to
- 00:34:28put uh Tower none okay Queen we have two
- 00:34:34Q so q and u exist in the word but
- 00:34:37they're not in the right position so
- 00:34:39let's try Squat and you can see SQ and U
- 00:34:42are in the right position
- 00:34:44so uh we can also write yeah let's just
- 00:34:48Solve IT squid okay so that's pretty
- 00:34:51much wordo and I can you know put in
- 00:34:54other words too actually I didn't do a
- 00:34:56check to make sure that it's valid word
- 00:34:58so maybe that's a feature that you can
- 00:35:01Implement so I can just do this just
- 00:35:06random and you can see squid is revealed
- 00:35:09to me at the end okay so some things you
- 00:35:12can do to improve is add a database of
- 00:35:15words you can make a long array of words
- 00:35:18um you can also add that keyboard from
- 00:35:20wo if you
- 00:35:22want there's also one more thing and
- 00:35:24that is the highlighting issue so if I
- 00:35:26put in five s's in wordo only one of the
- 00:35:30s's should be highlighted because only
- 00:35:33one exists in the word squid but this
- 00:35:36implies somehow that there are five s's
- 00:35:39which uh so this is uh something that
- 00:35:42maybe you might want to fix and uh yeah
- 00:35:45that's it for this tutorial and if you
- 00:35:47found this tutorial helpful and you
- 00:35:49learned a lot please leave a like if you
- 00:35:51have any questions or comments uh leave
- 00:35:54them down below and I'll take a look and
- 00:35:57if you you you know if you decide to
- 00:35:59take this code and continue working on
- 00:36:02this clone of wo uh please do let me
- 00:36:05know I would like to see it and uh yeah
- 00:36:07that's pretty much it for this tutorial
- 00:36:09and hope you have a good one bye-bye
- Wordle
- HTML
- CSS
- JavaScript
- Web Development
- Game Development
- Tutorial
- Beginner
- User Input
- Color Coding