如何在python中打印板?

时间:2012-06-05 18:54:28

标签: python

我试图在Python上编写一个名为三重三重奏的游戏,但我对棋盘的输出有问题,它必须像每个方格一样,每个数字代表一个基点,有9个方格,每行三个。

| 1 | 1 | 9 |

|2@3|1*6|7*2|

| 4 | 1 | 2 |

我想为每一行做一个列表,并为每个基点开始编号,例如,如果它是北或类似的话,则为“0”,所以当我必须用卡的号码替换时,我确切地知道在哪里提出每个基本点,有什么建议吗?

提前致谢

4 个答案:

答案 0 :(得分:4)

您可以使用三个列表代表此主板:

["1", "1", "9"]
["2@3", "1*6", "7*2"]
["4", "1", "2"]

或者列在一起:

board = [["1", "1", "9"], ["2@3", "1*6", "7*2"], ["4", "1", "2"]]

您现在需要编写两个函数:

  • 一个函数,它接受此列表并打印您的电路板,其中包含|行。
  • 计算上述列表并确定电路板中每个单元格的正确值的函数。

你不需要处理数字或其他任何事情,这都是做这两件事的问题。

答案 1 :(得分:1)

以下是获取所需格式的简单方法:

def format_row(row):
    return '|' + '|'.join('{0:^3s}'.format(x) for x in row) + '|'

def format_board(board):
    # for a single list with 9 elements uncomment the following line:
    # return '\n\n'.join(format_row(row) for row in zip(*[iter(board)]*3))
    # for a 3x3 list:
    return '\n\n'.join(format_row(row) for row in board)

示例:

>>> print format_board([['1', '1', '9'], ['2@3', '1*6', '7*2'], ['4', '1', '2']])
| 1 | 1 | 9 |

|2@3|1*6|7*2|

| 4 | 1 | 2 |

答案 2 :(得分:1)

我查看了Triple Triad的牌(我现在记得那场比赛,有趣的东西),我不明白你板上的@或*是什么意思。如果你试图表示它,那么这里有一些显示数字的代码:

def cardString(card):
    if (card):
        return '{west},{north},{south},{east}'.format(**card)
    else:
        return '-,-,-,-'

def printBoard(board):
    for row in board:
        print('{}|{}|{}'.format(cardString(row[0]), cardString(row[1]), cardString(row[2])))

cardMoogle = {'north':9, 'east':3, 'west':2, 'south':9}
cardNull = {'north':'-', 'east':'-', 'west':'-', 'south':'-'}
# I'm sure there's a better way to do this list, but I'm new to Python.
board = [cardNull]*3
board = [list(board), list(board), list(board)]
board[0][0] = cardMoogle

printBoard(board)

这有用吗?

答案 3 :(得分:0)

这可能有点过分,但以下Matrix类可以自动创建自身的表示形式:

class Matrix:

    def __init__(self, rows, columns):
        self.__data = tuple([None] * columns for row in range(rows))
        self.__rows, self.__columns = rows, columns

    def __repr__(self):
        table = Matrix(self.rows, self.columns)
        rows, columns = [0] * self.rows, [0] * self.columns
        for (row, column), value in self:
            lines = tuple(repr(value).replace('\r\n', '\n')
                          .replace('\r', '\n').split('\n'))
            table[row, column] = self.__yield(lines)
            rows[row] = max(rows[row], len(lines))
            columns[column] = max(columns[column], max(map(len, lines)))
        return ('\n' + '+'.join('-' * column for column in columns) + '\n') \
               .join('\n'.join('|'.join(next(table[row, column])
               .ljust(columns[column]) for column in range(table.columns))
               for line in range(rows[row])) for row in range(table.rows))

    def __len__(self):
        return self.rows * self.columns

    def __getitem__(self, key):
        row, column = key
        return self.__data[row][column]

    def __setitem__(self, key, value):
        row, column = key
        self.__data[row][column] = value

    def __delitem__(self, key):
        self[key] = None

    def __iter__(self):
        for row in range(self.rows):
            for column in range(self.columns):
                key = row, column
                yield key, self[key]

    def __reversed__(self):
        for row in range(self.rows - 1, -1, -1):
            for column in range(self.columns - 1, -1, -1):
                key = row, column
                yield key, self[key]

    def __contains__(self, item):
        for row in self.__data:
            if item in row:
                return True
        return False

    def freeze(self):
        self.__data = tuple(map(tuple, self.__data))

    def thaw(self):
        self.__data = tuple(map(list, self.__data))

    @property
    def rows(self):
        return self.__rows

    @property
    def columns(self):
        return self.__columns

    @staticmethod
    def __yield(lines):
        for line in lines:
            yield line
        while True:
            yield ''

如果您需要一种设置,添加或乘以矩阵的方法,这些功能可作为您的运营补充:

def set_matrix(matrix, array):
    for y, row in enumerate(array):
        for x, item in enumerate(row):
            matrix[y, x] = item

def add_matrix(a, b):
    assert a.rows == b.rows and a.columns == b.columns
    c = Matrix(a.rows, a.columns)
    for key, _ in c:
        c[key] = a[key] + b[key]
    return c

def mul_matrix(a, b):
    assert a.columns == b.rows
    c = Matrix(a.rows, b.columns)
    for key, _ in c:
        row, column = key
        c[key] = sum(j * k for j, k in
                     zip((a[row, i] for i in range(a.columns)),
                         (b[i, column] for i in range(b.rows))))
    return c