Coding Challenge #94: 2048 - Part 2

preview_player
Показать описание


Other Parts of this Challenge:

References:

Videos:

Related Coding Challenges:

Timestamps:
0:00 Part 2 of 2048
1:24 Use a keyCode to control the direction of slide
2:21 Write a function to flip the grid
5:46 Write a function to rotate the grid
9:49 Only flip or rotate the grid if a play has been made
11:32 Conclusion and next steps

Editing by Mathieu Blanchette
Animations by Jason Heglund
Music from Epidemic Sound

#2048 #slidingpuzzlegame #javascript #p5js
Рекомендации по теме
Комментарии
Автор

I haven't seen part 3 yet, but you only need to do the RotareGrid once instead of 3 times, as you are essentially mirroring the array through a diagonal, not really rotating it.

I might be wrong, but you can try removing two of those rotategrids and if it still works, great!

sadhlife
Автор

You are always saying that this might not be the best code for the things you tried to do, but I´m pretty sure is one of the best ways to show and teach (at least for beginners like me) how you can code. We can see that you are really passionate about it, and that makes a video of 12 minutes pass like it was 3 minutes. I love your videos, keep up with the good work! :D

acco
Автор

The "rotate" actually reflects in the diagonal. But it doesn't matter, it still works. Just don't need 3 "rotates" to flip it back again! For a real rotate you would have newGrid[i][j] = oldGrid[j][3-i] for example.

WildAnimalChannel
Автор

What I would suggest for moves would be for your rotate function to take 2 variables, the grid and a number between 0 and 3:
rotateGrid (grid, turns) {
turns = 4 - turns;
for (let i = 0; i < turns; i++) {
rotate(grid);
}
}

Then your keypresses need only set the number of turns:
played = true;
if (keyCode === DOWN_ARROW) {
turns = 0;
} else if (keyCode === RIGHT_ARROW) {
turns = 1;
} else if (keyCode === UP_ARROW) {
turns = 2;
} else if (keyCode === LEFT_ARROW {
turns = 3;
} else {
played = false;
}
if (played) {
rotateGrid(grid, turns);
<shift code>
rotateGrid(grid, -turns);
}

This means every turn the grid is rotating 8 times, thus resetting itself, and not requiring the flipGrid() function at all.

FlameStrykeShadowDark
Автор

Why do I enjoy these videos? I can't even code.

RyanLaker
Автор

Man, this is such a mess. But that's cool! Sometimes just hacking stuff together is what you need.

PerMortensen
Автор

An interesting observation about the original game: When you have a row of four identical values and you try to merge it into one value, it instead merges into two, by grouping together the two left-most together and the two right-most together (or upper-most and lower-most if it's vertical). I don't know if you implemented that in your code, but you probably should, if what you're aiming for is an exact replica of the core mechanics of the original game. Also, about the tiles that spawn, for every one, there's like a 1/10 chance that a 4 will spawn instead of a 2. Again, I don't know if you added that in yet.

Tsskyx
Автор

I'm kinda surprised that it took only 200 lines to make 2048 :D

likeyou
Автор

Hello ! Maybe you should try to apply Neural Network with Genetic Algorithm to this and add this to neural networks playlist? It would be perfect as a teaching example :D

aleksandermagda
Автор

Where can I download The Coding Train background?

albertkovtoun
Автор

this game is a nightmare to make! my version still has a few glitches but, what the heck, let's see how an ai plays it... I can't figure out when to backpropagate and on what basis to judge the accuracy, because there's no expected output here. can you please help me out?

amanmahendroo
Автор

It wouldn't be easier to use a cell as an object of a grid ? I dont know if your understand what I mean. A cell could interact with other cells like an object interacts with other objects. The cell could be aware of its sorroundings and the merge operations could be easier. Or is it just for the sake of using arrays and high order functions? My question is just out of curiosity. I'm not criticizing anything. Sorry for my english, not my mother tongue.

crisiberny
Автор

Here's a relevant One Direction song: "Drag me down"




Am I funny yet?

sadhlife
Автор

Don't forget to scan the grid and win you the game if there's a 2048 on the board! :P

Baltr
Автор

Mean hel me please, page nnot found, of source code .

ionmorosan
Автор

First Comment!! WooHoo!




Oh, this was unlisted with only 142 views.

brotherandy
Автор

Why you are here why you Won t teach at MIT

takikirouani