Exemple #1
0
def test_matrix_save():
    m, n = 2, 2
    matrix = Matrix(m, n)
    matrix.colorize(0, 1, 'X')
    matrix.save('/tmp/matrix.bmp')

    with open('/tmp/matrix.bmp') as f:
        assert str(matrix) == f.read()
Exemple #2
0
def test_matrix_vfind():
    m, n = 3, 3
    matrix = Matrix(m, n)

    matrix.colorize(0, 0, 'X')
    matrix.colorize(0, 2, 'X')
    matrix.colorize(1, 2, 'X')

    assert 0 in matrix.vfind(0, 'X')
    assert 2 in matrix.vfind(0, 'X')
    assert 2 in matrix.vfind(1, 'X')
Exemple #3
0
def test_matrix_hfind():
    m, n = 3, 3
    matrix = Matrix(m, n)

    matrix.colorize(0, 0, 'X')
    matrix.colorize(0, 2, 'X')
    matrix.colorize(1, 2, 'X')

    assert 0 in matrix.hfind(0, 'X')
    assert 0 in matrix.hfind(2, 'X')
    assert 1 in matrix.hfind(2, 'X')
Exemple #4
0
def test_matrix_colorize():
    m, n = 3, 3
    matrix = Matrix(m, n)

    matrix.colorize(0, 0, 'X')
    matrix.colorize(m - 1, n - 1, 'X')

    assert matrix[0][0] == 'X'
    assert matrix[m - 1][n - 1]

    return matrix
Exemple #5
0
def test_matrix_replace():
    m, n = 2, 2
    matrix = Matrix(m, n)

    matrix.colorize(0, 1, 'X')
    matrix.replace(0, 0, 'C')

    assert matrix[0][0] == 'C'
    assert matrix[0][1] == 'C'
    assert matrix[1][0] == 'X'
    assert matrix[1][1] == 'C'
Exemple #6
0
def test_matrix_fill():
    m, n = 5, 5
    matrix = Matrix(m, n)

    matrix.colorize(2, 0, 'X')
    matrix.colorize(2, 4, 'X')

    matrix.colorize(0, 2, 'X')
    matrix.colorize(4, 2, 'X')

    matrix.fill(2, 2, 'X')

    assert ['X'] * 5 == matrix[2]

    for i in range(0, 5):
        matrix[i][2] == 'X'
Exemple #7
0
def test_matrix_str():
    m, n = 2, 2
    matrix = Matrix(m, n)
    matrix.colorize(0, 1, 'X')

    assert str(matrix) == 'OO\nXO'
class MatrixParser:

    def init(self, m, n):
        m, n = int(m), int(n)
        self.matrix = Matrix(m, n)

    def clean(self):
        self.matrix.clean()

    def colorize(self, x, y, color):
        # Humanaze
        x, y = int(x), int(y)
        x, y = x - 1, y - 1
        self.matrix.colorize(x, y, color)

    def vcolorize(self, x, y1, y2, color):
        x, y1, y2 = int(x), int(y1), int(y2)
        x, y1, y2 = x - 1, y1 - 1, y2 - 1
        self.matrix.vcolorize(x, y1, y2, color)

    def hcolorize(self, x1, x2, y, color):
        y, x1, x2 = int(y), int(x1), int(x2)
        y, x1, x2 = y - 1, x1 - 1, x2 - 1
        self.matrix.hcolorize(y, x1, x2, color)

    def fill(self, x, y, color):
        x, y = int(x), int(y)
        x, y = x - 1, y - 1
        self.matrix.fill(x, y, color)

    def rect(self, x1, y1, x2, y2, color):
        x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
        x1, y1, x2, y2 = x1 - 1, y1 - 1, x2 - 1, y2 - 1

        self.matrix.rect(x1, y1, x2, y2, color)

    def replace(self, x, y, color):
        x, y = int(x), int(y)
        x, y = x - 1, y - 1
        self.matrix.replace(x, y, color)

    def save(self, name):
        self.matrix.save(name)

    def parse(self, command):
        command, *args = command.split()
        method = self._REGISTER.get(command)

        if method:
            method(self, *args)

    _REGISTER = {
        'I': init,
        'C': clean,
        'L': colorize,
        'V': vcolorize,
        'H': hcolorize,
        'Q': fill,
        'F': replace,
        'K': rect,
        'S': save
    }