0

我有以下(Python)代码来检查是否有任何行或列包含相同的值:

    # Test rows ->  

        # Check each row for a win
        for i in range(self.height):                    # For each row ...

            firstValue = None                           # Initialize first value placeholder

            for j in range(self.width):                 # For each value in the row
                if (j == 0):                                # If it's the first value ...
                    firstValue = b[i][j]                        # Remember it
                else:                                       # Otherwise ...
                    if b[i][j] != firstValue:                   # If this is not the same as the first value ...
                        firstValue = None                           # Reset first value
                        break                                       # Stop checking this row, there's no win here

            if (firstValue != None):                    # If first value has been set
                                                            # First value placeholder now holds the winning player's code
                return firstValue                           # Return it

    # Test columns ->

        # Check each column for a win
        for i in range(self.width):                 # For each column ...

            firstValue = None                           # Initialize first value placeholder

            for j in range(self.height):                # For each value in the column
                if (j == 0):                                # If it's the first value ...
                    firstValue = b[j][i]                        # Remember it
                else:                                       # Otherwise ...
                    if b[j][i] != firstValue:                   # If this is not the same as the first value ...
                        firstValue = None                           # Reset first value
                        break                                       # Stop checking this column, there's no win here

            if (firstValue != None):                    # If first value has been set
                                                            # First value placeholder now holds the winning player's code
                return firstValue                           # Return it

显然,这里有很多代码重复。如何重构此代码?

谢谢!

4

3 回答 3

2

要检查一行中的所有元素是否相等,我建议构建set该行的 python,然后检查它是否只有一个元素。对于列也是如此。

比如像这样

def testRowWin(b):
    for row in b:
        if len(set(row)) == 1:
            return True
    return False

def testColWin(b):
    return testRowWin(zip(*b))
于 2010-06-16T08:19:24.557 回答
2

一般来说,当你想重构时,取类似的代码片段并将它们变成函数。因此,您可以有一个函数来测试一个索引(行或列)相同的所有单元格,以及另一个在所有列(或行)上调用该函数的函数。尽管正如 Pär 在对您的问题的评论中指出的那样,如果您提供一些有关您尝试过的内容的信息,那么提供帮助会容易得多。

但是......另一个单独的(可能稍微相关的)问题是您的代码没有利用 Python 的功能。这很好,但正如您所知,像这样您必须检查数组(实际上是列表)的一堆不同元素的任务通常在函数式编写时更加简洁例如,您的示例可以这样完成:

f = lambda x,y: x if x == y else False
# for Python <= 2.4 use this instead:
# f = lambda x,y: x == y and x or False
# test rows
[reduce(f,r) for r in array]
# test columns
reduce(lambda r,s: map(f,r,s), array)

尽管如果您想了解代码的工作原理,这并没有太大帮助。

于 2010-06-16T08:21:03.053 回答
1
def test_values(self, first, second, HeightWidth):
    for i in range(first):
        firstValue = None
        for j in range(second):
            (firstDimension, secondDimension) = (i, j) if HeightWidth else (j, i)
            if secondDimension == 0:
                firstValue = b[firstDimension][secondDimension]
            else:
                if b[firstDimension][secondDimension] != firstValue:
                    firstValue = None
                    break
    return firstValue

firstValue = test_values(self, self.height, self.width, true)
if firstValue:
    return firstValue

firstValue test_values(self, self.width, self.height, false)
if firstValue:
    return firstValue
于 2010-06-16T08:52:04.757 回答