<commit_before>from Tetramino import *
import copy

Position = namedtuple('Position', ['row','col'])

class Model(object):
    "model stores grid state, pieces and their position"
    GRID_ROWS = 22
    GRID_COLS = 10

    def __init__(self):
        self.grid = np.empty((self.GRID_ROWS, self.GRID_COLS), dtype=str)
        self.grid[:,:] = '.'
        self.active_grid = np.empty((self.GRID_ROWS, self.GRID_COLS), dtype=str)
        self.active_grid[:,:] = '.'
        self.tetraminos = []
        self.active_tetramino = Tetramino()
        self.score = 0
        self.cleared_lines = 0
        self._observers = []
        self.game_mode = 'board'  # can be board, menu, pause

    def add_tetramino(self, tetramino):
        """ appends input Tetramino to model, and adds it to the model.active_grid
        :param tetramino: Tetramino to be added
        :return: None
        """
        self.tetraminos.append(tetramino)
        self.active_tetramino = tetramino
        # insert tetramino in the active grid at the initiation position
        self.move_active_tetramino(tetramino.position)


    def check_wall_collision(self, position, tetramino):
        """ check for wall collision
        :param position: tenative position of the tetramino
        :param tetramino: tetramino to be checked
        :return: True if position is within walls, False otherwise
        """
        blocks_row, blocks_col = np.where(tetramino.piece_grid != '.')
        if (position.col+blocks_col.min() >= 0) \
                and (position.col+blocks_col.max() <= self.GRID_COLS-1) \
                and (position.row+blocks_row.max() <= self.GRID_ROWS-1):
            return True
        else:
            return False

    def check_unit_collision(self, position, tetramino):
        """ check for unit collision
        :param position: tenative position of the tetramino
        :param tetramino: tetramino to be checked
        :return: True if position does NOT collide with settled units, False otherwise
        """
        # local position of the filled blocks
        blocks_row, blocks_col = np.where(tetramino.piece_grid != '.')
        # global position of the blocks in the grid
        blocks_row = [x+position.row for x in blocks_row]
        blocks_col = [x+position.col for x in blocks_col]

        return all(self.grid[blocks_row,blocks_col]=='.')

    def move_active_tetramino(self, position):
        """ move the active tetramino at the given position
        :param position: tentative new position for the tetramino
        :return: True if movement was successful, False otherwise
        """
        # if wall/unit collision is OK, return false if it fails
        if self.check_wall_collision(position, self.active_tetramino) and \
                self.check_unit_collision(position, self.active_tetramino):
            self.active_tetramino.position = position
            self.update_active_grid()
            return True
        # couldn't move? was it trying to move downwards?
        elif position.row > self.active_tetramino.position.row:
            # stuck in the first two rows? -> game over!
            if self.active_tetramino.position.row < 2:
                self.settle_active_tetramino()
                self.game_mode = 'game over'
            # otherwise settle it
            # TODO leave uncommented..?
            else:
                self.settle_active_tetramino()
            return False
        else:
            return False

    def update_active_grid(self):
        piece_grid = self.active_tetramino.piece_grid
        position = self.active_tetramino.position
        # clear active grid first, then replace
        self.active_grid[:,:] = '.'
        for i, entry in enumerate(np.where(piece_grid != '.')[0]):
            r = np.where(piece_grid != '.')[0][i] # row of non-empty entry
            c = np.where(piece_grid != '.')[1][i] # col of non-empty entry
            self.active_grid[position.row+r, position.col+c] = np.char.capitalize(piece_grid[r,c])

    def step(self):
        # if row is complete, erase it
        for i, row in enumerate(self.grid):
            if '.' not in row:
                # erase row
                self.grid[i][:] = ['.']*self.GRID_COLS
                # move existing grid above cancelled row one step down
                temp_grid = np.empty((self.GRID_ROWS,self.GRID_COLS), dtype=str)
                temp_grid[:,:] = '.'
                for i_temp, row_temp in enumerate(temp_grid):
                    if i_temp <= i and i_temp >= 1:
                        temp_grid[i_temp][:] = self.grid[i_temp-1][:]
                    else:
                        temp_grid[i_temp][:] = self.grid[i_temp][:]
                self.grid = temp_grid
                self.cleared_lines += 1
                self.score += 100

    def move_left(self):
        """ move active tetramino one step to the left """
        old_column = self.active_tetramino.position.col
        new_position = self.active_tetramino.position._replace(col=old_column-1)
        self.move_active_tetramino(new_position)

    def move_right(self):
        """ move active tetramino one step to the right """
        old_column = self.active_tetramino.position.col
        new_position = self.active_tetramino.position._replace(col=old_column+1)
        self.move_active_tetramino(new_position)

    def move_downward(self):
        """ move active tetramino one step downward """
        old_row = self.active_tetramino.position.row
        new_position = self.active_tetramino.position._replace(row=old_row+1)
        self.move_active_tetramino(new_position)

    def move_downward_deep(self):
        """ move tetramino at the bottom """
        position = self.active_tetramino.position
        while self.move_active_tetramino(position):
            self.move_active_tetramino(position)
            position = position._replace(row=position.row+1)
        self.settle_active_tetramino()

    def rotate_right(self):
        # TODO here it should be checked whether the rotation is OK
        # if it is OK rotate otherwise pass
        self.active_tetramino.rotate_right()
        self.update_active_grid()

    def rotate_left(self):
        # TODO here it should be checked whether the rotation is OK
        # if it is OK rotate otherwise pass
        self.active_tetramino.rotate_left()
        self.update_active_grid()

    def test_tetramino(self):
        self.active_tetramino.test_tetramino()

    def settle_active_tetramino(self):
        """ pushes active tetramino into the settle grid model.grid """
        piece_grid = self.active_tetramino.piece_grid
        position = self.active_tetramino.position
        # push tetramino into settled grid
        for i, entry in enumerate(np.where(piece_grid != '.')[0]):
            r = np.where(piece_grid != '.')[0][i] # row of non-empty entry
            c = np.where(piece_grid != '.')[1][i] # col of non-empty entry<commit_msg>all done. (TODO: speedup with level)<commit_after>from Tetramino import *
