matrix.matrix_based_game¶
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.
Functions:¶
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.
- move_y: Reorganizes the matrix by shifting columns leftward when a column becomes
empty.
play: Executes a single move, updating the matrix and returning the score.
Input Format:¶
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
Example Input:¶
4 RRBG RBBG YYGG XYGG 2 0 1,1 1
Example (0,0) = X
Output:¶
The script outputs the total score after processing all moves.
Usage:¶
Run the script and provide the required inputs as prompted.
Attributes¶
Functions¶
|
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. |
|
|
|
Module Contents¶
- matrix.matrix_based_game.find_repeat(matrix_g: list[list[str]], row: int, column: int, size: int) set[tuple[int, int]] ¶
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()
- matrix.matrix_based_game.increment_score(count: int) int ¶
Calculates the score for a move based on the number of elements removed.
>>> increment_score(3) 6 >>> increment_score(0) 0
- matrix.matrix_based_game.move_x(matrix_g: list[list[str]], column: int, size: int) list[list[str]] ¶
Simulates gravity in a specific column.
>>> move_x([['-', 'A'], ['-', '-'], ['-', 'C']], 1, 2) [['-', '-'], ['-', 'A'], ['-', 'C']]
- matrix.matrix_based_game.move_y(matrix_g: list[list[str]], size: int) list[list[str]] ¶
Shifts all columns leftward when an entire column becomes empty.
>>> move_y([['-', 'A'], ['-', '-'], ['-', 'C']], 2) [['A', '-'], ['-', '-'], ['-', 'C']]
- matrix.matrix_based_game.parse_moves(input_str: str) list[tuple[int, int]] ¶
>>> 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.
- matrix.matrix_based_game.play(matrix_g: list[list[str]], pos_x: int, pos_y: int, size: int) tuple[list[list[str]], int] ¶
Processes a single move, updating the matrix and calculating the score.
>>> play([['R', 'G'], ['R', 'G']], 0, 0, 2) ([['G', '-'], ['G', '-']], 3)
- matrix.matrix_based_game.process_game(size: int, matrix: list[str], moves: list[tuple[int, int]]) int ¶
Processes the game logic for the given matrix and moves.
- Args:
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.
- Returns:
int: The total score obtained.
>>> process_game(3, ['aaa', 'bbb', 'ccc'], [(0, 0)]) 6
- matrix.matrix_based_game.validate_matrix_content(matrix: list[str], size: int) None ¶
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.
- matrix.matrix_based_game.validate_matrix_size(size: int) None ¶
>>> validate_matrix_size(-1) Traceback (most recent call last): ... ValueError: Matrix size must be a positive integer.
- matrix.matrix_based_game.validate_moves(moves: list[tuple[int, int]], size: int) None ¶
>>> validate_moves([(1, 2), (-1, 0)], 3) Traceback (most recent call last): ... ValueError: Move is out of bounds for a matrix.
- matrix.matrix_based_game.size¶