2

我正在尝试在 Python 上编写名为三元组的游戏,但我的棋盘输出有问题,每个方格都必须是这样,每个数字代表一个基点,有 9 个方格,每个方格三个线。

| 1 | 1 | 9 |

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

| 4 | 1 | 2 |

我想为每一行做一个列表,并以每个基点的数字开始棋盘,例如,如果它是北方或类似的东西,则为“0”,所以当我必须用卡片的数字替换时,我完全知道每个要点放在哪里,有什么建议吗?

提前致谢

4

4 回答 4

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"]]

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

  • 一个函数,它获取这个列表并|在中间打印你的板。
  • 计算上述列表并确定板上每个单元格的正确值的函数。

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

于 2012-06-05T18:59:25.180 回答
2

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

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 |
于 2012-06-05T19:12:42.657 回答
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)

这有用吗?

于 2012-06-05T19:26:36.903 回答
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
于 2012-06-06T13:19:58.633 回答