Personal tools

Eth1winter pygame stitch

From Eth0Wiki

Revision as of 20:19, 31 January 2009 by Elmer (talk | contribs) (New page: == Stitch's entry (2550 chars, no score (fail test-suite)) == <pre> class AreaMax: block = () recordset = {} # public methods def __init__(self, block = None): if ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Stitch's entry (2550 chars, no score (fail test-suite))

class AreaMax:
    block = ()
    recordset = {}

    # public methods
    def __init__(self, block = None):
        if (block != None):
            self.setBlock(block)

    def calculate(self):
        self.recordset = self.convertToRecordset(self.block)
        return self.getHighestValueCoords(self.recordset)


    def setBlock(self, block):
        self.block = block

    # private methods
    # transforms the text string with the numerals to a multidimensional list (2d)
    def convertToRecordset(self, block):
        nrows = [] #
        rows = block.split('\n')
        for n in rows:
            n.strip()
            nrows.append(n.split(' '))

        return nrows

    # traverse the calculated 2d array to get all coords with the highest value
    def getHighestValueCoords(self, recordset):
        highestValue = 0
        highestValueCoords = []

        for x, row in enumerate(recordset):
            for y, col in enumerate(row) :
                currentValue = self.calculateSurrounding(self.getSurroundingValues(x, y))

                if (currentValue > highestValue):
                    highestValueCoords = [] # reinitialize... een delete function wss
                    highestValueCoords.append({'x' : x, 'y' : y, 'value' : currentValue})
                    highestValue = col
                elif (currentValue == highestValue):
                        highestValueCoords.append({'x' : x, 'y' : y, 'value' : currentValue})
        return highestValueCoords

    # calculate, out of a list of values, what the outcome is
    def calculateSurrounding(self, surroundingset):
        result = 0
        for value in surroundingset:
            if int(value) % 2 == 0:
                result += int(value)
            else:
                result -= int(value);

        return result

    # can get all surrounding values from a coordinate and returns it in a list
    # this is where i stranded...
    def getSurroundingValues(self, x, y):
        vals = []
        if (self.recordset[x-1][y-1] != None):
            vals.append(self.recordset[x-1][y-1])
        if (self.recordset[x][y-1] != None):
            vals.append(self.recordset[x][y-1])
        if (self.recordset[x+1][y-1] != None):
            vals.append(self.recordset[x+1][y-1])
        if (self.recordset[x-1][y] != None):
            vals.append(self.recordset[x-1][y])
        if (self.recordset[x+1][y] != None):
            vals.append(self.recordset[x+1][y])
        if (self.recordset[x-1][y+1] != None):
            vals.append(self.recordset[x-1][y+1])
        if (self.recordset[x][y+1] != None):
            vals.append(self.recordset[x][y+1])
        if (self.recordset[x+1][y+1] != None):
            vals.append(self.recordset[x+1][y+1])
        return vals

#the game:
PATTERN_ONE = """83 17 48 41 50 85 56 98 87 20 38 12 76 11 89
27 47 15 38 45 14 79 74 42 85 16 14 41 91 78
70 60 82 47 12 82 27 44 42 21 41 25 65 68 11
51 59 88 54 17 14 87 81 87 33 68 18 84 40 95
52 12 91 66 35 13 43 24 59 23 25 92 67 31 89
66 95 53 89 71 13 31 35 25 31 30 89 51 88 22
60 11 93 10 45 82 99 11 84 55 13 79 20 65 80
70 44 12 49 92 39 32 22 55 58 16 46 69 96 48
49 52 30 45 68 45 62 85 99 89 43 11 65 52 31
13 38 81 96 19 88 14 74 12 47 88 45 93 74 64
24 40 46 63 99 35 55 94 41 66 78 66 77 27 96
25 62 36 67 36 48 71 34 75 41 21 65 24 48 45
16 73 71 80 59 59 25 28 30 57 83 42 89 76 74
40 20 96 86 46 87 90 40 55 39 72 92 98 76 37
89 99 41 95 56 96 99 83 71 23 10 63 73 94 56"""

cm = AreaMax(PATTERN_ONE)
print cm.calculate()