تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Debugging letter status logic0:50

so at the moment, we iterate over each tile in the current row, and then we call updateStatus. updateStatus begins by deciding if the status of tile should be marked as present or absent. Okay, so let's start from scratch, t-a-t. So for the first letter, we would expect this to be absent, but let's see what we're actually doing. Does the word include a t? Well, the secret word, of course, is cat. Does cat include t? Yes, so we mark it as present. Okay, well, then the only other check we do, so to make this crystal clear, our guess is tat, and the word is cat. All right, what is the index of the current letter? Current letter is t. All right, well, the index of t is zero. It's in the very first spot. And then what is the index of t for cat? Two. Zero, one, two. So we do not apply the status of correct for this letter. Okay, let's move on. We know this is correct, so let's move on to the final iteration. Once again, does the word include t? Yes, so we

Refactoring into Tile method6:58

if the other t was marked as yellow, I wouldn't have that realization. So yeah, I think this is actual Wordle behavior. In the event that there is more than one t, it does not mark the other t? I think that's right. Again, please fact-check me. And if I'm wrong, after this series is out and released, I will, behind the scenes, make an update to the underlying GitHub repo. Okay. Anyways, let's bring it back to cat. And yeah, that solves it. Now I get to refactor. Right down here. We had something fairly simple, and now I have all of this, which is pretty messy. So I think this step right here could pretty easily go on Tile. So let's open up a split. And I'll go to my Tile class. And yeah, right here, I'm going to paste all of that in. Like so. Clear it out. And I can then say Tile::updateStatus. And then let's see here. We're going to pass through the Word. So I could update this to the Word.

Like so. Clear it out. And I can then say Tile.updateStatus. And then let's see here. We're going to pass through the word. So I could update this to the word. Tile, that can then become this, because we're now on the Tile instance. So this.status. And then finally, it wants the index. Okay, so maybe for now we accept the word and the index. Again, baby steps. this.theWord and index. I just want to make a small change and make sure I didn't blow everything up. So is this still working? Try tat. Yes. tar. Yes, this is correct. All right, what next? Yeah, we talked about this before. Passing through an index for where the Tile occurs in the loop is weird to me. I don't love that. So maybe it would be useful to know for every Tile we create, what column does it exist at? Maybe that would be useful. It's sort of like finding the position in the current row. So maybe when I generate my board

Adding tile position property8:43

would be useful to know for every Tile we create, what column does it exist at? Maybe that would be useful. It's sort of like finding the position in the current row. So maybe when I generate my board right here, I could pass through the index. So let's see if this works. The item and the index. And then I'll pass that through, because that's basically, again, the position where the Tile occurs. And then that would accept it. Let's try this out and see if I'm onto anything here. I'm going to call it position. And then let's do this. After I create the board, let's log it to the console and have a look. Refresh. All right, there's our board. Here's our target. Here's each row. And if I open it up, notice position is zero. One. Two. And that's what we want. Okay, so now I have a corresponding position for each Tile that I think I can probably use now when we submit our guess. So if I come back, what if we didn't pass

we want. Okay, so now I have a corresponding position for each Tile that I think I can probably use now when we submit our guess. So if I come back, what if we didn't pass the index here? So remove that on Tile. And instead, I can say if the current letter equals the word in the exact same position as this current letter, then set it to correct. Let's see if that works. Refresh. Tat, tar, cat. That looks right to me. Okay, cool. Baby steps. Now that we're no longer using the index here, yeah, if we want, I could switch this over to a for...of. And I think phpStorm can do that for me. Yeah. Let's do let. I have a thing about const. So that's not too bad. But then where do I put this second pass? Like, I feel like that should go on Tile as well. But it can't. I can't run this bit of logic until we've updated the status for all the Tiles, at least in its current iteration. We might be able to figure out a

Static row status updater10:37

go on Tile as well. But it can't. I can't run this bit of logic until we've updated the status for all the Tiles, at least in its current iteration. We might be able to figure out a cleaner approach. But in the current iteration, yeah, I can't do a second pass until we finish the first pass. So if we want this to happen on Tile, maybe we could have like a static method here, like updateStatusesForRow. Is this gross? What do you think? And we'd accept the row. And then this, let's try this out. This would then iterate over the row. And then it needs the word. So we would accept that. Let's try it out. Yeah, a lot of refactoring is just trying it out. And if this doesn't work, or it looks bad, then we undo it. But don't be afraid to try it out. Let's see if this is cleaner. And I may decide this is horrible. And if that's the case, then you then you undo it. Okay, so if we did that, I would now say Tile, and I'm calling a static method.

Let's see if this is cleaner. And I may decide this is horrible. And if that's the case, then you then you undo it. Okay, so if we did that, I would now say Tile, and I'm calling a static method. And we're passing through the current row and the word. Okay, so now when I call that method, the Tile class will iterate over the given row, update the status, and then it could also do the second pass. And that's kind of what I wanted here. I wanted the full process of updating the tiles to exist on Tile. So this might be one option. Or again, I have a feeling there's a cleaner way to do this that I'm just not thinking of right now. We're not using here, let's hide this. And then we could remove the index. Now we could swap this out again with a for of. And I think that would be fine. Or we could filter the array down. So if I brought it back to what we had before, I could first say, filter the row down to where the tileStatus equals present. Those are

Prettier setup and cleanup12:21

And I think that would be fine. Or we could filter the array down. So if I brought it back to what we had before, I could first say, filter the row down to where the tileStatus equals present. Those are the only items that I'm interested in. So if we did that, I could then get rid of this conditional here. Let's move this on its own line. And actually, let's bring this back. If you use prettier, which I would recommend, we could npm install prettier. And I think that would do it. And editors like phpStorm, I think they pick up on that. So this is a very quick aside. Yeah, so the path to prettier, and then I want to run it for JavaScript files on save and when I reformat. So this will take care of some nice formatting for me. Let's see what happens when I save. There we go. So it automatically reformats some tricky bits of code or some tricky lines, according to prettier defaults. So let's see. You know what, this is still kind of part of

sometimes that can clean things up just a little bit. For example, I could remove the { braces. And now I can see, okay, row, filter out anything that's not present. Then we do another filter where I can parse what that means. And then I can loop over the resulting array. All right, that was a bit of work, but thanks for sticking around.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