Bạn đang quan tâm đến Amazon – Online Rubik'S Cube Scramble Generator phải không? Nào hãy cùng Vuihecungchocopie đón xem bài viết này ngay sau đây nhé, vì nó vô cùng thú vị và hay đấy!
XEM VIDEO Amazon – Online Rubik'S Cube Scramble Generator tại đây.
IntroductionThis is a follow up post to the post I wrote just the other day on scrambling a Rubik’s Cube with JavaScript. If you haven’t read that post yet be sure to check it out here and then come back to this post. In this post, I will improve the JavaScript scrambler. To do that, we will unfortunately have to rewrite most of the code, but the fundamental design stays the same. Let’s get to it!
What’s a Scramble
You should already know what a Rubik’s cube scramble is (assuming you read the first post), however, here is a quick refresher just in case. A scramble is a sequence of 20 moves that are performed on a solved cube. These 20 moves are picked randomly from the following set of moves:
F, R, U, B, L, D, F2, R2, U2, B2, L2, D2, F', R', U', B', L', D'Here are some example scrambles:
B2 R F' L2 R B L2 F' D' L2 F L2 R' U B D' F D' U2 R' B' D U2 F' L U' R2 L' D B D' R2 B' F' R2 B2 D2 R B D2 L' R2 U2 F U' B' D2 B D B' F2 U R2 F2 U' L2 B2 R2 L U'The ProblemThe problem with the last post was that our scrambling algorithm doesn’t account for the possibility of 2 of the same move being next to each other. For example, we might get a scrambles that look like these:
B2 R F' L2 R B L2 F' L' L2 F L2 R' U B D' F D' U2 R' B' D U2 F' L U' R2 R2 D B D' R2 B' F' R2 B2 D2 R B D2 L' R2 U2 F U' B' D2 B D B' F2 U R2 D D L2 B2 R2 L U'At first these scrambles might not look like an issue, however if you look closer, you will see that the first scramble has a L’ and a L2 directly next to each other. The last scramble has two D moves next to each other. This makes no sense. Take the last scramble for example. It’s the one with two D moves next to each other. That is the same as saying D2. Essentially, that scramble is now 19 moves, not 20. This is a problem, especially when we get 3 or 4 moves like this in our scrambles
Additionally, having a R followed by an R’ makes no sense because they just cancel each other out! All we did was move the cube twice to get to the same spot. When this happens multiple times in a scramble, our scrambles could be 15 or less moves instead of 20!
The Fix
This may seem like a simple fix but it is not quite that easy. It is hard to check if 2 adjacent moves are the same type. We need to think about solving this in a different way than we did last time. Let’s take a look at the options for each scramble again.
Đang xem: Amazon
F, R, U, B, L, D, F2, R2, U2, B2, L2, D2, F', R', U', B', L', D'We can see that there are 6 different moves by letter. There is F, R, U, B, L, and D. Furthermore, there are 3 different moves for each of these. A single clockwise move (F), a double move (F2), and single counter clockwise move (F’). These make up our 18 possible moves (6 x 3 = 18).
We can use numbers to represent the scramble. We will assign F to 0, B to 1, and so on. Then at the end, we can substitute the letters back in and can be sure that no of the same letters will be next to each other.
Coding the Solution in JavaScript
We can now approach this problem using this knowledge. Let’s start off by giving each of the 6 possibilities a number 0 through 6 and store them in an array. We will call this array numOptions.
var numOptions = <0,1,2,3,4,5>Now, we can randomly add these numbers to another array named scramble until we have 20 random letters in the array. Remember, a Rubik’s cube scramble is 20 moves.
var scramble = <> // initialize to empty for (var i = 0; i scramble.push(numOptions
Xem thêm: Cuộc Thi Ảnh Trên Facebook, Thể Lệ Cuộc Thi Ảnh “Như Ngày Nào”
var bad = true while (bad) { scramble = <> for (var i = 0; i scramble.push(numOptions
Adding the Letters
The final step is to substitute letters for the numbers. We will do this by declaring an array of all possible options and then writing a switch statement nested inside a for loop to substitute the right letters for each number.
Ex. If the number is 3, we will place either a B, B2, or B’ (randomly of course).
var options = <"F", "F2", "F'", "R", "R2", "R'", "U", "U2", "U'", "B", "B2", "B'", "L", "L2", "L'", "D", "D2", "D'">var movefor (var i = 0; i switch (scramble) { case 0: move = options
Xem thêm: Hướng Dẫn Cách Giải Rubik 2X2 Nhanh Nhất, Hướng Dẫn Cách Chơi Và Giải Rubik 2X2 Cơ Bản
Conclusion
After seeing how I solved this problem, can you solve it better? I would love to see if there is a better solution! If you give it a try, let me know in the comments. You can get the code for this tutorial on my GitHub.
Công khai: VUIHECUNGCHOCOPIE.VN là trang web Tổng hợp Ẩm Thực - Game hay và Thủ Thuật hàng đầu VN, thuộc Chocopie Vietnam. Mời thính giả đón xem.
Chúng tôi trân trọng cảm ơn quý độc giả luôn ủng hộ và tin tưởng!