tetris_ballistic_Le module

This module simulates the surface growth by Tetris pieces. It includes functions to generate random Tetris pieces, calculate their landing positions on a substrate, and simulate a game of Tetris for a given number of steps and a defined grid size.

https://en.wikipedia.org/wiki/Tetromino

By Le Chen, Mauricio Montes and Ian Ruau

class tetris_ballistic_Le.Tetris_Ballistic(width=16, height=32, steps=30, seed=None, sticky=True, config_file=None)

Bases: object

Initialize_Substrate()

Initializes the substrate manually.

Place_I(position, landing_row, i, rot=0)
Place a square with pivot at the bottom left corner on the substrate:

1000 or 0 0 0 1

Parameters:
  • position (int) – The position of the pivot.

  • landing_row (int) – The landing row of the pivot.

  • i (int) – The step number.

  • rot (int) – The rotation of the piece; 0 and 2 for horizontal, 1 and 3 for vertical.

Returns:

None

Place_J(position, landing_row, i, rot=0)

Place a J with pivot at the corner. + rot = 0

0 0 01

  • rot = 1

    001 0

  • rot = 2

    10 0 0

  • rot = 3

    0 100

Parameters:
  • position (int) – The position or column of the pivot.

  • landing_row (int) – The landing row of the pivot.

  • i (int) – The step number.

  • rot (int) – The rotation of the piece as described above.

Returns:

None

Place_L(position, landing_row, i, rot=0)

Place an L with pivot at the corner. + rot = 0

0 0 10

  • rot = 1

    0 001

  • rot = 2

    01 0 0

  • rot = 3

    100 0

Parameters:
  • position (int) – The position or column of the pivot.

  • landing_row (int) – The landing row of the pivot.

  • i (int) – The step number.

  • rot (int) – The rotation of the piece as described above.

Returns:

None

Place_O(position, landing_row, i)
Place a square with pivot at the bottom left corner on the :

00 10

Parameters:
  • position (int) – The position of the pivot.

  • landing_row (int) – The landing row of the pivot.

  • i (int) – The step number.

Returns:

None

Place_S(position, landing_row, i, rot=0)

Place an S with pivot given as follows: + rot = 0 or 2

00

01

  • rot = 1 or 3

    0 01

    0

Parameters:
  • position (int) – The position or column of the pivot.

  • landing_row (int) – The landing row of the pivot.

  • i (int) – The step number.

  • rot (int) – The rotation of the piece as described above.

Returns:

None

Place_T(position, landing_row, i, rot=0)

Place a T with pivot at the center: + rot = 0

010 0

  • rot = 1

    0 10 0

  • rot = 2

    0 010

  • rot = 3

    0 01 0

Parameters:
  • position (int) – The position or column of the pivot.

  • landing_row (int) – The landing row of the pivot.

  • i (int) – The step number.

  • rot (int) – The rotation of the piece as described above.

Returns:

None

Place_Z(position, landing_row, i, rot=0)

Place a Z with pivot given as follows: + rot = 0 or 2 00

10

  • rot = 1 or 3

    0 01 0

Parameters:
  • position (int) – The position or column of the pivot.

  • landing_row (int) – The landing row of the pivot.

  • i (int) – The step number.

  • rot (int) – The rotation of the piece as described above.

Returns:

None

Sample_Tetris()

Samples a Tetris piece given the probability distribution specified in the configuration file.

Returns:

A 2-element array: the first element is the piece type (0-6); the second element is the orientation (0-3).

Return type:

numpy.ndarray

Test_All()

This function simulates the Tetris Decomposition model on a substrate.

Parameters:

steps (int) – The steps to simulate.

Returns:

None (print the final substrate)

Test_I()

This is a test function for the Line piece.

Test_J()

This is a test function for the J piece.

Test_L()

This is a test function for the L piece.

Test_O()

This is a test function for the square piece.

Test_S()

This is a test function for the S piece.

Test_T()

This is a test function for the J piece.

Test_WhichPiece()
Test_Z()

This is a test function for the Z piece.

Tetris_Choice()

Randomly selects a Tetris piece and its orientation.

