Tetris is a famous puzzle video game. The objective of the game is to complete lines by moving different shaped pieces (tetromonioes) that falls onto the grid one by one. The scores are calculated by...



Python: Tetris Simulator


Tetris is a famous puzzle video game. The objective of the game is to complete lines by moving different shaped<br>pieces (tetromonioes) that falls onto the grid one by one. The scores are calculated by the number of lines completed<br>before the playing grid is filled to the top.<br>Given an n x m playing grid (n rows, m columns), your task is to count how many lines are already completed. Each<br>cell is either occupied by a block or empty. A line is considered complete when there is no empty space in the line.<br>For example, for the following 10 x 5 grid,<br>there are 4 completed lines in the above example.<br>You are to write a function tetris(grid:list[list[str]]) -> int that returns the number of completed lines where<br>the grid is given as a list of rows and each row is a list of string. Each cell can only be either 'x' or<br>.However,<br>some input grids might be non-rectangle. In such case, the function must raise a ValueError with the message non-<br>rectangle grid is invalid .<br>

Extracted text: Tetris is a famous puzzle video game. The objective of the game is to complete lines by moving different shaped pieces (tetromonioes) that falls onto the grid one by one. The scores are calculated by the number of lines completed before the playing grid is filled to the top. Given an n x m playing grid (n rows, m columns), your task is to count how many lines are already completed. Each cell is either occupied by a block or empty. A line is considered complete when there is no empty space in the line. For example, for the following 10 x 5 grid, there are 4 completed lines in the above example. You are to write a function tetris(grid:list[list[str]]) -> int that returns the number of completed lines where the grid is given as a list of rows and each row is a list of string. Each cell can only be either 'x' or .However, some input grids might be non-rectangle. In such case, the function must raise a ValueError with the message non- rectangle grid is invalid .
def check_tetris(grid:list[list[str]], expected_output, exception:bool):<br>Verifies if `tetris` function works correctly<br>try:<br>assert tetris(grid)<br>expected_output<br>==<br>except ValueError:<br>return exception<br>if exception:<br>return False<br>return True<br>grid1<br>[<br>'),<br>'),<br>['<br>'x',' '],<br>['x'<br>'x'],<br>['x',<br>,'x'],<br>['x',<br>'x',' '],<br>['x','x','<br>,'x', 'x'],<br>['x','x','x','x','x'],<br>['x',' ','x','x',' '],<br>['x','x','x','x','x'],<br>check_tetris(grid1, 4, False) # rectangle grid, 4 completed lines<br>grid2<br>[<br>['<br>[<br>'],<br>['<br>'x',' '],<br>['x','x'<br>,'x', 'x'],<br>check_tetris(grid2, 1, True) # non-rectangle grid, expecting a ValueError<br>

Extracted text: def check_tetris(grid:list[list[str]], expected_output, exception:bool): Verifies if `tetris` function works correctly try: assert tetris(grid) expected_output == except ValueError: return exception if exception: return False return True grid1 [ '), '), [' 'x',' '], ['x' 'x'], ['x', ,'x'], ['x', 'x',' '], ['x','x',' ,'x', 'x'], ['x','x','x','x','x'], ['x',' ','x','x',' '], ['x','x','x','x','x'], check_tetris(grid1, 4, False) # rectangle grid, 4 completed lines grid2 [ [' [ '], [' 'x',' '], ['x','x' ,'x', 'x'], check_tetris(grid2, 1, True) # non-rectangle grid, expecting a ValueError
Jun 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here