Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sit-In Lab 1 Twenty-Forty-Eight

Similar presentations


Presentation on theme: "Sit-In Lab 1 Twenty-Forty-Eight"— Presentation transcript:

1 Sit-In Lab 1 Twenty-Forty-Eight

2 2048 Puzzle 4 by 4 matrix You can slide the tiles in 4 directions: up, down, left, right Tiles will hit each other If 2 same-number tiles hit each other, they are “merged” Tiles can only be merged once Write a piece of code to print the result of the move Input: 4 lines of 4 numbers - Initial state of the puzzle Followed by d - the direction of the move 0 (left) 1 (up) 2 (right) 3 (down) Output the outcome of the move

3 Sample 1 2 4 16 8 64 32 Move left

4 Similar numbers will be merged
Sample 1 2 4 16 8 64 32 4 16 8 2 64 32 Move left Similar numbers will be merged Before After

5 Sample 2 2 4 16 8 64 32 Move up

6 Sample 2 2 4 16 8 64 32 2 16 8 4 64 32 Move up Numbers take the place of the “0” Similar numbers will be merged Before After

7 Sample 3 2 4 16 8 64 32 Move right

8 Sample 3 2 4 16 8 64 32 4 16 8 2 64 32 Move right Numbers take the place of the “0” Similar numbers will be merged Before After

9 Sample 4 2 4 16 8 64 32 Move down

10 Similar numbers will be merged
Sample 4 2 4 16 8 64 32 2 4 16 8 64 32 Move down Similar numbers will be merged Before After

11 Algorithm #1 To move up… Use a combination of rotateRight moveLeft 2
16 8 4 64 32 4 8 32 64 16 2 Use a combination of rotateRight moveLeft 4. Move left To move up… 1. Rotate right 2. Rotate right 3. Rotate right 2 4 16 8 64 32 32 2 4 64 16 8 64 32 4 2 8 16 2 4 8 32 64 16

12 Algorithm #1 To move right… Use a combination of rotateRight moveLeft
4 16 8 2 64 32 4 2 64 8 32 16 Use a combination of rotateRight moveLeft 4. Rotate right To move right… 1. Rotate right 2. Rotate right 3. Move left 2 4 16 8 64 32 32 2 4 64 16 8 64 32 4 2 8 16 64 4 32 2 8 16

13 Algorithm #1 To move down… Use a combination of rotateRight moveLeft 2
4 16 8 64 32 4 8 32 64 16 2 Use a combination of rotateRight moveLeft 4. Rotate right To move down… 1. Rotate right 2. Move left 3. Rotate right 2 4 16 8 64 32 32 2 4 64 16 8 32 2 4 64 16 8 8 64 32 4 2 16

14 Main method Helper methods Read in the inputs
Call helper methods and print the output Helper methods rotateRight: rotate the matrix 90 degrees right moveLeft: move numbers to the left, merging numbers if necessary

15 Algorithm #2 Assemble 1D array Perform moveLeft on each of the array
Copy result back to array 1 2 3 4 4 3 2 1 Move left Move right 1 2 3 4 4 3 2 1 Move up Move down

16 Main method Helper methods Read in the inputs
Call helper methods and print the output Helper methods assemble1DArray: retrieve 1D array based on direction of move, set first element to be the direction of the move moveLeft: move numbers to the left, merging numbers if necessary

17 How to assemble?

18 2 4 16 8 64 32 Sample 1 Move left [0, 0] [0, 1] [0, 2] [0, 3] …
4 16 8 64 32 Move left (first element is the direction of the move) [0, 0] [0, 1] [0, 2] [0, 3] …

19 Sample 2 2 4 16 8 64 32 Move up [0, 0] [1, 0] [2, 0] [3, 0] …

20 Sample 3 2 4 16 8 64 32 Move right [0, 3] [0, 2] [0, 1] [0, 0] …

21 Sample 4 2 4 16 8 64 32 Move down [3, 0] [2, 0] [1, 0] [0, 0] …

22 How to collapse and merge?
(moveLeft)

23 moveLeft 2 4 Move left Collapse all numbers to left 2 4

24 2 4 2 4 moveLeft Compare 2 numbers if similar
If not, move onto the next set of numbers 2 4

25 2 4 2 8 moveLeft Compare the next set of numbers
If yes, merge the numbers 2 8

26 2 8 2 8 moveLeft Continue until no more comparisons
Collapse everything to the left again, if necessary 2 8

27 Questions?


Download ppt "Sit-In Lab 1 Twenty-Forty-Eight"

Similar presentations


Ads by Google