This script implements a matrix-based game where players interact with a grid of elements. The primary goals are to: - Identify connected elements of the same type from a selected position. - Remove those elements, adjust the matrix by simulating gravity, and reorganize empty
columns.
Calculate and display the score based on the number of elements removed in each move.
find_repeat: Finds all connected elements of the same type.
increment_score: Calculates the score for a given move.
move_x: Simulates gravity in a column.
empty.
play: Executes a single move, updating the matrix and returning the score.
Matrix size (lines): Integer specifying the size of the matrix (N x N).
Matrix content (matrix): Rows of the matrix, each consisting of characters.
Number of moves (movs): Integer indicating the number of moves.
List of moves (movements): A comma-separated string of coordinates for each move.
(0,0) position starts from first left column to last right, and below row to up row
4 RRBG RBBG YYGG XYGG 2 0 1,1 1
Example (0,0) = X
The script outputs the total score after processing all moves.
Run the script and provide the required inputs as prompted.
|
Finds all connected elements of the same type from a given position. |
|
Calculates the score for a move based on the number of elements removed. |
|
Simulates gravity in a specific column. |
|
Shifts all columns leftward when an entire column becomes empty. |
|
|
|
Processes a single move, updating the matrix and calculating the score. |
|
Processes the game logic for the given matrix and moves. |
|
Validates that the number of elements in the matrix matches the given size. |
|
|
|
Finds all connected elements of the same type from a given position.
>>> find_repeat([['A', 'B', 'A'], ['A', 'B', 'A'], ['A', 'A', 'A']], 0, 0, 3)
{(1, 2), (2, 1), (0, 0), (2, 0), (0, 2), (2, 2), (1, 0)}
>>> find_repeat([['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']], 1, 1, 3)
set()
Calculates the score for a move based on the number of elements removed.
>>> increment_score(3)
6
>>> increment_score(0)
0
Simulates gravity in a specific column.
>>> move_x([['-', 'A'], ['-', '-'], ['-', 'C']], 1, 2)
[['-', '-'], ['-', 'A'], ['-', 'C']]
Shifts all columns leftward when an entire column becomes empty.
>>> move_y([['-', 'A'], ['-', '-'], ['-', 'C']], 2)
[['A', '-'], ['-', '-'], ['-', 'C']]
>>> parse_moves("0 1, 1 1")
[(0, 1), (1, 1)]
>>> parse_moves("0 1, 1 1, 2")
Traceback (most recent call last):
...
ValueError: Each move must have exactly two numbers.
>>> parse_moves("0 1, 1 1, 2 4 5 6")
Traceback (most recent call last):
...
ValueError: Each move must have exactly two numbers.
Processes a single move, updating the matrix and calculating the score.
>>> play([['R', 'G'], ['R', 'G']], 0, 0, 2)
([['G', '-'], ['G', '-']], 3)
Processes the game logic for the given matrix and moves.
size (int): Size of the game board. matrix (List[str]): Initial game matrix. moves (List[Tuple[int, int]]): List of moves as (x, y) coordinates.
int: The total score obtained.
>>> process_game(3, ['aaa', 'bbb', 'ccc'], [(0, 0)])
6
Validates that the number of elements in the matrix matches the given size.
>>> validate_matrix_content(['aaaa', 'aaaa', 'aaaa', 'aaaa'], 3)
Traceback (most recent call last):
...
ValueError: The matrix dont match with size.
>>> validate_matrix_content(['aa%', 'aaa', 'aaa'], 3)
Traceback (most recent call last):
...
ValueError: Matrix rows can only contain letters and numbers.
>>> validate_matrix_content(['aaa', 'aaa', 'aaaa'], 3)
Traceback (most recent call last):
...
ValueError: Each row in the matrix must have exactly 3 characters.
>>> validate_matrix_size(-1)
Traceback (most recent call last):
...
ValueError: Matrix size must be a positive integer.
>>> validate_moves([(1, 2), (-1, 0)], 3)
Traceback (most recent call last):
...
ValueError: Move is out of bounds for a matrix.