import copy

Position = namedtuple('Position', ['row','col'])

class Model(object):
    "model stores grid state, pieces and their position"
    GRID_ROWS = 22
    GRID_COLS = 10

    def __init__(self):
        self.grid = np.empty((self.GRID_ROWS, self.GRID_COLS), dtype=str)
        self.grid[:,:] = '.'
        self.active_grid = np.empty((self.GRID_ROWS, self.GRID_COLS), dtype=str)
        self.active_grid[:,:] = '.'
        self.tetraminos = []
        self.active_tetramino = Tetramino()
        self.score = 0
        self.cleared_lines = 0
        self._observers = []
        self.game_mode = 'board'  # can be board, menu, pause

    def add_tetramino(self, tetramino):
        """ appends input Tetramino to model, and adds it to the model.active_grid
        :param tetramino: Tetramino to be added
        :return: None
        """
        self.tetraminos.append(tetramino)
        self.active_tetramino = tetramino
        # insert tetramino in the active grid at the initiation position
        self.move_active_tetramino(tetramino.position)


    def check_wall_collision(self, position, tetramino):
        """ check for wall collision
        :param position: tenative position of the tetramino
        :param tetramino: tetramino to be checked
        :return: True if position is within walls, False otherwise
        """
        blocks_row, blocks_col = np.where(tetramino.piece_grid != '.')
        if (position.col+blocks_col.min() >= 0) \
                and (position.col+blocks_col.max() <= self.GRID_COLS-1) \
                and (position.row+blocks_row.max() <= self.GRID_ROWS-1):
            return True
        else:
            return False

    def check_unit_collision(self, position, tetramino):
        """ check for unit collision
        :param position: tenative position of the tetramino
        :param tetramino: tetramino to be checked
        :return: True if position does NOT collide with settled units, False otherwise
        """
        # local position of the filled blocks
        blocks_row, blocks_col = np.where(tetramino.piece_grid != '.')
        # global position of the blocks in the grid
        blocks_row = [x+position.row for x in blocks_row]
        blocks_col = [x+position.col for x in blocks_col]

        return all(self.grid[blocks_row,blocks_col]=='.')

    def move_active_tetramino(self, position):
        """ move the active tetramino at the given position
        :param position: tentative new position for the tetramino
        :return: True if movement was successful, False otherwise
        """
        # if wall/unit collision is OK, return false if it fails
        if self.check_wall_collision(position, self.active_tetramino) and \
                self.check_unit_collision(position, self.active_tetramino):
            self.active_tetramino.position = position
            self.update_active_grid()
            return True
        # couldn't move? was it trying to move downwards?
        elif position.row > self.active_tetramino.position.row:
            # stuck in the first two rows? -> game over!
            if self.active_tetramino.position.row < 2:
                self.settle_active_tetramino()
                self.game_mode = 'game over'
            # otherwise settle it
            # TODO leave uncommented..?
            else:
                self.settle_active_tetramino()
            return False
        else:
            return False

    def update_active_grid(self):
        piece_grid = self.active_tetramino.piece_grid
        position = self.active_tetramino.position
        # clear active grid first, then replace
        self.active_grid[:,:] = '.'
        for i, entry in enumerate(np.where(piece_grid != '.')[0]):
            r = np.where(piece_grid != '.')[0][i] # row of non-empty entry
            c = np.where(piece_grid != '.')[1][i] # col of non-empty entry
            self.active_grid[position.row+r, position.col+c] = np.char.capitalize(piece_grid[r,c])

    def step(self):
        # if row is complete, erase it
        for i, row in enumerate(self.grid):
            if '.' not in row:
                # erase row
                self.grid[i][:] = ['.']*self.GRID_COLS
                # move existing grid above cancelled row one step down
                temp_grid = np.empty((self.GRID_ROWS,self.GRID_COLS), dtype=str)
                temp_grid[:,:] = '.'
                for i_temp, row_temp in enumerate(temp_grid):
                    if i_temp <= i and i_temp >= 1:
                        temp_grid[i_temp][:] = self.grid[i_temp-1][:]
                    else:
                        temp_grid[i_temp][:] = self.grid[i_temp][:]
                self.grid = temp_grid
                self.cleared_lines += 1
                self.score += 100

    def move_left(self):
        """ move active tetramino one step to the left """
        old_column = self.active_tetramino.position.col
        new_position = self.active_tetramino.position._replace(col=old_column-1)
        self.move_active_tetramino(new_position)

    def move_right(self):
        """ move active tetramino one step to the right """
        old_column = self.active_tetramino.position.col
        new_position = self.active_tetramino.position._replace(col=old_column+1)
        self.move_active_tetramino(new_position)

    def move_downward(self):
        """ move active tetramino one step downward """
        old_row = self.active_tetramino.position.row
        new_position = self.active_tetramino.position._replace(row=old_row+1)
        self.move_active_tetramino(new_position)

    def move_downward_deep(self):
        """ move tetramino at the bottom """
        position = self.active_tetramino.position
        while self.move_active_tetramino(position):
            self.move_active_tetramino(position)
            position = position._replace(row=position.row+1)
        self.settle_active_tetramino()

    def rotate_right(self):
        # if it is OK rotate, rotate
        test_tetramino = copy.deepcopy(self.active_tetramino)
        test_tetramino.rotate_right()
        if self.check_wall_collision(test_tetramino.position, test_tetramino) and \
                self.check_unit_collision(test_tetramino.position, test_tetramino):
            self.active_tetramino.rotate_right()
            self.update_active_grid()

    def rotate_left(self):
        # if it is OK rotate, rotate
        test_tetramino = copy.deepcopy(self.active_tetramino)
        test_tetramino.rotate_left()
        if self.check_wall_collision(test_tetramino.position, test_tetramino) and \
                self.check_unit_collision(test_tetramino.position, test_tetramino):
            self.active_tetramino.rotate_left()
            self.update_active_grid()
        # otherwise pass

    def test_tetramino(self):
        self.active_tetramino.test_tetramino()

    def settle_active_tetramino(self):
        """ pushes active tetramino into the settle grid model.grid """
        piece_grid = self.active_tetramino.piece_grid
        position = self.active_tetramino.position
        # push tetramino into settled grid
        for i, entry in enumerate(np.where(piece_grid != '.')[0]):
            r = np.where(piece_grid != '.')[0][i] # row of non-empty entry
            c = np.where(piece_grid != '.')[1][i] # col of non-empty entry
{'fraction_non_alphanumeric': 0.09754229617767876, 'fraction_numerical': 0.0036204135626261924, 'mean_word_length': 2.3188539741219962, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1935595', 'n_tokens_mistral': 4461, 'n_tokens_neox': 4171, 'n_words': 1038}