Answered step by step
Verified Expert Solution
Question
1 Approved Answer
CS1010S, Semester I, 2022/2023-Side Quest 10.1 Task 3c: Merging Tiles (7 marks) Your task is to create four functions - merge_up, merge_down, merge_left, merge_right
CS1010S, Semester I, 2022/2023-Side Quest 10.1 Task 3c: Merging Tiles (7 marks) Your task is to create four functions - merge_up, merge_down, merge_left, merge_right - that will handle the movements in the respective direction for a given game matrix mat. For ease of description, we will state the rules for this movement of tiles in the left direction. We will refer the pre-movement matrix as the 'old' matrix and the post-movement one as the 'new' matrix. To obtain the new matrix, for each row of the old matrix, process the tiles from left to right (exclude empty cells). We will refer to tile being processed as CurrentTile and tile right to CurrentTile, if any, as NextTile If there is no NextTile, add a tile of the same value as CurrentTile to leftmost available (empty) cell in the corresponding row of the new matrix. If NextTile is of the same value as CurrentTile, add a tile of their combined value to leftmost available cell in the corresponding row of the new matrix. Move on to process the tile on the right of NextTile if there is one. If NextTile has a different value than the CurrentTile, add a tile that has the same value as the CurrentTile to leftmost available cell in the corresponding row of the new matrix. Move on to process NextTile. Example 1: old matrix: [2, 0, 2, 4] new matrix: [4, 0, 0, 0] old matrix: [2, 0, 2, 4] new matrix: [4, 4, 0, 0] Example 2: (CurrentTile = index 0) (CurrentTile = index 4) (CurrentTile index 0) (NextTile = index 2) (CurrentTile = index 2) 6 (no NextTile) old matrix: [2, 0, 4, 0] new matrix: [2, 0, 0, 0] old matrix: [2, 0, 4, 0] new matrix: [2, 4, 0, 0] Substitute the appropriate directions to obtain the other sets of instructions. For merge_up and merge_down you should be processing the old matrix columnwise, starting from the top and bottom respectively. You are encouraged to play the game and come up with some equivalent formulation of the rules that might be easier to implement. (NextTile index 2) (no NextTile) Each of the four functions should take a game matrix mat as input and return a tuple in the form (new_matrix, is_valid, score_increment) where: new_matrix is the resulting matrix after applying the movement and merging rules above. is_valid is False if an invalid move is made and True otherwise. An invalid move is one where mat remains unchanged after applying the above rules, i.e. where new_matrix is equal to mat. score_increment is sum of the values of all newly formed tiles.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
1 mergeupmat python def mergeupmat newmat 0 for in rangelenmat0 for in rangelenmat isvalid True scor...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started