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,

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Python: Tetris Simulator

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 .
Transcribed Image 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):
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
Transcribed Image 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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Adjacency Matrix
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education