There are 7 Tetris pieces:

  • 0 : the square;

  • 1 : the line;

  • 2 : the L;

  • 3 : the J;

  • 4 : the T;

  • 5 : the S;

  • 6 : the Z.

There are 4 orientations for each piece:

  • 0 is the original orientation;

  • 1 is the 90 degree rotation;

  • 2 is the 180 degree rotation;

  • 3 is the 270 degree rotation.

Returns:

A 2-element array: the first element is the piece type (0-6); the second element is the orientation (0-3).

Return type:

numpy.ndarray

To-Do’s

  • Add input file to specify the probability of each piece.

Update_I(i, rot=0, sticky=True)

Updates the substrate with a line piece.

Parameters:
  • i (int) – The step number.

  • rot (int) –

    The rotation of the piece. + rot = 0

    1000

    • rot = 1

      0 0 0 1

    • rot = 2

      0001

    • rot = 3

      1 0 0 0

Returns:

The particle ID or the step number that has been placed in this step.
  • If the value is -1, it means it reaches to the top.

Return type:

int

Update_J(i, rot=0, sticky=True)

Updates the substrate with a J piece. + rot = 0

0 0

01

  • rot = 1
    001

    0

  • rot = 2

    10 0 0

  • rot = 3

    0 100

Parameters:
  • i (int) – The step number.

  • rot (int) – The rotation of the piece.

int: The particle ID or the step number that has been placed in this step.
  • If the value is -1, it means it reaches to the top.

Update_L(i, rot=0, sticky=True)

Updates the substrate with an L piece. + rot = 0

0 0 10

  • rot = 1

    0

    001

  • rot = 2
    01

    0 0

  • rot = 3

    100 0

Parameters:
  • i (int) – The step number.

  • rot (int) – The rotation of the piece.

int: The particle ID or the step number that has been placed in this step.
  • If the value is -1, it means it reaches to the top.

Update_O(i, sticky=True)
Updates the substrate with a square piece.

00 10

Parameters:
  • i (int) – The step number.

  • sticky (bool) – Whether the piece is sticky or not.

Returns:

The particle ID or the step number that has been placed in this step.
  • If the value is -1, it means it reaches to the top.

Return type:

int

Update_S(i, rot=0, sticky=True)

Updates the substrate with an S piece.

Parameters:
  • i (int) – The step number.

  • rot (int) – The rotation of the piece.

Returns:

The particle ID or the step number that has been placed in this step.
  • If the value is -1, it means it reaches to the top.

Return type:

int

Update_T(i, rot=0, sticky=True)

Updates the substrate with a T piece.

Parameters:
  • i (int) – The step number.

  • rot (int) – The rotation of the piece.

Returns:

The particle ID or the step number that has been placed in this step.
  • If the value is -1, it means it reaches to the top.

Return type:

int

Update_Z(i, rot=0, sticky=True)

Updates the substrate with a Z piece.

Parameters:
  • i (int) – The step number.

  • rot (int) – The rotation of the piece.

Returns:

The particle ID or the step number that has been placed in this step.
  • If the value is -1, it means it reaches to the top.

Return type:

int

ffnz(column)

Finds the first non-zero entry in the specified column of the substrate.

This method scans the column from top to bottom (starting from index 0) and returns the index of the first non-zero entry. If all entries in the column are zero, a special value (indicating this condition) is returned.

Parameters:

column (int) – The column index in the substrate to search in. It should be within the range of the substrate’s columns.

Returns:

The index of the first non-zero entry in the specified column. If the

column contains only zeros, returns the value of self.height, which indicates that no non-zero entry was found.

Return type:

int

load_config(filename)

Loads configuration data from a specified YAML file into the config_data attribute.

This method attempts to open and read the contents of the YAML file specified by filename. It then validates whether the file contains exactly 38 entries. If the file does not meet this requirement, a ValueError is raised. In case of a successful load, the configuration data is stored in the config_data attribute of the class instance.

Parameters: filename (str): The path to the YAML configuration file to be loaded.

Returns: bool: True if the file is successfully loaded and contains the correct number of entries, False otherwise.

Raises: ValueError: If the YAML file does not contain exactly 38 entries. yaml.YAMLError: If there is an error parsing the YAML file.

reset()

Resets the substrate to all zeros.