Placeholder Image

字幕表 動画を再生する

  • Hello, everyone.

  • In today's view, we're gonna be taking a look at making a fun project, which is tick tack toe, and this is a great project because it's a very simple game.

  • But there's a lot of complex stuff that you have to think about and understand in orderto actually build a really good working tick tack toe project.

  • So let's get started now.

  • Welcome back to Web, Debs simplified.

  • My name's Kyle, and my job is to simplify the Web for you.

  • So if that sounds interesting, make sure you subscribe to my channel for more videos just like this one.

  • And to get started on the right hand side.

  • Over here, I have the finished version of our tick tack toe.

  • As you can see, it's very simple, and when we have her over, we can select exes, and then it's going to go over to let us select, sit close and then exes and oh's and exes and oh's.

  • And eventually, when someone wins, it's going to say that they won and we have the option to restart, and we can do it all over again.

  • But if for some reason no one actually wins so If we get aboard that ends up looking like this, it'll say draw and then it'll allow us to restart.

  • This may look really simple, but there's actually a lot of complex logic that goes into it, which I'm going to be covering throughout this entire video.

  • So make sure you don't miss any of it to get started, though All we need to do is first created html page.

  • We'll call it index dot html and if we type exclamation point and hit enter, it's going to generate all of the boilerplate clothes for us.

  • As you can see here next, we're gonna add in a link tag for STI SS, and we're gonna use a style sheet called stiles dot CSS.

  • This is where we're gonna put all of our styling so we can have all the hover effects, and then we're gonna create a script tag, which is just going to be here script dot Js And this is where we're gonna put all of the logic and we're just gonna make sure we defer this.

  • So what loads after the body of our HTML.

  • So that way we have access to everything inside of the body and now to actually open up this index file.

  • If you install the extension called live server, you can just right click this and open it with live server.

  • And it's gonna show up on the left hand side here on the right here, as you can see, and that's just a completely blank screen.

  • And every time we make changes and save, you see that they're going to be persistent over here.

  • So what we need to do is first create our board for our tick tack toe so you could just create a div with the class of board.

  • This is going to be where all of our tick tack toe elements are going to be inside of it.

  • And we also want to be able to access this and the JavaScript.

  • So we're going to give it an I.

  • D.

  • Of board as well.

  • This will just make it so we can easily access this in the Java script and the reason I'm not using the class to access it in the Java script s so that I can separate my CSS and my JavaScript.

  • I have an entire block article on this which I will link to down their description.

  • If you want more information about that now, the next thing we need to do is we need to have nine different cells, one for each one of the places we can put a mark.

  • So an easy way to do that with end it Because we can say dot Sell This is going to give us one cell.

  • But if we want nine, we can just say times nine and now you can see that we have nine cells being generated.

  • Also, I'm going to use data cell to denote thes again.

  • This is so we can easily access this information in the Java script without actually using the class cell.

  • Since this is going to be for our CSS, and that's all we need to create our board, all that's left to do is to create the page that shows up when someone actually wins.

  • So if we just come here and have them when we want to be able to create this page here, so an easy way to do that is we're gonna create something we're gonna call it winning message.

  • This is going to be that def.

  • That's gonna have that black section around it and inside of here we're gonna first want to have a div which is going to have this text here so we can just say div, you want this toe?

  • Have a data.

  • What's winning message text just like that.

  • This is where our winning message text is gonna go.

  • So exes, winos win, draw whatever it is.

  • And then also, we need to have that button so it's create a button and inside of here we just want to put the text restart and we're gonna give it an I D.

  • Which is just going to be restart button and up here.

  • We also want to give an I d toe are winning message.

  • This is just again so we can access it easily in the Java script.

  • And this right here is all of the HTML we meet.

  • If we refresh you see, we just have a single restart button.

  • That's fine for now, because to actually make this look good like this, we're gonna need to do a bunch of styling and R C s s.

  • So it's actually create that styles that CSS and start working on all of the CSS styles Now, the first thing that I want to do is set up some basic box sizing, so we can just say everything selector.

  • We want everything in the after and we want everything that is going to be a before element.

  • Essentially, this selects absolutely everything.

  • We're gonna set this to box sizing of border box.

  • This'll make styling our wits and heights so much easier.

  • The next thing that I want to do is to work on actually styling the body.

  • This is again just going to be really easy.

  • We just want to remove margins.

  • And if we say that you'll see this button over here moves up because we got rid of all of the margins on the page that'll make this black background so much easier to put in place.

  • The next thing to work on is the actual board.

  • If you remember, this is the element that wraps all of our cells.

  • And we want this to be here a with for example, of 100 view with and we want to give it a height lives right of 100 of you height and what this is going to allow us to do is actually make it so that our board is gonna fill the entire screen, which will make it so we can very easily center it inside of our screen.

  • So dissenter it.

  • We're gonna use displayed grid toe, lay out our items inside this grid, and we're also going to justify content in the center and a line I content in the center this is going to center are bored element.

  • So everything inside of the board is gonna be centered inside of the board, Dave.

  • And we can see that by just selecting our cells.

  • We're just gonna give it a width for now of 100 pixels and a background color of black.

  • And if we come over here and make sure that we give this height as well of 100 pixels, you can see we have nine different cells in here, and they're all perfectly centered inside of this screen, which is great now to make it so that we actually have our grid elements inside of this three by three grid instead of just in a line we can do is we can select grid template two columns and we want to repeat three separate times So we're gonna have three separate columns, all just auto sized.

  • So they're gonna be the same size as the individual cell down here.

  • And if we save, we now have this three by three grid.

  • If I just give this a little bit of a border will say one pick soul, solid white, you can see it.

  • That's three by three grid showing up dead center inside of our screen.

  • Which is exactly what we want now.

  • Too easily modify things with our width and height and make sure everything's super automatic.

  • What we're gonna do is come up here and select our route element and we're gonna create some variables.

  • The first variable we're gonna create is going to be our cell with which is going to be 100 pixels.

  • What?

  • She just changed us to sell size, since it's the same width and height.

  • And then we can set our with to be that cell size because the same thing tore height.

  • And if we save you see, nothing has changed.

  • And if you're not familiar with CSS variables, I have an entire video covering them, which I'll link in the cards and the description down below.

  • Now the next thing we need to do is, as you can see, here are exes and oh's are slightly smaller than the size of our container that were put into a slightly smaller than this cell.

  • So what we want to do is we want to create a market size.

  • This is going to be how large these X's and O's they're gonna be.

  • And we can actually just calculate that from our cell size so well, access our cell size.

  • We're gonna multiply this by 0.9.

  • Essentially, we want this market size to be 90% of the cell size, and we'll use this a little bit later when we create our exes and ah Rose.

  • Now the last thing we have left to do to ensure that our exes and oh's are always centered inside their box.

  • We can do justify items center, and we could do a line items in the center, and this is going to align the actual individual things inside of our cells and not the actual cells themselves.

  • And if you're confused about great it all, I have an entire video covering CSS grid on like that again in the cards and the description down below.

  • So now that we have our board set up, we can actually work on finishing up what our cell is gonna look like.

  • Obviously, the background color is not gonna be black.

  • So the corner move that.

  • But we are going to use a black border around these.

  • If we come over here, you can see we have our black border set up, and it looks pretty good.

  • Other than the fact that we have all these extra borders around the outside that we want to get rid of.

  • This is actually fairly straightforward to get rid of.

  • We know that this is a three by three grid.

  • So the 1st 1 here is 123456789 So what we want to do is we want to remove the grid from our 1st 3 elements here.

  • So we can say so.

  • First child, we want to again come in here, and we want to get this cell of the second child so we can use and child to weaken, do the exact same thing before 1/3 child.

  • So and the child of three and we want to remove the border top to be none.

  • And if you say that you can see we got rid of the border on the top of our page, the next thing we can do is we can select ourselves of and child.

  • And instead of selecting individual child's, we can say three n plus one on with is going to do is going to go through all of our Children inserting end here.

  • So for one, it's going to be three times one, which is three plus one, which is four.

  • That's going to be this cell right here for zero.

  • It's gonna be cell number one, and for two it's gonna be cell number seven.

  • So it's these left three cells we can do as we can.

  • Just set the border to the left to be nun.

  • And if we save, you can see we got rid of left border on those left three cells.

  • We could do essentially the exact same thing.

  • If we just copy of this and we want to do instead of here three and plus one, we're gonna do three and plus three, so that'll get the right side of ourselves.

  • We can save border right is none.

  • And there we go.

  • We got rid of that right side, and the last thing left to do is the exact same thing we did here.

  • But instead of the first child, we want the last child.

  • So we can say last child, we want the eighth child and the seventh child.

  • So these are gonna be our bottom three, so we'll get rid of the bottom order.

  • And there we go.

  • Now we have our tick tack board set up exactly the same sort tick tack forward over here.

  • The next thing I want to work on is the individual exes and those that go inside of our cells.

  • And we're gonna use a class of dot X.

  • So we'll have dot So dot X and this is going to be where the styles for Excel was gonna go.

  • So let's make sure our first so here is just going to be an XL.

  • And for now, we can just change the background color to red.

  • And as you can see, our first cell is going to be that ex cell so we can remove this packer and color and get started on adding in the X And you may think the easy way to do this is just to add it X inside the cell like this.

  • But then we have to worry about font sizes.

  • And this ex, depending on what fun you use, is gonna look different on different browsers and with different funds.

  • And it's gonna be hard to center.

  • It's just not gonna look very good.

  • So instead, what I think we should do and what we are going to do is we're gonna use just plain CSS to create the X shape for us.

  • It's actually fairly straightforward as long as we use pseudo elements.

  • So we're gonna use the before and the after element so we can get here the before and the after element.

  • And inside it here, all we need to do is set a content to something.

  • In this case, it's going to be an empty string that way, these before and after elements render.

  • And then we can come in here and create a with what we're just going to calculate the size of this with, for example, was going to be our mark sighs, and we're just gonna multiply that by 0.15 we want it to be about 15% of the height of our X seven.

  • Our height is going to be that variable mark size and just to see how this looks but set in the background color of black.

  • And if we save and you'll notice, nothing is actually happening yet, which means there's probably a problem with our mark size variable.

  • Let's go take a look at how we define that and you'll see that I missed to see in calculus.

  • So now let's say that and you'll still notice nothing is happening.

  • And the main reason for this is the fact that our cell is not actually displaying the contents inside of it very well.

  • We want to change this, to be displayed flex, justify the content in the center and align the items, what items to be in the center.

  • And now if we say that, you can see that our bars air showing up and they're looking exactly like we want them to.

  • But right now, word rendering an L and we want to render an X.

  • Right now we have two different levels being shown so we could just select one of these cells were going to say that before one, for example, and we can actually just rotated by saying, Transform, rotate, and we're gonna rotate this 45 degrees.

  • And as you can see, that's one part of our ex done.

  • Now let's do the same thing for after Element and we're gonna wrote date this the opposite direction and you can see we have the other portion of our ex done, but it looks not very good.

  • Luckily, there's a really easy fix to make this work right now.

  • The reason that they're showing up offset like this is because they're both displayed next to each other in the document float because their position static.

  • What we need to do is position them absolutely that way they're going to have the exact same base, which is going to be the parent, and they won't overlap over top of each other, and always to do now is set it so that the cell is position of relative just like this.

  • And now, as you can see, we have a perfectly scaled X, which has the exact same menace based between all of the different edge is exactly like we want the next thing we want to work on is the O class.

  • The circle class.

  • So we can do is just make this a class of circle, which is what we're gonna use to create these O's right here and again.

  • You may think, Let's just put in a big capitals.

  • Oh, and that may work.

  • But depending on your fonts and everything, it's not gonna be a perfect circle.

  • It's not gonna look right.

  • It's just not gonna be what you want.

  • So instead, we're gonna create this entirely inside of CSS.

  • We're gonna do it very similar to how we did this X So just copy down this X will just save this portion right here.

  • We want a position.

  • Absolutely.

  • We want a blank content.

  • We want the width and height to both be the same.

  • We're both gonna be this mark size just like that.

  • Make sure that we put thesame Colin here.

  • And now if we say that you can see hopes for change this to be circle and this one to be circle, you can see that we have a giant rectangle or a square.

  • Actually, that's black right here, which is perfect for now.

  • All we need to do is turn these into two separate circles.

  • So the first thing that we can do is we can give the before elements.

  • So we could say about circle before.

  • And we want this to be the larger of our circles because we wanted to be behind the other smaller circle so we can copy over this width and height.

  • We want the large with a night we're gonna make sure we set a border radius here of 50%.

  • So it's going to be a circle, as you can see, and we're gonna change the background color of only the before one to be black.

  • Now, if we do the exact same thing but for the after looks after and we changed this to be slightly smaller, we're just going to say maybe times 0.7, so it's gonna be slightly smaller.

  • So now we have a wit and a height that are both 0.7 of the original width and height of the larger circle.

  • And if we change the background corrida white, you can see now we have a perfect looking circle inside of our box here.

  • Whenever we put the circle class Now this is working great and everything is showing up perfectly.

  • But there's a few extra things we can do to make this work even better.

  • As you can see in this example, when we hover over a row, we get that nice little pointer icon.

  • But we also get the symbol that whoever's turn it is so, for example, it's excess terms we getting X.

  • Also, if we hover over a place that already has an indicator, we get this nice, not allowed symbol showing up saying that we cannot click on this.

  • So the first thing we can do is for all of ourselves.

  • What we want to do is you want to add a cursor, lips a cursor.

  • We want this to be the point, your cursor.

  • And now you can see we get a pointer cursor for every cell we highlight over.

  • But if we highlight over one of these cells, that already has something inside of it.

  • So if we highlight over a so that is either an ex so or a cell that has the circle class, so essentially it already has an element inside of it.

  • We want to change the cursor here to be not allowed.

  • Now, As you can see, we get that nice little not allowed color, sir, over these ones and the pointer cursor over all of these ones.

  • Now let's work on actual hover effect, and this is going to be by far the most complex part of the CSS.

  • So in order to determine whose turn it is, we're gonna apply a class to the board, either circle in the case of the circle, or we're gonna use X in the case of X.

  • So let's just start with ex, just like we did before.

  • So now every time I hover over one of these elements, it should show me the X hover.

  • So it should show me this X that you see right here.

  • And in order to do this, we need to add hover effects to ourselves.

  • So the first thing we want to do is select our board when it has the X class applied to it, and we want to get the cells and cited that board.

  • And whenever we hover them, we want to do something to them.

  • And what we want to do in here is actually the exact same thing that we're doing down here, we're just gonna be changing the background color.

  • So what we can do is we could just copy this over and added down to here.

  • So we want to get the before and then we also wanted to the exact same thing, but for the after element.

  • So now whenever we hover over a cell, when it's X's turn, we're going to apply this class to it.

  • We also want to do the exact same things down here with this before, So we want to make sure that we're only selecting the before.

  • So here now, when we hover, we're doing the exact same thing.

  • And what's copy over there after and do it down here.

  • And if we go over here, you can see now when we hover, get this X, and it's going to do that on all of them.

  • As you can see, it's even doing it when we have over here or over this one.

  • But we want to do is actually just make us That's only gonna work when we hover over an empty cell, essentially a cell that doesn't have an ex or oh in it.

  • And this is where we can use the not selector so we can say colon not dot x versus ST It doesn't have an X, and we want to do colon not dot circle.

  • So what this is saying is on.

  • Li applied this hover effect when you're not already on a dot x or a dot circle cell and we want to do this for all of ourselves.

  • So she's copy this down, all of them.

  • And now when we hover over one of these X's or O's, you can see it's not overriding what we've already done there.

  • It's not doing this hover effect because it's checking this, not condition right here.

  • It's fairly complex, but if you think about and analyze it a little bit hopeful, it'll make sense for you.

  • Now.

  • The last thing we need to do is obviously change this background color, so it's just come up here.

  • We have our hover.

  • We want to make sure we copy this just like this one were hovering over a cell that's not an X, and it's not a circle.

  • What we want to do is change the background color to be light gray just like that, and we want to do the exact same thing for after So I've come appeared and we're gonna say after.

  • But of course, if we hover over here, you see that that's not actually working.

  • And that's because we already have black being defiant in here, and it's overriding the light gray that's above it.

  • So we need to do is also just select our cell dot Exe.

  • We want to do our before and we also want to do it for after.

  • So we'll say dot sell dot Exe after What we want to do is put the background color of black inside of there and just make sure that this is defined above our other selector.

  • Just so it'll override properly.

  • And now, as you can see, we get a light gray ex every time we hover over and up.

  • And so when we have this X class on our board, So now let's just do the exact same thing.

  • But for circle now, we should get a circle whenever we hover over any of these different rose.

  • This is gonna be a very similar tactic to what we did before so we could just find our circle code, which is right down here.

  • And what we want to do is you want to say when we're on a board that has the circle class, we want to get the cells inside of it that are not already in X and that they're not already a circle We want to check when we hover over.

  • Then we want to style the before element.

  • And we also want to style the after element to be exactly the same.

  • And we want them to have this different position and such just like this, we also down here want to do the exact same thing.

  • So we want to select the before to go with this before and you can see that's working here and the exact same thing here.

  • But we want to do this for the after.

  • Now, if we hover, you see, we're getting the black circle so again, waiting to remove this black out of this area and put it up here just like we did this with this cell for ex soldiers say so dot circle and we want to do it before and we want to change the background color here to be black and actually, since this is exactly the same.

  • We can just put this up with this class over here and then what we can do down here.

  • Just say when we have a board that has the circle class and a so inside of it, that is not an ex, and it's not a circle.

  • Whenever we have her over it, we want to select it before element and change the color to light gray.

  • Now, lastly, to get this to work, all we need to do is come down here, remove this background color of black and if we save, you can see we now have light grey circles appearing whenever we hover over one of these cells.

  • And if we change this back here to be X, we now get the excess, which is exactly what we want.

  • Now, finally, the very last thing we have left.

  • His style is going to be this button down here, this restart button, so it shows up dead center in the middle of the screen.

  • Luckily, the styling for this is going to be so much easier.

  • What we want to do is we want to select that winning message did that we created and we're gonna do is we want to make sure this is positioned always in the center of the screen.

  • And we wanted to always take up every space.

  • So we're in a position it fixed.

  • Britta said the top 20 the left to zero, the rate to zero and the bottom 20 And if we come in here and we just add a small background color so we can actually see this, we're gonna use our G B A.

  • We want us to be Blacks will say 000 and 90% transparent.

  • And as you can see, we have a black overly covering the entire screen.

  • The next thing to do is to make it so that our button is centered inside this.

  • So we'll just come in here display of flicks, justified content in the center and will align items in the center.

  • And as you see it is now Senator Button Force, which is exactly what we want.

  • Also, what's at a little bit of text in here?

  • We'll say ex winds just so we know what this text is going to look like when we get to that point of styling and what we can do instead of here as we can change the color toe.

  • Wait because as you can see, it's impossible to read this text.

  • So now we have white text and we'll change the font size to be five r e m.

  • So we have really big farm, so we can easily see it, which is exactly what we want.

  • The next thing to do is we want to style are winning message button.

  • This is the button inside of this winning message, and what we can do is set the font size to three r e m.

  • So it's a lot easier to read.

  • We can set the background color to be white lips.

  • Wait, We want a border around it, which we're just gonna do one pixel solid black.

  • We use a little bit of padding 0.25 PM and 0.5 e m.

  • And then lastly will make sure the cursor is pointer.

  • So now, as you can see that button, it doesn't look too much different.

  • But it's a little bit different, and it looks pretty good in my opinion.

  • Lastly, we're gonna select that winning message button again, and we're going to give it a hover state.

  • So we want to come in here and just invert everything.

  • So we're gonna change the background color, toe white The border color gives order.

  • Color is going to be white In our background color, it's actually gonna be black.

  • And we're gonna change our text color to be Wait, wait Now if we ever you can see it just inverts that button which is exactly what we want And to stack these vertically We can just change our flex direction to colon And as you can see these air now stacked on top of each other And it looks really good The last thing to do is just by default make it display of none Since we don't want this to show it by default and then we can move our display flicks into a separate class.

  • Well, we're just gonna call winning message that show and inside of here we're gonna do display flex.

  • So now if we apply the show class, do this, it's gonna show up.

  • But without that class, it's not gonna show up.

  • Okay, we're finally now down with all the styling.

  • I know it's a lot, but we're gonna get into the fun part, which is the logic to make this tick tack toe actually work.

  • But before we do that, let's just clean up all of these safe and extra classes and text.

  • We added in here, since we don't want any of that showing up in the final version And there we go, we don't have our empty cell exactly like we want it.

  • So now the first thing we need to do is just create that script dot Js file, which is going to be all of our JavaScript and the very first.

  • Do you want to do this?

  • Select all of these different cells.

  • So what we can do is just say, constant cell elements is going to be equal to document lips document dot queries selector all and remember, these are just a selector of data cell just like that.

  • This is going to be over different cells and we can look through them by saying so little men stopped for each cell we want to do is add event listeners will say so that at event listener, every time we click on the cell, we want to add this British gonna call it handle, click and an important thing as we want to say once is true.

  • So inside of the object we're gonna say Once true, which essentially means on Lee Ever fire this event listener once.

  • So once we click on the cell, it's not gonna fire again.

  • So if I create that function of handle, click just going to take our event I just cancelled out Log clicked.

  • And if I inspect this drag over this inspection and I click on one of these cells, you see, it says clicked if I click the same cell again, is not going to fire until I click a different cell.

  • Essentially it on.

  • Lee doesn't click event once, and we only ever want to be able to add once to this element.

  • So it makes sense.

  • We only want to fire this once and no more because we don't want to be able to override something that's already been done.

  • So inside of this function, we actually need to do quite a few things.

  • The first thing that we need to do is we need to place the mark.

  • The next thing we need to do is check for win.

  • Then we need to check for a draw.

  • And then, lastly, if none of those happen, we need to switch turns.

  • So the easiest thing to do first is to actually place the mark.

  • But to do that, we need to know whose turn it is.

  • So we're gonna create a variable.

  • I was gonna say circle, turn.

  • And if this variable is true, then it's circles Turn.

  • If it's false, it's excess turn.

  • So we can easily determine which class were using by checking whose turn it is.

  • And let's actually create a few constant variables, which is going to be our X class.

  • And we're gonna create a constant, which is our circle class.

  • And the Circle class is just hopes Circle and her ex classes just x this way.

  • We can easily use these strings throughout our application without having to duplicate them all over the place.

  • So let's get our current class that we're going to be applying and we first need to get our Sellas well, so we'll say so is going to be our target, which is whatever we clicked on.

  • So whichever said we clicked on is gonna be here, and our current class is going to be.

  • If it's circles turn, then we want to return the circle class.

  • Otherwise we're going to return the X Class.

  • So now we have our class that we're gonna be applying right here, and we can actually place the mark.

  • And to do this, we're actually just gonna do it inside of a function will pass the current so and the current class and we'll do it inside of this function here which we call place Mark with a So and it's going to have a current class.

  • Luckily, this function is super easy.

  • We're going to say it so dot class list dot ad our current class just like that.

  • And now when we click on something, you can see it's adding it X and gonna add an ex every time.

  • Because right now a circle is always false and were never actually switching turns.

  • So this is all working perfectly fine.

  • Placing marks is going well.

  • We're gonna skip checking for wind and checking for a draw for now, because switching turns is the most important thing to focus on ceviche minute creative function called swap turns.

  • And this function is super easy.

  • So what does create this here.

  • Swap turns and all this function is going to do is it's gonna take circle turn, and it's gonna set it to the opposite of circle turn.

  • Now we click.

  • It's going to be X's turn in circles and exits in circles, then exits in circles and exits and so on, always swapping every single time.

  • The next thing that we want to do is if I refresh this and I hover, You see, we're not getting those hover states.

  • So I want to apply those hover states.

  • And we can do that by just calling a function called set board hover class And inside of this function, we're gonna determine which class we actually apply.

  • Soldiers say set Ford hover class, and we want to make sure we do this after we swap turns so that we know which current player it is.

  • So we want this hover class to be based on whose turn it currently is not whose turn it used to be.

  • So now instead of here, we first actually need to get our board so we can just say our board is going to be document that get element by I D, and if you remember, we had an I.

  • D.

  • Of board is our board element.

  • How we wanted to do is bored dot class list dot remove.

  • So you want to remove the X Class, and we want to make sure we remove the circle class that we have no classes on it.

  • And then if it is circles turn currently.

  • Then we want to add in the circle class.

  • So we'll say Add class groups Class list dot ad and we want to add in clips, Circle class and then otherwise we want to add in the Ex classes was a board dot classless dot ad X class just like that.

  • Now, after we click on our 1st 1 you can see we now have the proper hover state based on which class we're currently using, which is exactly what we want.

  • But you'll notice a problem that our first instance is not actually setting the board hover class.

  • So it's just create a function which is going to be called start game and inside of this function, we want to set up all over cell elements just like this, and we also want to set the board hover class inside of here and we're gonna set these circle turn to equal false just to start now, as you can see hoops if we make sure that we call this function.

  • So we want to make sure that we call start game at the beginning is that we're actually starting the game.

  • And now, as you can see, we get that X hover than the circle hover and X, and so on, and it's working for the rest of our app.

  • Now the only thing left to do is to check for wins and for draws, and what we need to do is create an array for all of the winning combinations, and this winning combinations is going to be an array full of a raise.

  • So if, for example, you had element in the ray and spot 12 and three, you would be a winner if you had it in 14 and seven u.

  • N V.

  • A winner and so on.

  • But since this is an array, it needs to be zero index.

  • So this spot is 012 345 678 So, for example, if you had a spot in zero of one or two that would be considered a win.

  • So if you had three elements right here, that's a win again.

  • We can do 345 That's the very next room.

  • And then lastly, we could do 67 and eight, which is again going to be the row after that.

  • Now we can work on the verticals so we can come in here we can 036 That's going to be these vertical ones here.

  • We can also come in clubs and do essentially the same thing.

  • But we're gonna do 14 and seven, and then we're also going to have to five and eight.

  • Now all that's left is to do that a diagonals.

  • So one for eight.

  • I'm sorry.

  • Zero for an eight, and we're also going to do to ever diagnose, which is 24 and six.

  • These air, all possible winning combinations for a tick tack toe game.

  • And then we can use this inside of a function here, which we're gonna check for win.

  • So we could just say if check win, then we want to do stuff inside of here and inside of this check win.

  • We're actually gonna pass in the current class.

  • And now let's create that function, check, win.

  • And we're gonna pass it to the current class just like that.

  • And inside of here.

  • What we want to do is we're gonna check all of the winning combinations to see if some of the winning combinations are met by the current combinations.

  • So we'll say winning combinations, not some.

  • Essentially, this is going to return.

  • True, if any of the values inside of it hard, True.

  • So we can say return winning combinations dot some and this is going to loop over all of the different combinations.

  • And for each one of the combinations, we want to check if all of the indexes So essentially, if all the values in our cell elements have the same class, so we can do is our combination dot every because we want to make sure every element has the same class, we're gonna get the index, and then we're gonna be instead of here.

  • So we're gonna say what we want to return are so elements of that index.

  • So we're checking which cell.

  • So, for example, that 012 combination is going to be these top three cells.

  • It's gonna check Cell 01 and two.

  • We want to check the class list, and we want to see if it contains the class for the current class.

  • So essentially, what this is saying is, if the current class is in all three of these elements inside of the combination, then we are a winner.

  • So every single cell inside of the combination is correct for at least one of the winning combinations than it's a win and what we can do inside of here.

  • For now, it's just cancer, that log winner.

  • And if we inspect this page, you can see we have a console over here.

  • So we do.

  • X o X, then, Oh, when we click X, you can see that it's a winner because we have three exes in a row.

  • Wanna refresh this?

  • Inspect the page again.

  • We could do X, O X O and oh, and you can see again.

  • We have a winner, and this time it's those instead of the exes.

  • But obviously we don't want to just stop it right there.

  • We want to actually show up a message.

  • Say that the X is a rose one and go from there.

  • In order to do that, we could just call a function called Endgame.

  • And for now, we're gonna pass in false.

  • This is going to be whether or not it's a draw.

  • We can create that function called endgame.

  • It's goingto have that draw passed into it.

  • So whether or not it to draw in this case, it's a win.

  • So what we want to do is if it's a draw, we're gonna do something.

  • We're not going to find that it.

  • But if it's not a draw, we want to select our winning message text element, which we have not created yet.

  • So it's come up here and say Our winning message text element is document died.

  • Kids whoops dot query selector I believe when Mixture, I hope it's actually get right here.

  • Date a winning message.

  • That is the selector that we're gonna use.

  • So it's a date.

  • A winning message text.

  • This is our winning message.

  • Text element that we want to do is we're gonna set the inner text inside of here to be equal to a string, and what we're gonna do is just check to see whose turn it is So if it's circles turn, we want to say that The O's win so will, say, owes win.

  • Look, we don't even need a win.

  • We can just put those and then we can also do the same thing for exes.

  • And then after that, we want to put the text winds.

  • So what this is going to do is is going to check if it's circles turn print out those if it's X's turn printout exes and then print out the text winds and then we obviously want to make sure that we actually show this.

  • So we're gonna say winning message elements dot class the list dot ad ever gonna ad in here that show class and the way that this works is we need to select this winning message element so we can say winning message element.

  • And it has an I D.

  • Of winning message.

  • So now when someone wins, the game is going to print out the text and it's going to show us that winning message helmet.

  • So let's just have the exes win.

  • And, as you can see, it says, exes went, Obviously, restart doesn't work, But if we refresh and come in here and make it so that the O's win, you can see it says O's win and again, Refresh doesn't work yet.

  • Now what's actually going to figure out what we do if there's a draw?

  • So we were starting to say here is else if there is a draw, so we say is a draw, then what we want to do is and the game the past, True for the fact that it is a draw and then obviously if it's neither of those, we want to make sure we swapped the turn and set the board.

  • We only want to swap turns if there's no winner yet.

  • So instead of a draw, we could do a very similar thing winning message dot in her text.

  • We could just set it equal to hear draw and now you've noticed immediately there's an error.

  • Obviously we haven't defined is drawing yet, so it's actually create that function is draw and inside of this function we need to do a very similar thing that we did with our winning combinations.

  • But essentially we just need to check to make sure every single cell has been filled so we can say sell elements dot Every we want to do is we're gonna get every cell, and we want to check if it has a class, so we could just say so.

  • That class list dot contains the X Class or if it contains the circle class, so we'll say it contains circle class.

  • So if every single one of the cells has either an X or circle class, then we want to return.

  • True because it is a draw.

  • And one problem is that this cell elements does not actually have in every method.

  • But to get around that, we could just d structure this cell elements into an array and it'll have the method.

  • And if you're not familiar with de structuring, I have an entire video link in the cards and description that you can check out after this.

  • So now let's actually make sure this works.

  • We click x o X than O X o put an X here, Oh, here, X, and as you can see, we get that draw text showing up.

  • But obviously we're sending it on the wrong element.

  • We want to set this on the text element that it doesn't delete our button so Let's try that one more time we'll put X o x o just like that.

  • And now you can see it says Draw.

  • And the restart button is right here.

  • So the last bit of functionality we have is to actually implement the restart button.

  • So let's select the restart button so we could just stay up here.

  • Constant Restart button.

  • Make sure I spell that quickly is going to be equal to document that Get element by I d.

  • Of Restart Button just like that.

  • Let me make sure that's correct.

  • And then what we can do is we can just select a quick event listener so we can say restart, button dot ad event listener Every time we want to click on it, we just want to call our start came function.

  • So now if we come in here and we click on everything exes, when that's fine.

  • But if we restart again, you'll notice nothing's happening.

  • And because our start game function is not reversing the state of everything that's happened right now, we're setting everything up, but we also need to unset everything up.

  • So the first thing that we can do in here is we can just change our winning message element.

  • And we wanna make sure remove that class.

  • So you want to remove the show class?

  • So now if I just make the exes win, click restart, you can see it actually removed that show class.

  • But of course, it hasn't removed any of this other information.

  • So when in terms of that, next we can do that really easily.

  • When we lived through all of our cells, we could just select the class list and we just want to remove the X X Class.

  • We want to do the exact same thing.

  • Look for this circle class just like that.

  • And we also want to remove our event listener.

  • So say so that remove event listener on the click.

  • And we want to remove the handle Click function just like that.

  • Now we set this up so that X is when we click Restart, it clears absolute everything out.

  • You can go right back into it this time.

  • What say O's win and everything is working exactly like we want it to.

  • And that's all there is to create an incredibly robust and fun take Tak Tok.

  • If you enjoy this video make sure check on my other videos linked over here and subscribe to the channel for more videos.

  • Just like this.

  • Thank you very much for watching and have a good day.

Hello, everyone.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

A2 初級

JavaScriptでTic Tac Tac Toeを構築する - チュートリアル (Build Tic Tac Toe With JavaScript - Tutorial)

  • 6 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語