CS1010S, Semester I, 2022/2023-Side Quest 10.1 Task 3c: Merging Tiles (7 marks) Your task is to...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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. 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.
Expert Answer:
Answer rating: 100% (QA)
1 mergeupmat python def mergeupmat newmat 0 for in rangelenmat0 for in rangelenmat isvalid True scor... View the full answer
Related Book For
Principles Of Managerial Finance
ISBN: 978-0136119463
13th Edition
Authors: Lawrence J. Gitman, Chad J. Zutter
Posted Date:
Students also viewed these programming questions
-
Briefly describe ASCII and Unicode and draw attention to any relationship between them. [3 marks] (b) Briefly explain what a Reader is in the context of reading characters from data. [3 marks] A...
-
Portray in words what transforms you would have to make to your execution to some degree (a) to accomplish this and remark on the benefits and detriments of this thought.You are approached to compose...
-
Prestopino Corporation produces motorcycle batteries. Prestopino turns out 1,500 batteries a day at a cost of $6 per battery for materials and labor. It takes the firm 22 days to convert raw...
-
The before-tax income for Sarasota Co. for 2017 was $111,000 and $75,100 for 2018. However, the accountant noted that the following errors had been made: 1. Sales for 2017 included amounts of $37,400...
-
Professional standards require auditors to make a preliminary judgment about materiality levels during the planning of an audit. Statement of Auditing Standards (SAS) No. 47 states that the auditor...
-
"The rate at which a substance reacts is proportional to its active mass and the rate of a chemical reaction is proportional to the product of the active masses of the reacting substances." This is...
-
The trial balance for Prima Consultants Company on December 31, 2010, appears below. The following information is also available: a. Ending inventory of office supplies, $97 b. Prepaid rent expired,...
-
Viva sells its waterproof phone case for $90 per unit. Fixed costs total $194,400, and variable costs are $36 per unit. (1) Determine the contribution margin per unit. Contribution margin (2)...
-
1. What problems might have contributed to the firms poor performance? 2. Although several problems were encountered in implementing the business plan, the primary reason for low profits turned out...
-
An electron moves at speed 8 . 0 times 1 0 6 ?m / s toward the velocity selector shown in ( Figure ) , ?where the electric field is hidden from view. A 0 . 1 2 ?T magnetic field points into the...
-
City Bank is considering making a $50 million loan to a company named SheetOil that wants to commercialize a process for turning used blankets, pillowcases, and sheets into oil. This companys chances...
-
A DVCs population is growing 2 percent per year and its output is growing 3 percent per year. If the government wants to improve living standards over the coming decades, which of the following would...
-
At the beginning of July, Patti Dwyer established PD Company, investing \(\$ 20000\) cash in the business. On 5 July the company purchased land and a building, making a \(\$ 6000\) down payment...
-
If an investment has 35 percent more nondiversifiable risk than the market portfolio, its beta will be: a. 35 b. 1.35 c. 0.35
-
If a $50 billion initial increase in spending leads to a $250 billion change in real GDP, how big is the multiplier? a. 1.0 b. 2.5 c. 4.0 d. 5.0
-
FLEXIBLE BUDGET ABC Inc. employs 15 production workers, working 8 hours a day, 22 days per month, at a normal capacity of 8,400 units. The direct labor wage rate is Rs. 75 per hour; direct materials...
-
Grace is training to be an airplane pilot and must complete five days of flying training in October with at least one day of rest between trainings. How many ways can Grace schedule her flying...
-
What is the cost of capital?
-
Your broker calls to offer you the investment opportunity of a lifetime, the chance to invest in mortgage-backed securities. The broker explains that these securities are entitled to the principal...
-
A hedge fund charged with managing part of Harvard University's endowment purchased more than 1 million put options on Enron stock not long before the company went bankrupt, making tens of millions...
-
Frosting of evaporator tubes results in (a) increased heat transfer rate (b) decreased heat transfer rate (c) increased compressor pressure (d) decreased compressor pressure
-
The chemical formula of R-12 is (a) CCl2F (b) CClF3 (c) CCl2F2 (d) CHClF2
-
The use of chlorofluoro carbon refrigerants is banned because these react with (a) water vapours and cause acid rain (b) plants and cause green house effect (c) oxygen and cause its depletion (d)...
Study smarter with the SolutionInn App